xgo/runtime/vm_equality_test.go
Daniel 61890b15cb
module refactor (#148)
* wip

* move print and JSON functions to modules

* builtin functions are not replacable now

* builtin functions are added for default nil symbol table

* importables: builtin modules and source modules

* refactoring runtime tests

* fix tests

* update documentation

* cleanup

* clean up cli

* fix REPL prints
2019-03-18 08:15:26 -07:00

50 lines
1.6 KiB
Go

package runtime_test
import (
"fmt"
"testing"
)
func TestEquality(t *testing.T) {
testEquality(t, `1`, `1`, true)
testEquality(t, `1`, `2`, false)
testEquality(t, `1.0`, `1.0`, true)
testEquality(t, `1.0`, `1.1`, false)
testEquality(t, `true`, `true`, true)
testEquality(t, `true`, `false`, false)
testEquality(t, `"foo"`, `"foo"`, true)
testEquality(t, `"foo"`, `"bar"`, false)
testEquality(t, `'f'`, `'f'`, true)
testEquality(t, `'f'`, `'b'`, false)
testEquality(t, `[]`, `[]`, true)
testEquality(t, `[1]`, `[1]`, true)
testEquality(t, `[1]`, `[1, 2]`, false)
testEquality(t, `["foo", "bar"]`, `["foo", "bar"]`, true)
testEquality(t, `["foo", "bar"]`, `["bar", "foo"]`, false)
testEquality(t, `{}`, `{}`, true)
testEquality(t, `{a: 1, b: 2}`, `{b: 2, a: 1}`, true)
testEquality(t, `{a: 1, b: 2}`, `{b: 2}`, false)
testEquality(t, `{a: 1, b: {}}`, `{b: {}, a: 1}`, true)
testEquality(t, `1`, `"foo"`, false)
testEquality(t, `1`, `true`, false)
testEquality(t, `[1]`, `["1"]`, false)
testEquality(t, `[1, [2]]`, `[1, ["2"]]`, false)
testEquality(t, `{a: 1}`, `{a: "1"}`, false)
testEquality(t, `{a: 1, b: {c: 2}}`, `{a: 1, b: {c: "2"}}`, false)
}
func testEquality(t *testing.T, lhs, rhs string, expected bool) {
// 1. equality is commutative
// 2. equality and inequality must be always opposite
expect(t, fmt.Sprintf("out = %s == %s", lhs, rhs), nil, expected)
expect(t, fmt.Sprintf("out = %s == %s", rhs, lhs), nil, expected)
expect(t, fmt.Sprintf("out = %s != %s", lhs, rhs), nil, !expected)
expect(t, fmt.Sprintf("out = %s != %s", rhs, lhs), nil, !expected)
}