Skip to content

Commit bbef1bb

Browse files
committed
Adding tests of intersection types hiding and unhiding
1 parent bc32447 commit bbef1bb

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

test/Base_Tests/src/Main.enso

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import project.Semantic.Error_Spec
1010
import project.Semantic.Import_Loop.Spec as Import_Loop_Spec
1111
import project.Semantic.Meta_Spec
1212
import project.Semantic.Meta_Location_Spec
13+
import project.Semantic.Multi_Value_As_Type_Refinement_Spec
1314
import project.Semantic.Multi_Value_Convert_Spec
1415
import project.Semantic.Multi_Value_Spec
1516
import project.Semantic.Names_Spec
@@ -132,6 +133,7 @@ main filter=Nothing =
132133
Maybe_Spec.add_specs suite_builder
133134
Meta_Spec.add_specs suite_builder
134135
Meta_Location_Spec.add_specs suite_builder
136+
Multi_Value_As_Type_Refinement_Spec.add_specs suite_builder
135137
Multi_Value_Convert_Spec.add_specs suite_builder
136138
Multi_Value_Spec.add_specs suite_builder
137139
Names_Spec.add_specs suite_builder
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from Standard.Base import all
2+
from Standard.Test import all
3+
import Standard.Base.Errors.Common.Type_Error
4+
import Standard.Base.Errors.Common.No_Such_Conversion
5+
import Standard.Base.Errors.Common.No_Such_Method
6+
7+
from project.Semantic.Type_Refinement.Types import A, B, make_a_and_b
8+
9+
id_a (x : A) -> A = x
10+
id_b (x : B) -> B = x
11+
12+
add_specs suite_builder =
13+
suite_builder.group "Multi Value as type refinement" group_builder->
14+
group_builder.specify "conversion A -> B should not be available" <|
15+
just_a = A.A_Ctor 1
16+
Test.expect_panic Type_Error (just_a:B)
17+
Test.expect_panic No_Such_Conversion (B.from just_a)
18+
Test.expect_panic Type_Error (id_b just_a)
19+
20+
group_builder.specify "make_a_and_b->A&B presents as both A and B" <|
21+
ab = make_a_and_b
22+
ab.is_a A . should_be_true
23+
ab.is_a B . should_be_true
24+
(ab:A).to_text . should_equal "(A_Ctor 1)"
25+
(ab:B).to_text . should_equal "(B_Ctor (A_Ctor 1))"
26+
(id_a ab).to_text . should_equal "(A_Ctor 1)"
27+
(id_b ab).to_text . should_equal "(B_Ctor (A_Ctor 1))"
28+
29+
group_builder.specify "after passing A&B value to a function expecting A argument, B becomes hidden" <|
30+
ab = make_a_and_b
31+
a2 = id_a ab
32+
a2.is_a A . should_be_true
33+
a2.is_a B . should_be_false
34+
35+
# Passing a2 to a function expecting B fails because B part was hidden
36+
Test.expect_panic Type_Error (id_b a2)
37+
38+
# But it can be uncovered via explicit cast
39+
(a2:B).to_text . should_equal "(B_Ctor (A_Ctor 1))"
40+
(id_b (a2:B)).to_text . should_equal "(B_Ctor (A_Ctor 1))"
41+
42+
main filter=Nothing =
43+
suite = Test.build suite_builder->
44+
add_specs suite_builder
45+
suite.run_with_filter filter

test/Base_Tests/src/Semantic/Multi_Value_Spec.enso

-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ add_specs suite_builder =
163163
Test.expect_panic Type_Error <|
164164
_ = a_or_b : (A&B)
165165

166-
# This test is failing
167166
group_builder.specify "B --> (A|C) is possible" <|
168167
b = B.B 42
169168
a_or_c = b : (A|C)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
private
2+
3+
from project.Semantic.Type_Refinement.Types import A, B
4+
5+
## This conversion should only be imported in `Type_Refinement.Types` and nowhere else.
6+
B.from (that : A) -> B =
7+
B.B_Ctor that
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from project.Semantic.Type_Refinement.Hidden_Conversions import all
2+
3+
type A
4+
A_Ctor x
5+
6+
type B
7+
B_Ctor x
8+
9+
make_a_and_b -> A & B =
10+
a = A.A_Ctor 1
11+
# Relies on the hidden conversion
12+
(a : A & B)

0 commit comments

Comments
 (0)