generate: Run encoder output through decoder in tests
This commit is contained in:
@@ -78,6 +78,42 @@ func testGenerateRun(test *testing.T, protocol *Protocol, title, imports, testCa
|
||||
log.Fatalln("not equal")
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: possibly combine the two above functions into this one,
|
||||
// also take a data parameter here (snake)
|
||||
func testEncodeDecode(message Message, data tu.Snake) {buffer := bytes.Buffer { }
|
||||
log.Println("encoding:")
|
||||
encoder := tape.NewEncoder(&buffer)
|
||||
n, err := message.Encode(encoder)
|
||||
if err != nil { log.Fatalf("at %d: %v\n", n, err) }
|
||||
encoder.Flush()
|
||||
got := buffer.Bytes()
|
||||
log.Printf("got: [%s]", tu.HexBytes(got))
|
||||
log.Println("correct:", data)
|
||||
if n != len(got) {
|
||||
log.Fatalf("n incorrect: %d != %d\n", n, len(got))
|
||||
}
|
||||
if ok, n := data.Check(got); !ok {
|
||||
log.Fatalln("not equal at", n)
|
||||
}
|
||||
|
||||
log.Println("decoding:")
|
||||
destination := reflect.New(reflect.ValueOf(message).Elem().Type()).Interface().(Message)
|
||||
flat := data.Flatten()
|
||||
log.Println("before: ", destination)
|
||||
decoder := tape.NewDecoder(bytes.NewBuffer(flat))
|
||||
n, err = destination.Decode(decoder)
|
||||
if err != nil { log.Fatalf("at %d: %v\n", n, err) }
|
||||
log.Println("got: ", destination)
|
||||
log.Println("correct:", message)
|
||||
if n != len(flat) {
|
||||
log.Fatalf("n incorrect: %d != %d\n", n, len(flat))
|
||||
}
|
||||
if !reflect.DeepEqual(destination, message) {
|
||||
log.Fatalln("not equal")
|
||||
}
|
||||
|
||||
}
|
||||
`
|
||||
fmt.Fprintf(
|
||||
mainFile, "package main\n%s\nfunc main() {\n%s\n%s\n%s\n}\n%s",
|
||||
|
||||
Reference in New Issue
Block a user