-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcompress_test.go
89 lines (78 loc) · 1.79 KB
/
compress_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
package grange
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
type RangeSpec struct {
path string
line int
expr string
results Result
}
func (spec *RangeSpec) String() string {
return fmt.Sprintf("%s:%d", spec.path, spec.line)
}
func (spec *RangeSpec) Ignore(ignore_list []string) bool {
for _, ignore := range ignore_list {
if strings.HasSuffix(spec.String(), ignore) {
return true
}
}
return false
}
func TestCompress(t *testing.T) {
spec_dir := os.Getenv("RANGE_SPEC_PATH")
if spec_dir == "" {
// Skip compress tests
fmt.Fprintln(os.Stderr, "Skipping Compress() tests, RANGE_SPEC_PATH not set.")
return
}
filepath.Walk(spec_dir+"/spec/compress", func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
return nil
}
specs, err := filepath.Glob(path + "/*.spec")
if err == nil && specs != nil {
for _, spec := range specs {
loadSpec(t, spec)
}
}
return nil
})
}
func runSpec(t *testing.T, spec RangeSpec) {
actual := Compress(&spec.results)
if actual != spec.expr {
t.Errorf("failed %s:%d\n got: %s\nwant: %s",
spec.path, spec.line, actual, spec.expr)
}
}
func loadSpec(t *testing.T, specpath string) {
file, _ := os.Open(specpath)
scanner := bufio.NewScanner(file)
currentSpec := RangeSpec{results: NewResult(), path: specpath}
line := 0
for scanner.Scan() {
line++
if strings.HasPrefix(strings.Trim(scanner.Text(), " "), "#") {
continue
} else if scanner.Text() == "" {
runSpec(t, currentSpec)
currentSpec = RangeSpec{results: NewResult(), path: specpath}
} else {
if currentSpec.expr == "" {
currentSpec.expr = scanner.Text()
currentSpec.line = line
} else {
currentSpec.results.Add(scanner.Text())
}
}
}
if currentSpec.expr != "" {
runSpec(t, currentSpec)
}
}