ginger/parse/parse_test.go

79 lines
1.5 KiB
Go
Raw Normal View History

2014-10-07 00:55:15 +00:00
package parse
import (
2014-10-20 03:28:21 +00:00
"bytes"
"io"
2014-10-07 00:55:15 +00:00
. "testing"
2014-10-20 03:28:21 +00:00
"github.com/mediocregopher/ginger/seq"
"github.com/mediocregopher/ginger/types"
2014-10-07 00:55:15 +00:00
)
func TestParseBareString(t *T) {
2014-10-20 03:28:21 +00:00
m := map[string][]types.Elem{
2014-10-21 01:35:21 +00:00
"1": {types.GoType{int(1)}},
"-1": {types.GoType{int(-1)}},
"+1": {types.GoType{int(1)}},
2014-10-20 03:28:21 +00:00
2014-10-21 01:35:21 +00:00
"1.5": {types.GoType{float32(1.5)}},
"-1.5": {types.GoType{float32(-1.5)}},
"+1.5": {types.GoType{float32(1.5)}},
"1.5e1": {types.GoType{float32(15)}},
"foo": {types.GoType{":foo"}},
"foo 4 bar": {
2014-10-20 03:28:21 +00:00
types.GoType{":foo"},
2014-10-21 01:35:21 +00:00
types.GoType{4},
2014-10-20 03:28:21 +00:00
types.GoType{":bar"},
},
2014-10-21 01:35:21 +00:00
"foo \"bar\"": {
2014-10-20 03:28:21 +00:00
types.GoType{":foo"},
types.GoType{"bar"},
},
2014-10-21 01:35:21 +00:00
"()": {seq.NewList()},
2014-10-20 03:28:21 +00:00
2014-10-21 01:35:21 +00:00
"(foo)": {seq.NewList(
2014-10-20 03:28:21 +00:00
types.GoType{":foo"},
)},
2014-10-21 01:35:21 +00:00
"(foo (bar))": {seq.NewList(
2014-10-20 03:28:21 +00:00
types.GoType{":foo"},
seq.NewList(types.GoType{":bar"}),
)},
2014-10-21 01:35:21 +00:00
"{}": {seq.NewHashMap()},
2014-10-20 03:28:21 +00:00
2014-10-21 01:35:21 +00:00
"{foo bar}": {seq.NewHashMap(
2014-10-20 03:28:21 +00:00
seq.KeyVal(types.GoType{":foo"}, types.GoType{":bar"}),
)},
}
for input, output := range m {
buf := bytes.NewBufferString(input)
p := NewParser(buf)
parsed := make([]types.Elem, 0, len(output))
for {
el, err := p.ReadElem()
if err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
}
parsed = append(parsed, el)
}
if len(output) != len(parsed) {
t.Fatalf("input: %q %#v != %#v", input, output, parsed)
}
for i := range output {
if !output[i].Equal(parsed[i]) {
t.Fatalf("input: %q (%d) %#v != %#v", input, i, output[i], parsed[i])
}
}
}
}