File: walker_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.16.18%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports, experimental
  • size: 93,084 kB
  • sloc: ruby: 193; makefile: 174; xml: 11
file content (144 lines) | stat: -rw-r--r-- 3,078 bytes parent folder | download
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// +build go1.7

package ini

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestValidDataFiles(t *testing.T) {
	const expectedFileSuffix = "_expected"
	filepath.Walk("./testdata/valid", func(path string, info os.FileInfo, err error) error {
		if strings.HasSuffix(path, expectedFileSuffix) {
			return nil
		}

		if info.IsDir() {
			return nil
		}

		f, err := os.Open(path)
		if err != nil {
			t.Errorf("%s: unexpected error, %v", path, err)
		}
		defer f.Close()

		tree, err := ParseAST(f)
		if err != nil {
			t.Errorf("%s: unexpected parse error, %v", path, err)
		}

		v := NewDefaultVisitor()
		err = Walk(tree, v)
		if err != nil {
			t.Errorf("%s: unexpected walk error, %v", path, err)
		}

		expectedPath := path + "_expected"
		e := map[string]interface{}{}

		b, err := ioutil.ReadFile(expectedPath)
		if err != nil {
			// ignore files that do not have an expected file
			return nil
		}

		err = json.Unmarshal(b, &e)
		if err != nil {
			t.Errorf("unexpected error during deserialization, %v", err)
		}

		for profile, tableIface := range e {
			p, ok := v.Sections.GetSection(profile)
			if !ok {
				t.Fatal("could not find profile " + profile)
			}

			table := tableIface.(map[string]interface{})
			for k, v := range table {
				switch e := v.(type) {
				case string:
					a := p.String(k)
					if e != a {
						t.Errorf("%s: expected %v, but received %v", path, e, a)
					}
				case int:
					a := p.Int(k)
					if int64(e) != a {
						t.Errorf("%s: expected %v, but received %v", path, e, a)
					}
				case float64:
					v := p.values[k]
					if v.Type == IntegerType {
						a := p.Int(k)
						if int64(e) != a {
							t.Errorf("%s: expected %v, but received %v", path, e, a)
						}
					} else {
						a := p.Float64(k)
						if e != a {
							t.Errorf("%s: expected %v, but received %v", path, e, a)
						}
					}
				default:
					t.Errorf("unexpected type: %T", e)
				}
			}
		}

		return nil
	})
}

func TestInvalidDataFiles(t *testing.T) {
	cases := []struct {
		path               string
		expectedParseError bool
		expectedWalkError  bool
	}{
		{
			path:               "./testdata/invalid/bad_syntax_1",
			expectedParseError: true,
		},
		{
			path:               "./testdata/invalid/incomplete_section_profile",
			expectedParseError: true,
		},
		{
			path:               "./testdata/invalid/syntax_error_comment",
			expectedParseError: true,
		},
	}

	for i, c := range cases {
		t.Run(c.path, func(t *testing.T) {
			f, err := os.Open(c.path)
			if err != nil {
				t.Errorf("unexpected error, %v", err)
			}
			defer f.Close()

			tree, err := ParseAST(f)
			if err != nil && !c.expectedParseError {
				t.Errorf("%d: unexpected error, %v", i+1, err)
			} else if err == nil && c.expectedParseError {
				t.Errorf("%d: expected error, but received none", i+1)
			}

			if c.expectedParseError {
				return
			}

			v := NewDefaultVisitor()
			err = Walk(tree, v)
			if err == nil && c.expectedWalkError {
				t.Errorf("%d: expected error, but received none", i+1)
			}
		})
	}
}