431 lines
11 KiB
Go
431 lines
11 KiB
Go
package treerack
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
type formatDefinitionTestItem struct {
|
|
title string
|
|
definition string
|
|
syntax string
|
|
output string
|
|
}
|
|
|
|
func testDefinitionFormatItem(t *testing.T, treerack *Syntax, f formatFlags, test formatDefinitionTestItem) func(t *testing.T) {
|
|
return func(t *testing.T) {
|
|
syntax := test.syntax
|
|
if test.definition != "" {
|
|
syntax = fmt.Sprintf("def = %s", test.definition)
|
|
}
|
|
|
|
nodes, err := treerack.Parse(bytes.NewBufferString(syntax))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s := &Syntax{}
|
|
if err := define(s, nodes); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
def, ok := s.registry.definition["def"]
|
|
if !ok {
|
|
t.Fatal("failed to register definition")
|
|
}
|
|
|
|
output := def.format(s.registry, formatOptions{mode: f})
|
|
if output != test.output {
|
|
t.Error("invalid definition format")
|
|
t.Log("got: ", output)
|
|
t.Log("expected:", test.output)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testDefinitionFormat(t *testing.T, f formatFlags, tests []formatDefinitionTestItem) {
|
|
treerack, err := bootSyntax()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.title, testDefinitionFormatItem(t, treerack, f, test))
|
|
}
|
|
}
|
|
|
|
func TestCharFormat(t *testing.T) {
|
|
testDefinitionFormat(t, formatNone, []formatDefinitionTestItem{{
|
|
title: "empty",
|
|
definition: "[]",
|
|
output: "[]",
|
|
}, {
|
|
title: "one char",
|
|
definition: "[a]",
|
|
output: `"a"`,
|
|
}, {
|
|
title: "escaped char",
|
|
definition: "[\\a]",
|
|
output: `"a"`,
|
|
}, {
|
|
title: "escaped control char",
|
|
definition: "[\\^]",
|
|
output: `"^"`,
|
|
}, {
|
|
title: "escaped whitespace char",
|
|
definition: "[\\n]",
|
|
output: `"\n"`,
|
|
}, {
|
|
title: "escaped verbatim whitespace char",
|
|
definition: "[\n]",
|
|
output: `"\n"`,
|
|
}, {
|
|
title: "escaped range",
|
|
definition: "[\\b-\\v]",
|
|
output: "[\\b-\\v]",
|
|
}, {
|
|
title: "anything",
|
|
definition: ".",
|
|
output: ".",
|
|
}, {
|
|
title: "not something",
|
|
definition: "[^abc]",
|
|
output: "[^abc]",
|
|
}, {
|
|
title: "range",
|
|
definition: "[a-z]",
|
|
output: "[a-z]",
|
|
}, {
|
|
title: "range and char mixed",
|
|
definition: "[a-z_\\-A-Z]",
|
|
output: "[_\\-a-zA-Z]",
|
|
}})
|
|
}
|
|
|
|
func TestSequenceFormat(t *testing.T) {
|
|
testDefinitionFormat(t, formatNone, []formatDefinitionTestItem{{
|
|
title: "empty char sequence",
|
|
syntax: `def = ""`,
|
|
output: `""`,
|
|
}, {
|
|
title: "char sequence",
|
|
syntax: `def = "abc"`,
|
|
output: `"abc"`,
|
|
}, {
|
|
title: "char sequence, escaped",
|
|
syntax: `def = "\\n"`,
|
|
output: `"\\n"`,
|
|
}, {
|
|
title: "chars",
|
|
syntax: `def = "abc" [a-z]`,
|
|
output: `"abc" [a-z]`,
|
|
}, {
|
|
title: "quantifiers, 0-or-more, single char",
|
|
syntax: `def = "a"*`,
|
|
output: `"a"*`,
|
|
}, {
|
|
title: "quantifiers, 0-or-more",
|
|
syntax: `def = "abc"*`,
|
|
output: `"abc"*`,
|
|
}, {
|
|
title: "quantifiers, 1-or-more, single char",
|
|
syntax: `def = "a"+`,
|
|
output: `"a"+`,
|
|
}, {
|
|
title: "quantifiers, 1-or-more",
|
|
syntax: `def = "abc"+`,
|
|
output: `"abc"+`,
|
|
}, {
|
|
title: "quantifiers, 0-or-one, single char",
|
|
syntax: `def = "a"?`,
|
|
output: `"a"?`,
|
|
}, {
|
|
title: "quantifiers, 0-or-one",
|
|
syntax: `def = "abc"?`,
|
|
output: `"abc"?`,
|
|
}, {
|
|
title: "quantifiers, exact number, single char",
|
|
syntax: `def = "a"{3}`,
|
|
output: `"a"{3}`,
|
|
}, {
|
|
title: "quantifiers, exact number",
|
|
syntax: `def = "abc"{3}`,
|
|
output: `"abc"{3}`,
|
|
}, {
|
|
title: "quantifiers, max, single char",
|
|
syntax: `def = "a"{0, 3}`,
|
|
output: `"a"{,3}`,
|
|
}, {
|
|
title: "quantifiers, max",
|
|
syntax: `def = "abc"{0, 3}`,
|
|
output: `"abc"{,3}`,
|
|
}, {
|
|
title: "quantifiers, min, single char",
|
|
syntax: `def = "a"{3,}`,
|
|
output: `"a"{3,}`,
|
|
}, {
|
|
title: "quantifiers, min",
|
|
syntax: `def = "abc"{3,}`,
|
|
output: `"abc"{3,}`,
|
|
}, {
|
|
title: "quantifiers, range, single char",
|
|
syntax: `def = "a"{3, 9}`,
|
|
output: `"a"{3,9}`,
|
|
}, {
|
|
title: "quantifiers, range",
|
|
syntax: `def = "abc"{3, 9}`,
|
|
output: `"abc"{3,9}`,
|
|
}, {
|
|
title: "symbols",
|
|
syntax: `a = "a"; b = "b"; c = "c"; def = a b c`,
|
|
output: "a b c",
|
|
}, {
|
|
title: "choice in sequence, single char",
|
|
syntax: `def = "a" ("b" | "c")`,
|
|
output: `"a" ("b" | "c")`,
|
|
}, {
|
|
title: "choice in sequence",
|
|
syntax: `def = "abc" ("def" | "ghi")`,
|
|
output: `"abc" ("def" | "ghi")`,
|
|
}, {
|
|
title: "grouped quantifier, single char",
|
|
syntax: `def = ("a" "b"){3}`,
|
|
output: `("a" "b"){3}`,
|
|
}, {
|
|
title: "grouped quantifier",
|
|
syntax: `def = ("abc" "def"){3}`,
|
|
output: `("abc" "def"){3}`,
|
|
}})
|
|
}
|
|
|
|
func TestChoiceFormat(t *testing.T) {
|
|
testDefinitionFormat(t, formatNone, []formatDefinitionTestItem{{
|
|
title: "choice of char sequences, single char",
|
|
syntax: `def = "a" | "b" | "c"`,
|
|
output: `"a" | "b" | "c"`,
|
|
}, {
|
|
title: "choice of char sequences",
|
|
syntax: `def = "abc" | "def" | "ghi"`,
|
|
output: `"abc" | "def" | "ghi"`,
|
|
}, {
|
|
title: "choice of inline sequences, single char",
|
|
syntax: `def = "a" "b" | "c" "d" | "e" "f"`,
|
|
output: `"a" "b" | "c" "d" | "e" "f"`,
|
|
}, {
|
|
title: "choice of inline sequences",
|
|
syntax: `def = "abc" "def" | "ghi" "jkl" | "mno" "pqr"`,
|
|
output: `"abc" "def" | "ghi" "jkl" | "mno" "pqr"`,
|
|
}, {
|
|
title: "choice of symbol",
|
|
syntax: `a = "a"; b = "b"; c = "c"; def = a | b | c`,
|
|
output: "a | b | c",
|
|
}})
|
|
}
|
|
|
|
const testDoc = `/*
|
|
foo
|
|
*/
|
|
// bar
|
|
// bar
|
|
//baz
|
|
/* foo
|
|
bar baz */// foo bar baz
|
|
wschar:alias =// foo
|
|
/* bar */ " " | "\t" | "\n" | "\b" | "\f" | "\r" | "\v";
|
|
wsc:ws = wschar | comment;
|
|
|
|
block-comment:alias:nows /* foo */ // bar
|
|
= "/*" ("*" [^/] | [^*])* "*/";
|
|
line-comment:alias:nows /*
|
|
foo
|
|
*/ = "//" [^\n]*;
|
|
comment-segment:alias:nows = // bar
|
|
line-comment | block-comment;
|
|
ws-no-nl:alias:nows = " " | "\t" | "\b" | /* this one */ /* is a */ "\f" /* form feed */ // for sure
|
|
| "\r" | "\v";
|
|
comment:nows = comment-segment /* segment is not the best name */ /* but */ (ws-no-nl* "\n"? ws-no-nl* // fine
|
|
comment-segment)*;
|
|
|
|
any-char = "."; // equivalent to [^]
|
|
|
|
// caution: newline is accepted
|
|
/* class not */ class-not = "^";
|
|
class-char:nows = [^\\\[\]\^\-] | "\\" . /* foo
|
|
bar */;
|
|
char-range:nows = class-char "-" class-char // foo
|
|
;
|
|
char-class:nows = "[" class-not? (class-char | char-range)* "]"; // foo
|
|
/* bar
|
|
baz */
|
|
|
|
// newline is accepted
|
|
sequence-char:nows = [^\\"] | "\\" .;
|
|
char-sequence:nows = "\"" sequence-char* "\"";
|
|
|
|
terminal:alias = any-char | char-class | char-sequence;
|
|
|
|
symbol:nows = [^\\ \n\t\b\f\r\v/.\[\]\"{}\^+*?|():=;]+;
|
|
|
|
group:alias = "(" expression ")";
|
|
|
|
number:alias:nows = [0-9]+;
|
|
count = number;
|
|
count-quantifier = "{" count "}";
|
|
range-from = number;
|
|
range-to = number;
|
|
range-quantifier = "{" range-from? "," range-to? "}";
|
|
one-or-more = "+";
|
|
zero-or-more = "*";
|
|
zero-or-one = "?";
|
|
quantity:alias = count-quantifier
|
|
| range-quantifier
|
|
| one-or-more
|
|
| zero-or-more
|
|
| zero-or-one;
|
|
|
|
item:nows = (terminal | symbol | group) quantity?;
|
|
sequence = item+;
|
|
|
|
option:alias = terminal | symbol | group | sequence;
|
|
|
|
// DOC: how the order matters
|
|
choice = option ("|" option)+;
|
|
|
|
// DOC: not having 'not' needs some tricks sometimes
|
|
|
|
expression:alias = terminal | symbol | group | sequence | choice;
|
|
|
|
alias = "alias";
|
|
ws = "ws";
|
|
nows = "nows";
|
|
kw = "kw";
|
|
nokw = "nokw";
|
|
failpass = "failpass";
|
|
root = "root";
|
|
flag:alias = alias | ws | nows | kw | nokw | failpass | root;
|
|
definition-name:alias:nows = symbol (":" flag)*;
|
|
definition = definition-name "=" expression;
|
|
|
|
definitions:alias = definition (";"+ definition)*;
|
|
syntax:root = ";"* definitions? ";"*;
|
|
`
|
|
|
|
const testDocCheck = `/*
|
|
foo
|
|
*/
|
|
// bar
|
|
// bar
|
|
// baz
|
|
/* foo
|
|
bar baz */ // foo bar baz
|
|
wschar:alias = // foo
|
|
/* bar */
|
|
" " | "\t" | "\n" | "\b" | "\f" | "\r" | "\v";
|
|
wsc:ws = wschar | comment;
|
|
block-comment:alias:nows = /* foo */ // bar
|
|
"/*" ("*" [^/] | [^*])* "*/";
|
|
line-comment:alias:nows = /*
|
|
foo
|
|
*/
|
|
"//" [^\n]*;
|
|
comment-segment:alias:nows = // bar
|
|
line-comment | block-comment;
|
|
ws-no-nl:alias:nows = " "
|
|
| "\t"
|
|
| "\b"
|
|
/* this one */ /* is a */
|
|
| "\f"
|
|
/* form feed */ // for sure
|
|
| "\r"
|
|
| "\v";
|
|
comment:nows = comment-segment
|
|
/* segment is not the best name */ /* but */
|
|
( ws-no-nl*
|
|
"\n"?
|
|
ws-no-nl*
|
|
// fine
|
|
comment-segment
|
|
)*;
|
|
any-char = "."; // equivalent to [^]
|
|
|
|
// caution: newline is accepted
|
|
/* class not */
|
|
class-not = "^";
|
|
class-char:nows = [^\\\[\]\^\-] | "\\" .; /* foo
|
|
bar */
|
|
char-range:nows = class-char "-" class-char; // foo
|
|
char-class:nows = "[" class-not? (class-char | char-range)* "]"; // foo
|
|
/* bar
|
|
baz */
|
|
|
|
// newline is accepted
|
|
sequence-char:nows = [^\\"] | "\\" .;
|
|
char-sequence:nows = "\"" sequence-char* "\"";
|
|
terminal:alias = any-char | char-class | char-sequence;
|
|
symbol:nows = [^\\ \n\t\b\f\r\v/.\[\]\"{}\^+*?|():=;]+;
|
|
group:alias = "(" expression ")";
|
|
number:alias:nows = [0-9]+;
|
|
count = number;
|
|
count-quantifier = "{" count "}";
|
|
range-from = number;
|
|
range-to = number;
|
|
range-quantifier = "{" range-from? "," range-to? "}";
|
|
one-or-more = "+";
|
|
zero-or-more = "*";
|
|
zero-or-one = "?";
|
|
quantity:alias = count-quantifier | range-quantifier | one-or-more | zero-or-more | zero-or-one;
|
|
item:nows = (terminal | symbol | group) quantity?;
|
|
sequence = item+;
|
|
option:alias = terminal | symbol | group | sequence;
|
|
|
|
// DOC: how the order matters
|
|
choice = option ("|" option)+;
|
|
|
|
// DOC: not having 'not' needs some tricks sometimes
|
|
|
|
expression:alias = terminal | symbol | group | sequence | choice;
|
|
alias = "alias";
|
|
ws = "ws";
|
|
nows = "nows";
|
|
kw = "kw";
|
|
nokw = "nokw";
|
|
failpass = "failpass";
|
|
root = "root";
|
|
flag:alias = alias | ws | nows | kw | nokw | failpass | root;
|
|
definition-name:alias:nows = symbol (":" flag)*;
|
|
definition = definition-name "=" expression;
|
|
definitions:alias = definition (";"+ definition)*;
|
|
syntax:root = ";"* definitions? ";"*;`
|
|
|
|
func TestDocFormat(t *testing.T) {
|
|
for _, test := range []struct{ title, in, out string }{{
|
|
title: "format",
|
|
in: testDoc,
|
|
out: testDocCheck,
|
|
}, {
|
|
title: "check",
|
|
in: testDocCheck,
|
|
out: testDocCheck,
|
|
}} {
|
|
t.Run(test.title, func(t *testing.T) {
|
|
in := bytes.NewBufferString(test.in)
|
|
s := &Syntax{}
|
|
if err := s.ReadSyntax(in); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
if err := s.Format(out); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if out.String() != test.out {
|
|
t.Log(test.out)
|
|
t.Log(out.String())
|
|
t.Fatal()
|
|
}
|
|
})
|
|
}
|
|
}
|