Test a calc's operand list, one empty-segment check, and an empty segment after a step up

This commit is contained in:
2026-10-05 12:25:22 +02:00
committed by Lillem4n
parent 5e56b44dfa
commit 3479f9cf98
5 changed files with 26 additions and 16 deletions
+4 -4
View File
@@ -101,12 +101,12 @@ func TestCalcReproducible(t *testing.T) {
// call that is not a calc, and for one whose expression does not parse: none, either way. // call that is not a calc, and for one whose expression does not parse: none, either way.
func TestParsedCallReadsOnlyAnOperandBuiltin(t *testing.T) { func TestParsedCallReadsOnlyAnOperandBuiltin(t *testing.T) {
for _, format := range []string{"{luhn()}", "{calc()}", "{calc(1 +)}", "{calc(()}"} { for _, format := range []string{"{luhn()}", "{calc()}", "{calc(1 +)}", "{calc(()}"} {
if toks, err := grammar.ParseFormat(format, builtinOperands); err != nil || len(toks) != 1 || toks[0].Names != nil { if toks, err := grammar.ParseFormat(format); err != nil || len(toks) != 1 || tokenReads(toks[0]) != nil {
t.Errorf("ParseFormat(%q) = %+v, %v, want one call reading no operand", format, toks, err) t.Errorf("tokenReads(%q) = %+v, %v, want one call reading no operand", format, toks, err)
} }
} }
if toks, err := grammar.ParseFormat("{calc(net * qty)}", builtinOperands); err != nil || len(toks) != 1 || len(toks[0].Names) != 2 { if toks, err := grammar.ParseFormat("{calc(net * qty)}"); err != nil || len(toks) != 1 || len(tokenReads(toks[0])) != 2 {
t.Errorf("ParseFormat({calc(net * qty)}) = %+v, %v, want both operands", toks, err) t.Errorf("tokenReads({calc(net * qty)}) = %+v, %v, want both operands", toks, err)
} }
} }
+6 -6
View File
@@ -6,18 +6,18 @@ import (
) )
func TestParseCalcPlacesEachOperandAtItsFirstReading(t *testing.T) { func TestParseCalcPlacesEachOperandAtItsFirstReading(t *testing.T) {
n, err := ParseCalc("b * (a - b) / -2") c, err := ParseCalc("b * (a - b) / -2")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
want := CalcBin{'/', CalcBin{'*', CalcVar{"b", 0}, CalcBin{'-', CalcVar{"a", 1}, CalcVar{"b", 0}}}, CalcNeg{CalcNum(2)}} want := CalcBin{'/', CalcBin{'*', CalcVar{"b", 0}, CalcBin{'-', CalcVar{"a", 1}, CalcVar{"b", 0}}}, CalcNeg{CalcNum(2)}}
if !reflect.DeepEqual(n, want) { if !reflect.DeepEqual(c.Expr, want) {
t.Errorf("ParseCalc = %#v, want %#v", n, want) t.Errorf("ParseCalc = %#v, want %#v", c.Expr, want)
} }
if got := CalcVars(n); !reflect.DeepEqual(got, []string{"b", "a"}) { if !reflect.DeepEqual(c.Operands, []string{"b", "a"}) {
t.Errorf("CalcVars = %q, want [b a]", got) t.Errorf("Operands = %q, want [b a]", c.Operands)
} }
if got := CalcText(n); got != "((b * (a - b)) / -2)" { if got := CalcText(c.Expr); got != "((b * (a - b)) / -2)" {
t.Errorf("CalcText = %q", got) t.Errorf("CalcText = %q", got)
} }
} }
+4 -6
View File
@@ -21,17 +21,15 @@ func TestSplitArgsQuotesOutsideSelectors(t *testing.T) {
} }
func TestParseFormatReadsEachTokenKind(t *testing.T) { func TestParseFormatReadsEachTokenKind(t *testing.T) {
toks, err := ParseFormat("a{{{x|/y.z[a.b]}{/p as q}{f(1, n)}", func(fn string, args []string) []string { toks, err := ParseFormat("a{{{x|/y.z[a.b]}{/p as q}{f(1, n)}")
return args[1:]
})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
want := []Token{ want := []Token{
{Kind: LiteralRun, Lit: "a{"}, {Kind: LiteralRun, Lit: "a{"},
{Kind: NameRead, Body: "x|/y.z[a.b]", Names: []string{"x", "/y.z[a.b]"}}, {Kind: NameRead, Body: "x|/y.z[a.b]", Arms: []string{"x", "/y.z[a.b]"}},
{Kind: NameBind, Body: "/p as q", BoundRef: "/p", Bound: "q"}, {Kind: NameBind, Body: "/p as q", BoundRef: "/p", Bound: "q"},
{Kind: BuiltinCall, Body: "f(1, n)", Fn: "f", Args: []string{"1", "n"}, Names: []string{"n"}}, {Kind: BuiltinCall, Body: "f(1, n)", Fn: "f", Args: []string{"1", "n"}},
} }
if !reflect.DeepEqual(toks, want) { if !reflect.DeepEqual(toks, want) {
t.Errorf("ParseFormat = %+v, want %+v", toks, want) t.Errorf("ParseFormat = %+v, want %+v", toks, want)
@@ -40,7 +38,7 @@ func TestParseFormatReadsEachTokenKind(t *testing.T) {
func TestParseFormatRefusesUnbalancedBraces(t *testing.T) { func TestParseFormatRefusesUnbalancedBraces(t *testing.T) {
for _, format := range []string{"{a", "a}", "{a{b}}", "{f(}"} { for _, format := range []string{"{a", "a}", "{a{b}}", "{f(}"} {
if _, err := ParseFormat(format, nil); err == nil { if _, err := ParseFormat(format); err == nil {
t.Errorf("ParseFormat(%q) = nil error, want one", format) t.Errorf("ParseFormat(%q) = nil error, want one", format)
} }
} }
+11
View File
@@ -38,3 +38,14 @@ func TestNameSegmentsDropSelectorsAndStepsUp(t *testing.T) {
t.Errorf("NameSegments = %q, want [a c]", got) t.Errorf("NameSegments = %q, want [a c]", got)
} }
} }
func TestCheckSegmentsRefusesAnEmptySegment(t *testing.T) {
for _, segs := range [][]string{{""}, {"a", ""}, {"", "a"}, {"a", "", "b"}} {
if CheckSegments(segs) == nil {
t.Errorf("CheckSegments(%q) = nil, want an error", segs)
}
}
if err := CheckSegments([]string{"a", "[b]", "..", "c"}); err != nil {
t.Errorf("CheckSegments = %v, want nil", err)
}
}
+1
View File
@@ -318,6 +318,7 @@ func TestTableStepUpRefusals(t *testing.T) {
"locality[L4]..municipality[0180]": "municipality[0180]", "locality[L4]..municipality[0180]": "municipality[0180]",
"locality[L4]..region": "..municipality..region", "locality[L4]..region": "..municipality..region",
"locality[L4]...municipality": "empty segment", "locality[L4]...municipality": "empty segment",
"locality[L4]..municipality.": "empty segment",
"locality[L4].municipality.name": "..municipality.name", "locality[L4].municipality.name": "..municipality.name",
"locality[L4].name..municipality": "column", "locality[L4].name..municipality": "column",
"x..y": "only from a table's row", "x..y": "only from a table's row",