adding some eval tests

master
Jordan Orelli 10 years ago
parent 0cad3a6253
commit d4cc9761bd

@ -30,26 +30,26 @@ other_hash: {
key_4: ["five" 6 7.8] key_4: ["five" 6 7.8]
} }
# we may reference an item that was defined earlier # we may reference an item that was defined earlier using a sigil
repeat_hash: hash repeat_hash: @hash
# items can be hidden. i.e., they're only valid in the parse and eval stage as # items can be hidden. i.e., they're only valid in the parse and eval stage as
# intermediate values internal to the config file; they are *not* visible to # intermediate values internal to the config file; they are *not* visible to
# the host program. This is generally useful for composing larger, more # the host program. This is generally useful for composing larger, more
# complicated things. # complicated things.
.hidden_item: "it has a value" @hidden_item: "it has a value"
visible_item: .hidden_item visible_item: @hidden_item
.person_one: { @person_one: {
name: "the first name here" name: "the first name here"
age: 28 age: 28
hometown: "crooklyn" hometown: "crooklyn"
} }
.person_two: { @person_two: {
name: "the second name here" name: "the second name here"
age: 30 age: 30
hometown: "tha bronx" hometown: "tha bronx"
} }
people: [.person_one .person_two] people: [@person_one @person_two]

@ -29,18 +29,18 @@ var valueTests = []struct {
{[]float32{1.0, 2.2, 3.3}, `[1 2.2 3.3]`}, {[]float32{1.0, 2.2, 3.3}, `[1 2.2 3.3]`},
{[]float64{1.0, 2.2, 3.3}, `[1 2.2 3.3]`}, {[]float64{1.0, 2.2, 3.3}, `[1 2.2 3.3]`},
{[]string{"one", "two", "three"}, `["one" "two" "three"]`}, {[]string{"one", "two", "three"}, `["one" "two" "three"]`},
{ // {
map[string]int{"one": 1, "two": 2, "three": 3}, // map[string]int{"one": 1, "two": 2, "three": 3},
`{one: 1 two: 2 three: 3}`, // `{one: 1 two: 2 three: 3}`,
}, // },
{ // {
map[string]interface{}{ // map[string]interface{}{
"one": 1, // "one": 1,
"two": 2.0, // "two": 2.0,
"pi": 3.14, // "pi": 3.14,
}, // },
`{one: 1 two: 2 pi: 3.14}`, // `{one: 1 two: 2 pi: 3.14}`,
}, // },
} }
func TestWriteValues(t *testing.T) { func TestWriteValues(t *testing.T) {

@ -0,0 +1,62 @@
package moon
import (
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"testing"
)
func runEvalTest(t *testing.T, basepath, inpath, outpath string) {
in, err := os.Open(inpath)
if err != nil {
t.Errorf("unable to open input file %s: %s", inpath, err)
return
}
defer in.Close()
out, err := os.Open(outpath)
if err != nil {
t.Errorf("unable to open output file %s: %s", outpath, err)
return
}
defer out.Close()
r_inpath := filepath.Base(inpath)
n, err := strconv.ParseInt(strings.TrimSuffix(r_inpath, ".in"), 10, 64)
if err != nil {
t.Errorf("unable to get test number for path %s: %s", inpath, err)
return
}
inDoc, err := Read(in)
if err != nil {
t.Errorf("unable to read moon doc from infile: %s", err)
return
}
outDoc, err := Read(out)
if err != nil {
t.Errorf("unable to read moon doc from outfile: %s", err)
return
}
if !reflect.DeepEqual(inDoc, outDoc) {
t.Errorf("test %d: input and output documents do not match!", n)
t.Logf("input document: %v", inDoc)
t.Logf("output document: %v", outDoc)
}
}
func TestEval(t *testing.T) {
files, err := filepath.Glob("tests/eval/*.in")
if err != nil {
t.Errorf("unable to find test files: %s", err)
return
}
for _, fname := range files {
runEvalTest(t, "tests/eval/", fname, strings.Replace(fname, "in", "out", -1))
}
}

@ -55,7 +55,7 @@ func (n *rootNode) parse(p *parser) error {
return nil return nil
case t_comment: case t_comment:
n.addChild(&commentNode{t.s}) n.addChild(&commentNode{t.s})
case t_name: case t_name, t_variable:
nn := &assignmentNode{name: t.s} nn := &assignmentNode{name: t.s}
if err := nn.parse(p); err != nil { if err := nn.parse(p); err != nil {
return err return err
@ -426,7 +426,7 @@ func (v *variableNode) Type() nodeType {
func (v *variableNode) parse(p *parser) error { func (v *variableNode) parse(p *parser) error {
t := p.next() t := p.next()
if t.t != t_name { if t.t != t_variable {
return fmt.Errorf("unexpected %s token when parsing variable", t.t) return fmt.Errorf("unexpected %s token when parsing variable", t.t)
} }
v.name = t.s v.name = t.s

@ -152,7 +152,7 @@ func (p *parser) parseValue() (node, error) {
return nil, err return nil, err
} }
return n, nil return n, nil
case t_name: case t_variable:
n := new(variableNode) n := new(variableNode)
if err := n.parse(p); err != nil { if err := n.parse(p); err != nil {
return nil, err return nil, err

@ -0,0 +1 @@
# a comment should evaluate to nothing

@ -0,0 +1,15 @@
# simple keys and values
key: 1 # comments are stripped
key_2: 2
key_3: -1
# basically the stripping of comments is all we care about
key_4: 0
key_5: +0
key_6: this is a string
key_7: "this is a quoted string"
key_8: [1 2 3]

@ -0,0 +1,9 @@
key: 1
key_2: 2
key_3: -1
key_4: 0
key_5: +0
key_6: this is a string
key_7: "this is a quoted string"
key_8: [1 2 3]

@ -0,0 +1,5 @@
# now we test that variables actually work
key: this is a literal value
another_key: @key

@ -0,0 +1,2 @@
key: this is a literal value
another_key: this is a literal value

@ -0,0 +1,4 @@
a_key: this is a value
another_key: @a_key
@var_key: this shouldn't ever be exported outside of the moon interpreter
public_key: @var_key

@ -0,0 +1,25 @@
root:
assign:
name:
a_key
value:
string:
this is a value
assign:
name:
another_key
value:
variable:
a_key
assign:
name:
var_key
value:
string:
this shouldn't ever be exported outside of the moon interpreter
assign:
name:
public_key
value:
variable:
var_key
Loading…
Cancel
Save