package main import ( "bytes" "fmt" "io/ioutil" "path" "testing" ) func exampleFiles() []string { files, err := ioutil.ReadDir("example") if err != nil { panic(err) } names := []string{} for _, file := range files { names = append(names, path.Join("example", file.Name())) } return names } func referenceFile(infile string, suffix string) string { return path.Join("test", path.Base(infile)+suffix) } func readFile(t *testing.T, file string) string { bytes, err := ioutil.ReadFile(file) if err != nil { t.Error(err) } return string(bytes) } func TestLexer(t *testing.T) { for _, file := range exampleFiles() { t.Run(file, func(t *testing.T) { input := readFile(t, file) var buf bytes.Buffer DebugLexer([]rune(input), &buf) output := buf.String() reference := readFile(t, referenceFile(file, ".lexer")) if output != reference { t.Fail() } }) } } func TestParser(t *testing.T) { for _, file := range exampleFiles() { t.Run(file, func(t *testing.T) { input := readFile(t, file) var buf bytes.Buffer DebugParser([]rune(input), &buf) output := buf.String() reference := readFile(t, referenceFile(file, ".parser")) if output != reference { t.Fail() } }) } } func TestSearch(t *testing.T) { available := map[float64]int{ 20: 26, 10: 2, 5: 2, 2.5: 2, 1.25: 2, 0.5: 2, } tests := map[float64][]float64{ 17: []float64{5, 2.5, 0.5}, 42: []float64{20, 0.5}, 80: []float64{20, 20}, } for input, reference := range tests { t.Run("search"+fmt.Sprint(input), func(t *testing.T) { output := SearchPlates(available, input) if len(reference) != len(output) { t.Fail() } for i := 0; i < len(reference); i++ { if reference[i] != output[i] { t.Fail() } } }) } }