test: add printing unit test for part two (as it returns string)

This commit is contained in:
2025-12-08 22:39:11 +01:00
parent 6720bbabc1
commit 0e8563b216

View File

@@ -1,6 +1,9 @@
package daytwo package daytwo
import ( import (
"bytes"
"os"
"strings"
"testing" "testing"
) )
@@ -13,3 +16,27 @@ func TestPartOne(t *testing.T) {
t.Errorf("PartOne() = %d, want %d", got, expected) 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)
}
}