hopp/generate/parse_test.go

69 lines
1.7 KiB
Go

package generate
import "fmt"
import "strings"
import "testing"
import "git.tebibyte.media/sashakoshka/goparse"
func TestParse(test *testing.T) {
correct := defaultProtocol()
correct.Messages[0x0000] = Message {
Name: "Connect",
Type: TypeTableDefined {
Fields: map[uint16] Field {
0x0000: Field { Name: "Name", Type: TypeNamed { Name: "String" } },
0x0001: Field { Name: "Password", Type: TypeNamed { Name: "String" } },
},
},
}
correct.Messages[0x0001] = Message {
Name: "UserList",
Type: TypeTableDefined {
Fields: map[uint16] Field {
0x0000: Field { Name: "Users", Type: TypeArray { Element: TypeNamed { Name: "User" } } },
},
},
}
correct.Types["User"] = TypeTableDefined {
Fields: map[uint16] Field {
0x0000: Field { Name: "Name", Type: TypeNamed { Name: "String" } },
0x0001: Field { Name: "Bio", Type: TypeNamed { Name: "String" } },
0x0002: Field { Name: "Followers", Type: TypeNamed { Name: "U32" } },
},
}
test.Log("CORRECT:", &correct)
got, err := ParseReader(strings.NewReader(`
M0000 Connect {
0000 Name String,
0001 Password String,
}
M0001 UserList {
0000 Users []User,
}
User {
0000 Name String,
0001 Bio String,
0002 Followers U32,
}
`))
if err != nil { test.Fatal(parse.Format(err)) }
test.Log("GOT: ", got)
correctStr := fmt.Sprint(&correct)
gotStr := fmt.Sprint(got)
if correctStr != gotStr {
test.Error("not equal")
for index := range min(len(correctStr), len(gotStr)) {
if correctStr[index] == gotStr[index] { continue }
test.Log("C:", correctStr[max(0, index - 8):min(len(correctStr), index + 8)])
test.Log("G:", gotStr[max(0, index - 8):min(len(gotStr), index + 8)])
break
}
test.FailNow()
}
}