Skip to content

Commit

Permalink
Merge pull request #1211 from RickBarretto/fix-comparison-equal-for-u…
Browse files Browse the repository at this point in the history
…nits

[Comparisons]: Add support for Unit values
  • Loading branch information
drkameleon authored Jun 13, 2023
2 parents 784a3cc + a397769 commit 5fe5cff
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/vm/values/comparison.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ proc `==`*(x: Value, y: Value): bool =
if not (v==y.d[k]): return false

return true

of Unit:
return x.u == y.u
of Object:
if (let compareMethod = x.proto.methods.getOrDefault("compare", nil); not compareMethod.isNil):
return x.proto.doCompare(x,y) == 0
Expand Down Expand Up @@ -391,6 +392,8 @@ proc `<`*(x: Value, y: Value): bool {.inline.}=
return x.a.len < y.a.len
of Dictionary:
return false
of Unit:
return false
of Object:
if (let compareMethod = x.proto.methods.getOrDefault("compare", nil); not compareMethod.isNil):
return x.proto.doCompare(x, y) == -1
Expand Down Expand Up @@ -501,6 +504,8 @@ proc `>`*(x: Value, y: Value): bool {.inline.}=
return x.a.len > y.a.len
of Dictionary:
return false
of Unit:
return false
of Object:
if (let compareMethod = x.proto.methods.getOrDefault("compare", nil); not compareMethod.isNil):
return x.proto.doCompare(x,y) == 1
Expand Down
104 changes: 104 additions & 0 deletions tests/unittests/lib.comparison.art
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,16 @@ do [
to :date .format: "MMM dd" "Jan 16"
passed

topic « between? - :unit

ensure -> between? `USD `USD `USD
ensure -> between? `USD `USD `EUR
ensure -> between? `EUR `USD `EUR
passed

; TODO: fix me soon
; ensure -> not? between? `BRL `USD `EUR
; passed

]

Expand Down Expand Up @@ -1172,6 +1182,14 @@ do [
ensure -> 1 = compare e d
passed

topic « compare - :unit

ensure -> 0 = compare `USD `USD
ensure -> 1 = compare `USD `EUR
ensure -> 1 = compare `EUR `USD
passed


]

topic « equal?
Expand Down Expand Up @@ -2266,6 +2284,20 @@ do [
-> equal? var 'regex? {/regex?/}
] => not? passed


topic « equal? - :unit

ensure -> equal? `USD `USD
ensure -> equal? `EUR `EUR
ensure -> `USD = `USD
ensure -> `EUR = `EUR
passed

ensure -> not? equal? `USD `EUR
ensure -> not? equal? `EUR `USD
ensure -> not? `EUR = `USD
passed

]

topic « greater?
Expand Down Expand Up @@ -2967,6 +2999,18 @@ do [
passed


topic « greater? - :unit

ensure -> not? greater? `USD `USD
ensure -> not? greater? `EUR `EUR
ensure -> not? greater? `USD `EUR
ensure -> not? greater? `EUR `USD
ensure -> not? `USD > `USD
ensure -> not? `EUR > `EUR
ensure -> not? `EUR > `USD
passed


topic « greater? - incompatible types

; NOTES:
Expand Down Expand Up @@ -4191,6 +4235,19 @@ do [
ensure -> not? greaterOrEqual? a d
passed

topic « greaterOrEqual? - :unit

ensure -> greaterOrEqual? `USD `USD
ensure -> greaterOrEqual? `EUR `EUR
ensure -> `USD >= `USD
ensure -> `EUR >= `EUR
passed

ensure -> not? greaterOrEqual? `USD `EUR
ensure -> not? greaterOrEqual? `EUR `USD
ensure -> not? `EUR >= `USD
passed


topic « greaterOrEqual? - incompatible types

Expand Down Expand Up @@ -5373,6 +5430,17 @@ do [
ensure -> not? less? a d
passed

topic « less? - :unit

ensure -> not? less? `USD `USD
ensure -> not? less? `EUR `EUR
ensure -> not? less? `USD `EUR
ensure -> not? less? `EUR `USD
ensure -> not? `USD < `USD
ensure -> not? `EUR < `EUR
ensure -> not? `EUR < `USD
passed


topic « less? - incompatible types

Expand Down Expand Up @@ -6599,6 +6667,19 @@ do [
ensure -> not? lessOrEqual? a d
passed

topic « lessOrEqual? - :unit

ensure -> lessOrEqual? `USD `USD
ensure -> lessOrEqual? `EUR `EUR
ensure -> `USD =< `USD
ensure -> `EUR =< `EUR
passed

ensure -> not? lessOrEqual? `USD `EUR
ensure -> not? equal? `EUR `USD
ensure -> not? `EUR =< `USD
passed


topic « lessOrEqual? - incompatible types

Expand Down Expand Up @@ -7690,6 +7771,19 @@ do [
ensure -> notEqual? a\year c\year
passed

topic « notEqual? - :unit

ensure -> not? notEqual? `USD `USD
ensure -> not? notEqual? `EUR `EUR
ensure -> not? `USD <> `USD
ensure -> not? `EUR <> `EUR
passed

ensure -> notEqual? `USD `EUR
ensure -> notEqual? `EUR `USD
ensure -> `EUR <> `USD
passed


topic « notEqual? - incompatible types

Expand Down Expand Up @@ -8740,6 +8834,16 @@ do [
ensure -> not? same? a\year c\year
passed

topic « same? - :unit

ensure -> same? `USD `USD
ensure -> same? `EUR `EUR
passed

ensure -> not? same? `USD `EUR
ensure -> not? same? `EUR `USD
passed


topic « same? - incompatible types

Expand Down
32 changes: 32 additions & 0 deletions tests/unittests/lib.comparison.res
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
>> between? - :date
[+] passed!

>> between? - :unit
[+] passed!

>> between? - test with reversed index
[+] passed!
[+] passed!
Expand Down Expand Up @@ -208,6 +211,9 @@
[+] passed!
[+] passed!

>> compare - :unit
[+] passed!

>> equal?

>> equal? - :integer :floating :rational
Expand Down Expand Up @@ -365,6 +371,10 @@
[+] passed!
[+] passed!

>> equal? - :unit
[+] passed!
[+] passed!

>> greater?

>> greater? - :integer :floating :rational
Expand Down Expand Up @@ -496,6 +506,9 @@
[+] passed!
[+] passed!

>> greater? - :unit
[+] passed!

>> greater? - incompatible types
[+] passed!
[+] passed!
Expand Down Expand Up @@ -659,6 +672,10 @@
[+] passed!
[+] passed!

>> greaterOrEqual? - :unit
[+] passed!
[+] passed!

>> greaterOrEqual? - incompatible types
[+] passed!
[+] passed!
Expand Down Expand Up @@ -822,6 +839,9 @@
[+] passed!
[+] passed!

>> less? - :unit
[+] passed!

>> less? - incompatible types
[+] passed!
[+] passed!
Expand Down Expand Up @@ -984,6 +1004,10 @@
[+] passed!
[+] passed!

>> lessOrEqual? - :unit
[+] passed!
[+] passed!

>> lessOrEqual? - incompatible types
[+] passed!
[+] passed!
Expand Down Expand Up @@ -1139,6 +1163,10 @@
[+] passed!
[+] passed!

>> notEqual? - :unit
[+] passed!
[+] passed!

>> notEqual? - incompatible types
[+] passed!
[+] passed!
Expand Down Expand Up @@ -1299,6 +1327,10 @@
[+] passed!
[+] passed!

>> same? - :unit
[+] passed!
[+] passed!

>> same? - incompatible types
[+] passed!
[+] passed!
Expand Down

0 comments on commit 5fe5cff

Please sign in to comment.