Skip to content

Commit b1781e7

Browse files
Ajit Pratap SinghAjit Pratap Singh
authored andcommitted
test: add comprehensive tests for trailing whitespace rule
- Test all check scenarios: no violations, single/multiple violations, edge cases - Test auto-fix functionality with various whitespace types - Test rule metadata (ID, name, severity, canAutoFix) - All tests passing, addresses critical test coverage gap from PR #111 review
1 parent 3cf11db commit b1781e7

1 file changed

Lines changed: 162 additions & 0 deletions

File tree

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package whitespace
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ajitpratap0/GoSQLX/pkg/linter"
7+
)
8+
9+
func TestTrailingWhitespaceRule_Check(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
sql string
13+
expectedViolations int
14+
}{
15+
{
16+
name: "No trailing whitespace",
17+
sql: "SELECT id, name\nFROM users\nWHERE active = true",
18+
expectedViolations: 0,
19+
},
20+
{
21+
name: "Single line with trailing whitespace",
22+
sql: "SELECT id ",
23+
expectedViolations: 1,
24+
},
25+
{
26+
name: "Multiple lines with trailing whitespace",
27+
sql: "SELECT id \nFROM users \nWHERE active = true ",
28+
expectedViolations: 3,
29+
},
30+
{
31+
name: "Empty lines should be skipped",
32+
sql: "SELECT id\n\nFROM users",
33+
expectedViolations: 0,
34+
},
35+
{
36+
name: "Tab as trailing whitespace",
37+
sql: "SELECT id\t",
38+
expectedViolations: 1,
39+
},
40+
{
41+
name: "Mixed spaces and tabs as trailing",
42+
sql: "SELECT id \t ",
43+
expectedViolations: 1,
44+
},
45+
}
46+
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
rule := NewTrailingWhitespaceRule()
50+
ctx := linter.NewContext(tt.sql, "test.sql")
51+
52+
violations, err := rule.Check(ctx)
53+
if err != nil {
54+
t.Fatalf("Unexpected error: %v", err)
55+
}
56+
57+
if len(violations) != tt.expectedViolations {
58+
t.Errorf("Expected %d violations, got %d", tt.expectedViolations, len(violations))
59+
for i, v := range violations {
60+
t.Logf("Violation %d: %s at line %d", i+1, v.Message, v.Location.Line)
61+
}
62+
}
63+
64+
// Verify violation details
65+
for _, v := range violations {
66+
if v.Rule != "L001" {
67+
t.Errorf("Expected rule ID 'L001', got '%s'", v.Rule)
68+
}
69+
if v.Severity != linter.SeverityWarning {
70+
t.Errorf("Expected severity 'warning', got '%s'", v.Severity)
71+
}
72+
if !v.CanAutoFix {
73+
t.Error("Expected CanAutoFix to be true")
74+
}
75+
}
76+
})
77+
}
78+
}
79+
80+
func TestTrailingWhitespaceRule_Fix(t *testing.T) {
81+
tests := []struct {
82+
name string
83+
input string
84+
expected string
85+
}{
86+
{
87+
name: "Remove trailing spaces",
88+
input: "SELECT id \nFROM users",
89+
expected: "SELECT id\nFROM users",
90+
},
91+
{
92+
name: "Remove trailing tabs",
93+
input: "SELECT id\t\t\nFROM users",
94+
expected: "SELECT id\nFROM users",
95+
},
96+
{
97+
name: "Remove mixed trailing whitespace",
98+
input: "SELECT id \t \nFROM users",
99+
expected: "SELECT id\nFROM users",
100+
},
101+
{
102+
name: "Preserve lines without trailing whitespace",
103+
input: "SELECT id\nFROM users",
104+
expected: "SELECT id\nFROM users",
105+
},
106+
{
107+
name: "Handle empty lines",
108+
input: "SELECT id\n\nFROM users",
109+
expected: "SELECT id\n\nFROM users",
110+
},
111+
{
112+
name: "Multiple lines with trailing whitespace",
113+
input: "SELECT id \nFROM users \nWHERE active = true ",
114+
expected: "SELECT id\nFROM users\nWHERE active = true",
115+
},
116+
}
117+
118+
for _, tt := range tests {
119+
t.Run(tt.name, func(t *testing.T) {
120+
rule := NewTrailingWhitespaceRule()
121+
ctx := linter.NewContext(tt.input, "test.sql")
122+
123+
violations, err := rule.Check(ctx)
124+
if err != nil {
125+
t.Fatalf("Unexpected error during check: %v", err)
126+
}
127+
128+
fixed, err := rule.Fix(tt.input, violations)
129+
if err != nil {
130+
t.Fatalf("Unexpected error during fix: %v", err)
131+
}
132+
133+
if fixed != tt.expected {
134+
t.Errorf("Fix result mismatch:\nExpected: %q\nGot: %q", tt.expected, fixed)
135+
}
136+
})
137+
}
138+
}
139+
140+
func TestTrailingWhitespaceRule_Metadata(t *testing.T) {
141+
rule := NewTrailingWhitespaceRule()
142+
143+
if rule.ID() != "L001" {
144+
t.Errorf("Expected ID 'L001', got '%s'", rule.ID())
145+
}
146+
147+
if rule.Name() != "Trailing Whitespace" {
148+
t.Errorf("Expected name 'Trailing Whitespace', got '%s'", rule.Name())
149+
}
150+
151+
if rule.Severity() != linter.SeverityWarning {
152+
t.Errorf("Expected severity 'warning', got '%s'", rule.Severity())
153+
}
154+
155+
if !rule.CanAutoFix() {
156+
t.Error("Expected CanAutoFix to be true")
157+
}
158+
159+
if rule.Description() == "" {
160+
t.Error("Expected non-empty description")
161+
}
162+
}

0 commit comments

Comments
 (0)