From 858179ca2856e924e4ba2ddc35dc28c8c58df5f1 Mon Sep 17 00:00:00 2001 From: Francesco Bertolaccini Date: Thu, 25 Jul 2024 17:42:22 +0200 Subject: [PATCH] test: additional roundtrip tests for sarif --- test/sarif/definitions.cpp | 70 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/test/sarif/definitions.cpp b/test/sarif/definitions.cpp index 43122e6..8d06c9f 100644 --- a/test/sarif/definitions.cpp +++ b/test/sarif/definitions.cpp @@ -4,7 +4,7 @@ #include namespace gap::sarif::definitions { - TEST_CASE("Roundtrip") { + TEST_CASE("Struct to JSON roundtrip") { // Sample SARIF output from https://github.com/microsoft/sarif-tutorials/blob/main/docs/1-Introduction.md root_struct root{ .version = version_enum::k2_1_0, @@ -73,6 +73,72 @@ namespace gap::sarif::definitions { root_struct deser = root; nlohmann::json deser_json = deser; - CHECK(root_json == deser_json); + CHECK_EQ(root_json, deser_json); + } + + TEST_CASE("JSON to struct roundtrip") { + using namespace nlohmann::literals; + auto j = R"({ + "version": "2.1.0", + "runs": [ + { + "tool": { + "driver": { + "name": "ESLint", + "informationUri": "https://eslint.org", + "rules": [ + { + "id": "no-unused-vars", + "shortDescription": { + "text": "disallow unused variables" + }, + "helpUri": "https://eslint.org/docs/rules/no-unused-vars", + "properties": { + "category": "Variables" + } + } + ] + } + }, + "artifacts": [ + { + "location": { + "uri": "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js" + } + } + ], + "results": [ + { + "level": "error", + "message": { + "text": "'x' is assigned a value but never used." + }, + "locations": [ + { + "physicalLocation": { + "artifactLocation": { + "uri": "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js", + "index": 0 + }, + "region": { + "startLine": 1, + "startColumn": 5 + } + } + } + ], + "ruleId": "no-unused-vars", + "ruleIndex": 0 + } + ] + } + ] +})"_json; + + root_struct root = j; + + nlohmann::json j2 = root; + + CHECK_EQ(j, j2); } } \ No newline at end of file