Skip to content

Commit 70aae2c

Browse files
committedNov 16, 2021
move testing from advent-of-code-2020
1 parent e68f862 commit 70aae2c

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
 

‎testing/error.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package testing
2+
3+
import "fmt"
4+
5+
// FixtureFileNotFoundError occurs when the requested fixture file does not exist.
6+
type FixtureFileNotFoundError struct {
7+
Path string
8+
Name string
9+
}
10+
11+
func (e FixtureFileNotFoundError) Error() string {
12+
return fmt.Sprintf("Fixture file does not exist: %s/fixtures/%s", e.Path, e.Name)
13+
}

‎testing/fixture.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package testing
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
)
8+
9+
// LoadFixture loads the content of a fixture file.
10+
func LoadFixture(name string) ([]byte, error) {
11+
content, err := ioutil.ReadFile(fmt.Sprintf("fixtures/%s", name))
12+
if err != nil {
13+
path, _ := os.Getwd()
14+
15+
return []byte{}, FixtureFileNotFoundError{path, name}
16+
}
17+
18+
return content, nil
19+
}
20+
21+
// Must be content. Basically go and panic if error happened.
22+
func Must(content []byte, err error) []byte {
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
return content
28+
}

‎testing/fixture_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package testing_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
test "github.com/yitsushi/go-aoc/testing"
8+
)
9+
10+
func TestLoadFixture_found(t *testing.T) {
11+
data, err := test.LoadFixture("data")
12+
if err != nil {
13+
t.Errorf("TestLoadFixture_found should find the 'data' file")
14+
}
15+
16+
expected := "yey\n"
17+
if string(data) != expected {
18+
t.Errorf("Expected file content: %s, got: %s", expected, data)
19+
}
20+
}
21+
22+
func TestLoadFixture_notFound(t *testing.T) {
23+
data, err := test.LoadFixture("nodata")
24+
if err == nil {
25+
t.Errorf("TestLoadFixture_notFound should not find the 'nodata' file")
26+
}
27+
28+
workingDirectory, _ := os.Getwd()
29+
30+
expectedError := test.FixtureFileNotFoundError{
31+
Path: workingDirectory,
32+
Name: "nodata",
33+
}
34+
35+
if err.Error() != expectedError.Error() {
36+
t.Errorf("expected error: '%s', got '%s'", expectedError.Error(), err.Error())
37+
}
38+
39+
expected := ""
40+
if string(data) != expected {
41+
t.Errorf("Expected file content: %s, got: %s", expected, data)
42+
}
43+
}
44+
45+
func TestMust_noError(t *testing.T) {
46+
input := []byte("Yey")
47+
expected := "Yey"
48+
49+
output := test.Must(input, nil)
50+
if string(output) != expected {
51+
t.Errorf("Must() = %v, want %v", output, expected)
52+
}
53+
}
54+
55+
func TestMust_hasError(t *testing.T) {
56+
defer func() {
57+
if r := recover(); r == nil {
58+
t.Errorf("TestMust_hasError should have panicked!")
59+
}
60+
}()
61+
62+
_ = test.Must([]byte{}, test.FixtureFileNotFoundError{Path: "/path", Name: "file"})
63+
}

‎testing/fixtures/data

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yey

0 commit comments

Comments
 (0)
Please sign in to comment.