-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresult_test.go
69 lines (57 loc) · 1.74 KB
/
result_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package codacytool
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func TestResultsToJSON(t *testing.T) {
// Arrange
issue := Issue{
File: "file",
Line: 5,
Message: "message",
PatternID: "pattern ID",
}
fileError := FileError{
File: "file-error",
Message: "file-error",
}
sbom := SBOM{}
badResult := BadResult{}
expectedJSONResults := []string{
`{"filename":"file","line":5,"message":"message","patternId":"pattern ID"}`,
`{"filename":"file-error","message":"file-error"}`,
`{"bomFormat":"","specVersion":"SpecVersion(0)","version":0}`,
}
// Act
jsonResults := Results{issue, fileError, sbom, badResult}.ToJSON()
// Assert
// Since a JSON object does not have order, we can't simply assert by doing `assert.ElementsMatch`.
// To guarantee that the results are in the same order for proper comparison we can simply sort them by length.
sort.Slice(jsonResults, func(a, b int) bool { return len(jsonResults[a]) > len(jsonResults[b]) })
sort.Slice(expectedJSONResults, func(a, b int) bool { return len(expectedJSONResults[a]) > len(expectedJSONResults[b]) })
for i, jsonResult := range jsonResults {
assert.JSONEq(t, expectedJSONResults[i], jsonResult)
}
}
func TestResultsGetFile(t *testing.T) {
// Arrange
issue := Issue{File: "issue-file"}
fileError := FileError{File: "file-error"}
sbom := SBOM{}
// Act
issueFile := issue.GetFile()
fileErrorFile := fileError.GetFile()
sbomFile := sbom.GetFile()
// Assert
assert.Equal(t, "issue-file", issueFile)
assert.Equal(t, "file-error", fileErrorFile)
assert.Empty(t, sbomFile)
}
type BadResult struct{}
func (r BadResult) ToJSON() ([]byte, error) {
return nil, assert.AnError
}
func (r BadResult) GetFile() string {
return "not used"
}