forked from codecrafters-io/tester-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester_definition.go
73 lines (57 loc) · 1.54 KB
/
tester_definition.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
package tester_utils
import (
"io/ioutil"
"github.com/mitchellh/go-testing-interface"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
type TesterDefinition struct {
// Example: spawn_redis_server.sh
ExecutableFileName string
Stages []Stage
AntiCheatStages []Stage
}
func (t TesterDefinition) StageBySlug(slug string) Stage {
for _, stage := range t.Stages {
if stage.Slug == slug {
return stage
}
}
return Stage{}
}
type stageYAML struct {
Slug string `yaml:"slug"`
Title string `yaml:"name"`
}
type courseYAML struct {
Stages []stageYAML `yaml:"stages"`
}
// TestAgainstYaml tests whether the stage slugs in TesterDefintion match those in the course YAML at yamlPath.
func (testerDefinition TesterDefinition) TestAgainstYAML(t testing.T, yamlPath string) {
bytes, err := ioutil.ReadFile(yamlPath)
if err != nil {
t.Fatal(err)
}
c := courseYAML{}
if err := yaml.Unmarshal(bytes, &c); err != nil {
t.Fatal(err)
}
slugsInYaml := []string{}
for _, stage := range c.Stages {
slugsInYaml = append(slugsInYaml, stage.Slug)
}
slugsInDefinition := []string{}
for _, stage := range testerDefinition.Stages {
slugsInDefinition = append(slugsInDefinition, stage.Slug)
}
if !assert.Equal(t, slugsInYaml, slugsInDefinition) {
return
}
for stageIndex, _ := range c.Stages {
assert.Equal(t, stageIndex+1, testerDefinition.Stages[stageIndex].Number)
}
for _, stage := range c.Stages {
stageInDefinition := testerDefinition.StageBySlug(stage.Slug)
assert.Equal(t, stage.Title, stageInDefinition.Title)
}
}