Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephTLyons committed Oct 17, 2024
1 parent 5ec906a commit d9f46a1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 93 deletions.
14 changes: 4 additions & 10 deletions src/coerce.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}

Expand All @@ -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
Expand Down
122 changes: 39 additions & 83 deletions test/coerce_test.gleam
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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) {
Expand All @@ -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(
Expand All @@ -62,25 +77,32 @@ 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))
}),
]
|> 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",
[
Expand All @@ -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"),
Expand All @@ -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))
}),
]
Expand All @@ -119,77 +141,11 @@ 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))
}),
]
|> list.concat,
),
])
}

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,
),
])
}

0 comments on commit d9f46a1

Please sign in to comment.