Skip to content

Commit

Permalink
Refactor Eq_Spec to the newest comparator pattern.
Browse files Browse the repository at this point in the history
- We should remove Eq_Spec in the future.
  • Loading branch information
Akirathan committed Jan 24, 2023
1 parent ec53a77 commit fd14bb8
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions test/Tests/src/Semantic/Eq_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,26 @@ spec =
10 === 10 . should_be_true

Test.specify "Different hash prevents equality" <|
x = Wrong_Hash.Elem1
y = Wrong_Hash.Elem2

t = x === y
t . should_equal False
Wrong_Hash.Elem1 === Wrong_Hash.Elem2 . should_be_false

Test.group "Test inequality on numbers" <|
Test.specify "Compare two numbers" <|
x = 10
y = 11
t = x < y
t . should_equal True
10 <== 11 . should_be_true

Test.group "Rational Numbers" <|
Test.specify "3/4 == 6/8" <|
r1 = Rational.Fraction 3 4
r2 = Rational.Fraction 6 8
t = r1 === r2
t . should_equal True
Rational.Fraction 3 4 === Rational.Fraction 6 8 . should_be_true

Test.specify "1/2 != 2/6" <|
r1 = Rational.Fraction 1 2
r2 = Rational.Fraction 2 6
t = r1 === r2
t . should_equal False
Rational.Fraction 1 2 === Rational.Fraction 2 6 . should_be_false

Test.group "Other" <|
Test.specify "texts" <|
"aaa" === "bbb" . should_be_false
"aaa" === "aaa" . should_be_true
"xxx" === "XxX" . should_be_false
"aaa" <== "xxx" . should_be_true
"aaa" >== "xxx" . should_be_false

type Wrong_Hash
Elem1
Expand All @@ -49,23 +44,27 @@ type Wrong_Hash
Comparable.from (_ : Wrong_Hash) = Wrong_Hash_Eq

type Wrong_Hash_Eq
hash self e = case e of
is_ordered = True
hash e = case e of
Wrong_Hash.Elem1 -> 1
Wrong_Hash.Elem2 -> 2
compare self _ _ = True
compare _ _ = True

type Rational
Fraction (numerator:Integer) (denominator:Integer)

Comparable.from (_ : Rational) = Rational_Ordering

type Rational_Ordering
compare self r1 r2 =
is_ordered = True

compare r1 r2 =
v1 = r1.numerator * r2.denominator
v2 = r2.numerator * r1.denominator
if v1 < v2 then Ordering.Less else
if v1 > v2 then Ordering.Greater else
Ordering.Equal
hash self _ = 42

hash _ = 42

main = Test_Suite.run_main spec

0 comments on commit fd14bb8

Please sign in to comment.