@@ -6,54 +6,101 @@ import (
66 "encoding/json"
77 "io/ioutil"
88 "os"
9+ "path/filepath"
10+ "strings"
911 "testing"
1012)
1113
12- func TestDataFiles (t * testing.T ) {
14+ func TestValidDataFiles (t * testing.T ) {
15+ const expectedFileSuffix = "_expected"
16+ filepath .Walk ("./testdata/valid" , func (path string , info os.FileInfo , err error ) error {
17+ if strings .HasSuffix (path , expectedFileSuffix ) {
18+ return nil
19+ }
20+
21+ if info .IsDir () {
22+ return nil
23+ }
24+
25+ f , err := os .Open (path )
26+ if err != nil {
27+ t .Errorf ("%s: unexpected error, %v" , path , err )
28+ }
29+ defer f .Close ()
30+
31+ tree , err := ParseAST (f )
32+ if err != nil {
33+ t .Errorf ("%s: unexpected parse error, %v" , path , err )
34+ }
35+
36+ v := NewDefaultVisitor ()
37+ err = Walk (tree , v )
38+ if err != nil {
39+ t .Errorf ("%s: unexpected walk error, %v" , path , err )
40+ }
41+
42+ expectedPath := path + "_expected"
43+ e := map [string ]interface {}{}
44+
45+ b , err := ioutil .ReadFile (expectedPath )
46+ if err != nil {
47+ // ignore files that do not have an expected file
48+ return nil
49+ }
50+
51+ err = json .Unmarshal (b , & e )
52+ if err != nil {
53+ t .Errorf ("unexpected error during deserialization, %v" , err )
54+ }
55+
56+ for profile , tableIface := range e {
57+ p , ok := v .Sections .GetSection (profile )
58+ if ! ok {
59+ t .Fatal ("could not find profile " + profile )
60+ }
61+
62+ table := tableIface .(map [string ]interface {})
63+ for k , v := range table {
64+ switch e := v .(type ) {
65+ case string :
66+ a := p .String (k )
67+ if e != a {
68+ t .Errorf ("%s: expected %v, but received %v" , path , e , a )
69+ }
70+ case int :
71+ a := p .Int (k )
72+ if int64 (e ) != a {
73+ t .Errorf ("%s: expected %v, but received %v" , path , e , a )
74+ }
75+ case float64 :
76+ v := p .values [k ]
77+ if v .Type == IntegerType {
78+ a := p .Int (k )
79+ if int64 (e ) != a {
80+ t .Errorf ("%s: expected %v, but received %v" , path , e , a )
81+ }
82+ } else {
83+ a := p .Float64 (k )
84+ if e != a {
85+ t .Errorf ("%s: expected %v, but received %v" , path , e , a )
86+ }
87+ }
88+ default :
89+ t .Errorf ("unexpected type: %T" , e )
90+ }
91+ }
92+ }
93+
94+ return nil
95+ })
96+ }
97+
98+ func TestInvalidDataFiles (t * testing.T ) {
1399 cases := []struct {
14100 path string
15101 expectedParseError bool
16102 expectedWalkError bool
17103 }{
18- {
19- path : "./testdata/valid/empty_profile" ,
20- },
21- {
22- path : "./testdata/valid/array_profile" ,
23- },
24- {
25- path : "./testdata/valid/simple_profile" ,
26- },
27- {
28- path : "./testdata/valid/arn_profile" ,
29- },
30- {
31- path : "./testdata/valid/commented_profile" ,
32- },
33- {
34- path : "./testdata/valid/sections_profile" ,
35- },
36- {
37- path : "./testdata/valid/number_lhs_expr" ,
38- },
39- {
40- path : "./testdata/valid/base_numbers_profile" ,
41- },
42- {
43- path : "./testdata/valid/exponent_profile" ,
44- },
45- {
46- path : "./testdata/valid/escaped_profile" ,
47- },
48- {
49- path : "./testdata/valid/global_values_profile" ,
50- },
51- {
52- path : "./testdata/valid/utf_8_profile" ,
53- },
54- {
55- path : "./testdata/valid/profile_name" ,
56- },
57104 {
58105 path : "./testdata/invalid/bad_syntax_1" ,
59106 expectedParseError : true ,
@@ -74,6 +121,7 @@ func TestDataFiles(t *testing.T) {
74121 if err != nil {
75122 t .Errorf ("unexpected error, %v" , err )
76123 }
124+ defer f .Close ()
77125
78126 tree , err := ParseAST (f )
79127 if err != nil && ! c .expectedParseError {
@@ -88,64 +136,9 @@ func TestDataFiles(t *testing.T) {
88136
89137 v := NewDefaultVisitor ()
90138 err = Walk (tree , v )
91- if err != nil && ! c .expectedWalkError {
92- t .Errorf ("%d: unexpected error, %v" , i + 1 , err )
93- } else if err == nil && c .expectedWalkError {
139+ if err == nil && c .expectedWalkError {
94140 t .Errorf ("%d: expected error, but received none" , i + 1 )
95141 }
96-
97- expectedPath := c .path + "_expected"
98- e := map [string ]interface {}{}
99-
100- b , err := ioutil .ReadFile (expectedPath )
101- if err != nil {
102- return
103- }
104-
105- err = json .Unmarshal (b , & e )
106- if err != nil {
107- t .Errorf ("unexpected error during deserialization, %v" , err )
108- }
109-
110- for profile , tableIface := range e {
111- p , ok := v .Sections .GetSection (profile )
112- if ! ok {
113- t .Fatal ("could not find profile " + profile )
114- }
115-
116- table := tableIface .(map [string ]interface {})
117- for k , v := range table {
118- switch e := v .(type ) {
119- case string :
120- a := p .String (k )
121- if e != a {
122- t .Errorf ("%d: expected %v, but received %v" , i + 1 , e , a )
123- }
124- case int :
125- a := p .Int (k )
126- if int64 (e ) != a {
127- t .Errorf ("%d: expected %v, but received %v" , i + 1 , e , a )
128- }
129- case float64 :
130- v := p .values [k ]
131- if v .Type == IntegerType {
132- a := p .Int (k )
133- if int64 (e ) != a {
134- t .Errorf ("%d: expected %v, but received %v" , i + 1 , e , a )
135- }
136- } else {
137- a := p .Float64 (k )
138- if e != a {
139- t .Errorf ("%d: expected %v, but received %v" , i + 1 , e , a )
140- }
141- }
142- default :
143- t .Errorf ("unexpected type: %T" , e )
144- }
145- }
146- }
147-
148- f .Close ()
149142 })
150143 }
151144}
0 commit comments