diff --git a/internal/2018/DayTwo/code_test.go b/internal/2018/DayTwo/code_test.go index 17d6195..15f99b8 100644 --- a/internal/2018/DayTwo/code_test.go +++ b/internal/2018/DayTwo/code_test.go @@ -1,21 +1,58 @@ package daytwo -import "testing" - -var testInput = []string{ - "abcdef", - "bababc", - "abbcde", - "abcccd", - "aabcdd", - "abcdee", - "ababab", -} +import ( + "bytes" + "os" + "strings" + "testing" +) func TestPartOne(t *testing.T) { + input := []string{ + "abcdef", + "bababc", + "abbcde", + "abcccd", + "aabcdd", + "abcdee", + "ababab", + } expected := 12 - got := PartOne(testInput) + got := PartOne(input) if got != expected { t.Errorf("PartOne() = %d, want %d", got, expected) } } + +func TestPartTwo(t *testing.T) { + input := []string{ + "abcde", + "fghij", + "klmno", + "pqrst", + "fguij", + "axcye", + "wvxyz", + } + expected := "fgij" + + oldStdout := os.Stdout + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("Failed to create pipe: %v", err) + } + os.Stdout = w + + PartTwo(input) + + w.Close() + os.Stdout = oldStdout + + var buffer bytes.Buffer + buffer.ReadFrom(r) + got := strings.TrimSpace(buffer.String()) + + if got != expected { + t.Errorf("PartTwo() printed %q, want %q", got, expected) + } +}