From 5cc89d5b4276f9e2581fb4d747aff68751d5ff58 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 31 Aug 2024 22:54:54 -0400 Subject: [PATCH] Add some tests --- asv_test.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 asv_test.go diff --git a/asv_test.go b/asv_test.go new file mode 100644 index 0000000..944197c --- /dev/null +++ b/asv_test.go @@ -0,0 +1,70 @@ +package asv + +import "io" +import "strings" +import "testing" +import "reflect" + +func TestDecodeCollection (test *testing.T) { + decoder := NewDecoder(strings.NewReader( +"record\x1fone\x1erecord\x1ftwo\x1drecord one in group two\x1crecord one in file two" + +"\x1flots of \x1b\x1b\x1b\x1f\x1b\x1d\x1b\x1e\x1b\x1cescape sequences")) + got, err := decoder.ReadCollection() + testErr(test, err) + testEqual(test, "structures", got, + Collection { + File { + Group { + Record { + "record", + "one", + }, + Record { + "record", + "two", + }, + }, + Group { + Record { + "record one in group two", + }, + }, + }, + File { + Group { + Record { + "record one in file two", + "lots of \x1b\x1f\x1d\x1e\x1cescape sequences", + }, + }, + }, + }) +} + +func TestDecodeRecord (test *testing.T) { + decoder := NewDecoder(strings.NewReader( +"record\x1fone")) + got, next, err := decoder.ReadRecord() + testEqual(test, "errors", err, io.EOF) + testEqual(test, "next", next, rune(0)) + testEqual(test, "structures", got, + Record { + "record", + "one", + }) +} + +func testErr (test *testing.T, err error) { + if err != nil { + test.Fatalf("unexpected error: %v", err) + } +} + +func testEqual (test *testing.T, desc string, got, correct any) { + if !reflect.DeepEqual(got, correct) { + test.Error(desc, "not equal") + test.Errorf("GOT:\n%v", got) + test.Errorf("CORRECT:\n%v", correct) + test.FailNow() + } +}