MCPcopy
hub / github.com/adonovan/gopl.io / TestCoverage

Function TestCoverage

ch7/eval/coverage_test.go:13–46  ·  view source on GitHub ↗

!+TestCoverage

(t *testing.T)

Source from the content-addressed store, hash-verified

11
12//!+TestCoverage
13func TestCoverage(t *testing.T) {
14 var tests = []struct {
15 input string
16 env Env
17 want string // expected error from Parse/Check or result from Eval
18 }{
19 {"x % 2", nil, "unexpected '%'"},
20 {"!true", nil, "unexpected '!'"},
21 {"log(10)", nil, `unknown function "log"`},
22 {"sqrt(1, 2)", nil, "call to sqrt has 2 args, want 1"},
23 {"sqrt(A / pi)", Env{"A": 87616, "pi": math.Pi}, "167"},
24 {"pow(x, 3) + pow(y, 3)", Env{"x": 9, "y": 10}, "1729"},
25 {"5 / 9 * (F - 32)", Env{"F": -40}, "-40"},
26 }
27
28 for _, test := range tests {
29 expr, err := Parse(test.input)
30 if err == nil {
31 err = expr.Check(map[Var]bool{})
32 }
33 if err != nil {
34 if err.Error() != test.want {
35 t.Errorf("%s: got %q, want %q", test.input, err, test.want)
36 }
37 continue
38 }
39
40 got := fmt.Sprintf("%.6g", expr.Eval(test.env))
41 if got != test.want {
42 t.Errorf("%s: %v => %s, want %s",
43 test.input, test.env, got, test.want)
44 }
45 }
46}
47
48//!-TestCoverage

Callers

nothing calls this directly

Calls 3

ParseFunction · 0.85
CheckMethod · 0.65
EvalMethod · 0.65

Tested by

no test coverage detected