| 1 | package calc |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func evalStr(t *testing.T, src string, env Env) string { |
| 6 | t.Helper() |
| 7 | v := Eval(mustParse(t, src), env) |
| 8 | if v.Kind != KindStr { |
| 9 | t.Fatalf("result Kind = %v, want KindStr (value %+v)", v.Kind, v) |
| 10 | } |
| 11 | return v.Str |
| 12 | } |
| 13 | |
| 14 | func TestConcat(t *testing.T) { |
| 15 | if got := evalStr(t, `CONCAT("foo","bar")`, emptyEnv{}); got != "foobar" { |
| 16 | t.Errorf("CONCAT = %q, want %q", got, "foobar") |
| 17 | } |
| 18 | if got := evalStr(t, `CONCAT("a",1,"b")`, emptyEnv{}); got != "a1b" { |
| 19 | t.Errorf("CONCAT with number = %q, want %q", got, "a1b") |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestLeftRight(t *testing.T) { |
| 24 | if got := evalStr(t, `LEFT("hello",3)`, emptyEnv{}); got != "hel" { |
| 25 | t.Errorf("LEFT = %q, want %q", got, "hel") |
| 26 | } |
| 27 | if got := evalStr(t, `LEFT("hello")`, emptyEnv{}); got != "h" { |
| 28 | t.Errorf("LEFT default = %q, want %q", got, "h") |
| 29 | } |
| 30 | if got := evalStr(t, `RIGHT("hello",3)`, emptyEnv{}); got != "llo" { |
| 31 | t.Errorf("RIGHT = %q, want %q", got, "llo") |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestLeftLongerThanTextReturnsWholeText(t *testing.T) { |
| 36 | if got := evalStr(t, `LEFT("hi",10)`, emptyEnv{}); got != "hi" { |
| 37 | t.Errorf("LEFT overrun = %q, want %q", got, "hi") |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestMid(t *testing.T) { |
| 42 | if got := evalStr(t, `MID("hello",2,3)`, emptyEnv{}); got != "ell" { |
| 43 | t.Errorf("MID = %q, want %q", got, "ell") |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestMidStartPastEndIsEmptyNotError(t *testing.T) { |
| 48 | if got := evalStr(t, `MID("hi",10,3)`, emptyEnv{}); got != "" { |
| 49 | t.Errorf("MID past end = %q, want empty string", got) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestLen(t *testing.T) { |
| 54 | v := Eval(mustParse(t, `LEN("hello")`), emptyEnv{}) |
| 55 | if v.Kind != KindNum || v.Num != 5 { |
| 56 | t.Fatalf(`LEN("hello") = %v, want 5`, v) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestTrim(t *testing.T) { |
| 61 | if got := evalStr(t, `TRIM(" a b ")`, emptyEnv{}); got != "a b" { |
| 62 | t.Errorf("TRIM = %q, want %q", got, "a b") |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestUpperLower(t *testing.T) { |
| 67 | if got := evalStr(t, `UPPER("MixedCase")`, emptyEnv{}); got != "MIXEDCASE" { |
| 68 | t.Errorf("UPPER = %q, want %q", got, "MIXEDCASE") |
| 69 | } |
| 70 | if got := evalStr(t, `LOWER("MixedCase")`, emptyEnv{}); got != "mixedcase" { |
| 71 | t.Errorf("LOWER = %q, want %q", got, "mixedcase") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestConcatPropagatesError(t *testing.T) { |
| 76 | env := mapEnv{{1, 1}: {Kind: KindErr, Err: ErrRef}} |
| 77 | v := Eval(mustParse(t, `CONCAT(A1,"x")`), env) |
| 78 | if v.Kind != KindErr || v.Err != ErrRef { |
| 79 | t.Fatalf(`CONCAT(A1,"x") = %v, want #REF!`, v) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestLeftNegativeCountIsValueError(t *testing.T) { |
| 84 | v := Eval(mustParse(t, `LEFT("hi",-1)`), emptyEnv{}) |
| 85 | if v.Kind != KindErr || v.Err != ErrValue { |
| 86 | t.Fatalf(`LEFT("hi",-1) = %v, want #VALUE!`, v) |
| 87 | } |
| 88 | } |
| 89 | |