Skip to content

Commit

Permalink
Code Review Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AdRiley committed Apr 26, 2024
1 parent ef40f24 commit 6eafb5f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 50 deletions.
70 changes: 42 additions & 28 deletions test/Table_Tests/src/Util.enso
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ polyglot java import org.enso.base_test_helpers.FileSystemHelper
Table.should_equal : Any -> Integer -> Any
Table.should_equal self expected frames_to_skip=0 =
loc = Meta.get_source_location 1+frames_to_skip
Panic.catch Any (self.should_equal_impl expected loc) error->
Test.fail error.payload
Panic.catch Test_Failure_Error.Error (table_should_equal_impl self expected loc) error->
Test.fail error.payload.message

Column.should_equal : Any -> Integer -> Any
Column.should_equal self expected frames_to_skip=0 =
loc = Meta.get_source_location 1+frames_to_skip
Panic.catch Any (self.should_equal_impl expected loc) error->
Test.fail error.payload
Panic.catch Test_Failure_Error.Error (column_should_equal_impl self expected loc) error->
Test.fail error.payload.message

DB_Table.should_equal : DB_Table -> Integer -> Any
DB_Table.should_equal self expected frames_to_skip=0 =
Expand All @@ -32,40 +32,54 @@ DB_Column.should_equal self expected frames_to_skip=0 =
t1 = expected.read
t0 . should_equal t1 frames_to_skip

type Test_Failure_Error
## PRIVATE
The runtime representation of a test failure.

Arguments:
- message: A description of the test failure.
Error message

## PRIVATE
to_display_text : Text
to_display_text self = "Test failure error: "+self.message

## PRIVATE
Table.should_equal_impl self expected loc =
table_should_equal_impl actual expected loc =
case expected of
_ : Table ->
if self.columns.length != expected.columns.length then
Panic.throw 'Tables differ at '+loc+'.\nActual:\n'+self.display+'\nExpected:\n'+expected.display+'\nExpected '+expected.columns.length.to_text+" columns, but got "+self.columns.length.to_text+'.'
Panic.catch Any (self.columns.zip expected.columns a-> e->(a.should_equal_impl e)) error->
msg = 'Tables differ at '+loc+'.\nActual:\n'+self.display+'\nExpected:\n'+expected.display+'\n'+error.payload
Panic.throw msg
_ -> Panic.throw "Got a Table, but expected a "+expected.to_display_text+(display_loc loc)+'.'
if actual.columns.length != expected.columns.length then
Panic.throw (Test_Failure_Error.Error 'Tables differ at '+loc+'.\nActual:\n'+actual.display+'\nExpected:\n'+expected.display+'\nExpected '+expected.columns.length.to_text+" columns, but got "+actual.columns.length.to_text+'.')
Panic.catch Test_Failure_Error.Error (actual.columns.zip expected.columns a-> e->(column_should_equal_impl a e)) error->
msg = 'Tables differ at '+loc+'.\nActual:\n'+actual.display+'\nExpected:\n'+expected.display+'\n'+error.payload.message
Panic.throw (Test_Failure_Error.Error msg)
_ -> Panic.throw (Test_Failure_Error.Error "Got a Table, but expected a "+expected.to_display_text+(display_loc loc)+'.')

## PRIVATE
Column.should_equal_impl self expected loc='' =
column_should_equal_impl actual expected loc='' =
case expected of
_ : Column ->
if self.name != expected.name then
Panic.throw "Expected column name "+expected.name+", but got "+self.name+(display_loc loc)+'.'
if self.length != expected.length then
Panic.throw "Expected column length "+expected.length.to_text+", but got "+self.length.to_text+(display_loc loc)+'.'
if self.value_type != expected.value_type then
Panic.throw "Expected column type "+expected.value_type.to_text+", but got "+self.value_type.to_text+(display_loc loc)+'.'
self.zip expected a-> e->
if a != e then
if (a.is_a Number && e.is_a Number && a.is_nan && e.is_nan).not then
self.report_fail expected loc
_ -> Panic.throw "Got a Column, but expected a "+expected.to_display_text+(display_loc loc)+'.'
if actual.name != expected.name then
Panic.throw (Test_Failure_Error.Error "Expected column name "+expected.name+", but got "+actual.name+(display_loc loc)+'.')
if actual.length != expected.length then
Panic.throw (Test_Failure_Error.Error "Expected column length "+expected.length.to_text+", but got "+actual.length.to_text+(display_loc loc)+'.')
if actual.value_type != expected.value_type then
Panic.throw (Test_Failure_Error.Error "Expected column type "+expected.value_type.to_text+", but got "+actual.value_type.to_text+(display_loc loc)+'.')
actual.zip expected a-> e->
if values_equal a e then
report_fail actual expected loc
_ -> Panic.throw (Test_Failure_Error.Error "Got a Column, but expected a "+expected.to_display_text+(display_loc loc)+'.')

## PRIVATE
values_equal a e =
a != e && (a.is_a Number && e.is_a Number && a.is_nan && e.is_nan).not

## PRIVATE
Column.report_fail self expected loc =
indexed = self.zip (0.up_to self.length) a-> i-> Pair.new a i
report_fail actual expected loc =
indexed = actual.zip (0.up_to actual.length) a-> i-> Pair.new a i
indexed.zip expected a-> e->
if a.first != e then
if (a.first.is_a Number && e.is_a Number && a.first.is_nan && e.is_nan).not then
Panic.throw "Column: "+self.name+" differs at row "+a.second.to_text+'.\n\t Actual : '+a.first.to_text+'\n\t Expected: '+e.to_text+'\n\t'+(display_loc loc)+'.'
if values_equal a.first e then
Panic.throw (Test_Failure_Error.Error "Column: "+actual.name+" differs at row "+a.second.to_text+'.\n\t Actual : '+a.first.to_text+'\n\t Expected: '+e.to_text+'\n\t'+(display_loc loc)+'.')

## PRIVATE
display_loc loc:Text =
Expand Down
44 changes: 22 additions & 22 deletions test/Table_Tests/src/Util_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ add_specs suite_builder =
group_builder.specify "Two Columns With Different Name are Not Equal" <|
expected_column = Column.from_vector "Col1" ["Quis", "custodiet", "ipsos", "custodes?"]
actual_column = Column.from_vector "Col2" ["Quis", "custodiet", "ipsos", "custodes?"]
res = Panic.recover Any (actual_column.should_equal_impl expected_column "LOCATION_PATH")
res.catch.should_equal "Expected column name Col1, but got Col2 (at LOCATION_PATH)."
res = Panic.recover Test_Failure_Error (column_should_equal_impl actual_column expected_column "LOCATION_PATH")
res.catch.message.should_equal "Expected column name Col1, but got Col2 (at LOCATION_PATH)."
group_builder.specify "Two Columns With Different Lengths are Not Equal" <|
expected_column = Column.from_vector "Col" ["Quis", "custodiet", "ipsos", "custodes?"]
actual_column = Column.from_vector "Col" ["Quis", "custodiet", "ipsos"]
res = Panic.recover Any (actual_column.should_equal_impl expected_column "LOCATION_PATH")
res.catch.should_equal "Expected column length 4, but got 3 (at LOCATION_PATH)."
res = Panic.recover Test_Failure_Error (column_should_equal_impl actual_column expected_column "LOCATION_PATH")
res.catch.message.should_equal "Expected column length 4, but got 3 (at LOCATION_PATH)."
group_builder.specify "Two Columns with different content Are Not Equal" <|
expected_column = Column.from_vector "Col" ["Quis", "custodiet", "ipsos", "custodes?"]
actual_column = Column.from_vector "Col" ["Who", "guards", "the", "guards?"]
res = Panic.recover Any (actual_column.should_equal_impl expected_column "LOCATION_PATH")
res.catch.should_equal 'Column: Col differs at row 0.\n\t Actual : Who\n\t Expected: Quis\n\t (at LOCATION_PATH).'
res = Panic.recover Test_Failure_Error (column_should_equal_impl actual_column expected_column "LOCATION_PATH")
res.catch.message.should_equal 'Column: Col differs at row 0.\n\t Actual : Who\n\t Expected: Quis\n\t (at LOCATION_PATH).'
group_builder.specify "Two Columns Are Not Equal in Row 3" <|
expected_column = Column.from_vector "My Column" ["Quis", "custodiet", "ipsos", "custodes?"]
actual_column = Column.from_vector "My Column" ["Quis", "custodiet", "ipsos", "guards?"]
res = Panic.recover Any (actual_column.should_equal_impl expected_column "LOCATION_PATH")
res.catch.should_equal 'Column: My Column differs at row 3.\n\t Actual : guards?\n\t Expected: custodes?\n\t (at LOCATION_PATH).'
res = Panic.recover Test_Failure_Error (column_should_equal_impl actual_column expected_column "LOCATION_PATH")
res.catch.message.should_equal 'Column: My Column differs at row 3.\n\t Actual : guards?\n\t Expected: custodes?\n\t (at LOCATION_PATH).'
group_builder.specify "Two Columns with different types Are Not Equal" <|
expected_column = Column.from_vector "Col" ["1", "2", "3", "4"]
actual_column = Column.from_vector "Col" [1, 2, 3, 4]
res = Panic.recover Any (actual_column.should_equal_impl expected_column "LOCATION_PATH")
res.catch.should_equal "Expected column type (Char Nothing True), but got (Integer 64 bits) (at LOCATION_PATH)."
res = Panic.recover Test_Failure_Error (column_should_equal_impl actual_column expected_column "LOCATION_PATH")
res.catch.message.should_equal "Expected column type (Char Nothing True), but got (Integer 64 bits) (at LOCATION_PATH)."
group_builder.specify "Comparing a Column to non column" <|
expected_column = 42
actual_column = Column.from_vector "Col" [1, 2, 3, 4]
res = Panic.recover Any (actual_column.should_equal_impl expected_column "LOCATION_PATH")
res.catch.should_equal "Got a Column, but expected a 42 (at LOCATION_PATH)."
res = Panic.recover Test_Failure_Error (column_should_equal_impl actual_column expected_column "LOCATION_PATH")
res.catch.message.should_equal "Got a Column, but expected a 42 (at LOCATION_PATH)."
group_builder.specify "Two Columns Containg NaNs Are Equal" <|
# This is somewhat of a special case, as NaN != NaN but for the purposes of testing we consider them equal
expected_column = Column.from_vector "Col" [1.0, 2.0, Number.nan]
Expand All @@ -52,28 +52,28 @@ add_specs suite_builder =
group_builder.specify "Two Tables With Different Values" <|
expected_table = Table.new [Column.from_vector "Col1" ["Quis", "custodiet", "ipsos", "custodes?"], Column.from_vector "Col2" ["Who", "guards", "the", "guards?"]]
actual_table = Table.new [Column.from_vector "Col1" ["Quis", "custodiet", "ipsos", "custodes?"], Column.from_vector "Col2" ["Who", "guards", "teh", "guards?"]]
res = Panic.recover Any (actual_table.should_equal_impl expected_table "LOCATION_PATH")
res.catch.should_end_with 'Column: Col2 differs at row 2.\n\t Actual : teh\n\t Expected: the\n\t.'
res = Panic.recover Test_Failure_Error (table_should_equal_impl actual_table expected_table "LOCATION_PATH")
res.catch.message.should_end_with 'Column: Col2 differs at row 2.\n\t Actual : teh\n\t Expected: the\n\t.'
group_builder.specify "Tables different number of columns" <|
expected_table = Table.new [Column.from_vector "Col1" ["Quis", "custodiet", "ipsos", "custodes?"]]
actual_table = Table.new [Column.from_vector "Col1" ["Quis", "custodiet", "ipsos", "custodes?"], Column.from_vector "Col2" ["Who", "guards", "the", "guards?"]]
res = Panic.recover Any (actual_table.should_equal_impl expected_table "LOCATION_PATH")
res.catch.should_end_with "Expected 1 columns, but got 2."
res = Panic.recover Test_Failure_Error (table_should_equal_impl actual_table expected_table "LOCATION_PATH")
res.catch.message.should_end_with "Expected 1 columns, but got 2."
group_builder.specify "Tables different number of columns2" <|
expected_table = Table.new [Column.from_vector "Col1" ["Quis", "custodiet", "ipsos", "custodes?"], Column.from_vector "Col2" ["Who", "guards", "the", "guards?"]]
actual_table = Table.new [Column.from_vector "Col1" ["Quis", "custodiet", "ipsos", "custodes?"]]
res = Panic.recover Any (actual_table.should_equal_impl expected_table "LOCATION_PATH")
res.catch.should_end_with "Expected 2 columns, but got 1."
res = Panic.recover Test_Failure_Error (table_should_equal_impl actual_table expected_table "LOCATION_PATH")
res.catch.message.should_end_with "Expected 2 columns, but got 1."
group_builder.specify "Tables With Mismatched Column names" <|
expected_table = Table.new [Column.from_vector "Col1" ["Quis", "custodiet", "ipsos", "custodes?"], Column.from_vector "Col2" ["Who", "guards", "the", "guards?"]]
actual_table = Table.new [Column.from_vector "Col" ["Quis", "custodiet", "ipsos", "custodes?"], Column.from_vector "Col2" ["Who", "guards", "the", "guards?"]]
res = Panic.recover Any (actual_table.should_equal_impl expected_table "LOCATION_PATH")
res.catch.should_end_with "Expected column name Col1, but got Col."
res = Panic.recover Test_Failure_Error (table_should_equal_impl actual_table expected_table "LOCATION_PATH")
res.catch.message.should_end_with "Expected column name Col1, but got Col."
group_builder.specify "Comparing a Table to non Table" <|
expected_table = 42
actual_table = Table.new [Column.from_vector "Col1" ["Quis", "custodiet", "ipsos", "custodes?"]]
res = Panic.recover Any (actual_table.should_equal_impl expected_table "LOCATION_PATH")
res.catch.should_equal "Got a Table, but expected a 42 (at LOCATION_PATH)."
res = Panic.recover Test_Failure_Error (table_should_equal_impl actual_table expected_table "LOCATION_PATH")
res.catch.message.should_equal "Got a Table, but expected a 42 (at LOCATION_PATH)."

main filter=Nothing =
suite = Test.build suite_builder->
Expand Down

0 comments on commit 6eafb5f

Please sign in to comment.