test: add tests for csv reader and writer

This commit is contained in:
2025-08-31 15:20:52 +03:00
parent 454c632462
commit fe9568d7ae
4 changed files with 73 additions and 0 deletions

42
output_csv_test.go Normal file
View File

@@ -0,0 +1,42 @@
package script
import (
"bytes"
"os"
"testing"
)
func TestCSVWriter(t *testing.T) {
path := "testdata/output_csv.csv"
os.Remove(path)
defer os.Remove(path)
w, err := newCSVwriter(path)
if err != nil {
t.Fatal(err)
}
want := []byte("one,two,three\none,two,three\n")
row := []string{"one", "two", "three"}
for range 2 {
if err := w.Write(row); err != nil {
t.Fatal(err)
}
}
if err := w.Close(); err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(path)
if err != nil {
t.Fatalf("err reading output file: %s", err)
}
if !bytes.Equal(want, b) {
t.Errorf("incorrect result, want: %s, have: %s", string(want), string(b))
}
}