test: add printing unit test for part two (as it returns string)
This commit is contained in:
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user