From 54fb06845a6344e26d036a9d67362c5105aef55f Mon Sep 17 00:00:00 2001 From: hahwul Date: Sat, 11 Nov 2023 20:14:06 +0900 Subject: [PATCH] Add testing --- docs/contribute/testing.md | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 docs/contribute/testing.md diff --git a/docs/contribute/testing.md b/docs/contribute/testing.md new file mode 100644 index 0000000..6833185 --- /dev/null +++ b/docs/contribute/testing.md @@ -0,0 +1,73 @@ +--- +layout: page +title: Testing +permalink: /contribute/testing +parent: Contribute +nav_order: 4 +--- + +## Testing +Noir manages code quality through both Unit and Functional testing. You can execute test code using Crystal's spec feature. + +```bash +# Basic testing +crystal spec + +# If you want more detail? +crystal spec -v +``` + +## Unit Test +Unit tests are defined under `spec/unit_test/` in Noir. + +```crystal +# Example + +describe "Detect Java Armeria" do + options = default_options() + instance = DetectorJavaArmeria.new options + + it "pom.xml" do + instance.detect("pom.xml", "com.linecorp.armeria").should eq(true) + end + it "build.gradle" do + instance.detect("build.gradle", "com.linecorp.armeria").should eq(true) + end +end +``` + +## Functional Test +Functional tests are defined under `spec/functional_test/` in Noir. Functional tests consist of Fixtures and Testers. + +- `spec/functional_test/fixtures/*` +- `spec/functional_test/testers/*` + +Fixtures involve creating sample code that resembles the actual code under analysis. Testers, on the other hand, use Fixture paths to verify if detection is accurate, the number of discovered endpoints, and whether the expected information matches the detailed information of detected endpoints (such as paths, methods, parameters, etc.). + +```crystal +# Example of Rails Analyzer + +extected_endpoints = [ + Endpoint.new("/secret.html", "GET"), + Endpoint.new("/posts", "GET"), + Endpoint.new("/posts/1", "GET"), + Endpoint.new("/posts", "POST", [ + Param.new("id", "", "json"), + Param.new("title", "", "json"), + Param.new("context", "", "json"), + Param.new("X-API-KEY", "", "header"), + ]), + Endpoint.new("/posts/1", "PUT", [ + Param.new("id", "", "json"), + Param.new("title", "", "json"), + Param.new("context", "", "json"), + Param.new("X-API-KEY", "", "header"), + ]), + Endpoint.new("/posts/1", "DELETE"), +] + +FunctionalTester.new("fixtures/rails/", { + :techs => 1, + :endpoints => 6, +}, extected_endpoints).test_all +``` \ No newline at end of file