2 Commits

Author SHA1 Message Date
MSWS
89c940dfc8 test: Add and update UCI engine output tests
- Update `TestInit` in `uci/engine_test.go` to use `out` instead of `in` for expected output source
- Add `TestPrint` test to validate output generation of a custom message ("Hello, World!")
- Add `TestCopyProtection` test to ensure `CopyProtection` method outputs "copyprotection ok"

[uci/engine_test.go]
- Replaced `in` with `out` in the `TestInit` function to align with the expected output source.
- Added a new `TestPrint` function to test output generation for a custom message ("Hello, World!").
- Added a new `TestCopyProtection` function to verify the output of the `CopyProtection` method, ensuring it produces "copyprotection ok".
2025-03-30 21:45:00 -07:00
MSWS
137e694a38 test: Refactor TestInit with reusable helper functions
- Refactor `TestInit` in `uci/engine_test.go` for improved readability and maintainability by introducing helper functions (`expectLine`, `readLine`) for input handling and validation
- Add `strings` package import to support string manipulation in input/output processing

[uci/engine_test.go]
- Added the `strings` package import, likely for string manipulation on input or output data.
- Rewritten the `TestInit` function to use helper functions (`expectLine`) instead of manually evaluating read results:
  - Improved readability and structure by separating line reading and validation logic.
- Introduced a new `expectLine` function to handle assertions on expected input, providing clearer error messages for test failures.
- Created a new `readLine` function to encapsulate the logic for reading a line of input and trimming newline characters, promoting code reusability and cleanliness.
- Removed manual error handling and data validation in `TestInit` by delegating these responsibilities to the new helper functions.
2025-03-30 21:42:56 -07:00

View File

@@ -2,6 +2,7 @@ package uci
import (
"io"
"strings"
"testing"
)
@@ -13,22 +14,48 @@ func base() (Engine, io.Reader, io.Writer) {
}
func TestInit(t *testing.T) {
e, in, _ := base()
e, out, _ := base()
go e.Init()
buf := make([]byte, 1024)
n, err := in.Read(buf)
expectLine(t, out, "id name GoChess2")
expectLine(t, out, "id author MS")
expectLine(t, out, "uciok")
}
func TestPrint(t *testing.T) {
e, out, _ := base()
go e.Print("Hello, World!")
expectLine(t, out, "Hello, World!")
}
func TestCopyProtection(t *testing.T) {
e, out, _ := base()
go e.CopyProtection()
expectLine(t, out, "copyprotection ok")
}
func expectLine(t *testing.T, in io.Reader, expected string) {
line, err := readLine(in)
if err != nil {
t.Error(err)
t.Fatalf("Error reading line: %v", err)
}
if n == 0 {
t.Error("No data read")
}
if string(buf[:n]) != "id name GoChess2\n" {
t.Error("Unexpected output, got:", string(buf[:n]))
if line != expected {
t.Fatalf("Expected '%s', got '%s'", expected, line)
}
}
func readLine(in io.Reader) (string, error) {
buf := make([]byte, 1024)
n, err := in.Read(buf)
if err != nil {
return "", err
}
return strings.Trim(string(buf[:n]), "\n"), nil
}