mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-12 22:52:05 +00:00
good good study, day day up!
This commit is contained in:
48
vendor/gopl.io/ch7/eval/coverage_test.go
generated
vendored
Normal file
48
vendor/gopl.io/ch7/eval/coverage_test.go
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
// 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
|
Reference in New Issue
Block a user