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