Debian Codegraph @

strict/utils_test.go

raw
1 // Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package strict
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 )
11
12 func TestCompilePattern_EscapedDoubleAsterisk(t *testing.T) {
13 // Pattern \*\* should match literal "**" in path (lines 78-80)
14 // This escapes the glob ** so it matches the literal string **
15 re := compilePattern(`$.body.\*\*`)
16 assert.NotNil(t, re)
17
18 // Should match literal ** in path
19 assert.True(t, re.MatchString("$.body.**"))
20
21 // Should NOT match arbitrary depth (that's what unescaped ** does)
22 assert.False(t, re.MatchString("$.body.foo.bar"))
23 }
24
25 func TestCompilePattern_EscapedNonAsterisk(t *testing.T) {
26 // Pattern with escaped character that's not * (lines 88-90)
27 // \n should match literal 'n', \. should match literal '.'
28 re := compilePattern(`$.body\nvalue`)
29 assert.NotNil(t, re)
30
31 // Should match with literal 'n' (the escape just includes the next char)
32 assert.True(t, re.MatchString("$.bodynvalue"))
33 }
34
35 func TestCompilePattern_EscapedDot(t *testing.T) {
36 // Escaped dot should be literal dot
37 re := compilePattern(`$.body\.name`)
38 assert.NotNil(t, re)
39
40 // Should match path with literal dot
41 assert.True(t, re.MatchString("$.body.name"))
42 }
43
44 func TestCompilePattern_Empty(t *testing.T) {
45 // Empty pattern returns nil
46 re := compilePattern("")
47 assert.Nil(t, re)
48 }
49
50 func TestBuildPath_WithDot(t *testing.T) {
51 // Property with dot uses bracket notation
52 result := buildPath("$.body", "a.b")
53 assert.Equal(t, "$.body['a.b']", result)
54 }
55
56 func TestBuildPath_WithBrackets(t *testing.T) {
57 // Property with brackets uses bracket notation
58 result := buildPath("$.body", "x[0]")
59 assert.Equal(t, "$.body['x[0]']", result)
60 }
61
62 func TestBuildPath_Simple(t *testing.T) {
63 // Simple property uses dot notation
64 result := buildPath("$.body", "name")
65 assert.Equal(t, "$.body.name", result)
66 }
67
68 func TestBuildArrayPath(t *testing.T) {
69 result := buildArrayPath("$.body.items", 5)
70 assert.Equal(t, "$.body.items[5]", result)
71 }
72
73 func TestCompileIgnorePaths_Empty(t *testing.T) {
74 result := compileIgnorePaths(nil)
75 assert.Nil(t, result)
76
77 result = compileIgnorePaths([]string{})
78 assert.Nil(t, result)
79 }
80
81 func TestCompileIgnorePaths_WithPatterns(t *testing.T) {
82 patterns := []string{
83 "$.body.metadata",
84 "$.body.items[*].internal",
85 }
86 result := compileIgnorePaths(patterns)
87 assert.Len(t, result, 2)
88 }
89
90 func TestTruncateValue_LongString(t *testing.T) {
91 // String > 50 chars gets truncated
92 longStr := "this is a very long string that exceeds fifty characters in length"
93 result := TruncateValue(longStr)
94 assert.Equal(t, "this is a very long string that exceeds fifty c...", result)
95 }
96
97 func TestTruncateValue_ShortString(t *testing.T) {
98 shortStr := "short"
99 result := TruncateValue(shortStr)
100 assert.Equal(t, "short", result)
101 }
102
103 func TestTruncateValue_LargeMap(t *testing.T) {
104 // Map with > 3 keys shows {...}
105 m := map[string]any{"a": 1, "b": 2, "c": 3, "d": 4}
106 result := TruncateValue(m)
107 assert.Equal(t, "{...}", result)
108 }
109
110 func TestTruncateValue_SmallMap(t *testing.T) {
111 m := map[string]any{"a": 1, "b": 2}
112 result := TruncateValue(m)
113 assert.Equal(t, m, result)
114 }
115
116 func TestTruncateValue_LargeSlice(t *testing.T) {
117 // Slice with > 3 elements shows [...]
118 s := []any{1, 2, 3, 4}
119 result := TruncateValue(s)
120 assert.Equal(t, "[...]", result)
121 }
122
123 func TestTruncateValue_SmallSlice(t *testing.T) {
124 s := []any{1, 2}
125 result := TruncateValue(s)
126 assert.Equal(t, s, result)
127 }
128
129 func TestTruncateValue_OtherTypes(t *testing.T) {
130 // Other types returned as-is
131 assert.Equal(t, 42, TruncateValue(42))
132 assert.Equal(t, true, TruncateValue(true))
133 assert.Equal(t, 3.14, TruncateValue(3.14))
134 }