separate core from cli into submodules, add support for boolean and and or operations, make more fields public, fix passing arguments to functions
This commit is contained in:
parent
7498c85424
commit
62656c6dff
19 changed files with 202 additions and 99 deletions
93
core/values_test.go
Normal file
93
core/values_test.go
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
package core
|
||||
|
||||
import "testing"
|
||||
|
||||
func CompareValues(t *testing.T, got Value, want Value) {
|
||||
if got == nil || want == nil {
|
||||
t.Fatalf("a value is nil: got %v; want %v", got, want)
|
||||
}
|
||||
|
||||
if got.Type() != want.Type() {
|
||||
t.Fatalf("type mismatch: got %v want %v", got.Type(), want.Type())
|
||||
}
|
||||
|
||||
switch got.Type() {
|
||||
case NilValueType:
|
||||
t.Logf("Both are nil")
|
||||
return
|
||||
case BoolValueType:
|
||||
if got.(BoolValue) != want.(BoolValue) {
|
||||
t.Errorf("bool value mismatch: got %v, want %v", got.(BoolValue), want.(BoolValue))
|
||||
} else {
|
||||
t.Logf("Both are same boolean (%s)", want.(BoolValue).String())
|
||||
}
|
||||
case NumberValueType:
|
||||
if got.(NumberValue) != want.(NumberValue) {
|
||||
t.Errorf("number value mismatch: got %v, want %v", got.(NumberValue), want.(NumberValue))
|
||||
} else {
|
||||
t.Logf("Both are same number (%s)", got.(NumberValue).String())
|
||||
}
|
||||
case StringValueType:
|
||||
if got.(StringValue) != want.(StringValue) {
|
||||
t.Errorf("string value mismatch: got %v, want %v", got.(StringValue), want.(StringValue))
|
||||
} else {
|
||||
t.Logf("Both are same string (%s)", got.(StringValue).String())
|
||||
}
|
||||
case FunctionValueType:
|
||||
n := got.(FunctionValue)
|
||||
m := want.(FunctionValue)
|
||||
|
||||
if n.Name != m.Name {
|
||||
t.Errorf("function name mismatch: got %v, want %v", n.Name, m.Name)
|
||||
}
|
||||
|
||||
if len(n.Params) != len(m.Params) {
|
||||
t.Errorf("function params length mismatch: got %v, want %v", len(m.Params), len(n.Params))
|
||||
}
|
||||
|
||||
for i, v := range n.Params {
|
||||
if v != m.Params[i] {
|
||||
t.Errorf("function params mismatch: got %v, want %v", v, m.Params[i])
|
||||
}
|
||||
}
|
||||
|
||||
CompareChunks(t, n.Chunk, m.Chunk)
|
||||
case BuiltinFunctionValueType:
|
||||
n := got.(BuiltinFunctionValue)
|
||||
m := want.(BuiltinFunctionValue)
|
||||
|
||||
if n.Name != m.Name {
|
||||
t.Errorf("builtin function name mismatch: got %v, want %v", n.Name, m.Name)
|
||||
}
|
||||
|
||||
if len(n.Parameters) != len(m.Parameters) {
|
||||
t.Errorf("builtin function parameter count mismatch: got %v, want %v", n.Parameters, m.Parameters)
|
||||
}
|
||||
|
||||
for i, v := range n.Parameters {
|
||||
if v != m.Parameters[i] {
|
||||
t.Errorf("builtin function parameter %d mismatch: got %v, want %v", i, v, m.Parameters[i])
|
||||
}
|
||||
}
|
||||
|
||||
if &n.F != &m.F {
|
||||
t.Errorf("builtin function f mismatch: got %v, want %v", &n.F, &m.F)
|
||||
}
|
||||
case VariableValueType:
|
||||
n := got.(*VariableValue)
|
||||
m := want.(*VariableValue)
|
||||
|
||||
if n.name != m.name {
|
||||
t.Errorf("variable name mismatch: got %v, want %v", n.name, m.name)
|
||||
}
|
||||
|
||||
if n.scope != m.scope {
|
||||
t.Errorf("variable scope mismatch: got %v, want %v", n.scope, m.scope)
|
||||
}
|
||||
|
||||
CompareValues(t, n.value, m.value)
|
||||
|
||||
default:
|
||||
panic("unimplemented comparison")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue