From 52922512e544d9026e3c7b1546cd9aaba84df95f Mon Sep 17 00:00:00 2001 From: Daniel Kang Date: Thu, 10 Jan 2019 00:01:02 -0800 Subject: [PATCH] add equality tests --- vm/vm_equality_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 vm/vm_equality_test.go diff --git a/vm/vm_equality_test.go b/vm/vm_equality_test.go new file mode 100644 index 0000000..4bb2cf5 --- /dev/null +++ b/vm/vm_equality_test.go @@ -0,0 +1,50 @@ +package vm_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), expected) + expect(t, fmt.Sprintf("out = %s == %s", rhs, lhs), expected) + expect(t, fmt.Sprintf("out = %s != %s", lhs, rhs), !expected) + expect(t, fmt.Sprintf("out = %s != %s", rhs, lhs), !expected) +}