From 4a2720b1047df9bc5224c87dcae202656936cc33 Mon Sep 17 00:00:00 2001 From: GregoryTravis Date: Wed, 26 Apr 2023 11:37:09 -0400 Subject: [PATCH] Add parse extensions to Text type. #6330 (#6404) Add type-specific parse stubs to Text, e.g.: Text.parse_json self -> Json = Text.parse_url self -> Url = Text.parse_number self -> Number = --- CHANGELOG.md | 3 + .../Base/0.0.0-dev/src/Data/Json.enso | 10 +- .../Base/0.0.0-dev/src/Data/Numbers.enso | 5 +- .../0.0.0-dev/src/Data/Text/Extensions.enso | 321 +++++++++++++++++- .../Base/0.0.0-dev/src/Data/Time/Date.enso | 14 +- .../0.0.0-dev/src/Data/Time/Date_Time.enso | 61 +--- .../0.0.0-dev/src/Data/Time/Time_Of_Day.enso | 14 +- test/Tests/src/Data/Text/Parse_Spec.enso | 55 +++ test/Tests/src/Main.enso | 2 + 9 files changed, 423 insertions(+), 62 deletions(-) create mode 100644 test/Tests/src/Data/Text/Parse_Spec.enso diff --git a/CHANGELOG.md b/CHANGELOG.md index 41eeafb934b5a..96f58985f8182 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -392,6 +392,8 @@ for thousands and decimal point automatic detection.][6253] - [Implemented `Table.parse_text_to_table`.][6294] - [Added `Table.parse_to_columns`.][6383] +- [Added parsing methods for `Integer`, `Decimal`, `Json`, `Date`, `Date_Time`, + `Time_Of_Day`, `Time_Zone`, and `URI` to `Text`.][6404] [debug-shortcuts]: https://github.com/enso-org/enso/blob/develop/app/gui/docs/product/shortcuts.md#debug @@ -593,6 +595,7 @@ [6253]: https://github.com/enso-org/enso/pull/6253 [6294]: https://github.com/enso-org/enso/pull/6294 [6383]: https://github.com/enso-org/enso/pull/6383 +[6404]: https://github.com/enso-org/enso/pull/6404 #### Enso Compiler diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Json.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Json.enso index fd71ad369db9d..c74c5270d3cc5 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Json.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Json.enso @@ -28,7 +28,15 @@ from project.Data.Boolean import Boolean, True, False ## Methods for serializing from and to JSON. type Json - ## Parse a Text value into a `JS_Object` or an Enso primitive value (like `Text`, `Number`, `Boolean`, `Nothing`), or a `Vector` of values. + ## ALIAS From Text + + Parse a Text value into a `JS_Object` or an Enso primitive value (like + `Text`, `Number`, `Boolean`, `Nothing`), or a `Vector` of values. + + > Example + Parse the text "[null, null, true, false]". + + Json.parse "[null, null, true, false]" parse : Text -> JS_Object | Boolean | Number | Nothing | Text | Vector ! Invalid_JSON parse json = error_handler js_exception = diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso index 60cb49f039ca4..183710f49c35a 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso @@ -926,13 +926,14 @@ type Integer Arguments: - text: The text to parse into a integer. - - radix: The number base to use for parsing (defaults to 10). + - radix: The number base to use for parsing (defaults to 10). `radix` + must be between 2 and 36 (inclusive) -- see https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/Character.html#MIN_RADIX. > Example Parse the text "20220216" into an integer number. Integer.parse "20220216" - parse : Text -> Text -> Integer ! Number_Parse_Error + parse : Text -> Integer -> Integer ! Number_Parse_Error parse text (radix=10) = Integer.parse_builtin text radix ## PRIVATE diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso index fef524fb9ae6a..d1c7918dc9fbd 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso @@ -4,7 +4,6 @@ import project.Any.Any import project.Data.Array.Array import project.Data.Index_Sub_Range.Index_Sub_Range import project.Data.Locale.Locale -import project.Data.Numbers.Integer import project.Data.Range.Extensions import project.Data.Range.Range import project.Data.Text.Case.Case @@ -20,16 +19,25 @@ import project.Data.Text.Span.Utf_16_Span import project.Data.Text.Text import project.Data.Text.Text_Sub_Range.Codepoint_Ranges import project.Data.Text.Text_Sub_Range.Text_Sub_Range +import project.Data.Time.Date.Date +import project.Data.Time.Date_Time.Date_Time +import project.Data.Time.Time_Of_Day.Time_Of_Day +import project.Data.Time.Time_Zone.Time_Zone import project.Data.Vector.Vector import project.Errors.Common.Index_Out_Of_Bounds +import project.Errors.Common.Syntax_Error import project.Error.Error import project.Errors.Encoding_Error.Encoding_Error import project.Errors.Illegal_Argument.Illegal_Argument import project.Errors.Problem_Behavior.Problem_Behavior +import project.Errors.Time_Error.Time_Error import project.Meta +import project.Network.URI.URI import project.Nothing.Nothing from project.Data.Boolean import Boolean, True, False +from project.Data.Json import Json, Invalid_JSON, JS_Object +from project.Data.Numbers import Decimal, Integer, Number, Number_Parse_Error from project.Data.Text.Text_Sub_Range import Codepoint_Ranges, Text_Sub_Range import project.Data.Index_Sub_Range as Index_Sub_Range_Module @@ -1362,6 +1370,317 @@ Text.last_index_of self term="" start=-1 case_sensitivity=Case_Sensitivity.Sensi span = used.locate term Matching_Mode.Last case_sensitivity if span.is_nothing then Nothing else span.start +## ALIAS Decimal From Text + + Parses a textual representation of a decimal into a decimal number, returning + a `Number_Parse_Error` if the text does not represent a valid decimal. + + Arguments: + - locale: The locale that specifies the format to use when parsing + + > Example + Parse the text "7.6" into a decimal number. + + "7.6".parse_decimal +Text.parse_decimal : Locale | Nothing -> Decimal ! Number_Parse_Error +Text.parse_decimal self locale=Nothing = Decimal.parse self locale + +## ALIAS Integer From Text + + Parses a textual representation of an integer into an integer number, returning + a `Number_Parse_Error` if the text does not represent a valid integer. + + Arguments: + - radix: The number base to use for parsing (defaults to 10). `radix` + must be between 2 and 36 (inclusive) -- see https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/Character.html#MIN_RADIX. + + > Example + Parse the text "20220216" into an integer number. + + "20220216".parse +Text.parse_integer : Integer -> Integer ! Number_Parse_Error +Text.parse_integer self (radix=10) = Integer.parse_builtin self radix + +## ALIAS JSON From Text + + Parse a Text value into a `JS_Object` or an Enso primitive value (like + `Text`, `Number`, `Boolean`, `Nothing`), or a `Vector` of values. + + > Example + Parse the text "[null, null, true, false]". + + "[null, null, true, false]".parse_json +Text.parse_json : JS_Object | Boolean | Number | Nothing | Text | Vector ! Invalid_JSON +Text.parse_json self = Json.parse self + +## ALIAS Date from Text + + Converts text containing a date into a Date object. + + Arguments: + - format: An optional format describing how to parse the text. + + Returns a `Time_Error` if `self`` cannot be parsed using the provided + `format`. + + ? Format Syntax + A custom format string consists of one or more custom date and time format + specifiers. For example, "d MMM yyyy" will format "2011-12-03" as + "3 Dec 2011". See https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/time/format/DateTimeFormatter.html + for a complete format specification. + + ? Default Date Formatting + Unless you provide a custom format, the text must represent a valid date + that can be parsed using the ISO-8601 extended local date format. The + format consists of: + + - Four digits or more for the year. Years in the range 0000 to 9999 + will be pre-padded by zero to ensure four digits. Years outside + that range will have a prefixed positive or negative symbol. + - A dash + - Two digits for the month-of-year. This is pre-padded by zero to ensure + two digits. + - A dash + - Two digits for the day-of-month. This is pre-padded by zero to ensure two + digits. + + > Example + Parse the date of 23rd December 2020. + + import Standard.Base.Data.Text.Extensions + + example_parse = "2020-12-23".parse_date + + > Example + Recover from an error due to a wrong format. + + import Standard.Base.Data.Text.Extensions + from Standard.Base.Errors.Common import Time_Error + + example_parse_err = "my birthday".parse_date . catch Time_Error _-> + Date.new 2000 1 1 + + > Example + Parse "1999-1-1" as Date using a custom format. + + import Standard.Base.Data.Text.Extensions + + example_parse = "1999-1-1".parse_date "yyyy-M-d" + + > Example + Recover from the parse error. + + import Standard.Base.Data.Text.Extensions + from Standard.Base.Errors.Common import Time_Error + + example_parse_err = + date = "1999-1-1".parse_date "yyyy-MM-dd" + date.catch Time_Error (_->Date.new 2000 1 1) +Text.parse_date : (Text | Nothing) -> Date ! Time_Error +Text.parse_date self format=Nothing = Date.parse self format + +## ALIAS Date_Time from Text + + Obtains an instance of `Date_Time` from a text such as + "2007-12-03T10:15:30+01:00 Europe/Paris". + + Arguments: + - format: The format to use for parsing the input text. + - locale: The locale in which the format should be interpreted. + + ? Format Syntax + A custom format string consists of one or more custom date and time format + specifiers. For example, "d MMM yyyy" will format "2011-12-03" as + "3 Dec 2011". See https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/time/format/DateTimeFormatter.html + for a complete format specification. + + ? Default Date_Time Format + The text must represent a valid date-time as defined by the ISO-8601 + format. (See https://en.wikipedia.org/wiki/ISO_8601.) If a time zone is + present, it must be in the ISO-8601 Extended Date/Time Format (EDTF). + (See https://en.wikipedia.org/wiki/ISO_8601#EDTF.) The time zone format + consists of: + + - The ISO offset date time. + - If the zone ID is not available or is a zone offset then the format is + complete. + - An open square bracket '['. + - The zone ID. This is not part of the ISO-8601 standard. Parsing is case + sensitive. + - A close square bracket ']'. + + This method will return a `Time_Error` if the provided time cannot be parsed + using the above format. + + > Example + Parse UTC time. + + import Standard.Base.Data.Text.Extensions + + example_parse = "2020-10-01T04:11:12Z".parse_date_time + + > Example + Parse UTC-04:00 time. + + import Standard.Base.Data.Text.Extensions + + example_parse = "2020-10-01T04:11:12-04:00".parse_date_time + + > Example + Parse UTC-04:00 time specifying New York timezone. + + import Standard.Base.Data.Text.Extensions + + example_parse = "2020-10-01T04:11:12-04:00[America/New_York]".parse_date_time + + > Example + Parse UTC-04:00 time with nanoseconds. + + import Standard.Base.Data.Text.Extensions + + example_parse = "2020-10-01T04:11:12.177528-04:00".parse_date_time + + > Example + Recover from the parse error. + + import Standard.Base.Data.Text.Extensions + + example_parse = "2020-10-01".parse_date_time . catch Time_Error (_->Date_Time.now) + + > Example + Parse "2020-05-06 04:30:20" as Date_Time + + import Standard.Base.Data.Text.Extensions + + example_parse = "2020-05-06 04:30:20".parse_date_time "yyyy-MM-dd HH:mm:ss" + + > Example + Parse "06 of May 2020 at 04:30AM" as Date_Tme + + import Standard.Base.Data.Text.Extensions + + example_parse = + "06 of May 2020 at 04:30AM".parse_date_time "dd 'of' MMMM yyyy 'at' hh:mma" +Text.parse_date_time : Text | Nothing -> Locale -> Date_Time ! Time_Error +Text.parse_date_time self format=Nothing locale=Locale.default = Date_Time.parse self format locale + +## ALIAS Time_Of_Day from Text + + Obtains an instance of `Time_Of_Day` from a text such as "10:15". + + Arguments: + - format: The format to use for parsing the input text. + - locale: The locale in which the format should be interpreted. + + Returns a `Time_Error` if the provided text cannot be parsed using the + default format. + + ? Format Syntax + A custom format string consists of one or more custom date and time format + specifiers. For example, "d MMM yyyy" will format "2011-12-03" as + "3 Dec 2011". See https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/time/format/DateTimeFormatter.html + for a complete format specification. + + ? Default Time Format + The text must represent a valid time and is parsed using the ISO-8601 + extended local time format. The format consists of: + + - Two digits for the hour-of-day. This is pre-padded by zero to ensure two + digits. + - A colon + - Two digits for the minute-of-hour. This is pre-padded by zero to ensure + two digits. + - If the second-of-minute is not available then the format is complete. + - A colon + - Two digits for the second-of-minute. This is pre-padded by zero to ensure + two digits. + - If the nano-of-second is zero or not available then the format is + complete. + - A decimal point + - One to nine digits for the nano-of-second. As many digits will be output + as required. + + > Example + Get the time 15:05:30. + + import Standard.Base.Data.Text.Extensions + + example_parse = "15:05:30".parse_time_of_day + + > Example + Recover from the parse error. + + import Standard.Base.Data.Text.Extensions + from Standard.Base.Errors.Common import Time_Error + + example_parse = "half twelve".parse_time_of_day . catch Time_Error _-> + Time_Of_Day.new + + > Example + Parse "04:30:20" as Time_Of_Day. + + import Standard.Base.Data.Text.Extensions + + example_parse = "04:30:20".parse_time_of_day "HH:mm:ss" + + > Example + Parse "4:30AM" as Time_Of_Day + + import Standard.Base.Data.Text.Extensions + + example_parse = "4:30AM".parse_time_of_day "h:mma" +Text.parse_time_of_day : Text | Nothing -> Locale -> Time_Of_Day ! Time_Error +Text.parse_time_of_day self format=Nothing locale=Locale.default = Time_Of_Day.parse self format locale + +## ALIAS Time_Zone from Text + + This method parses the ID producing a `Time_Zone`. + + > Example + Get Central European Time. + + import Standard.Base.Data.Text.Extensions + + example_parse = "CET".parse_time_zone + + > Example + Get Moscow time. + + import Standard.Base.Data.Text.Extensions + + example_parse = "Europe/Moscow".parse_time_zone + + > Example + Get time zone -06:00. + + import Standard.Base.Data.Text.Extensions + + example_parse = "-06:00".parse_time_zone + + > Example + Get custom offset +03:02:01 of 3 hours 2 minutes an 1 second. + + import Standard.Base.Data.Text.Extensions + + example_parse = "+03:02:01".parse_time_zone +Text.parse_time_zone : Time_Zone ! Time_Error +Text.parse_time_zone self = Time_Zone.parse self + +## ALIAS URI from Text + + Parse a URI from a `Text`. + + Throws a Syntax_Error when the text cannot be parsed as a URI. + + > Example + Parse URI text. + + import Standard.Base.Data.Text.Extensions + + example_parse = "http://example.com".parse_uri +Text.parse_uri : URI ! Syntax_Error +Text.parse_uri self = URI.parse self + ## PRIVATE Returns a new Text constructed by slicing the input according to the provided ranges. The ranges are assumed to have step equal to 1 and bounds within the diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date.enso index 2ee29070e5943..3633dd911c1e7 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date.enso @@ -59,6 +59,8 @@ new_builtin year month day = @Builtin_Method "Date.new_builtin" ? Pattern Syntax Patterns are based on a simple sequence of letters and symbols. For example, "d MMM yyyy" will format "2011-12-03" as "3 Dec 2011". + See https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/time/format/DateTimeFormatter.html + for a complete format specification. ? Default Date Formatting Unless you provide a custom format, the text must represent a valid date @@ -146,8 +148,10 @@ type Date provided `pattern`. ? Pattern Syntax - Patterns are based on a simple sequence of letters and symbols. For - example, "d MMM yyyy" will format "2011-12-03" as "3 Dec 2011". + A custom pattern string consists of one or more custom date and time + format specifiers. For example, "d MMM yyyy" will format "2011-12-03" + as "3 Dec 2011". See https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/time/format/DateTimeFormatter.html + for a complete format specification. ? Default Date Formatting Unless you provide a custom format, the text must represent a valid date @@ -558,8 +562,10 @@ type Date - pattern: The text specifying the format for formatting the date. ? Pattern Syntax - Patterns are based on a simple sequence of letters and symbols. For - example, "d MMM yyyy" will format "2011-12-03" as "3 Dec 2011". + A custom pattern string consists of one or more custom date and time + format specifiers. For example, "d MMM yyyy" will format "2011-12-03" + as "3 Dec 2011". See https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/time/format/DateTimeFormatter.html + for a complete format specification. > Example Format "2020-06-02" as "2 June 2020" diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date_Time.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date_Time.enso index 43704d2cf1942..0c911b9763628 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date_Time.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date_Time.enso @@ -172,12 +172,17 @@ type Date_Time - locale: The locale in which the pattern should be interpreted. ? Pattern Syntax - For the list of accepted symbols in pattern refer to `Time.format` doc. + A custom pattern string consists of one or more custom date and time + format specifiers. For example, "d MMM yyyy" will format "2011-12-03" + as "3 Dec 2011". See https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/time/format/DateTimeFormatter.html + for a complete format specification. ? Default Date_Time Format - The text must represent a valid date-time and is parsed using the ISO-8601 - extended offset date-time format to add the timezone. The section in square - brackets is not part of the ISO-8601 standard. The format consists of: + The text must represent a valid date-time as defined by the ISO-8601 + format. (See https://en.wikipedia.org/wiki/ISO_8601.) If a time zone is + present, it must be in the ISO-8601 Extended Date/Time Format (EDTF). + (See https://en.wikipedia.org/wiki/ISO_8601#EDTF.) The time zone format + consists of: - The ISO offset date time. - If the zone ID is not available or is a zone offset then the format is @@ -603,50 +608,10 @@ type Date_Time - pattern: The pattern that specifies how to format the time. ? Pattern Syntax - Patterns are based on a simple sequence of letters and symbols. For - example, "d MMM uuuu" will format "2011-12-03" as "3 Dec 2011". - - The list of accepted symbols with examples: - - - 'G', era, "AD; Anno Domini" - - 'u', year, "2004; 04" - - 'y', year-of-era, "2004; 04" - - 'D', day-of-year, "189" - - 'M/L', month-of-year, "7; 07; Jul; July; J" - - 'd', day-of-month, "10" - - 'g', modified-julian-day, "2451334" - - 'Q/q', quarter-of-year, "3; 03; Q3; 3rd quarter" - - 'Y', week-based-year, "1996; 96" - - 'w', week-of-week-based-year, "27" - - 'W', week-of-month, "4" - - 'E', day-of-week, "Tue; Tuesday; T" - - 'e/c', localized day-of-week, "2; 02; Tue; Tuesday; T" - - 'F', day-of-week-in-month, "3" - - 'a', am-pm-of-day, "PM" - - 'h', clock-hour-of-am-pm (1-12), "12" - - 'K', hour-of-am-pm (0-11), "0" - - 'k', clock-hour-of-day (1-24), "24" - - 'H', hour-of-day (0-23), "0" - - 'm', minute-of-hour, "30" - - 's', second-of-minute, "55" - - 'S', fraction-of-second, "978" - - 'A', milli-of-day, "1234" - - 'n', nano-of-second, "987654321" - - 'N', nano-of-day, "1234000000" - - 'V', timezone ID, "America/Los_Angeles; Z; -08:30" - - 'v', generic timezone name, "Pacific Time; PT" - - 'z', timezone name, "Pacific Standard Time; PST" - - 'O', localized zone-offset, "GMT+8; GMT+08:00; UTC-08:00" - - 'X', zone-offset 'Z' for zero, "Z; -08; -0830; -08:30; -083015; -08:30:15" - - 'x', zone-offset, "+0000; -08; -0830; -08:30; -083015; -08:30:15" - - 'Z', zone-offset, "+0000; -0800; -08:00" - - 'p', pad next, "1" - - ''', (single quote) escape for text, "'Text'" - - '''', (double quote) single quote, "'" - - '[', optional section start - - ']', optional section end - - The count of pattern letters determines the format. + A custom pattern string consists of one or more custom date and time + format specifiers. For example, "d MMM yyyy" will format "2011-12-03" + as "3 Dec 2011". See https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/time/format/DateTimeFormatter.html + for a complete format specification. > Example Format "2020-10-08T16:41:13+03:00[Europe/Moscow]" as diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Time_Of_Day.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Time_Of_Day.enso index adcc5591342a2..38f58eb58c991 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Time_Of_Day.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Time_Of_Day.enso @@ -114,7 +114,10 @@ type Time_Of_Day default format. ? Pattern Syntax - For the list of accepted symbols in pattern refer to `Time.format` doc. + A custom pattern string consists of one or more custom date and time + format specifiers. For example, "d MMM yyyy" will format "2011-12-03" + as "3 Dec 2011". See https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/time/format/DateTimeFormatter.html + for a complete format specification. ? Default Time Format The text must represent a valid time and is parsed using the ISO-8601 @@ -303,11 +306,10 @@ type Time_Of_Day - pattern: The pattern specifying how to format the time of day. ? Pattern Syntax - Patterns are based on a simple sequence of letters and symbols. For - example, "HH-mm-ss.SSS" will format "16:21:10" as "16-21-10.323". - - For the list of accepted symbols in pattern refer to the - `Base.Data.Time.format` doc. + A custom pattern string consists of one or more custom date and time + format specifiers. For example, "d MMM yyyy" will format "2011-12-03" + as "3 Dec 2011". See https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/time/format/DateTimeFormatter.html + for a complete format specification. > Example Format "16:21:10" as "16:21:00.1234" diff --git a/test/Tests/src/Data/Text/Parse_Spec.enso b/test/Tests/src/Data/Text/Parse_Spec.enso new file mode 100644 index 0000000000000..15173ae02eeb7 --- /dev/null +++ b/test/Tests/src/Data/Text/Parse_Spec.enso @@ -0,0 +1,55 @@ +from Standard.Base import all + +import Standard.Base.Data.Text.Extensions +import Standard.Base.Data.Time.Date.Date +import Standard.Base.Data.Time.Time_Zone.Time_Zone +import Standard.Base.Errors.Common.Syntax_Error +import Standard.Base.Errors.Time_Error.Time_Error +import Standard.Base.Network.URI.URI +import Standard.Test.Extensions + +from Standard.Base.Data.Json import Json, Invalid_JSON, JS_Object +from Standard.Base.Data.Numbers import Decimal, Integer, Number_Parse_Error +from Standard.Test import Test, Test_Suite + +spec = + Test.group "parse" <| + Test.specify "Decimal" <| + "32.5".parse_decimal . should_equal <| Decimal.parse "32.5" + l = Locale.new "cs" + "32,5".parse_decimal l . should_equal <| Decimal.parse "32,5" l + "abc".parse_decimal . should_fail_with Number_Parse_Error + + Test.specify "Integer" <| + "12343456".parse_integer . should_equal <| Integer.parse "12343456" + "ABC123".parse_integer 16 . should_equal <| Integer.parse "ABC123" 16 + "abc".parse_integer . should_fail_with Number_Parse_Error + + Test.specify "Json" <| + "[null, null, true, false]".parse_json . should_equal <| Json.parse "[null, null, true, false]" + "[[".parse_json . should_fail_with Invalid_JSON + + Test.specify "Date" <| + "1999-01-01".parse_date . should_equal <| Date.parse "1999-01-01" + "1999 1 1".parse_date "yyyy M d" . should_equal <| Date.parse "1999 1 1" "yyyy M d" + "1999-01-01".parse_date "yyyy M d" . should_fail_with Time_Error + + Test.specify "Date_Time" <| + "2020-10-01T04:11:12-04:00".parse_date_time . should_equal <| Date_Time.parse "2020-10-01T04:11:12-04:00" + "2020-05-06 04:30:20".parse_date_time "yyyy-MM-dd HH:mm:ss" . should_equal <| Date_Time.parse "2020-05-06 04:30:20" "yyyy-MM-dd HH:mm:ss" + "asdf".parse_date_time . should_fail_with Time_Error + + Test.specify "Time_Of_Day" <| + "15:05:30".parse_time_of_day . should_equal <| Time_Of_Day.parse "15:05:30" + "4:30AM".parse_time_of_day "h:mma" . should_equal <| Time_Of_Day.parse "4:30AM" "h:mma" + "half twelve".parse_time_of_day . should_fail_with Time_Error + + Test.specify "Time_Zone" <| + "CET".parse_time_zone . should_equal <| Time_Zone.parse "CET" + "foo".parse_time_zone . should_fail_with Time_Error + + Test.specify "URI" <| + "http://example.com".parse_uri . should_equal <| URI.parse "http://example.com" + ":::".parse_uri . should_fail_with Syntax_Error + +main = Test_Suite.run_main spec diff --git a/test/Tests/src/Main.enso b/test/Tests/src/Main.enso index 6e0d35ebfa573..1c5db730d9918 100644 --- a/test/Tests/src/Main.enso +++ b/test/Tests/src/Main.enso @@ -51,6 +51,7 @@ import project.Data.Regression_Spec import project.Data.Text_Spec import project.Data.Text.Text_Sub_Range_Spec import project.Data.Text.Encoding_Spec +import project.Data.Text.Parse_Spec import project.Data.Text.Regex_Spec import project.Data.Text.Span_Spec import project.Data.Text.Utils_Spec @@ -121,6 +122,7 @@ main = Test_Suite.run_main <| Python_Interop_Spec.spec R_Interop_Spec.spec Pair_Spec.spec + Parse_Spec.spec Problems_Spec.spec Range_Spec.spec Ref_Spec.spec