Compare commits
5 Commits
a4da33536c
...
52aa07a98f
| Author | SHA1 | Date | |
|---|---|---|---|
| 52aa07a98f | |||
| 94041f2abc | |||
| 423f547da3 | |||
| 9278bdcb43 | |||
| 0acf44886a |
@ -1,151 +1,15 @@
|
|||||||
package generate
|
package generate
|
||||||
|
|
||||||
// import "fmt"
|
// import "fmt"
|
||||||
import "strings"
|
|
||||||
import "testing"
|
import "testing"
|
||||||
import "git.tebibyte.media/sashakoshka/goparse"
|
|
||||||
|
|
||||||
var testGenerateCorrect =
|
// TODO: once everything has been ironed out, test that the public API of the
|
||||||
`package protocol
|
// generator is equal to something specific
|
||||||
|
|
||||||
/* # Do not edit this package by hand!
|
var exampleProtocol = defaultProtocol()
|
||||||
*
|
|
||||||
* This file was automatically generated by the Holanet PDL compiler. The
|
|
||||||
* source file is located at input.pdl
|
|
||||||
* Please edit that file instead, and re-compile it to this location.
|
|
||||||
*
|
|
||||||
* HOPP, TAPE, METADAPT, PDL/0 (c) 2025 holanet.xyz
|
|
||||||
*/
|
|
||||||
|
|
||||||
import "git.tebibyte.media/sashakoshka/hopp/tape"
|
func init() {
|
||||||
|
exampleProtocol.Messages[0x0000] = Message {
|
||||||
// Table is a KTV table with an undefined schema.
|
|
||||||
type Table map[uint16] any
|
|
||||||
|
|
||||||
// Message is any message that can be sent along this protocol.
|
|
||||||
type Message interface {
|
|
||||||
tape.Encodable
|
|
||||||
tape.Decodable
|
|
||||||
|
|
||||||
// Method returns the method code of the message.
|
|
||||||
Method() uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
// User represents the protocol data type User.
|
|
||||||
type User struct {
|
|
||||||
Name string
|
|
||||||
Bio string
|
|
||||||
Followers uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
// EncodeValue encodes the value of this type without the tag. The value is
|
|
||||||
// encoded according to the parameters specified by the tag, if possible.
|
|
||||||
func (this *User) EncodeValue(encoder *tape.Encoder) (n int, err error) {
|
|
||||||
nn, err := tape.WriteTableHeader(2)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
nn, err := encoder.WriteUint16(0x0000)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
nn, err := tape.WriteString(encoder, this.Name)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
nn, err := encoder.WriteUint16(0x0001)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
nn, err := tape.WriteString(encoder, this.Bio)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode replaces the data in this User with information from the decoder.
|
|
||||||
func (this *User) Decode(decoder *tape.Decoder) (n int, err error) {
|
|
||||||
pull, nn, err := tape.ReadTableHeader(decoder)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
|
|
||||||
for {
|
|
||||||
key, tag, end, nn, err := pull()
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
if end { break }
|
|
||||||
|
|
||||||
switch key {
|
|
||||||
case 0x0000:
|
|
||||||
value, nn, err := tape.ReadString(decoder)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
this.Name = value
|
|
||||||
case 0x0001:
|
|
||||||
value, nn, err := tape.ReadString(decoder)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
this.Bio = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MessageConnect represents the protocol message M0000 Connect.
|
|
||||||
type MessageConnect struct {
|
|
||||||
Name string
|
|
||||||
Password string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method returns the method code, M0000.
|
|
||||||
func (this *MessageConnect) Method() uint16 {
|
|
||||||
return 0x0000
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode encodes the message to the encoder.
|
|
||||||
func (this *MessageConnect) Encode(encoder *tape.Encoder) (n int, err error) {
|
|
||||||
nn, err := tape.WriteTableHeader(2)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
nn, err := encoder.WriteUint16(0x0000)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
nn, err := tape.WriteString(encoder, this.Name)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
nn, err := encoder.WriteUint16(0x0001)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
nn, err := tape.WriteString(encoder, this.Password)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode replaces the data in this message with information from the decoder.
|
|
||||||
func (this *MessageConnect) Decode(decoder *tape.Decoder) (n int, err error) {
|
|
||||||
pull, nn, err := tape.ReadTableHeader(decoder)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
|
|
||||||
for {
|
|
||||||
key, tag, end, nn, err := pull()
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
if end { break }
|
|
||||||
|
|
||||||
switch key {
|
|
||||||
case 0x0000:
|
|
||||||
value, nn, err := tape.ReadString(decoder)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
this.Name = value
|
|
||||||
case 0x0001:
|
|
||||||
value, nn, err := tape.ReadString(decoder)
|
|
||||||
n += nn; if err != nil { return n, err }
|
|
||||||
this.Password = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MessageUserList represents the protocol message M0001 UserList.
|
|
||||||
type MessageUserList struct {
|
|
||||||
Users []User
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method returns the method code, M0001.
|
|
||||||
func (this *MessageUserList) Method() uint16 {
|
|
||||||
return 0x0001
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO methods
|
|
||||||
`
|
|
||||||
|
|
||||||
func TestGenerate(test *testing.T) {
|
|
||||||
protocol := defaultProtocol()
|
|
||||||
protocol.Messages[0x0000] = Message {
|
|
||||||
Name: "Connect",
|
Name: "Connect",
|
||||||
Type: TypeTableDefined {
|
Type: TypeTableDefined {
|
||||||
Fields: map[uint16] Field {
|
Fields: map[uint16] Field {
|
||||||
@ -154,7 +18,7 @@ func TestGenerate(test *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
protocol.Messages[0x0001] = Message {
|
exampleProtocol.Messages[0x0001] = Message {
|
||||||
Name: "UserList",
|
Name: "UserList",
|
||||||
Type: TypeTableDefined {
|
Type: TypeTableDefined {
|
||||||
Fields: map[uint16] Field {
|
Fields: map[uint16] Field {
|
||||||
@ -162,59 +26,7 @@ func TestGenerate(test *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
protocol.Types["User"] = TypeTableDefined {
|
exampleProtocol.Messages[0x0002] = Message {
|
||||||
Fields: map[uint16] Field {
|
|
||||||
0x0000: Field { Name: "Name", Type: TypeString { } },
|
|
||||||
0x0001: Field { Name: "Bio", Type: TypeString { } },
|
|
||||||
0x0002: Field { Name: "Followers", Type: TypeInt { Bits: 32 } },
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
correct := testGenerateCorrect
|
|
||||||
|
|
||||||
builder := strings.Builder { }
|
|
||||||
generator := Generator { Output: &builder }
|
|
||||||
/* TODO test n: */ _, err := generator.Generate(&protocol)
|
|
||||||
if err != nil { test.Fatal(parse.Format(err)) }
|
|
||||||
got := builder.String()
|
|
||||||
|
|
||||||
test.Log("CORRECT:")
|
|
||||||
test.Log(correct)
|
|
||||||
test.Log("GOT:")
|
|
||||||
test.Log(got)
|
|
||||||
|
|
||||||
if correct != got {
|
|
||||||
test.Error("not equal")
|
|
||||||
for index := range min(len(correct), len(got)) {
|
|
||||||
if correct[index] == got[index] { continue }
|
|
||||||
test.Log("C:", correct[max(0, index - 8):min(len(correct), index + 8)])
|
|
||||||
test.Log("G:", got[max(0, index - 8):min(len(got), index + 8)])
|
|
||||||
break
|
|
||||||
}
|
|
||||||
test.FailNow()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGenerateRun(test *testing.T) {
|
|
||||||
protocol := defaultProtocol()
|
|
||||||
protocol.Messages[0x0000] = Message {
|
|
||||||
Name: "Connect",
|
|
||||||
Type: TypeTableDefined {
|
|
||||||
Fields: map[uint16] Field {
|
|
||||||
0x0000: Field { Name: "Name", Type: TypeString { } },
|
|
||||||
0x0001: Field { Name: "Password", Type: TypeString { } },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
protocol.Messages[0x0001] = Message {
|
|
||||||
Name: "UserList",
|
|
||||||
Type: TypeTableDefined {
|
|
||||||
Fields: map[uint16] Field {
|
|
||||||
0x0000: Field { Name: "Users", Type: TypeArray { Element: TypeNamed { Name: "User" } } },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
protocol.Messages[0x0002] = Message {
|
|
||||||
Name: "Pulse",
|
Name: "Pulse",
|
||||||
Type: TypeTableDefined {
|
Type: TypeTableDefined {
|
||||||
Fields: map[uint16] Field {
|
Fields: map[uint16] Field {
|
||||||
@ -226,11 +38,11 @@ func TestGenerateRun(test *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
protocol.Messages[0x0003] = Message {
|
exampleProtocol.Messages[0x0003] = Message {
|
||||||
Name: "NestedArray",
|
Name: "NestedArray",
|
||||||
Type: TypeArray { Element: TypeArray { Element: TypeInt { Bits: 8 } } },
|
Type: TypeArray { Element: TypeArray { Element: TypeInt { Bits: 8 } } },
|
||||||
}
|
}
|
||||||
protocol.Messages[0x0004] = Message {
|
exampleProtocol.Messages[0x0004] = Message {
|
||||||
Name: "Integers",
|
Name: "Integers",
|
||||||
Type: TypeTableDefined {
|
Type: TypeTableDefined {
|
||||||
Fields: map[uint16] Field {
|
Fields: map[uint16] Field {
|
||||||
@ -250,14 +62,17 @@ func TestGenerateRun(test *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
protocol.Types["User"] = TypeTableDefined {
|
exampleProtocol.Types["User"] = TypeTableDefined {
|
||||||
Fields: map[uint16] Field {
|
Fields: map[uint16] Field {
|
||||||
0x0000: Field { Name: "Name", Type: TypeString { } },
|
0x0000: Field { Name: "Name", Type: TypeString { } },
|
||||||
0x0001: Field { Name: "Bio", Type: TypeString { } },
|
0x0001: Field { Name: "Bio", Type: TypeString { } },
|
||||||
0x0002: Field { Name: "Followers", Type: TypeInt { Bits: 32 } },
|
0x0002: Field { Name: "Followers", Type: TypeInt { Bits: 32 } },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
testGenerateRun(test, &protocol, `
|
}
|
||||||
|
|
||||||
|
func TestGenerateRunEncode(test *testing.T) {
|
||||||
|
testGenerateRun(test, &exampleProtocol, "encode", `
|
||||||
// imports
|
// imports
|
||||||
`, `
|
`, `
|
||||||
// test case
|
// test case
|
||||||
@ -384,3 +199,131 @@ func TestGenerateRun(test *testing.T) {
|
|||||||
))
|
))
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenerateRunDecode(test *testing.T) {
|
||||||
|
testGenerateRun(test, &exampleProtocol, "decode", `
|
||||||
|
// imports
|
||||||
|
`, `
|
||||||
|
log.Println("MessageConnect")
|
||||||
|
messageConnect := MessageConnect {
|
||||||
|
Name: "rarity",
|
||||||
|
Password: "gems",
|
||||||
|
}
|
||||||
|
testDecode(
|
||||||
|
&messageConnect,
|
||||||
|
tu.S(0xE1, 0x02).AddVar(
|
||||||
|
[]byte { 0x00, 0x00, 0x86, 'r', 'a', 'r', 'i', 't', 'y' },
|
||||||
|
[]byte { 0x00, 0x01, 0x84, 'g', 'e', 'm', 's' },
|
||||||
|
))
|
||||||
|
log.Println("MessageUserList")
|
||||||
|
messageUserList := MessageUserList {
|
||||||
|
Users: []User {
|
||||||
|
User {
|
||||||
|
Name: "rarity",
|
||||||
|
Bio: "asdjads",
|
||||||
|
Followers: 0x324,
|
||||||
|
},
|
||||||
|
User {
|
||||||
|
Name: "deez nuts",
|
||||||
|
Bio: "logy",
|
||||||
|
Followers: 0x8000,
|
||||||
|
},
|
||||||
|
User {
|
||||||
|
Name: "creekflow",
|
||||||
|
Bio: "im creekflow",
|
||||||
|
Followers: 0x3894,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
testEncode(
|
||||||
|
&messageUserList,
|
||||||
|
tu.S(0xE1, 0x01, 0x00, 0x00,
|
||||||
|
0xC1, 0x03, 0xE1,
|
||||||
|
).Add(0x03).AddVar(
|
||||||
|
[]byte { 0x00, 0x00, 0x86, 'r', 'a', 'r', 'i', 't', 'y' },
|
||||||
|
[]byte { 0x00, 0x01, 0x87, 'a', 's', 'd', 'j', 'a', 'd', 's' },
|
||||||
|
[]byte { 0x00, 0x02, 0x23, 0x00, 0x00, 0x03, 0x24 },
|
||||||
|
).Add(0x03).AddVar(
|
||||||
|
[]byte { 0x00, 0x00, 0x89, 'd', 'e', 'e', 'z', ' ', 'n', 'u', 't', 's' },
|
||||||
|
[]byte { 0x00, 0x01, 0x84, 'l', 'o', 'g', 'y' },
|
||||||
|
[]byte { 0x00, 0x02, 0x23, 0x00, 0x00, 0x80, 0x00 },
|
||||||
|
).Add(0x03).AddVar(
|
||||||
|
[]byte { 0x00, 0x00, 0x89, 'c', 'r', 'e', 'e', 'k', 'f', 'l', 'o', 'w' },
|
||||||
|
[]byte { 0x00, 0x01, 0x8C, 'i', 'm', ' ', 'c', 'r', 'e', 'e', 'k', 'f',
|
||||||
|
'l', 'o', 'w' },
|
||||||
|
[]byte { 0x00, 0x02, 0x23, 0x00, 0x00, 0x38, 0x94 },
|
||||||
|
))
|
||||||
|
log.Println("MessagePulse")
|
||||||
|
messagePulse := MessagePulse {
|
||||||
|
Index: 9,
|
||||||
|
Offset: -0x3521,
|
||||||
|
X: 45.389,
|
||||||
|
Y: 294.1,
|
||||||
|
Z: 384729384.234892034,
|
||||||
|
}
|
||||||
|
testEncode(
|
||||||
|
&messagePulse,
|
||||||
|
tu.S(0xE1, 0x05).AddVar(
|
||||||
|
[]byte { 0x00, 0x00, 0x09 },
|
||||||
|
[]byte { 0x00, 0x01, 0x41, 0xCA, 0xDF },
|
||||||
|
[]byte { 0x00, 0x02, 0x61, 0x51, 0xAC },
|
||||||
|
[]byte { 0x00, 0x03, 0x63, 0x43, 0x93, 0x0C, 0xCD },
|
||||||
|
[]byte { 0x00, 0x04, 0x67, 0x41, 0xB6, 0xEE, 0x81, 0x28, 0x3C, 0x21, 0xE2 },
|
||||||
|
))
|
||||||
|
log.Println("MessageNestedArray")
|
||||||
|
uint8s := func(n int) []uint8 {
|
||||||
|
array := make([]uint8, n)
|
||||||
|
for index := range array {
|
||||||
|
array[index] = uint8(index + 1) | 0xF0
|
||||||
|
}
|
||||||
|
return array
|
||||||
|
}
|
||||||
|
messageNestedArray := MessageNestedArray {
|
||||||
|
uint8s(6),
|
||||||
|
uint8s(35),
|
||||||
|
}
|
||||||
|
testEncode(
|
||||||
|
&messageNestedArray,
|
||||||
|
tu.S(0xC1, 0x02, 0xC1,
|
||||||
|
0x06, 0x20, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6,
|
||||||
|
35, 0x20, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6,
|
||||||
|
0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC,
|
||||||
|
0xFD, 0xFE, 0xFF, 0xF0, 0xF1, 0xF2,
|
||||||
|
0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
|
||||||
|
0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE,
|
||||||
|
0xFF, 0xF0, 0xF1, 0xF2, 0xF3))
|
||||||
|
log.Println("MessageIntegers")
|
||||||
|
messageIntegers := MessageIntegers {
|
||||||
|
U5: 0x13,
|
||||||
|
U8: 0xC9,
|
||||||
|
U16: 0x34C9,
|
||||||
|
U32: 0x10E134C9,
|
||||||
|
U64: 0x639109BC10E134C9,
|
||||||
|
I8: 0x35,
|
||||||
|
I16: 0x34C9,
|
||||||
|
I32: 0x10E134C9,
|
||||||
|
I64: 0x639109BC10E134C9,
|
||||||
|
NI8: -0x35,
|
||||||
|
NI16: -0x34C9,
|
||||||
|
NI32: -0x10E134C9,
|
||||||
|
NI64: -0x639109BC10E134C9,
|
||||||
|
}
|
||||||
|
testEncode(
|
||||||
|
&messageIntegers,
|
||||||
|
tu.S(0xE1, 13).AddVar(
|
||||||
|
[]byte { 0x00, 0x00, 0x13 },
|
||||||
|
[]byte { 0x00, 0x01, 0x20, 0xC9 },
|
||||||
|
[]byte { 0x00, 0x02, 0x21, 0x34, 0xC9 },
|
||||||
|
[]byte { 0x00, 0x03, 0x23, 0x10, 0xE1, 0x34, 0xC9 },
|
||||||
|
[]byte { 0x00, 0x04, 0x27, 0x63, 0x91, 0x09, 0xBC, 0x10, 0xE1, 0x34, 0xC9 },
|
||||||
|
[]byte { 0x00, 0x06, 0x40, 0x35 },
|
||||||
|
[]byte { 0x00, 0x07, 0x41, 0x34, 0xC9 },
|
||||||
|
[]byte { 0x00, 0x08, 0x43, 0x10, 0xE1, 0x34, 0xC9 },
|
||||||
|
[]byte { 0x00, 0x09, 0x47, 0x63, 0x91, 0x09, 0xBC, 0x10, 0xE1, 0x34, 0xC9 },
|
||||||
|
[]byte { 0x00, 0x0B, 0x40, 0xCB },
|
||||||
|
[]byte { 0x00, 0x0C, 0x41, 0xCB, 0x37 },
|
||||||
|
[]byte { 0x00, 0x0D, 0x43, 0xEF, 0x1E, 0xCB, 0x37 },
|
||||||
|
[]byte { 0x00, 0x0E, 0x47, 0x9C, 0x6E, 0xF6, 0x43, 0xEF, 0x1E, 0xCB, 0x37 },
|
||||||
|
))
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|||||||
@ -6,9 +6,9 @@ import "os/exec"
|
|||||||
import "testing"
|
import "testing"
|
||||||
import "path/filepath"
|
import "path/filepath"
|
||||||
|
|
||||||
func testGenerateRun(test *testing.T, protocol *Protocol, imports string, testCase string) {
|
func testGenerateRun(test *testing.T, protocol *Protocol, title, imports, testCase string) {
|
||||||
// reset data directory
|
// reset data directory
|
||||||
dir := "test/generate-run"
|
dir := filepath.Join("test", title)
|
||||||
err := os.RemoveAll(dir)
|
err := os.RemoveAll(dir)
|
||||||
if err != nil { test.Fatal(err) }
|
if err != nil { test.Fatal(err) }
|
||||||
err = os.MkdirAll(dir, 0750)
|
err = os.MkdirAll(dir, 0750)
|
||||||
@ -34,6 +34,7 @@ func testGenerateRun(test *testing.T, protocol *Protocol, imports string, testCa
|
|||||||
imports = `
|
imports = `
|
||||||
import "log"
|
import "log"
|
||||||
import "bytes"
|
import "bytes"
|
||||||
|
import "reflect"
|
||||||
import "git.tebibyte.media/sashakoshka/hopp/tape"
|
import "git.tebibyte.media/sashakoshka/hopp/tape"
|
||||||
import tu "git.tebibyte.media/sashakoshka/hopp/internal/testutil"
|
import tu "git.tebibyte.media/sashakoshka/hopp/internal/testutil"
|
||||||
` + imports
|
` + imports
|
||||||
@ -56,13 +57,34 @@ func testGenerateRun(test *testing.T, protocol *Protocol, imports string, testCa
|
|||||||
log.Fatalln("not equal at", n)
|
log.Fatalln("not equal at", n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testDecode(correct Message, data any) {
|
||||||
|
var flat []byte
|
||||||
|
switch data := data.(type) {
|
||||||
|
case []byte: flat = data
|
||||||
|
case tu.Snake: flat = data.Flatten()
|
||||||
|
}
|
||||||
|
message := reflect.New(reflect.ValueOf(correct).Elem().Type()).Interface().(Message)
|
||||||
|
log.Println("before: ", message)
|
||||||
|
decoder := tape.NewDecoder(bytes.NewBuffer(flat))
|
||||||
|
n, err := message.Decode(decoder)
|
||||||
|
if err != nil { log.Fatalf("at %d: %v\n", n, err) }
|
||||||
|
log.Println("got: ", message)
|
||||||
|
log.Println("correct:", correct)
|
||||||
|
if n != len(flat) {
|
||||||
|
log.Fatalf("n incorrect: %d != %d\n", n, len(flat))
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(message, correct) {
|
||||||
|
log.Fatalln("not equal")
|
||||||
|
}
|
||||||
|
}
|
||||||
`
|
`
|
||||||
fmt.Fprintf(
|
fmt.Fprintf(
|
||||||
mainFile, "package main\n%s\nfunc main() {\n%s\n%s\n%s\n}\n%s",
|
mainFile, "package main\n%s\nfunc main() {\n%s\n%s\n%s\n}\n%s",
|
||||||
imports, setup, testCase, teardown, static)
|
imports, setup, testCase, teardown, static)
|
||||||
|
|
||||||
// build and run test
|
// build and run test
|
||||||
command := exec.Command("go", "run", "./generate/test/generate-run")
|
command := exec.Command("go", "run", "./" + filepath.Join("generate", dir))
|
||||||
workingDirAbs, err := filepath.Abs("..")
|
workingDirAbs, err := filepath.Abs("..")
|
||||||
if err != nil { test.Fatal(err) }
|
if err != nil { test.Fatal(err) }
|
||||||
command.Dir = workingDirAbs
|
command.Dir = workingDirAbs
|
||||||
|
|||||||
@ -64,6 +64,18 @@ func (sn Snake) Check(data []byte) (ok bool, n int) {
|
|||||||
return true, n
|
return true, n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flatten returns the snake flattened to a byte array. The result of this
|
||||||
|
// function always satisfies the snake.
|
||||||
|
func (sn Snake) Flatten() []byte {
|
||||||
|
flat := []byte { }
|
||||||
|
for _, sector := range sn {
|
||||||
|
for _, variation := range sector {
|
||||||
|
flat = append(flat, variation...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return flat
|
||||||
|
}
|
||||||
|
|
||||||
func (sn Snake) String() string {
|
func (sn Snake) String() string {
|
||||||
if len(sn) == 0 || len(sn[0]) == 0 || len(sn[0][0]) == 0 {
|
if len(sn) == 0 || len(sn[0]) == 0 || len(sn[0][0]) == 0 {
|
||||||
return "EMPTY"
|
return "EMPTY"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user