MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / TestSQLFormatter_Options

Function TestSQLFormatter_Options

cmd/gosqlx/cmd/sql_formatter_test.go:500–571  ·  view source on GitHub ↗

TestSQLFormatter_Options tests different formatter options

(t *testing.T)

Source from the content-addressed store, hash-verified

498
499// TestSQLFormatter_Options tests different formatter options
500func TestSQLFormatter_Options(t *testing.T) {
501 sql := "SELECT id, name FROM users WHERE active = true"
502
503 t.Run("different indent sizes", func(t *testing.T) {
504 indentSizes := []int{2, 4, 8}
505
506 for _, indentSize := range indentSizes {
507 tkz := tokenizer.GetTokenizer()
508 tokens, _ := tkz.Tokenize([]byte(sql))
509 p := parser.NewParser()
510 astObj := ast.NewAST()
511 result, err := p.ParseFromModelTokens(tokens)
512 if err != nil {
513 tokenizer.PutTokenizer(tkz)
514 ast.ReleaseAST(astObj)
515 t.Skip("Parser not fully supported")
516 return
517 }
518 astObj.Statements = result.Statements
519
520 indent := strings.Repeat(" ", indentSize)
521 formatter := NewSQLFormatter(FormatterOptions{
522 Indent: indent,
523 UppercaseKw: false,
524 })
525 output, _ := formatter.Format(astObj)
526
527 tokenizer.PutTokenizer(tkz)
528 ast.ReleaseAST(astObj)
529
530 if output == "" {
531 t.Errorf("Formatter with indent=%d produced empty output", indentSize)
532 }
533 }
534 })
535
536 t.Run("uppercase vs lowercase", func(t *testing.T) {
537 for _, uppercase := range []bool{true, false} {
538 tkz := tokenizer.GetTokenizer()
539 tokens, _ := tkz.Tokenize([]byte(sql))
540 p := parser.NewParser()
541 astObj := ast.NewAST()
542 result, err := p.ParseFromModelTokens(tokens)
543 if err != nil {
544 tokenizer.PutTokenizer(tkz)
545 ast.ReleaseAST(astObj)
546 t.Skip("Parser not fully supported")
547 return
548 }
549 astObj.Statements = result.Statements
550
551 formatter := NewSQLFormatter(FormatterOptions{
552 Indent: " ",
553 UppercaseKw: uppercase,
554 })
555 output, _ := formatter.Format(astObj)
556
557 tokenizer.PutTokenizer(tkz)

Callers

nothing calls this directly

Calls 12

ParseFromModelTokensMethod · 0.95
FormatMethod · 0.95
GetTokenizerFunction · 0.92
NewParserFunction · 0.92
NewASTFunction · 0.92
PutTokenizerFunction · 0.92
ReleaseASTFunction · 0.92
NewSQLFormatterFunction · 0.85
RunMethod · 0.80
TokenizeMethod · 0.80
ErrorfMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected