Debian Codegraph @

tools/antithesis-go-instrumentor/scanners/assertions/catalog_output_test.go

raw
1 package assertions
2
3 import (
4 "os"
5 "path/filepath"
6 "testing"
7
8 qt "github.com/go-quicktest/qt"
9
10 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common"
11 )
12
13 func TestCatalogContents(t *testing.T) {
14 outputDir := t.TempDir()
15
16 expects := []*AntExpect{
17 {
18 AssertionFuncInfo: &AssertionFuncInfo{
19 TargetFunc: "Always",
20 AssertType: "always",
21 MustHit: true,
22 Condition: false,
23 MessageArg: 1,
24 },
25 Assertion: "Always",
26 Message: "test always",
27 Classname: "example.com/mymod",
28 Funcname: "main",
29 Filename: "main.go",
30 Line: 10,
31 },
32 {
33 AssertionFuncInfo: &AssertionFuncInfo{
34 TargetFunc: "Reachable",
35 AssertType: "reachability",
36 MustHit: true,
37 Condition: true,
38 MessageArg: 0,
39 },
40 Assertion: "Reachable",
41 Message: "test reachable",
42 Classname: "example.com/mymod",
43 Funcname: "init",
44 Filename: "main.go",
45 Line: 5,
46 },
47 }
48
49 genInfo := GenInfo{
50 PackageName: "main",
51 ExpectedVals: expects,
52 NumericGuidanceVals: nil,
53 BooleanGuidanceVals: nil,
54 AssertPackageName: common.AssertPackageName(),
55 VersionText: "test version",
56 CreateDate: "Mon Jan 1 00:00:00 UTC 2025",
57 HasAssertions: true,
58 HasNumericGuidance: false,
59 HasBooleanGuidance: false,
60 ConstMap: getConstMap(expects),
61 }
62
63 common.NewLogWriter("", common.Normal)
64 qt.Assert(t, qt.IsNil(GenerateAssertionsCatalog(outputDir, &genInfo)))
65
66 // Verify the file was created
67 outputPath := filepath.Join(outputDir, common.GENERATED_CATALOG_FILE)
68 content, err := os.ReadFile(outputPath)
69 qt.Assert(t, qt.IsNil(err))
70
71 text := string(content)
72
73 qt.Check(t, qt.StringContains(text, "package main"))
74 qt.Check(t, qt.StringContains(text, `import "github.com/antithesishq/antithesis-sdk-go/assert"`))
75 qt.Check(t, qt.StringContains(text, `assert.AssertRaw(`))
76 qt.Check(t, qt.StringContains(text, `"test always"`))
77 qt.Check(t, qt.StringContains(text, `"test reachable"`))
78 qt.Check(t, qt.StringContains(text, "test version"))
79 }
80
81 func TestCatalogNumericGuidance(t *testing.T) {
82 outputDir := t.TempDir()
83
84 numericGuidance := []*AntGuidance{
85 {
86 GuidanceFuncInfo: &GuidanceFuncInfo{
87 AssertionFuncInfo: AssertionFuncInfo{
88 TargetFunc: "AlwaysGreaterThan",
89 AssertType: "always",
90 MustHit: true,
91 Condition: false,
92 MessageArg: 2,
93 },
94 GuidanceFn: GuidanceFnMinimize,
95 },
96 Assertion: "AlwaysGreaterThan",
97 Message: "x > y",
98 Classname: "example.com/mymod",
99 Funcname: "compute",
100 Filename: "compute.go",
101 Line: 42,
102 },
103 }
104
105 genInfo := GenInfo{
106 PackageName: "main",
107 ExpectedVals: nil,
108 NumericGuidanceVals: numericGuidance,
109 BooleanGuidanceVals: nil,
110 AssertPackageName: common.AssertPackageName(),
111 VersionText: "test",
112 CreateDate: "now",
113 HasAssertions: false,
114 HasNumericGuidance: true,
115 HasBooleanGuidance: false,
116 ConstMap: make(map[string]bool),
117 }
118
119 qt.Assert(t, qt.IsNil(GenerateAssertionsCatalog(outputDir, &genInfo)))
120
121 outputPath := filepath.Join(outputDir, common.GENERATED_CATALOG_FILE)
122 content, err := os.ReadFile(outputPath)
123 qt.Assert(t, qt.IsNil(err))
124
125 text := string(content)
126 qt.Check(t, qt.StringContains(text, "assert.NumericGuidanceRaw("))
127 qt.Check(t, qt.StringContains(text, `"x > y"`))
128 }