-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
go_analyzer_test.go
221 lines (196 loc) · 5.05 KB
/
go_analyzer_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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"encoding/json"
"fmt"
"net/http"
"path"
"strings"
"testing"
"github.com/exercism/go-analyzer/analyzer"
"github.com/exercism/go-analyzer/assets"
"github.com/exercism/go-analyzer/suggester/sugg"
"github.com/logrusorgru/aurora"
"github.com/pmezard/go-difflib/difflib"
"github.com/stretchr/testify/assert"
"github.com/tehsphinx/astrav"
)
// Tests contains the test cases.
var Tests http.FileSystem = http.Dir("tests")
var runOnly = ""
func TestAnalyze(t *testing.T) {
exercises, err := ExercisesWithTests()
if err != nil {
t.Fatal(err)
}
for _, exercise := range exercises {
paths, err := ExerciseTests(exercise)
if err != nil {
t.Error(err)
continue
}
for _, dir := range paths {
if runOnly != "" && runOnly != dir {
continue
}
t.Run(dir[6:], func(t *testing.T) {
res := analyzer.Analyze(exercise, dir)
// if a specific exercise is set print ast for orientation while implementing
if runOnly != "" {
if err := printAST(dir); err != nil {
t.Fatal(err)
}
}
expected, err := analyzer.GetResultFromFile(dir)
if err != nil {
t.Errorf("error getting TestResult for path %s: %s", dir, err)
}
var fail bool
if !assert.Equal(t, expected.Status, res.Status,
fmt.Sprintf("Wrong status on %s/solution.go:1\n\t-> severity: %d, rating: %.2f", dir, res.Severity, res.Rating)) {
fail = true
}
if checkContains(t, expected.Comments, res.Comments, "Missing comment", dir) {
fail = true
}
if checkContains(t, res.Comments, expected.Comments, "Additional comment", dir) {
fail = true
}
if checkContainsError(t, expected.Errors, res.Errors, dir) {
fail = true
}
if fail {
diff, err := resultDiff(*expected, res)
if err != nil {
t.Error(err)
}
t.Errorf("Diff on %s\n%s", dir, diff)
}
})
}
}
}
func checkContains(t *testing.T, search, container []sugg.Comment, message, dir string) (fail bool) {
for _, comment := range search {
var (
diff string
err error
contains = sugg.Contains(container, comment)
msg = message
)
if !contains {
cmt := getCommentIDOnly(container, comment.ID())
if cmt != nil {
msg = "Different parameters on comment"
diff, err = commentDiff(comment, cmt)
if err != nil {
fail = true
t.Error(err)
}
}
}
if !assert.True(t, contains, fmt.Sprintf("%s `%s` \n\t-> on %s/solution.go:1\n%s", msg, comment.ID(), dir, diff)) {
fail = true
}
}
return fail
}
func checkContainsError(t *testing.T, expected, got []string, dir string) (fail bool) {
for _, err := range expected {
var found bool
for _, gotErr := range got {
if strings.Contains(gotErr, err) {
found = true
}
}
if !assert.True(t, found, "missing error analyzing the solution %s: %s", dir, err) {
fail = true
}
}
for _, gotErr := range got {
var found bool
for _, err := range expected {
if strings.Contains(gotErr, err) {
found = true
}
}
if !assert.True(t, found, "unexpected error analyzing the solution %s: %s", dir, gotErr) {
fail = true
}
}
return fail
}
// ExercisesWithTests returns a list of exercise slugs for which tests are provided.
func ExercisesWithTests() ([]string, error) {
return assets.GetDirs(".", Tests)
}
// ExerciseTests returns a list of paths containing tests for given exercise.
func ExerciseTests(exercise string) ([]string, error) {
paths, err := assets.GetDirs(exercise, Tests)
if err != nil {
return nil, err
}
for i, dir := range paths {
paths[i] = path.Join("tests", exercise, dir)
}
return paths, err
}
func commentDiff(expected, got sugg.Comment) (string, error) {
expectedB, err := json.MarshalIndent(expected, "", "\t")
if err != nil {
return "", err
}
gotB, err := json.MarshalIndent(got, "", "\t")
if err != nil {
return "", err
}
return getDiff(expectedB, gotB), nil
}
func resultDiff(expected, got analyzer.Result) (string, error) {
expB, err := json.MarshalIndent(expected, "", "\t")
if err != nil {
return "", err
}
gotB, err := json.MarshalIndent(got, "", "\t")
if err != nil {
return "", err
}
diff := getDiff(expB, gotB)
return diff, nil
}
func getDiff(expected, got []byte) string {
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(string(expected)),
B: difflib.SplitLines(string(got)),
FromFile: "Expected",
ToFile: "Got",
Context: 0,
}
text, err := difflib.GetUnifiedDiffString(diff)
if err != nil {
return fmt.Sprintf("error while diffing strings: %s", err)
}
return text
}
func getCommentIDOnly(comments []sugg.Comment, id string) sugg.Comment {
for _, cmt := range comments {
if cmt.ID() == id {
return cmt
}
}
return nil
}
func printAST(dir string) error {
solution, err := analyzer.LoadPackage(dir)
if err != nil {
return err
}
solution.Walk(func(node astrav.Node) bool {
src := node.GetSourceString()
if i := strings.Index(src, "\n"); i != -1 {
src = src[:i]
}
fmt.Printf("%s%s\t\t%s\n", strings.Repeat("\t", node.Level()), aurora.Cyan(fmt.Sprintf("%T", node)), src)
return true
})
return nil
}