aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-05-10 11:05:07 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-02 14:23:21 +0000
commitff9bf90b87c63a0aa3a6bf2989b3835130eb8ef1 (patch)
tree2c63643500ba972171331aa3535a6e6ef7667622
parenttest alv.type pp (diff)
downloadalive-ff9bf90b87c63a0aa3a6bf2989b3835130eb8ef1.tar.gz
alive-ff9bf90b87c63a0aa3a6bf2989b3835130eb8ef1.zip
add and test Type:eq(a,b)
-rw-r--r--alv/type.moon20
-rw-r--r--spec/type_spec.moon59
2 files changed, 64 insertions, 15 deletions
diff --git a/alv/type.moon b/alv/type.moon
index 4367bd2..c8808d4 100644
--- a/alv/type.moon
+++ b/alv/type.moon
@@ -29,6 +29,12 @@ class Type
-- @tparam any value
-- @treturn string
+ --- check two values of this type for equality.
+ -- @function eq
+ -- @tparam any a
+ -- @tparam any b
+ -- @treturn bool
+
--- create a `SigStream` of this type.
-- @tparam ?any init initial value
-- @treturn SigStream
@@ -59,6 +65,8 @@ class Primitive extends Type
else
tostring value
+ eq: (a, b) => a == b
+
__eq: (other) => @name == other.name
__tostring: => @name
@@ -78,6 +86,12 @@ class Struct extends Type
inner = table.concat ["#{k}: #{@types[k]\pp v}" for k, v in opairs value], ' '
"{#{inner}}"
+ eq: (a, b) =>
+ for key, type in pairs @types
+ if not type\eq a[key], b[key]
+ return false
+ true
+
__eq: (other) => same @types, other.types
__tostring: =>
inner = table.concat ["#{k}: #{v}" for k, v in opairs @types], ' '
@@ -105,6 +119,12 @@ class Array extends Type
inner = table.concat [@type\pp v for v in *value], ' '
"[#{inner}]"
+ eq: (a, b) =>
+ for i=1, @size
+ if not @type\eq a[i], b[i]
+ return false
+ true
+
__eq: (other) => @size == other.size and @type == other.type
__tostring: => "#{@type}[#{@size}]"
diff --git a/spec/type_spec.moon b/spec/type_spec.moon
index 4ec5e7e..a435067 100644
--- a/spec/type_spec.moon
+++ b/spec/type_spec.moon
@@ -11,11 +11,6 @@ describe 'Primitive', ->
assert.is.equal 'num', tostring num
assert.is.equal 'str', tostring str
- it 'pretty-prints values', ->
- assert.is.equal 'true', bool\pp true
- assert.is.equal '4.134', num\pp 4.134
- assert.is.equal '"hello"', str\pp "hello"
-
it 'implements __eq sensibly', ->
assert.is.equal (Primitive 'bool'), bool
assert.is.equal (Primitive 'num'), num
@@ -23,23 +18,49 @@ describe 'Primitive', ->
assert.not.equal num, bool
assert.not.equal str, num
+ it ':pp pretty-prints values', ->
+ assert.is.equal 'true', bool\pp true
+ assert.is.equal '4.134', num\pp 4.134
+ assert.is.equal '"hello"', str\pp "hello"
+
+ it ':eq compares values', ->
+ a, b = {}, {}
+ assert.is.true bool\eq true, true
+ assert.is.false bool\eq true, false
+ assert.is.true num\eq 1, 1
+ assert.is.false num\eq 1, -1
+ assert.is.true num\eq a, a
+ assert.is.false num\eq a, b
+
describe 'Array', ->
vec3 = Array 3, num
-
+ str32 = Array 2, Array 3, str
it 'stringifies well', ->
assert.is.equal 'num[3]', tostring vec3
assert.is.equal 'my-type[3][24]', tostring Array 24, Array 3, 'my-type'
- it 'pretty-prints values', ->
- assert.is.equal '[1 2 3]', vec3\pp { 1, 2, 3 }
- assert.is.equal '[["a" "b" "c"] ["d" "e" "f"]]',
- (Array 2, Array 3, str)\pp { {'a', 'b', 'c'}, {'d', 'e', 'f'} }
-
it 'implements __eq sensibly', ->
assert.is.equal vec3, Array 3, num
assert.not.equal vec3, Array 2, num
assert.not.equal vec3, Array 3, str
+ it ':pp pretty-prints values', ->
+ assert.is.equal '[1 2 3]', vec3\pp { 1, 2, 3 }
+ assert.is.equal '[["a" "b" "c"] ["d" "e" "f"]]',
+ str32\pp { {'a', 'b', 'c'}, {'d', 'e', 'f'} }
+
+ it ':eq compares values', ->
+ a, b, c = {1, 2, 3}, {1, 2, 3}, {}
+ assert.is.true vec3\eq a, a
+ assert.is.true vec3\eq a, b
+ assert.is.false vec3\eq a, {1, 2, 4}
+ assert.is.true vec3\eq {1, 2, c}, {1, 2, c}
+ assert.is.false vec3\eq {1, 2, c}, {1, 2, {}}
+ assert.is.true str32\eq { {'a', 'b', 'c'}, {'d', 'e', 'f'} },
+ { {'a', 'b', 'c'}, {'d', 'e', 'f'} }
+ assert.is.false str32\eq { {'a', 'b', 'c'}, {'d', 'e', 'f'} },
+ { {'a', 'b', 'c'}, {'d', 'e', 'g'} }
+
describe 'Struct', ->
play = Struct { note: str, dur: num }
abc = Struct { c: num, b: num, a: num }
@@ -48,16 +69,24 @@ describe 'Struct', ->
assert.is.equal '{dur: num note: str}', tostring play
assert.is.equal '{a: num b: num c: num}', tostring abc
- it 'pretty-prints values', ->
- assert.is.equal '{dur: 0.5 note: "a"}', play\pp { dur: 0.5, note: 'a' }
- assert.is.equal '{a: 1 b: 2 c: 3}', abc\pp { a: 1, b: 2, c: 3 }
-
it 'implements __eq sensibly', ->
assert.is.equal play, Struct { note: str, dur: num }
assert.not.equal play, Struct { note: str }
assert.not.equal play, Struct { note: str, dur: str }
assert.not.equal play, Struct { note: str, dur: num, extra: num }
+ it ':pp pretty-prints values', ->
+ assert.is.equal '{dur: 0.5 note: "a"}', play\pp { dur: 0.5, note: 'a' }
+ assert.is.equal '{a: 1 b: 2 c: 3}', abc\pp { a: 1, b: 2, c: 3 }
+
+ it ':eq compares values', ->
+ a, b, c = { dur: 0.5, note: 'a' }, { dur: 0.5, note: 'a' }, {}
+ assert.is.true play\eq a, a
+ assert.is.true play\eq a, b
+ assert.is.false play\eq a, { dur: 0.5, note: 'b' }
+ assert.is.true play\eq { dur: 0.5, note: c }, { dur: 0.5, note: c }
+ assert.is.false play\eq { dur: 0.5, note: c }, { dur: 0.5, note: {} }
+
describe 'T', ->
it 'provides shorthand for Primitives', ->
assert.is.equal num, T.num