Skip to content

Commit

Permalink
Fixes "false" in example/expr2 (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
spatecon committed Mar 24, 2023
1 parent a3dc15b commit 0eb1e57
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
9 changes: 8 additions & 1 deletion _examples/expr2/main.go
Expand Up @@ -58,11 +58,18 @@ type Unary struct {
type Primary struct {
Number *float64 ` @Float | @Int`
String *string `| @String`
Bool *bool `| ( @"true" | "false" )`
Bool *Boolean `| @( "true" | "false" )`
Nil bool `| @"nil"`
SubExpression *Expression `| "(" @@ ")" `
}

type Boolean bool

func (b *Boolean) Capture(values []string) error {
*b = values[0] == "true"
return nil
}

var parser = participle.MustBuild[Expression](participle.UseLookahead(2))

func main() {
Expand Down
37 changes: 37 additions & 0 deletions _examples/expr2/main_test.go
Expand Up @@ -12,3 +12,40 @@ func TestExe(t *testing.T) {
repr.Println(expr)
require.NoError(t, err)
}

func toPtr[T any](x T) *T {
return &x
}

func TestExe_BoolFalse(t *testing.T) {
got, err := parser.ParseString("", `1 + false`)

expected := &Expression{
Equality: &Equality{
Comparison: &Comparison{
Addition: &Addition{
Multiplication: &Multiplication{
Unary: &Unary{
Primary: &Primary{
Number: toPtr(float64(1)),
},
},
},
Op: "+",
Next: &Addition{
Multiplication: &Multiplication{
Unary: &Unary{
Primary: &Primary{
Bool: toPtr(Boolean(false)),
},
},
},
},
},
},
},
}

require.NoError(t, err)
require.Equal(t, expected, got)
}

0 comments on commit 0eb1e57

Please sign in to comment.