You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
moon/parse_test.go

156 lines
2.9 KiB
Go

package main
import (
"reflect"
"strings"
"testing"
)
var parseTests = []parseTest{
{
source: ``,
root: &rootNode{
children: []node{},
},
},
{
source: `# just a comment`,
root: &rootNode{
children: []node{
&commentNode{" just a comment"},
},
},
},
{
source: `name: "jordan"`,
root: &rootNode{
children: []node{
&assignmentNode{"name", "jordan"},
},
},
},
{
source: `
hostname: "jordanorelli.com"
port: 9000
freq: 1e9
duty: 0.2
neg: -2
neg2: -2.3
imag: 1+2i
`,
root: &rootNode{
children: []node{
&assignmentNode{"hostname", "jordanorelli.com"},
&assignmentNode{"port", 9000},
&assignmentNode{"freq", 1e9},
&assignmentNode{"duty", 0.2},
&assignmentNode{"neg", -2},
&assignmentNode{"neg2", -2.3},
&assignmentNode{"imag", 1 + 2i},
},
},
},
{
source: `
first_name: "jordan" # yep, that's my name
last_name: "orelli" # comments should be able to follow other shit
`,
root: &rootNode{
children: []node{
&assignmentNode{"first_name", "jordan"},
&commentNode{" yep, that's my name"},
&assignmentNode{"last_name", "orelli"},
&commentNode{" comments should be able to follow other shit"},
},
},
},
{
source: `
heroes: ["lina", "cm"]
`,
root: &rootNode{
children: []node{
&assignmentNode{"heroes", list{"lina", "cm"}},
},
},
},
10 years ago
{
source: `
nested: [["one", "two"], ["three", "four"]]
`,
root: &rootNode{
children: []node{
&assignmentNode{"nested", list{list{"one", "two"}, list{"three", "four"}}},
},
},
10 years ago
},
{
source: `
nested: [
["one", "two"],
["three", "four"],
]
`,
root: &rootNode{
children: []node{
&assignmentNode{"nested", list{list{"one", "two"}, list{"three", "four"}}},
},
},
10 years ago
},
{
source: `
admin: {first_name: "jordan", last_name: "orelli"}
10 years ago
`,
root: &rootNode{
children: []node{
&assignmentNode{"admin", object{
"first_name": "jordan",
"last_name": "orelli",
}},
},
},
10 years ago
},
{
source: `
http: {
port: 9000,
routes: "/path/to/some/file",
}
`,
root: &rootNode{
children: []node{
&assignmentNode{"http", object{
"port": 9000,
"routes": "/path/to/some/file",
}},
},
},
10 years ago
},
}
type parseTest struct {
source string
root node
}
func (p *parseTest) run(t *testing.T) {
r := strings.NewReader(p.source)
n, err := parse(r)
if err != nil {
t.Errorf("parse error: %v", err)
return
}
if !reflect.DeepEqual(p.root, n) {
t.Errorf("trees are not equal. expected:\n%v\nsaw:\n%v", p.root, n)
} else {
t.Logf("OK trees are equal: %v = %v", p.root, n)
}
}
func TestParse(t *testing.T) {
for _, test := range parseTests {
test.run(t)
}
}