From 5e8bccd11addaadead2c26aa371887e2dfe8ba07 Mon Sep 17 00:00:00 2001 From: attiasas Date: Thu, 14 Sep 2023 17:14:55 +0300 Subject: [PATCH] more tests --- xray/utils/sarifutils_test.go | 52 ++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/xray/utils/sarifutils_test.go b/xray/utils/sarifutils_test.go index 98810590a..6dc4f21bb 100644 --- a/xray/utils/sarifutils_test.go +++ b/xray/utils/sarifutils_test.go @@ -102,7 +102,24 @@ func TestGetLocationSnippet(t *testing.T) { } func TestSetLocationSnippet(t *testing.T) { + tests := []struct { + location *sarif.Location + expectedOutput string + }{ + { + location: nil, + expectedOutput: "", + }, + { + location: GetDummyLocation("filename", 1, 2, 3, 4, "snippet"), + expectedOutput: "changedSnippet", + }, + } + for _, test := range tests { + SetLocationSnippet(test.location, test.expectedOutput) + assert.Equal(t, test.expectedOutput, GetLocationSnippet(test.location)) + } } func TestGetLocationFileName(t *testing.T) { @@ -130,7 +147,23 @@ func TestGetRelativeLocationFileName(t *testing.T) { location *sarif.Location invocations []*sarif.Invocation expectedOutput string - }{} + }{ + { + location: GetDummyLocation("file:///root/someDir/another/file", 1, 2, 3, 4, "snippet"), + invocations: []*sarif.Invocation{}, + expectedOutput: "root/someDir/another/file", + }, + { + location: GetDummyLocation("file:///root/someDir/another/file", 1, 2, 3, 4, "snippet"), + invocations: []*sarif.Invocation{{WorkingDirectory: sarif.NewSimpleArtifactLocation("/not/relevant")}}, + expectedOutput: "root/someDir/another/file", + }, + { + location: GetDummyLocation("file:///root/someDir/another/file", 1, 2, 3, 4, "snippet"), + invocations: []*sarif.Invocation{{WorkingDirectory: sarif.NewSimpleArtifactLocation("/root/someDir/")}}, + expectedOutput: "another/file", + }, + } for _, test := range tests { assert.Equal(t, test.expectedOutput, GetRelativeLocationFileName(test.location, test.invocations)) @@ -138,7 +171,24 @@ func TestGetRelativeLocationFileName(t *testing.T) { } func TestSetLocationFileName(t *testing.T) { + tests := []struct { + location *sarif.Location + expectedOutput string + }{ + { + location: nil, + expectedOutput: "", + }, + { + location: GetDummyLocation("filename", 1, 2, 3, 4, "snippet"), + expectedOutput: "changedFilename", + }, + } + for _, test := range tests { + SetLocationFileName(test.location, test.expectedOutput) + assert.Equal(t, test.expectedOutput, GetLocationFileName(test.location)) + } } func TestGetLocationRegion(t *testing.T) {