VerifyStatementPrettyRoundtrip verifies that the SQL statements in s correctly round trip through the pretty printer.
(t *testing.T, sql string)
| 20 | // VerifyStatementPrettyRoundtrip verifies that the SQL statements in s |
| 21 | // correctly round trip through the pretty printer. |
| 22 | func VerifyStatementPrettyRoundtrip(t *testing.T, sql string) { |
| 23 | t.Helper() |
| 24 | |
| 25 | stmts, err := parser.Parse(sql) |
| 26 | if err != nil { |
| 27 | t.Fatalf("%s: %s", err, sql) |
| 28 | } |
| 29 | cfg := tree.DefaultPrettyCfg() |
| 30 | // Be careful to not simplify otherwise the tests won't round trip. |
| 31 | cfg.Simplify = false |
| 32 | for i := range stmts { |
| 33 | // Dataflow of the statement through these checks: |
| 34 | // |
| 35 | // sql (from test file) |
| 36 | // | |
| 37 | // (parser.Parse) |
| 38 | // v |
| 39 | // origStmt |
| 40 | // / \ |
| 41 | // (cfg.Pretty) (AsStringWithFlags,FmtParsable) |
| 42 | // v | |
| 43 | // prettyStmt | |
| 44 | // | | |
| 45 | // (parser.ParseOne) | |
| 46 | // v | |
| 47 | // parsedPretty | |
| 48 | // | | |
| 49 | // (AsStringWithFlags,FmtSimple) | |
| 50 | // v v |
| 51 | // prettyFormatted origFormatted |
| 52 | // |
| 53 | // == Check 1: prettyFormatted == origFormatted |
| 54 | // If false: |
| 55 | // |
| 56 | // | | |
| 57 | // | (parser.ParseOne) |
| 58 | // | v |
| 59 | // | reparsedStmt |
| 60 | // | | |
| 61 | // | (AsStringWithFlags,FmtParsable) |
| 62 | // v v |
| 63 | // prettyFormatted origFormatted |
| 64 | // |
| 65 | // == Check 2: prettyFormatted == origFormatted |
| 66 | // |
| 67 | origStmt := stmts[i].AST |
| 68 | // Be careful to not simplify otherwise the tests won't round trip. |
| 69 | prettyStmt := cfg.Pretty(origStmt) |
| 70 | parsedPretty, err := parser.ParseOne(prettyStmt) |
| 71 | if err != nil { |
| 72 | t.Fatalf("%s: %s", err, prettyStmt) |
| 73 | } |
| 74 | prettyFormatted := tree.AsStringWithFlags(parsedPretty.AST, tree.FmtSimple) |
| 75 | origFormatted := tree.AsStringWithFlags(origStmt, tree.FmtParsable) |
| 76 | if prettyFormatted != origFormatted { |
| 77 | // Type annotations and unicode strings don't round trip well. Sometimes we |
| 78 | // need to reparse the original formatted output and format that for these |
| 79 | // to match. |
searching dependent graphs…