11from Standard.Base import all
2+ import Standard.Base.Errors.Common.No_Such_Method
3+ import Standard.Base.Errors.Common.Type_Error
24
35from Standard.Table import Column, Table, Value_Type
46
@@ -8,59 +10,64 @@ add_specs suite_builder =
810 suite_builder.group "single-value Column" group_builder->
911 group_builder.specify "should be a Column but also the underlying value" <|
1012 c1 = Column.from_vector "c1" [1]
11- c1.is_a Column . should_be_true
12- c1.is_a Integer . should_be_true
13- c1.is_a Text . should_be_false
13+ c1.should_be_a Column
14+ (c1: Integer).should_equal 1
15+ Test.expect_panic Type_Error (c1:Text)
1416
1517 c2 = Column.from_vector "c2" [1.5]
16- c2.is_a Column . should_be_true
17- c2.is_a Float . should_be_true
18+ c2.should_be_a Column
19+ (c2:Float).should_equal 1.5
20+ # But a float is not an integer:
21+ Test.expect_panic Type_Error (c2:Integer)
1822
1923 c3 = Column.from_vector "c3" ["a"]
20- c3.is_a Column . should_be_true
21- c3.is_a Text . should_be_true
22- c3.is_a Integer . should_be_false
24+ c3.should_be_a Column
25+ (c3: Text).should_equal "a"
26+ Test.expect_panic Type_Error (c3:Integer)
2327
2428 c4 = Column.from_vector "c4" [True]
25- c4.is_a Column . should_be_true
26- c4.is_a Boolean . should_be_true
29+ c4.should_be_a Column
30+ (c4: Boolean).should_equal True
2731
2832 c5 = Column.from_vector "c5" [Date.new 2021 1 1]
29- c5.is_a Column . should_be_true
30- c5.is_a Date . should_be_true
33+ c5.should_be_a Column
34+ (c5:Date).should_equal ( Date.new 2021 1 1)
3135
3236 c6 = Column.from_vector "c6" [Time_Of_Day.new 12 0 0]
33- c6.is_a Column . should_be_true
34- c6.is_a Time_Of_Day . should_be_true
37+ c6.should_be_a Column
38+ (c6:Time_Of_Day).should_equal ( Time_Of_Day.new 12 0 0)
3539
3640 c7 = Column.from_vector "c7" [Date_Time.new 2021 1 1 12 0 0]
37- c7.is_a Column . should_be_true
38- c7.is_a Date_Time . should_be_true
41+ c7.should_be_a Column
42+ (c7:Date_Time).should_equal ( Date_Time.new 2021 1 1 12 0 0)
3943
4044 c8 = Column.from_vector "c8" [3^500]
41- c8.is_a Column . should_be_true
42- c8.is_a Integer . should_be_true
45+ c8.should_be_a Column
46+ (c8: Integer).should_equal (3^500)
4347
4448 c9 = Column.from_vector "c9" [Decimal.new "3.5"]
45- c9.is_a Column . should_be_true
46- c9.is_a Decimal . should_be_true
49+ c9.should_be_a Column
50+ (c9:Decimal).should_equal ( Decimal.new "3.5")
4751
4852 # Integral float is both an integer and a float
4953 c10 = Column.from_vector "c10" [1.0]
50- c10.is_a Column . should_be_true
51- c10.is_a Float . should_be_true
52- c10.is_a Integer . should_be_true
54+ c10.should_be_a Column
55+ (c10:Float).should_equal 1.0
56+ (c10:Integer).should_equal 1
57+ Test.expect_panic Type_Error (c10:Text)
5358
5459 group_builder.specify "does not apply to columns that have more rows" <|
5560 c1 = Column.from_vector "c1" [1, 2]
56- c1.is_a Column . should_be_true
57- c1.is_a Integer . should_be_false
61+ c1.should_be_a Column
62+ Test.expect_panic Type_Error (c1:Integer)
5863
5964 group_builder.specify "nothing column is not a single Nothing value" <|
6065 c = Column.from_vector "n" [Nothing]
61- c.is_a Column . should_be_true
62- c.is_a Integer . should_be_false
66+ c.should_be_a Column
6367 c.is_a Nothing . should_be_false
68+ Test.expect_panic Type_Error (c:Nothing)
69+ Test.expect_panic Type_Error (c:Integer)
70+ Test.expect_panic Type_Error (c:Text)
6471
6572 group_builder.specify "should still allow all column operations and preserve its single-value-ness" <|
6673 c1 = Column.from_vector "c1" [23]
@@ -70,42 +77,51 @@ add_specs suite_builder =
7077 c1.at 0 . should_equal 23
7178
7279 c2 = c1 + 100
73- c2.is_a Column . should_be_true
74- c2.is_a Integer . should_be_true
80+ c2.should_be_a Column
81+ (c2: Integer).should_equal 123
7582
76- group_builder.specify "will also allow to call methods of the single value" <|
83+ group_builder.specify "will also allow to call methods of the single value (if casted) " <|
7784 c1 = Column.from_vector "c1" [1000]
7885 # Assuming Column has no log function
79- r1 = c1.log 10
86+ Test.expect_panic No_Such_Method (c1.log 10)
87+ r1 = (c1:Number).log 10
8088 r1 . should_equal 3 epsilon=0.001
8189
8290 c2 = Column.from_vector "c2" [Date.new 2025 1 1]
83- c2.add_work_days 100 . should_equal (Date.new 2025 5 21)
91+ Test.expect_panic No_Such_Method (c2.add_work_days 100)
92+ (c2:Date).add_work_days 100 . should_equal (Date.new 2025 5 21)
8493
8594 my_integer_fn (x : Integer) -> Integer =
8695 1 + x*2
8796 my_text_fn (x : Text) -> Text =
8897 x + "!"
89- group_builder.specify "can be passed to functions that expect a matching single value" <|
98+ group_builder.specify "can be passed to functions that expect a matching single value (if explicitly casted) " <|
9099 c1 = Column.from_vector "c1" [10]
91- r1 = my_integer_fn c1
100+ # Without cast it is a panic
101+ Test.expect_panic Type_Error (my_integer_fn c1)
102+ # But passes with cast
103+ r1 = my_integer_fn (c1:Integer)
92104 r1 . should_equal 21
93- r1 . is_a Integer . should_be_true
105+ r1.should_be_a Integer
94106 # The value is no longer a column after passing it to a function
95107 r1 . is_a Column . should_be_false
96108
97109 c2 = Column.from_vector "c2" ["hello"]
98- r2 = my_text_fn c2
110+ Test.expect_panic Type_Error (my_text_fn c2)
111+ r2 = my_text_fn c2:Text
99112 r2 . should_equal "hello!"
100113
101114 group_builder.specify "should also work for columns extracted from a Table" <|
102- table = Table.new [["a", [1 ]], ["b", ["hello"]]]
115+ table = Table.new [["a", [42 ]], ["b", ["hello"]]]
103116 c1 = table.at "a"
104- c1.is_a Column . should_be_true
105- c1.is_a Integer . should_be_true
106- my_integer_fn c1 . should_equal 3
107-
108- my_text_fn (table.at "b") . should_equal "hello!"
117+ c1.should_be_a Column
118+ (c1:Integer).should_equal 42
119+ Test.expect_panic Type_Error (my_integer_fn c1)
120+ my_integer_fn c1:Integer . should_equal 85
121+
122+ Test.expect_panic No_Such_Method <|
123+ my_text_fn (table.at "b" : Text)
124+ my_text_fn (table.at "b" : Text) . should_equal "hello!"
109125
110126
111127main filter=Nothing =
0 commit comments