-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the seed for specification tests.
The goal is to have a single lexer definition that exercises all the functionality of the stateful lexer and generated equivalent. See #264
- Loading branch information
1 parent
0d264e9
commit 42e937f
Showing
5 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//go:build generated | ||
|
||
package spec_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/alecthomas/participle/v2/lexer/internal/spec" | ||
) | ||
|
||
// This should only be run by TestLexerSpecGenerated. | ||
func TestLexerSpecGeneratedInternal(t *testing.T) { | ||
testLexer(t, spec.GeneratedSpecLexer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package spec_test | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
"github.com/alecthomas/participle/v2/lexer" | ||
) | ||
|
||
var specLexer = lexer.MustStateful(lexer.Rules{ | ||
"Root": { | ||
{"String", `"`, lexer.Push("String")}, | ||
{"Heredoc", `<<(\w+\b)`, lexer.Push("Heredoc")}, | ||
}, | ||
"String": { | ||
{"Escaped", `\\.`, nil}, | ||
{"StringEnd", `"`, lexer.Pop()}, | ||
{"Expr", `\${`, lexer.Push("Expr")}, | ||
{"Char", `[^$"\\]+`, nil}, | ||
}, | ||
"Expr": { | ||
lexer.Include("Root"), | ||
{`Whitespace`, `\s+`, nil}, | ||
{`Oper`, `[-+/*%]`, nil}, | ||
{"Ident", `\w+`, lexer.Push("Reference")}, | ||
{"ExprEnd", `}`, lexer.Pop()}, | ||
}, | ||
"Reference": { | ||
{"Dot", `\.`, nil}, | ||
{"Ident", `\w+`, nil}, | ||
lexer.Return(), | ||
}, | ||
"Heredoc": { | ||
{"End", `\b\1\b`, lexer.Pop()}, | ||
lexer.Include("Expr"), | ||
}, | ||
}) | ||
|
||
type token struct { | ||
Type string | ||
Value string | ||
} | ||
|
||
func testLexer(t *testing.T, lex lexer.Definition) { | ||
t.Helper() | ||
tests := []struct { | ||
name string | ||
input string | ||
expected []token | ||
}{ | ||
{"NestedString", `"${"Hello ${name + "!"}"}"`, []token{ | ||
{"String", "\""}, | ||
{"Expr", "${"}, | ||
{"String", "\""}, | ||
{"Char", "Hello "}, | ||
{"Expr", "${"}, | ||
{"Ident", "name"}, | ||
{"Whitespace", " "}, | ||
{"Oper", "+"}, | ||
{"Whitespace", " "}, | ||
{"String", "\""}, | ||
{"Char", "!"}, | ||
{"StringEnd", "\""}, | ||
{"ExprEnd", "}"}, | ||
{"StringEnd", "\""}, | ||
{"ExprEnd", "}"}, | ||
{"StringEnd", "\""}, | ||
}}, | ||
{"Reference", `"${user.name}"`, []token{ | ||
{"String", "\""}, | ||
{"Expr", "${"}, | ||
{"Ident", "user"}, | ||
{"Dot", "."}, | ||
{"Ident", "name"}, | ||
{"ExprEnd", "}"}, | ||
{"StringEnd", "\""}, | ||
}}, | ||
} | ||
symbols := lexer.SymbolsByRune(lex) | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
l, err := lex.Lex(test.name, strings.NewReader(test.input)) | ||
assert.NoError(t, err) | ||
tokens, err := lexer.ConsumeAll(l) | ||
assert.NoError(t, err) | ||
actual := make([]token, len(tokens)-1) | ||
for i, t := range tokens { | ||
if t.Type == lexer.EOF { | ||
continue | ||
} | ||
actual[i] = token{Type: symbols[t.Type], Value: t.Value} | ||
} | ||
assert.Equal(t, test.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestLexerSpecGenerated(t *testing.T) { | ||
genLexer(t) | ||
args := []string{"test", "-run", "TestLexerSpecGeneratedInternal", "-tags", "generated"} | ||
// Propagate test flags. | ||
flag.CommandLine.VisitAll(func(f *flag.Flag) { | ||
if f.Value.String() != f.DefValue { | ||
args = append(args, fmt.Sprintf("-%s=%s", f.Name, f.Value.String())) | ||
} | ||
}) | ||
cmd := exec.Command("go", args...) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
err := cmd.Run() | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestLexerSpec(t *testing.T) { | ||
testLexer(t, specLexer) | ||
} | ||
|
||
func genLexer(t *testing.T) { | ||
t.Helper() | ||
lexerJSON, err := json.Marshal(specLexer) | ||
assert.NoError(t, err) | ||
cwd, err := os.Getwd() | ||
assert.NoError(t, err) | ||
generatedSpecLexer := filepath.Join(cwd, "spec_lexer_gen.go") | ||
t.Cleanup(func() { | ||
_ = os.Remove(generatedSpecLexer) | ||
}) | ||
cmd := exec.Command( | ||
"../../../scripts/participle", | ||
"gen", "lexer", "spec", | ||
"--name", "GeneratedSpec", | ||
"--output", generatedSpecLexer) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
w, err := cmd.StdinPipe() | ||
assert.NoError(t, err) | ||
defer w.Close() | ||
err = cmd.Start() | ||
assert.NoError(t, err) | ||
_, err = w.Write(lexerJSON) | ||
assert.NoError(t, err) | ||
err = w.Close() | ||
assert.NoError(t, err) | ||
err = cmd.Wait() | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
(cd "$(dirname $0)/../cmd/participle" && go install github.com/alecthomas/participle/v2/cmd/participle) | ||
(cd "$(dirname "$0")/../cmd/participle" && go install github.com/alecthomas/participle/v2/cmd/participle) | ||
exec "$(go env GOBIN)/participle" "$@" |