add parsing tests and make some fixes

This commit is contained in:
Brian Picciano 2014-10-19 23:28:21 -04:00
parent b9669ac79b
commit 6ec6a408f5
2 changed files with 66 additions and 8 deletions

View File

@ -22,19 +22,21 @@ var closers = map[string]string{
// The lexer only indicates a bare string, but technically an integer or a float
// is a bare string so we must try and convert to one of those first
func parseBareString(tok *lex.Token) types.Elem {
if i, err := strconv.ParseInt(tok.Val, 10, int_bits); err != nil {
if i, err := strconv.ParseInt(tok.Val, 10, int_bits); err == nil {
return types.GoType{int(i)}
}
if i64, err := strconv.ParseInt(tok.Val, 10, 64); err != nil {
} else if int_bits == 64 {
// We don't want to bother with the next case if int_bits is 64
} else if i64, err := strconv.ParseInt(tok.Val, 10, 64); err == nil {
return types.GoType{int64(i64)}
}
if f32, err := strconv.ParseInt(tok.Val, 10, 32); err != nil {
if f32, err := strconv.ParseFloat(tok.Val, 32); err == nil {
return types.GoType{float32(f32)}
}
if f64, err := strconv.ParseInt(tok.Val, 10, 64); err != nil {
if f64, err := strconv.ParseFloat(tok.Val, 64); err == nil {
return types.GoType{float64(f64)}
}

View File

@ -1,12 +1,68 @@
package parse
import (
"bytes"
"io"
. "testing"
//"github.com/mediocregopher/ginger/seq"
//"github.com/mediocregopher/ginger/types"
"github.com/mediocregopher/ginger/seq"
"github.com/mediocregopher/ginger/types"
)
func TestParseBareString(t *T) {
t.Fatal()
m := map[string][]types.Elem{
"foo": []types.Elem{types.GoType{":foo"}},
"foo bar": []types.Elem{
types.GoType{":foo"},
types.GoType{":bar"},
},
"foo \"bar\"": []types.Elem{
types.GoType{":foo"},
types.GoType{"bar"},
},
"()": []types.Elem{seq.NewList()},
"(foo)": []types.Elem{seq.NewList(
types.GoType{":foo"},
)},
"(foo (bar))": []types.Elem{seq.NewList(
types.GoType{":foo"},
seq.NewList(types.GoType{":bar"}),
)},
"{}": []types.Elem{seq.NewHashMap()},
"{foo bar}": []types.Elem{seq.NewHashMap(
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])
}
}
}
}