|
| 1 | +package command_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + |
| 13 | + "github.com/obalunenko/advent-of-code/internal/command" |
| 14 | + "github.com/obalunenko/advent-of-code/internal/puzzles" |
| 15 | + "github.com/obalunenko/advent-of-code/internal/puzzles/input" |
| 16 | +) |
| 17 | + |
| 18 | +// Custom type that allows setting the func that our Mock Do func will run instead |
| 19 | +type dofunc func(req *http.Request) (*http.Response, error) |
| 20 | + |
| 21 | +// MockClient is the mock client |
| 22 | +type mockHTTPClient struct { |
| 23 | + MockDo dofunc |
| 24 | +} |
| 25 | + |
| 26 | +// Overriding what the Do function should "do" in our MockClient |
| 27 | +func (m *mockHTTPClient) Do(req *http.Request) (*http.Response, error) { |
| 28 | + return m.MockDo(req) |
| 29 | +} |
| 30 | + |
| 31 | +type mockSolver struct { |
| 32 | + year string |
| 33 | + name string |
| 34 | +} |
| 35 | + |
| 36 | +func (m mockSolver) Year() string { |
| 37 | + return m.year |
| 38 | +} |
| 39 | + |
| 40 | +func (m mockSolver) Day() string { |
| 41 | + return m.name |
| 42 | +} |
| 43 | + |
| 44 | +func (m mockSolver) Part1(in io.Reader) (string, error) { |
| 45 | + split, err := read(in) |
| 46 | + if err != nil { |
| 47 | + return "", err |
| 48 | + } |
| 49 | + |
| 50 | + return split[1], nil |
| 51 | +} |
| 52 | + |
| 53 | +func (m mockSolver) Part2(in io.Reader) (string, error) { |
| 54 | + split, err := read(in) |
| 55 | + if err != nil { |
| 56 | + return "", err |
| 57 | + } |
| 58 | + |
| 59 | + return split[2], nil |
| 60 | +} |
| 61 | + |
| 62 | +func read(in io.Reader) ([]string, error) { |
| 63 | + all, err := io.ReadAll(in) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + split := strings.Split(string(all), ",") |
| 69 | + |
| 70 | + if len(split) != 3 { |
| 71 | + return nil, errors.New("wrong parts") |
| 72 | + } |
| 73 | + |
| 74 | + return split, nil |
| 75 | +} |
| 76 | + |
| 77 | +func TestRun(t *testing.T) { |
| 78 | + ctx := context.Background() |
| 79 | + |
| 80 | + year := "1992" |
| 81 | + day := "31" |
| 82 | + |
| 83 | + puzzles.Register(mockSolver{ |
| 84 | + year: year, |
| 85 | + name: day, |
| 86 | + }) |
| 87 | + |
| 88 | + t.Cleanup(func() { |
| 89 | + puzzles.UnregisterAllSolvers(t) |
| 90 | + }) |
| 91 | + |
| 92 | + r := io.NopCloser(strings.NewReader("1,2,3")) |
| 93 | + |
| 94 | + input.Client = &mockHTTPClient{ |
| 95 | + MockDo: func(req *http.Request) (*http.Response, error) { |
| 96 | + return &http.Response{ |
| 97 | + Status: http.StatusText(http.StatusOK), |
| 98 | + StatusCode: http.StatusOK, |
| 99 | + Proto: "HTTP/1.0", |
| 100 | + ProtoMajor: 1, |
| 101 | + ProtoMinor: 0, |
| 102 | + Header: nil, |
| 103 | + Body: r, |
| 104 | + ContentLength: 0, |
| 105 | + TransferEncoding: nil, |
| 106 | + Close: false, |
| 107 | + Uncompressed: false, |
| 108 | + Trailer: nil, |
| 109 | + Request: nil, |
| 110 | + TLS: nil, |
| 111 | + }, nil |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + got, err := command.Run(ctx, year, day) |
| 116 | + assert.NoError(t, err) |
| 117 | + |
| 118 | + assert.Equal(t, puzzles.Result{ |
| 119 | + Year: "1992", |
| 120 | + Name: "31", |
| 121 | + Part1: "2", |
| 122 | + Part2: "3", |
| 123 | + }, got) |
| 124 | +} |
0 commit comments