From 0e8563b21673f65a7a5baf2de496fd0b1cb1ec7d Mon Sep 17 00:00:00 2001 From: Kharec Date: Mon, 8 Dec 2025 22:39:11 +0100 Subject: [PATCH] test: add printing unit test for part two (as it returns string) --- internal/2016/DayTwo/code_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/internal/2016/DayTwo/code_test.go b/internal/2016/DayTwo/code_test.go index 3b5cbca..3265833 100644 --- a/internal/2016/DayTwo/code_test.go +++ b/internal/2016/DayTwo/code_test.go @@ -1,6 +1,9 @@ package daytwo import ( + "bytes" + "os" + "strings" "testing" ) @@ -13,3 +16,27 @@ func TestPartOne(t *testing.T) { t.Errorf("PartOne() = %d, want %d", got, expected) } } + +func TestPartTwo(t *testing.T) { + expected := "5DB3" + + oldStdout := os.Stdout + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("Failed to create pipe: %v", err) + } + os.Stdout = w + + PartTwo(testInput) + + _ = 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) + } +}