-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner_test.go
119 lines (101 loc) · 3.01 KB
/
runner_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2024 Highlander Paiva. All rights reserved.
// Use of this source code is governed by the MIT License that can be found in
// the LICENSE file.
package goaoc_test
import (
"errors"
"testing"
"github.com/hvpaiva/goaoc"
"github.com/hvpaiva/goaoc/mock"
)
func TestRunWithInvalidParts(t *testing.T) {
testCases := []struct {
name string
part string
expectErr string
}{
{"PartNotSpecified", "0", "invalid part: 0. The valid parts are (1/2)"},
{"WrongPartDefined", "3", "invalid part: 3. The valid parts are (1/2)"},
{"WrongPartTypeString", "ss", "invalid part type. The part type allowed is int"},
{"WrongPartTypeEmpty", "", "invalid part type. The part type allowed is int"},
{"WrongPartTypeStillString", "true", "invalid part type. The part type allowed is int"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mok := mock.NewManager(tc.part, nil, nil)
err := goaoc.Run("input", mockPartOne, mockPartTwo, goaoc.WithManager(&mok))
if err == nil || err.Error() != tc.expectErr {
t.Fatalf("Expected error '%s', but got: %v", tc.expectErr, err)
}
})
}
}
func TestRunWithErrors(t *testing.T) {
testCases := []struct {
name string
part string
selectErr error
outputErr error
expectErr string
}{
{"SelectingPartError", "2", errors.New("error when calling Read"), nil, "error when calling Read"},
{"OutputError", "1", nil, errors.New("output failed"), "output failed"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mok := mock.NewManager(tc.part, tc.selectErr, tc.outputErr)
err := goaoc.Run("input", mockPartOne, mockPartTwo, goaoc.WithManager(&mok))
if err == nil || err.Error() != tc.expectErr {
t.Fatalf("Expected error '%s', but got: %v", tc.expectErr, err)
}
})
}
}
func TestRunWithValidPart(t *testing.T) {
testCases := []struct {
name string
part string
expectedOutput string
copiedValue string
}{
{"PartOne", "1", "The challenge result is 42\n", "42"},
{"PartTwo", "2", "The challenge result is 24\n", "24"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mok := mock.NewManager(tc.part, nil, nil)
err := goaoc.Run("input", mockPartOne, mockPartTwo, goaoc.WithManager(&mok))
if err != nil {
t.Fatalf("Unexpected error when part is valid: %v", err)
}
output := mok.GetStdout()
expectedOutput := tc.expectedOutput
if output != expectedOutput {
t.Errorf("Expected output '%s', but got '%s'", expectedOutput, output)
}
})
}
}
func TestRunWithDefaultManager(t *testing.T) {
testCases := []struct {
name string
part int
}{
{"PartOne", 1},
{"PartTwo", 2},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := goaoc.Run("input", mockPartOne, mockPartTwo, goaoc.WithPart(tc.part))
if err != nil {
t.Fatalf("Unexpected error when part is valid: %v", err)
}
})
}
}
func mockPartOne(_ string) int {
return 42
}
func mockPartTwo(_ string) int {
return 24
}