github.com/rajeev159/opa@v0.45.0/ast/pretty_test.go (about) 1 // Copyright 2018 The OPA Authors. All rights reserved. 2 // Use of this source code is governed by an Apache2 3 // license that can be found in the LICENSE file. 4 5 package ast 6 7 import ( 8 "bytes" 9 "strings" 10 "testing" 11 ) 12 13 func TestPretty(t *testing.T) { 14 15 module := MustParseModule(` 16 package foo.bar 17 18 import data.baz as qux 19 20 p[x] = y { 21 x = a + b 22 y = {"foo": [{1, null}, true]} 23 } 24 25 f(x) = g(x) 26 `) 27 28 var buf bytes.Buffer 29 Pretty(&buf, module) 30 31 expected := `module 32 package 33 ref 34 data 35 "foo" 36 "bar" 37 import 38 ref 39 data 40 "baz" 41 qux 42 rule 43 head 44 p 45 x 46 y 47 body 48 expr index=0 49 ref 50 eq 51 x 52 call 53 ref 54 plus 55 a 56 b 57 expr index=1 58 ref 59 eq 60 y 61 object 62 "foo" 63 array 64 set 65 null 66 1 67 true 68 rule 69 head 70 f 71 args 72 x 73 call 74 ref 75 g 76 x 77 body 78 expr index=0 79 true` 80 81 result := strings.TrimSpace(buf.String()) 82 expected = strings.TrimSpace(expected) 83 84 if result != expected { 85 86 resultLines := strings.Split(result, "\n") 87 expectedLines := strings.Split(expected, "\n") 88 89 minLines := len(resultLines) 90 if minLines > len(expectedLines) { 91 minLines = len(expectedLines) 92 } 93 94 for i := 0; i < minLines; i++ { 95 if resultLines[i] != expectedLines[i] { 96 t.Fatalf("Expected line %d to be:\n\n%q\n\nGot:\n\n%q", i, expectedLines[i], resultLines[i]) 97 } 98 } 99 100 if len(resultLines) != len(expectedLines) { 101 t.Fatalf("Expected:\n\n%v\n\nGot:\n\n%v", expectedLines, resultLines) 102 } 103 } 104 }