|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +func TestFormatter_WithBeautify_Chaining(t *testing.T) { |
| 10 | + // Test that WithBeautify returns the formatter for chaining |
| 11 | + formatter := NewFormatter().WithBeautify() |
| 12 | + require.NotNil(t, formatter) |
| 13 | + require.Equal(t, FormatModeBeautify, formatter.mode) |
| 14 | +} |
| 15 | + |
| 16 | +func TestFormatter_WithIndent_Chaining(t *testing.T) { |
| 17 | + // Test that WithIndent returns the formatter for chaining |
| 18 | + formatter := NewFormatter().WithIndent(" ") |
| 19 | + require.NotNil(t, formatter) |
| 20 | + require.Equal(t, " ", formatter.indent) |
| 21 | +} |
| 22 | + |
| 23 | +func TestFormatter_ChainedMethods(t *testing.T) { |
| 24 | + // Test that methods can be chained together |
| 25 | + formatter := NewFormatter().WithBeautify().WithIndent("\t") |
| 26 | + require.NotNil(t, formatter) |
| 27 | + require.Equal(t, FormatModeBeautify, formatter.mode) |
| 28 | + require.Equal(t, "\t", formatter.indent) |
| 29 | +} |
| 30 | + |
| 31 | +func TestFormatter_WithIndent_CustomIndentation(t *testing.T) { |
| 32 | + // Test actual formatting with custom indent using parsed SQL |
| 33 | + sql := "SELECT col1, col2 FROM table1 WHERE col1 > 10" |
| 34 | + |
| 35 | + parser := NewParser(sql) |
| 36 | + stmts, err := parser.ParseStmts() |
| 37 | + require.NoError(t, err) |
| 38 | + require.Len(t, stmts, 1) |
| 39 | + |
| 40 | + // Test with default 2-space indent |
| 41 | + formatter1 := NewFormatter().WithBeautify() |
| 42 | + formatter1.WriteExpr(stmts[0]) |
| 43 | + result1 := formatter1.String() |
| 44 | + |
| 45 | + // Test with 4-space indent |
| 46 | + formatter2 := NewFormatter().WithBeautify().WithIndent(" ") |
| 47 | + formatter2.WriteExpr(stmts[0]) |
| 48 | + result2 := formatter2.String() |
| 49 | + |
| 50 | + // Test with tab indent |
| 51 | + formatter3 := NewFormatter().WithBeautify().WithIndent("\t") |
| 52 | + formatter3.WriteExpr(stmts[0]) |
| 53 | + result3 := formatter3.String() |
| 54 | + |
| 55 | + // Verify all results are different (due to different indentation) |
| 56 | + require.NotEqual(t, result1, result2) |
| 57 | + require.NotEqual(t, result1, result3) |
| 58 | + require.NotEqual(t, result2, result3) |
| 59 | + |
| 60 | + // Verify they all contain the basic SQL keywords |
| 61 | + require.Contains(t, result1, "SELECT") |
| 62 | + require.Contains(t, result2, "SELECT") |
| 63 | + require.Contains(t, result3, "SELECT") |
| 64 | + require.Contains(t, result1, "FROM") |
| 65 | + require.Contains(t, result2, "FROM") |
| 66 | + require.Contains(t, result3, "FROM") |
| 67 | +} |
| 68 | + |
| 69 | +func TestFormatter_DefaultIndent(t *testing.T) { |
| 70 | + // Test that default indent is 2 spaces |
| 71 | + formatter := NewFormatter() |
| 72 | + require.Equal(t, " ", formatter.indent) |
| 73 | +} |
0 commit comments