diff --git a/src/coerce.gleam b/src/coerce.gleam index cc8e49e..f6bbc3d 100644 --- a/src/coerce.gleam +++ b/src/coerce.gleam @@ -84,8 +84,7 @@ fn valid_character_set() -> Set(String) { digits |> set.union(signs) |> set.union(separators) } -@internal -pub fn coerce_into_valid_underscore_string( +fn coerce_into_valid_underscore_string( text: String, ) -> Result(String, ParseError) { text @@ -145,8 +144,7 @@ fn do_coerce_into_valid_underscore_string( } } -@internal -pub fn has_valid_characters(text: String) -> Result(Nil, ParseError) { +fn has_valid_characters(text: String) -> Result(Nil, ParseError) { let graphemes = text |> string.to_graphemes list.try_map(graphemes, fn(grapheme) { case valid_character_set() |> set.contains(grapheme) { @@ -157,8 +155,7 @@ pub fn has_valid_characters(text: String) -> Result(Nil, ParseError) { |> result.replace(Nil) } -@internal -pub fn has_valid_sign_position(text: String) -> Result(Nil, ParseError) { +fn has_valid_sign_position(text: String) -> Result(Nil, ParseError) { do_has_valid_sign_position(text |> string.to_graphemes, 0) } @@ -177,10 +174,7 @@ fn do_has_valid_sign_position( } } -@internal -pub fn coerce_into_valid_decimal_string( - text: String, -) -> Result(String, ParseError) { +fn coerce_into_valid_decimal_string(text: String) -> Result(String, ParseError) { let text_length = text |> string.length text diff --git a/test/coerce_test.gleam b/test/coerce_test.gleam index 4432460..7b83b19 100644 --- a/test/coerce_test.gleam +++ b/test/coerce_test.gleam @@ -1,9 +1,9 @@ import coerce.{ InvalidCharacter, InvalidDecimalPosition, InvalidUnderscorePosition, SignAtInvalidPosition, WhitespaceOnlyOrEmptyString, - coerce_into_valid_decimal_string, coerce_into_valid_number_string, - coerce_into_valid_underscore_string, has_valid_sign_position, + coerce_into_valid_number_string, } + import gleam/list import helpers.{into_printable_text} import startest.{describe, it} @@ -28,7 +28,7 @@ pub fn coerce_into_valid_number_string_tests() { |> list.concat, ), describe( - "invalid_character", + "has_invalid_character", [ [#("a", "a"), #("1b1", "b"), #("100.00c01", "c"), #("1 1", " ")] |> list.map(fn(pair) { @@ -45,7 +45,22 @@ pub fn coerce_into_valid_number_string_tests() { describe( "has_valid_sign_position", [ - [#("1+1", "+"), #("1-1", "-")] + [#("+1", "+1"), #("-1", "-1"), #("+1.0", "+1.0"), #("-1.0", "-1.0")] + |> list.map(fn(pair) { + let #(input, output) = pair + use <- it("\"" <> output <> "\" in \"" <> input <> "\"") + + input + |> coerce_into_valid_number_string + |> expect.to_equal(Ok(output)) + }), + ] + |> list.concat, + ), + describe( + "has_invalid_sign_position", + [ + [#("1+", "+"), #("1-", "-"), #("1+1", "+"), #("1-1", "-")] |> list.map(fn(pair) { let #(input, sign_at_invalid_position) = pair use <- it( @@ -62,13 +77,12 @@ pub fn coerce_into_valid_number_string_tests() { |> list.concat, ), describe( - "coerce_into_valid_number_string", + "has_valid_decimal_position", [ - [#(" +1", "+1")] + [#(".1", "0.1"), #("1.", "1.0")] |> list.map(fn(pair) { let #(input, output) = pair - use <- it("\"" <> output <> "\" in \"" <> input <> "\"") - + use <- it("\"" <> input <> "\"") input |> coerce_into_valid_number_string |> expect.to_equal(Ok(output)) @@ -76,11 +90,19 @@ pub fn coerce_into_valid_number_string_tests() { ] |> list.concat, ), - ]) -} - -pub fn coerce_into_valid_underscore_string_tests() { - describe("underscore_string_test", [ + describe( + "has_invalid_decimal_position", + [ + [".", "..", "0.0.", ".0.0"] + |> list.map(fn(text) { + use <- it("\"" <> text <> "\"") + text + |> coerce_into_valid_number_string + |> expect.to_equal(Error(InvalidDecimalPosition)) + }), + ] + |> list.concat, + ), describe( "has_valid_underscore_position", [ @@ -89,8 +111,8 @@ pub fn coerce_into_valid_underscore_string_tests() { #("0.0", "0.0"), #("+1000", "+1000"), #("-1000", "-1000"), - #(" 1000 ", " 1000 "), - #(" -1000 ", " -1000 "), + #(" 1000 ", "1000"), + #(" -1000 ", "-1000"), #("1_000", "1000"), #("1_000_000", "1000000"), #("1_000_000.0", "1000000.0"), @@ -102,7 +124,7 @@ pub fn coerce_into_valid_underscore_string_tests() { use <- it("\"" <> input <> "\" -> \"" <> output <> "\"") input - |> coerce_into_valid_underscore_string + |> coerce_into_valid_number_string |> expect.to_equal(Ok(output)) }), ] @@ -119,7 +141,7 @@ pub fn coerce_into_valid_underscore_string_tests() { use <- it("\"" <> text <> "\"") text - |> coerce_into_valid_underscore_string + |> coerce_into_valid_number_string |> expect.to_equal(Error(InvalidUnderscorePosition)) }), ] @@ -127,69 +149,3 @@ pub fn coerce_into_valid_underscore_string_tests() { ), ]) } - -pub fn has_valid_sign_position_tests() { - describe("has_valid_sign_position_test", [ - describe( - "has_valid_sign_position", - [ - ["+1", "-1", "+1.0", "-1.0"] - |> list.map(fn(text) { - use <- it("\"" <> text <> "\"") - text - |> has_valid_sign_position - |> expect.to_equal(Ok(Nil)) - }), - ] - |> list.concat, - ), - describe( - "has_invalid_sign_position", - [ - [#("1+", "+"), #("1-", "-"), #("1+1", "+"), #("1-1", "-")] - |> list.map(fn(pair) { - let #(input, sign_at_invalid_position) = pair - use <- it("\"" <> sign_at_invalid_position <> "\"") - input - |> has_valid_sign_position - |> expect.to_equal( - Error(SignAtInvalidPosition(sign_at_invalid_position)), - ) - }), - ] - |> list.concat, - ), - ]) -} - -pub fn check_for_valid_decimal_positions_tests() { - describe("check_for_valid_decimal_positions_test", [ - describe( - "has_valid_sign_position", - [ - [#(".1", "0.1"), #("1.", "1.0")] - |> list.map(fn(pair) { - let #(input, output) = pair - use <- it("\"" <> input <> "\"") - input - |> coerce_into_valid_decimal_string - |> expect.to_equal(Ok(output)) - }), - ] - |> list.concat, - ), - describe( - "has_valid_sign_position", - [ - [".", "..", "0.0.", ".0.0"] - |> list.map(fn(text) { - use <- it("\"" <> text <> "\"") - text - |> coerce_into_valid_decimal_string - |> expect.to_equal(Error(InvalidDecimalPosition)) - }), - ] - |> list.concat, - ), - ]) -}