|
| 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 | +} |
0 commit comments