Implement should_succeed (#3586)

Implements https://www.pivotaltracker.com/story/show/182709976
This commit is contained in:
Radosław Waśko 2022-07-14 21:58:44 +02:00 committed by GitHub
parent 35ddd2a89e
commit fc110659db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 147 additions and 75 deletions

View File

@ -358,6 +358,52 @@ Decimal.should_equal that epsilon=0 frames_to_skip=0 =
msg = self.to_text + " did not equal " + that.to_text + " (at " + loc + ")."
Panic.throw (Failure msg)
## Asserts that `self` value is not an error.
It returns the original value, so that it can be inspected further.
Arguments:
- frames_to_skip (optional, advanced): used to alter the location which is
displayed as the source of this error.
> Example
Assert that a given action did not result in errors or warnings.
"foobar".write (enso_project.data / "f.txt") . should_succeed
Any.should_succeed : Boolean -> Integer -> Any
Any.should_succeed frames_to_skip=0 =
_ = frames_to_skip
self
## Asserts that `self` value is not an error.
It returns the original value, so that it can be inspected further.
Arguments:
- frames_to_skip (optional, advanced): used to alter the location which is
displayed as the source of this error.
> Example
Assert that a given action did not result in errors or warnings.
"foobar".write (enso_project.data / "f.txt") . should_succeed
Error.should_succeed : Boolean -> Integer -> Any
Error.should_succeed frames_to_skip=0 =
fail_match_on_unexpected_error self 1+frames_to_skip
## Checks that the provided action returns without any errors or warnings.
If you just want to check for errors, usage of the `.should_succeed`
extension function is preferred.
assert_no_problems value frames_to_skip=0 =
value.catch Any _->
fail_match_on_unexpected_error value 2+frames_to_skip
warnings = Warning.get_all value . map .value
if warnings.not_empty then
loc = Meta.get_source_location 2+frames_to_skip
msg = "The action returned unexpected warnings: " + warnings.to_text + " (at " + loc + ")."
fail msg
## Asserts that the given `Boolean` is `True`
> Example

View File

@ -1,5 +1,5 @@
from Standard.Base import all
import Standard.Base.Error.Problem_Behavior
from Standard.Base.Error.Problem_Behavior import all
import Standard.Base.System.File.Existing_File_Behavior
from Standard.Base.Data.Text.Encoding as Encoding_Module import Encoding, Encoding_Error
import Standard.Base.Data.Time.Date
@ -37,7 +37,7 @@ spec =
table = Table.new [["A", [1,2,3]], ["B", [1.0,1.5,2.2]], ["C", ["x","y","z"]], ["D", ["a", 2, My_Type 10]]]
file = (enso_project.data / "transient" / "written.csv")
file.delete_if_exists
table.write file
table.write file on_problems=Report_Error . should_succeed
expected_text = normalize_lines <| """
A,B,C,D
1,1.0,x,a
@ -54,7 +54,7 @@ spec =
style=setting.first
separator=setting.second
file = (enso_project.data / "transient" / "endings.csv")
table.write file (File_Format.Delimited ',' line_endings=style)
table.write file (File_Format.Delimited ',' line_endings=style) on_problems=Report_Error . should_succeed
text = File.read_text file
text.should_equal (lines.join separator suffix=separator)
file.delete
@ -63,7 +63,7 @@ spec =
table = Table.new []
file = (enso_project.data / "transient" / "empty.csv")
file.delete_if_exists
table.write file
table.write file on_problems=Report_Error . should_succeed
text = File.read_text file
text.should_equal ''
file.delete
@ -73,7 +73,7 @@ spec =
table = Table.new [['The Column "Name"', ["foo","'bar'",'"baz"', 'one, two, three']], ["Hello, Column?", [1.0, 1000000.5, 2.2, -1.5]]]
file = (enso_project.data / "transient" / "quotes1.csv")
file.delete_if_exists
table.write file (File_Format.Delimited "," value_formatter=data_formatter)
table.write file (File_Format.Delimited "," value_formatter=data_formatter) on_problems=Report_Error . should_succeed
expected_text = normalize_lines <| """
"The Column ""Name""","Hello, Column?"
foo,"1,0"
@ -89,7 +89,7 @@ spec =
table = Table.new [['"A"', ["foo",'!"baz" ', 'one, two, three', "a;b; c ", "a\b"]], ["B", [1000000.5, 1000.0, 0.0, -1.2, Nothing]]]
file = (enso_project.data / "transient" / "quotes2.csv")
file.delete_if_exists
table.write file (File_Format.Delimited ";" value_formatter=data_formatter . with_quotes quote='"' quote_escape='\\')
table.write file (File_Format.Delimited ";" value_formatter=data_formatter . with_quotes quote='"' quote_escape='\\') on_problems=Report_Error . should_succeed
expected_text = normalize_lines <| """
"\"A\"";B
foo;1'000'000.5
@ -106,7 +106,7 @@ spec =
table = Table.new [['"A"', [Nothing,"The 'thing'.", 'one, "two", three', 'a\tb']], ["B\C", [1000000.5, 1000.0, Nothing, -1.2]]]
file = (enso_project.data / "transient" / "quotes3.csv")
file.delete_if_exists
table.write file (File_Format.Delimited '\t' value_formatter=data_formatter . with_quotes quote='\'' quote_escape='\'')
table.write file (File_Format.Delimited '\t' value_formatter=data_formatter . with_quotes quote='\'' quote_escape='\'') on_problems=Report_Error . should_succeed
expected_text = normalize_lines <| '''
"A"\tB\\C
\t'1''000''000.5'
@ -121,7 +121,7 @@ spec =
table = Table.new [["A", [1,Nothing,3]], ["B", [Nothing,"","abc"]]]
file = (enso_project.data / "transient" / "empty_vs_null.csv")
file.delete_if_exists
table.write file
table.write file on_problems=Report_Error . should_succeed
expected_text = normalize_lines <| """
A,B
1,
@ -136,7 +136,7 @@ spec =
table = Table.new [['The Column "Name"', ["foo","'bar'",'"baz"', 'one, two, three']], ["Hello, Column?", [1.0, 1000000.5, 2.2, -1.5]]]
file = (enso_project.data / "transient" / "quote_disabled.csv")
file.delete_if_exists
table.write file format
table.write file format on_problems=Report_Error . should_succeed
expected_text = normalize_lines <| """
The Column "Name",Hello, Column?
foo,1,0
@ -152,7 +152,7 @@ spec =
table = Table.new [['The Column "Name"', ["foo","'bar'",'"baz"', 'one, two, three']], ["B", [1.0, 1000000.5, 2.2, -1.5]], ["C", ["foo", My_Type 44, (Date.new 2022 06 21 . internal_local_date), 42]], ["D", [1,2,3,4000]], ["E", [Nothing, (Time_Of_Day.new 13 55 . internal_local_time), Nothing, Nothing]]]
file = (enso_project.data / "transient" / "quote_always.csv")
file.delete_if_exists
table.write file format
table.write file format on_problems=Report_Error . should_succeed
expected_text = normalize_lines <| """
"The Column \"Name\"","B","C","D","E"
"foo",1.0,"foo",1,
@ -167,7 +167,7 @@ spec =
table = Table.new [["ąęćś", [0]], ["ß", ["żółw 🐢"]]]
file = (enso_project.data / "transient" / "utf16.csv")
file.delete_if_exists
table.write file (File_Format.Delimited "," encoding=Encoding.utf_16_be)
table.write file (File_Format.Delimited "," encoding=Encoding.utf_16_be) on_problems=Report_Error . should_succeed
expected_text = normalize_lines <| """
ąęćś,ß
0,żółw 🐢
@ -199,7 +199,7 @@ spec =
table_1 = Table.new [["A", ["x", "y"]], ["B", ["z", "w"]]]
file_1 = (enso_project.data / "transient" / "textonly.csv")
file_1.delete_if_exists
result_1 = table_1.write file_1 format
result_1 = table_1.write file_1 format on_problems=Report_Error . should_succeed
expected_text = normalize_lines <| """
A,B
x,z
@ -223,7 +223,7 @@ spec =
table = Table.new [["A", [1,2,3]], ["B", [1.0,1.5,2.2]], ["C", ["x","y","z"]]]
file = (enso_project.data / "transient" / "append_nonexistent.csv")
file.delete_if_exists
table.write file on_existing_file=Existing_File_Behavior.Append
table.write file on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
got_table = file.read
got_table.should_equal table
file.delete
@ -233,7 +233,7 @@ spec =
file = (enso_project.data / "transient" / "append_empty.csv")
file.delete_if_exists
"".write file
table.write file on_existing_file=Existing_File_Behavior.Append
table.write file on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
got_table = file.read
got_table.should_equal table
file.delete
@ -243,8 +243,8 @@ spec =
appending_table = Table.new [["B", [33,44]], ["A", [Nothing, 0]], ["C", ["a","BB"]]]
file = (enso_project.data / "transient" / "append_by_name.csv")
file.delete_if_exists
existing_table.write file on_existing_file=Existing_File_Behavior.Overwrite
appending_table.write file on_existing_file=Existing_File_Behavior.Append
existing_table.write file on_existing_file=Existing_File_Behavior.Overwrite on_problems=Report_Error . should_succeed
appending_table.write file on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
got_table = file.read
expected_table = Table.new [["A", [1,2,Nothing,0]], ["B", [1.0,1.5,33,44]], ["C", ["x","y","a","BB"]]]
got_table.should_equal expected_table
@ -255,9 +255,9 @@ spec =
appending_table = Table.new [["B1", [33,44]], ["0", [Nothing, 0]], ["C", ["a","BB"]]]
file = (enso_project.data / "transient" / "append_by_name_2.csv")
file.delete_if_exists
existing_table.write file on_existing_file=Existing_File_Behavior.Overwrite
existing_table.write file on_existing_file=Existing_File_Behavior.Overwrite on_problems=Report_Error . should_succeed
format = File_Format.Delimited "," . with_headers
appending_table.write file format on_existing_file=Existing_File_Behavior.Append
appending_table.write file format on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
got_table = file.read format
expected_table = Table.new [["0", [1,2,Nothing,0]], ["B1", [1.0,1.5,33,44]], ["C", ["x","y","a","BB"]]]
got_table.should_equal expected_table
@ -303,8 +303,8 @@ spec =
test_append initial_file_format append_format expected_table =
file = (enso_project.data / "transient" / "append_by_position.csv")
file.delete_if_exists
existing_table.write file initial_file_format on_existing_file=Existing_File_Behavior.Overwrite
appending_table.write file append_format match_columns=Match_Columns.By_Position on_existing_file=Existing_File_Behavior.Append
existing_table.write file initial_file_format on_existing_file=Existing_File_Behavior.Overwrite on_problems=Report_Error . should_succeed
appending_table.write file append_format match_columns=Match_Columns.By_Position on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
read_format = initial_file_format
got_table = file.read read_format
got_table.should_equal expected_table
@ -352,8 +352,8 @@ spec =
style=setting.first
separator=setting.second
file = (enso_project.data / "transient" / "endings.csv")
initial_table.write file (File_Format.Delimited ',' line_endings=style)
table_to_append.write file on_existing_file=Existing_File_Behavior.Append . should_equal Nothing
initial_table.write file (File_Format.Delimited ',' line_endings=style) on_problems=Report_Error . should_succeed
table_to_append.write file on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
text = File.read_text file
text.should_equal (expected_lines.join separator suffix=separator)
file.delete
@ -365,8 +365,8 @@ spec =
nonexistent_file.delete_if_exists
table_to_append = Table.new [["a", ["x", "y"]], ["d", ["z", "w"]]]
table_to_append.write nonexistent_file on_existing_file=Existing_File_Behavior.Append
table_to_append.write empty_file on_existing_file=Existing_File_Behavior.Append
table_to_append.write nonexistent_file on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
table_to_append.write empty_file on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
expected_lines = ["a,d", "x,z", "y,w"]
expected_text = (expected_lines.join system_separator suffix=system_separator)
@ -383,7 +383,7 @@ spec =
file.delete_if_exists
(initial_lines.join separator suffix=separator).write file
format = File_Format.Delimited ',' . with_comments
table_to_append.write file format on_existing_file=Existing_File_Behavior.Append . should_equal Nothing
table_to_append.write file format on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
text = File.read_text file
expected_text = expected_lines.join separator suffix=separator
text.should_equal expected_text

View File

@ -2,6 +2,7 @@ from Standard.Base import Nothing, File, Illegal_Argument_Error, True, False
import Standard.Base.System.File.Existing_File_Behavior
from Standard.Base.System.File import File_Already_Exists_Error
import Standard.Base.Data.Time.Date
from Standard.Base.Error.Problem_Behavior import all
import Standard.Table
import Standard.Table.Io.File_Read
@ -84,14 +85,14 @@ spec_write suffix test_sheet_name =
Test.specify 'should write a table to non-existent file as a new sheet with headers' <|
out.delete_if_exists
table.write out
table.write out on_problems=Report_Error . should_succeed
written = out.read
written.should_equal table
out.delete_if_exists
Test.specify 'should write a table to non-existent file in append mode as a new sheet with headers' <|
out.delete_if_exists
table.write out on_existing_file=Existing_File_Behavior.Append
table.write out on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
written = out.read
written.should_equal table
out.delete_if_exists
@ -99,7 +100,7 @@ spec_write suffix test_sheet_name =
Test.specify 'should write a table to existing file in overwrite mode as a new sheet with headers' <|
out.delete_if_exists
(enso_project.data / test_sheet_name) . copy_to out
table.write out (File_Format.Excel (Sheet "Another")) on_existing_file=Existing_File_Behavior.Overwrite
table.write out (File_Format.Excel (Sheet "Another")) on_existing_file=Existing_File_Behavior.Overwrite on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Sheet "Another"))
written.should_equal table
out.delete_if_exists
@ -107,15 +108,15 @@ spec_write suffix test_sheet_name =
Test.specify 'should write a table to existing file in overwrite mode as a new sheet without headers' <|
out.delete_if_exists
(enso_project.data / test_sheet_name) . copy_to out
table.write out (File_Format.Excel (Sheet "NoHeaders")) on_existing_file=Existing_File_Behavior.Overwrite
table.write out (File_Format.Excel (Sheet "NoHeaders")) on_existing_file=Existing_File_Behavior.Overwrite on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Sheet "NoHeaders"))
written.should_equal (table.rename_columns (Column_Name_Mapping.By_Position ['A', 'B', 'C', 'D', 'E', 'F']))
out.delete_if_exists
Test.specify 'should create new sheets at the start if index is 0' <|
out.delete_if_exists
table.write out (File_Format.Excel (Sheet 0))
clothes.write out (File_Format.Excel (Sheet 0))
table.write out (File_Format.Excel (Sheet 0)) on_problems=Report_Error . should_succeed
clothes.write out (File_Format.Excel (Sheet 0)) on_problems=Report_Error . should_succeed
read_1 = out.read (File_Format.Excel (Sheet "Sheet1"))
read_1 . should_equal table
read_2 = out.read (File_Format.Excel (Sheet "Sheet2"))
@ -127,7 +128,7 @@ spec_write suffix test_sheet_name =
Test.specify 'should write a table to specific single cell location of an existing sheet' <|
out.delete_if_exists
(enso_project.data / test_sheet_name) . copy_to out
table.write out (File_Format.Excel (Cell_Range "Another!G1"))
table.write out (File_Format.Excel (Cell_Range "Another!G1")) on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Cell_Range "Another!G1"))
written.should_equal table
out.delete_if_exists
@ -135,7 +136,7 @@ spec_write suffix test_sheet_name =
Test.specify 'should clear out an existing fixed range and replace' <|
out.delete_if_exists
(enso_project.data / test_sheet_name) . copy_to out
sub_clothes.write out (File_Format.Excel (Cell_Range "Another!A1:D20"))
sub_clothes.write out (File_Format.Excel (Cell_Range "Another!A1:D20")) on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Cell_Range "Another!A1"))
written.should_equal sub_clothes
out.delete_if_exists
@ -143,7 +144,7 @@ spec_write suffix test_sheet_name =
Test.specify 'should clear out an existing range and replace' <|
out.delete_if_exists
(enso_project.data / test_sheet_name) . copy_to out
sub_clothes.write out (File_Format.Excel (Cell_Range "Another!A1"))
sub_clothes.write out (File_Format.Excel (Cell_Range "Another!A1")) on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Cell_Range "Another!A1"))
written.should_equal sub_clothes
out.delete_if_exists
@ -183,7 +184,7 @@ spec_write suffix test_sheet_name =
Test.specify 'should write a table to non-existent file as a new sheet without headers' <|
out.delete_if_exists
table.write out (File_Format.Excel (Sheet "Sheet1") headers=False)
table.write out (File_Format.Excel (Sheet "Sheet1") headers=False) on_problems=Report_Error . should_succeed
written = out.read
written.should_equal (table.rename_columns (Column_Name_Mapping.By_Position ['A', 'B', 'C', 'D', 'E', 'F']))
out.delete_if_exists
@ -193,7 +194,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['AA', ['d', 'e']], ['BB',[4, 5]], ['CC',[True, False]], ['DD', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['a','b','c','d', 'e']], ['BB',[1,2,3,4,5]], ['CC',[True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Sheet "Another")) on_existing_file=Existing_File_Behavior.Append
extra_another.write out (File_Format.Excel (Sheet "Another")) on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Sheet "Another")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists
@ -203,7 +204,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['A', ['d', 'e']], ['B',[4, 5]], ['C',[True, False]], ['D', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['a','b','c','d', 'e']], ['BB',[1,2,3,4,5]], ['CC',[True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Sheet "Another")) on_existing_file=Existing_File_Behavior.Append match_columns=Match_Columns.By_Position
extra_another.write out (File_Format.Excel (Sheet "Another")) on_existing_file=Existing_File_Behavior.Append match_columns=Match_Columns.By_Position on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Sheet "Another")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists
@ -213,7 +214,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['CC',[True, False]], ['BB',[4, 5]], ['AA', ['d', 'e']], ['DD', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['a','b','c','d', 'e']], ['BB',[1,2,3,4,5]], ['CC',[True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Sheet "Another")) on_existing_file=Existing_File_Behavior.Append
extra_another.write out (File_Format.Excel (Sheet "Another")) on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Sheet "Another")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists
@ -223,7 +224,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['AA', ['d', 'e']], ['BB',[4, 5]], ['CC',[True, False]], ['DD', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['a','b','c','d', 'e']], ['BB',[1,2,3,4,5]], ['CC',[True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1")) on_existing_file=Existing_File_Behavior.Append
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1")) on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Sheet "Another")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists
@ -233,7 +234,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['A', ['d', 'e']], ['B',[4, 5]], ['C',[True, False]], ['D', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['a','b','c','d', 'e']], ['BB',[1,2,3,4,5]], ['CC',[True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1")) on_existing_file=Existing_File_Behavior.Append match_columns=Match_Columns.By_Position
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1")) on_existing_file=Existing_File_Behavior.Append match_columns=Match_Columns.By_Position on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Sheet "Another")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists
@ -243,7 +244,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['CC',[True, False]], ['BB',[4, 5]], ['AA', ['d', 'e']], ['DD', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['a','b','c','d', 'e']], ['BB',[1,2,3,4,5]], ['CC',[True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1")) on_existing_file=Existing_File_Behavior.Append
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1")) on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Sheet "Another")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists
@ -253,7 +254,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['AA', ['d', 'e']], ['BB', [4, 5]], ['CC', [True, False]], ['DD', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['a', 'b', 'c', 'd', 'e']], ['BB', [1, 2, 3, 4, 5]], ['CC', [True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1:D6")) on_existing_file=Existing_File_Behavior.Append
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1:D6")) on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Sheet "Another")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists
@ -263,7 +264,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['A', ['d', 'e']], ['B',[4, 5]], ['C',[True, False]], ['D', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['a','b','c','d', 'e']], ['BB',[1,2,3,4,5]], ['CC',[True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1:D6")) on_existing_file=Existing_File_Behavior.Append match_columns=Match_Columns.By_Position
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1:D6")) on_existing_file=Existing_File_Behavior.Append match_columns=Match_Columns.By_Position on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Sheet "Another")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists
@ -273,7 +274,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['AA', ['d', 'e']], ['BB',[4, 5]], ['CC',[True, False]], ['DD', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['f', 'g', 'h', 'd', 'e']], ['BB',[1, 2, 3, 4, 5]], ['CC',[True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Cell_Range "Random!K9")) on_existing_file=Existing_File_Behavior.Append
extra_another.write out (File_Format.Excel (Cell_Range "Random!K9")) on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Cell_Range "Random!K9")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists
@ -283,7 +284,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['AA', ['d', 'e']], ['BB',[4, 5]], ['AA_1',[True, False]], ['BB_1', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['f', 'g', 'h', 'd', 'e']], ['BB',[1, 2, 3, 4, 5]], ['AA_1',[True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Cell_Range "Random!S3")) on_existing_file=Existing_File_Behavior.Append
extra_another.write out (File_Format.Excel (Cell_Range "Random!S3")) on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Cell_Range "Random!S3")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists
@ -293,7 +294,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['A', ['d', 'e']], ['B',[4, 5]], ['C',[True, False]], ['D', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['f', 'g', 'h', 'd', 'e']], ['BB',[1, 2, 3, 4, 5]], ['CC',[True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Cell_Range "Random!K9")) on_existing_file=Existing_File_Behavior.Append match_columns=Match_Columns.By_Position
extra_another.write out (File_Format.Excel (Cell_Range "Random!K9")) on_existing_file=Existing_File_Behavior.Append match_columns=Match_Columns.By_Position on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Cell_Range "Random!K9")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists
@ -303,7 +304,7 @@ spec_write suffix test_sheet_name =
(enso_project.data / test_sheet_name) . copy_to out
extra_another = Table.new [['CC',[True, False]], ['BB',[4, 5]], ['AA', ['d', 'e']], ['DD', ['2022-01-20', '2022-01-21']]]
expected = Table.new [['AA', ['a','b','c','d', 'e']], ['BB',[1,2,3,4,5]], ['CC',[True, False, False, True, False]]]
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1:D6")) on_existing_file=Existing_File_Behavior.Append
extra_another.write out (File_Format.Excel (Cell_Range "Another!A1:D6")) on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
written = out.read (File_Format.Excel (Sheet "Another")) . select_columns (By_Index [0, 1, 2])
written.should_equal expected
out.delete_if_exists

View File

@ -38,10 +38,14 @@ spec =
invalid_ascii_out = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 63]
Test.specify "should dump ASCII bytes to a vector via encoding" <|
test.bytes Encoding.ascii . should_equal test_ascii
dump = test.bytes Encoding.ascii
Test.assert_no_problems dump
dump . should_equal test_ascii
Test.specify "should convert an array of bytes to text" <|
Text.from_bytes test_ascii Encoding.ascii . should_equal test
result = Text.from_bytes test_ascii Encoding.ascii
Test.assert_no_problems result
result . should_equal test
Test.specify "Invalid ASCII should raise a warning when decoding" <|
action = Text.from_bytes invalid_ascii Encoding.ascii on_problems=_
@ -63,16 +67,24 @@ spec =
invalid_utf_8 = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, -62, -94, -62, -93, -62, -91, -62]
Test.specify "should dump utf-8 bytes to a vector via encoding" <|
kshi.bytes Encoding.utf_8 . should_equal kshi_utf_8
dump = kshi.bytes Encoding.utf_8
Test.assert_no_problems dump
dump . should_equal kshi_utf_8
Test.specify "should dump utf-8 bytes to a vector" <|
kshi.utf_8.should_equal kshi_utf_8
dump = kshi.utf_8
Test.assert_no_problems dump
dump.should_equal kshi_utf_8
Test.specify "should convert an array of bytes to text via encoding" <|
Text.from_bytes kshi_utf_8 Encoding.utf_8 . should_equal kshi
result = Text.from_bytes kshi_utf_8 Encoding.utf_8
Test.assert_no_problems result
result . should_equal kshi
Test.specify "should convert an array of bytes to text" <|
Text.from_utf_8 kshi_utf_8 . should_equal kshi
result = Text.from_utf_8 kshi_utf_8
Test.assert_no_problems result
result . should_equal kshi
Test.specify "Invalid UTF-8 should raise a warning when decoding via encoding" <|
action = Text.from_bytes invalid_utf_8 Encoding.utf_8 on_problems=_
@ -91,20 +103,28 @@ spec =
kshi_utf_16 = [9, 21, 9, 77, 9, 55, 9, 63]
Test.specify "should dump utf-16 bytes to a vector via encoding" <|
kshi.bytes Encoding.utf_16_be . should_equal kshi_utf_16
dump = kshi.bytes Encoding.utf_16_be
Test.assert_no_problems dump
dump . should_equal kshi_utf_16
Test.specify "should convert an array of bytes to text via encoding" <|
Text.from_bytes kshi_utf_16 Encoding.utf_16_be . should_equal kshi
result = Text.from_bytes kshi_utf_16 Encoding.utf_16_be
Test.assert_no_problems result
result . should_equal kshi
Test.group "UTF_16 LittleEndian" <|
kshi = '\u0915\u094D\u0937\u093F'
kshi_utf_16 = [21, 9, 77, 9, 55, 9, 63, 9]
Test.specify "should dump utf-16 bytes to a vector via encoding" <|
kshi.bytes Encoding.utf_16_le . should_equal kshi_utf_16
dump = kshi.bytes Encoding.utf_16_le
Test.assert_no_problems dump
dump . should_equal kshi_utf_16
Test.specify "should convert an array of bytes to text via encoding" <|
Text.from_bytes kshi_utf_16 Encoding.utf_16_le . should_equal kshi
result = Text.from_bytes kshi_utf_16 Encoding.utf_16_le
Test.assert_no_problems result
result . should_equal kshi
Test.group "codepoints" <|
facepalm = '\u{1F926}\u{1F3FC}\u200D\u2642\uFE0F'
@ -125,10 +145,14 @@ spec =
invalid_windows_out = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, -94, -93, -91, 63]
Test.specify "should dump Windows-1252 bytes to a vector via encoding" <|
test.bytes Encoding.windows_1252 . should_equal test_windows
dump = test.bytes Encoding.windows_1252
Test.assert_no_problems dump
dump . should_equal test_windows
Test.specify "should convert an array of bytes to text" <|
Text.from_bytes test_windows Encoding.windows_1252 . should_equal test
result = Text.from_bytes test_windows Encoding.windows_1252
Test.assert_no_problems result
result . should_equal test
Test.specify "Invalid Windows-1252 should raise a warning when decoding" <|
action = Text.from_bytes invalid_windows Encoding.windows_1252 on_problems=_

View File

@ -3,6 +3,7 @@ from Standard.Base import all
from Standard.Base.Data.Text.Encoding as Encoding_Module import Encoding, Encoding_Error
import Standard.Base.System.File.Existing_File_Behavior
from Standard.Base.System.File import File_Already_Exists_Error
from Standard.Base.Error.Problem_Behavior import all
import Standard.Test
import Standard.Test.Problems
@ -163,11 +164,11 @@ spec =
f.delete_if_exists
f.exists.should_be_false
data.write_bytes f
data_2.write_bytes f on_existing_file=Existing_File_Behavior.Append
data_2.write_bytes f on_existing_file=Existing_File_Behavior.Append . should_succeed
f.read_bytes.should_equal (data + data_2)
f.delete_if_exists
Test.specify "should fail will Illegal_Argument_Error when trying to write invalid byte vector" <|
Test.specify "should fail with Illegal_Argument_Error when trying to write invalid byte vector" <|
f = transient / "work.txt"
f.delete_if_exists
f.exists.should_be_false
@ -189,7 +190,7 @@ spec =
f.read_bytes.should_equal data
f.delete_if_exists
Test.specify "should allow to writing text to a new file" <|
Test.specify "should allow writing text to a new file" <|
f = transient / "work.txt"
f.delete_if_exists
f.exists.should_be_false
@ -199,11 +200,11 @@ spec =
f.delete
f.exists.should_be_false
Test.specify "should allow to appending text to a file" <|
Test.specify "should allow appending text to a file" <|
f = transient / "work.txt"
f.delete_if_exists
"line 1!".write f on_existing_file=Existing_File_Behavior.Append
'\nline 2!'.write f on_existing_file=Existing_File_Behavior.Append
"line 1!".write f on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
'\nline 2!'.write f on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed
f.read_text.should_equal 'line 1!\nline 2!'
f.delete
f.exists.should_be_false
@ -212,10 +213,10 @@ spec =
f = transient / "work.txt"
f.delete_if_exists
f.exists.should_be_false
"line 1!".write f on_existing_file=Existing_File_Behavior.Overwrite . should_equal Nothing
"line 1!".write f on_existing_file=Existing_File_Behavior.Overwrite on_problems=Report_Error . should_succeed . should_equal Nothing
f.exists.should_be_true
f.read_text.should_equal "line 1!"
"line 2!".write f on_existing_file=Existing_File_Behavior.Overwrite . should_equal Nothing
"line 2!".write f on_existing_file=Existing_File_Behavior.Overwrite on_problems=Report_Error . should_succeed . should_equal Nothing
f.read_text.should_equal "line 2!"
f.delete
f.exists.should_be_false
@ -224,7 +225,7 @@ spec =
f = transient / "work.txt"
f.delete_if_exists
f.exists.should_be_false
"line 1!".write f on_existing_file=Existing_File_Behavior.Error . should_equal Nothing
"line 1!".write f on_existing_file=Existing_File_Behavior.Error on_problems=Report_Error . should_succeed . should_equal Nothing
f.exists.should_be_true
f.read_text.should_equal "line 1!"
"line 2!".write f on_existing_file=Existing_File_Behavior.Error . should_fail_with File_Already_Exists_Error
@ -236,7 +237,7 @@ spec =
f = transient / "work.txt"
f.delete_if_exists
f.exists.should_be_false
"line 1!".write f . should_equal Nothing
"line 1!".write f on_problems=Report_Error . should_succeed . should_equal Nothing
if f.exists.not then
Test.fail "The file should have been created."
f.read_text.should_equal "line 1!"
@ -254,7 +255,7 @@ spec =
"new content".write n on_existing_file=Existing_File_Behavior.Overwrite
n3.delete_if_exists
"line 2!".write f . should_equal Nothing
"line 2!".write f on_problems=Report_Error . should_succeed . should_equal Nothing
f.read_text.should_equal "line 2!"
bak.read_text.should_equal "line 1!"
if n3.exists then

View File

@ -72,7 +72,7 @@ spec =
_ -> @Tail_Call read_rest Nothing
read_rest Nothing
Text.from_codepoints all_codepoints.to_vector . should_equal contents
result . should_equal Nothing
result.should_succeed
f.delete
Test.specify "should allow reading a UTF-8 file" <|
@ -111,7 +111,7 @@ spec =
Test.specify "should work correctly if no data is read from it" <|
result = windows_file.with_input_stream [File.Option.Read] stream->
stream.with_stream_decoder Encoding.ascii Problem_Behavior.Report_Error _->Nothing
result.should_equal Nothing
result.should_succeed
read_file_one_by_one file encoding expected_size on_problems=Problem_Behavior.Report_Error =
file.with_input_stream [File.Option.Read] stream->

View File

@ -62,7 +62,7 @@ spec =
result = f.with_output_stream [File.Option.Write, File.Option.Create_New] stream->
stream.with_stream_encoder encoding Problem_Behavior.Report_Warning reporting_stream_encoder->
reporting_stream_encoder.write contents
result . should_equal Nothing
result.should_succeed
Warning.get_all result . map .value . should_equal [Encoding_Error "Encoding issues at codepoints 1, 3."]
f.read_text encoding . should_equal "S?o?wka!"
@ -75,7 +75,7 @@ spec =
reporting_stream_encoder.write " -🚧- "
reporting_stream_encoder.write "bar"
result_2 . should_equal Nothing
result_2.should_succeed
Warning.get_all result_2 . map .value . should_equal [Encoding_Error "Encoding issues at codepoints 3, 9."]
f.read_text encoding . should_equal "ABC?foo -?- bar"
@ -85,7 +85,7 @@ spec =
f.delete_if_exists
result = f.with_output_stream [File.Option.Write, File.Option.Create_New] stream->
stream.with_stream_encoder encoding Problem_Behavior.Report_Error _->Nothing
result . should_equal Nothing
result.should_succeed
f.read_text encoding . should_equal ""
main = Test.Suite.run_main spec