mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-12-29 08:16:21 +00:00
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
|
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||
|
|
||
|
package eval
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
//!+TestCoverage
|
||
|
func TestCoverage(t *testing.T) {
|
||
|
var tests = []struct {
|
||
|
input string
|
||
|
env Env
|
||
|
want string // expected error from Parse/Check or result from Eval
|
||
|
}{
|
||
|
{"x % 2", nil, "unexpected '%'"},
|
||
|
{"!true", nil, "unexpected '!'"},
|
||
|
{"log(10)", nil, `unknown function "log"`},
|
||
|
{"sqrt(1, 2)", nil, "call to sqrt has 2 args, want 1"},
|
||
|
{"sqrt(A / pi)", Env{"A": 87616, "pi": math.Pi}, "167"},
|
||
|
{"pow(x, 3) + pow(y, 3)", Env{"x": 9, "y": 10}, "1729"},
|
||
|
{"5 / 9 * (F - 32)", Env{"F": -40}, "-40"},
|
||
|
}
|
||
|
|
||
|
for _, test := range tests {
|
||
|
expr, err := Parse(test.input)
|
||
|
if err == nil {
|
||
|
err = expr.Check(map[Var]bool{})
|
||
|
}
|
||
|
if err != nil {
|
||
|
if err.Error() != test.want {
|
||
|
t.Errorf("%s: got %q, want %q", test.input, err, test.want)
|
||
|
}
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
got := fmt.Sprintf("%.6g", expr.Eval(test.env))
|
||
|
if got != test.want {
|
||
|
t.Errorf("%s: %v => %s, want %s",
|
||
|
test.input, test.env, got, test.want)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//!-TestCoverage
|