Test a calc's operand list, one empty-segment check, and an empty segment after a step up
This commit is contained in:
@@ -6,18 +6,18 @@ import (
|
||||
)
|
||||
|
||||
func TestParseCalcPlacesEachOperandAtItsFirstReading(t *testing.T) {
|
||||
n, err := ParseCalc("b * (a - b) / -2")
|
||||
c, err := ParseCalc("b * (a - b) / -2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := CalcBin{'/', CalcBin{'*', CalcVar{"b", 0}, CalcBin{'-', CalcVar{"a", 1}, CalcVar{"b", 0}}}, CalcNeg{CalcNum(2)}}
|
||||
if !reflect.DeepEqual(n, want) {
|
||||
t.Errorf("ParseCalc = %#v, want %#v", n, want)
|
||||
if !reflect.DeepEqual(c.Expr, want) {
|
||||
t.Errorf("ParseCalc = %#v, want %#v", c.Expr, want)
|
||||
}
|
||||
if got := CalcVars(n); !reflect.DeepEqual(got, []string{"b", "a"}) {
|
||||
t.Errorf("CalcVars = %q, want [b a]", got)
|
||||
if !reflect.DeepEqual(c.Operands, []string{"b", "a"}) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,17 +21,15 @@ func TestSplitArgsQuotesOutsideSelectors(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 {
|
||||
return args[1:]
|
||||
})
|
||||
toks, err := ParseFormat("a{{{x|/y.z[a.b]}{/p as q}{f(1, n)}")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []Token{
|
||||
{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: 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) {
|
||||
t.Errorf("ParseFormat = %+v, want %+v", toks, want)
|
||||
@@ -40,7 +38,7 @@ func TestParseFormatReadsEachTokenKind(t *testing.T) {
|
||||
|
||||
func TestParseFormatRefusesUnbalancedBraces(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,3 +38,14 @@ func TestNameSegmentsDropSelectorsAndStepsUp(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user