diff options
Diffstat (limited to 'aoc/inputters_test.go')
| -rw-r--r-- | aoc/inputters_test.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/aoc/inputters_test.go b/aoc/inputters_test.go new file mode 100644 index 0000000..2230b1e --- /dev/null +++ b/aoc/inputters_test.go @@ -0,0 +1,53 @@ +package aoc_test + +import ( + "bufio" + "context" + "strconv" + "strings" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "mvinkio.online/aoc/aoc" +) + +func newScanner(input string) *bufio.Scanner { + return bufio.NewScanner(strings.NewReader(input)) +} + +var _ = Describe("Inputters", func() { + Describe("Bylines Inputter", func() { + in := aoc.ReadByLine[int](func(line string) (int, error) { + if i, err := strconv.Atoi(line); err != nil { + return 0, err + } else { + return i, nil + } + }) + It("should map each line in the input to the expected type", func() { + data, err := in.Read(context.TODO(), newScanner("1\n1\n8\n3\n5\n8")) + Expect(err).To(BeNil()) + Expect(data).To(Equal([]int{1, 1, 8, 3, 5, 8})) + }) + }) + + Describe("Lines Inputter", func() { + in := aoc.ReadLines[[]int](func(lines []string) ([]int, error) { + ints := make([]int, len(lines)) + for i, l := range lines { + t, err := strconv.Atoi(l) + if err != nil { + return nil, err + } + ints[i] = t + } + return ints, nil + }) + It("Should correct map to type given all lines", func() { + data, err := in.Read(context.TODO(), newScanner("1\n1\n8\n3\n5\n8")) + Expect(err).To(BeNil()) + Expect(data).To(Equal([]int{1, 1, 8, 3, 5, 8})) + }) + }) +}) |
