github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/optgen/cmd/optfmt/main_test.go (about) 1 // Copyright 2020 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package main 12 13 import ( 14 "fmt" 15 "strings" 16 "testing" 17 18 "github.com/cockroachdb/datadriven" 19 ) 20 21 func TestPretty(t *testing.T) { 22 datadriven.Walk(t, "testdata", func(t *testing.T, path string) { 23 datadriven.RunTest(t, path, prettyTest) 24 }) 25 } 26 27 func prettyTest(t *testing.T, d *datadriven.TestData) string { 28 switch d.Cmd { 29 case "pretty": 30 n := defaultWidth 31 if d.HasArg("n") { 32 d.ScanArgs(t, "n", &n) 33 } 34 exprgen := d.HasArg("expr") 35 s, err := prettyify(strings.NewReader(d.Input), n, exprgen) 36 if err != nil { 37 return fmt.Sprintf("ERROR: %s", err) 38 } 39 40 // Verify we round trip correctly by ensuring non-whitespace 41 // scanner tokens are encountered in the same order. 42 { 43 origToks := toTokens(d.Input) 44 prettyToks := toTokens(s) 45 for i, tok := range origToks { 46 if i >= len(prettyToks) { 47 t.Fatalf("pretty ended early after %d tokens", i+1) 48 } 49 if prettyToks[i] != tok { 50 t.Log(s) 51 t.Logf("expected %q", tok) 52 t.Logf("got %q", prettyToks[i]) 53 t.Fatalf("token %d didn't match", i+1) 54 } 55 } 56 if len(prettyToks) > len(origToks) { 57 t.Fatalf("orig ended early after %d tokens", len(origToks)) 58 } 59 } 60 // Verify lines aren't too long. 61 { 62 for i, line := range strings.Split(s, "\n") { 63 if strings.HasPrefix(line, "#") { 64 continue 65 } 66 if len(line) > defaultWidth { 67 t.Errorf("line %d is %d chars, expected <= %d:\n%s", i+1, len(line), defaultWidth, line) 68 } 69 } 70 } 71 72 return s 73 default: 74 t.Fatal("unknown command") 75 return "" 76 } 77 }