Skip to content

Commit

Permalink
sarif: add rountrip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frabert committed Aug 1, 2024
1 parent 9186606 commit 25d90b2
Showing 1 changed file with 139 additions and 4 deletions.
143 changes: 139 additions & 4 deletions test/sarif/sarif.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,143 @@
// Copyright (c) 2024-present, Trail of Bits, Inc.

#include <doctest/doctest.h>
#include <gap/sarif/definitions.hpp>
#include <memory>
#include <vector>
#include <gap/sarif/sarif.hpp>

namespace gap::sarif {}
namespace gap::sarif {
TEST_CASE("Struct to JSON roundtrip") {
// Sample SARIF output from https://github.com/microsoft/sarif-tutorials/blob/main/docs/1-Introduction.md
root obj{
.version = version::k2_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 = {{
.additional_properties = {
{"category", "Variable"},
},
}},
}
},
},
},
.artifacts = {
{
.location = {{
.uri = "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js",
}},
},
},
.results = {{
{
.ruleId = "no-unused-vars",
.ruleIndex = 0,
.level = level::kError,
.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,
}},
}},
.properties = {{
.additional_properties = {
{"region", {
{"startLine", 1},
{"startColumn", 5},
}},
},
}},
},
}},
},
}},
}
},
};

json obj_json = obj;
root deser = obj;
json deser_json = deser;

CHECK_EQ(obj_json, deser_json);
}

TEST_CASE("JSON to struct roundtrip") {
auto j = json::parse(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
}
]
}
]
})");

root root = j;

json j2 = root;

CHECK_EQ(j, j2);
}
}

0 comments on commit 25d90b2

Please sign in to comment.