1
1
from Standard.Base import all
2
+ import Standard.Base.Errors.Common.No_Such_Method
3
+ import Standard.Base.Errors.Common.Type_Error
2
4
3
5
from Standard.Table import Column, Table, Value_Type
4
6
@@ -8,59 +10,64 @@ add_specs suite_builder =
8
10
suite_builder.group "single-value Column" group_builder->
9
11
group_builder.specify "should be a Column but also the underlying value" <|
10
12
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)
14
16
15
17
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)
18
22
19
23
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)
23
27
24
28
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
27
31
28
32
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)
31
35
32
36
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)
35
39
36
40
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)
39
43
40
44
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)
43
47
44
48
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")
47
51
48
52
# Integral float is both an integer and a float
49
53
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)
53
58
54
59
group_builder.specify "does not apply to columns that have more rows" <|
55
60
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)
58
63
59
64
group_builder.specify "nothing column is not a single Nothing value" <|
60
65
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
63
67
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)
64
71
65
72
group_builder.specify "should still allow all column operations and preserve its single-value-ness" <|
66
73
c1 = Column.from_vector "c1" [23]
@@ -70,42 +77,51 @@ add_specs suite_builder =
70
77
c1.at 0 . should_equal 23
71
78
72
79
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
75
82
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) " <|
77
84
c1 = Column.from_vector "c1" [1000]
78
85
# 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
80
88
r1 . should_equal 3 epsilon=0.001
81
89
82
90
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)
84
93
85
94
my_integer_fn (x : Integer) -> Integer =
86
95
1 + x*2
87
96
my_text_fn (x : Text) -> Text =
88
97
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) " <|
90
99
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)
92
104
r1 . should_equal 21
93
- r1 . is_a Integer . should_be_true
105
+ r1.should_be_a Integer
94
106
# The value is no longer a column after passing it to a function
95
107
r1 . is_a Column . should_be_false
96
108
97
109
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
99
112
r2 . should_equal "hello!"
100
113
101
114
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"]]]
103
116
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!"
109
125
110
126
111
127
main filter=Nothing =
0 commit comments