Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do some general cleanup #8

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parse_error.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub type ParseError {
}

@internal
pub fn parse_error_to_string(error: ParseError) -> String {
pub fn to_string(error: ParseError) -> String {
case error {
GleamIntParseError -> "GleamIntParseError"
InvalidCharacter(character) -> "InvalidCharacter(\"" <> character <> "\")"
Expand Down
68 changes: 22 additions & 46 deletions test/coerce_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import startest/expect
pub fn coerce_into_valid_number_string_tests() {
describe("coerce_into_valid_number_string_test", [
describe(
"whitespace_only_or_empty_string",
[
["", " ", "\t", "\n", "\r", "\f", " \t\n\r\f "]
"should_error_as_whitespace_only_or_empty_string",
["", " ", "\t", "\n", "\r", "\f", " \t\n\r\f "]
|> list.map(fn(text) {
let printable_text = text |> into_printable_text

Expand All @@ -23,13 +22,10 @@ pub fn coerce_into_valid_number_string_tests() {
|> coerce_into_valid_number_string
|> expect.to_equal(Error(WhitespaceOnlyOrEmptyString))
}),
]
|> list.concat,
),
describe(
"has_invalid_character",
[
[#("a", "a"), #("1b1", "b"), #("100.00c01", "c"), #("1 1", " ")]
[#("a", "a"), #("1b1", "b"), #("100.00c01", "c"), #("1 1", " ")]
|> list.map(fn(pair) {
let #(input, invalid_character) = pair
use <- it("\"" <> invalid_character <> "\" in \"" <> input <> "\"")
Expand All @@ -38,13 +34,10 @@ pub fn coerce_into_valid_number_string_tests() {
|> coerce_into_valid_number_string
|> expect.to_equal(Error(InvalidCharacter(invalid_character)))
}),
]
|> list.concat,
),
describe(
"has_valid_sign_position",
[
[#("+1", "+1"), #("-1", "-1"), #("+1.0", "+1.0"), #("-1.0", "-1.0")]
[#("+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 <> "\"")
Expand All @@ -53,13 +46,10 @@ pub fn coerce_into_valid_number_string_tests() {
|> coerce_into_valid_number_string
|> expect.to_equal(Ok(output))
}),
]
|> list.concat,
),
describe(
"has_invalid_sign_position",
[
[#("1+", "+"), #("1-", "-"), #("1+1", "+"), #("1-1", "-")]
[#("1+", "+"), #("1-", "-"), #("1+1", "+"), #("1-1", "-")]
|> list.map(fn(pair) {
let #(input, sign_at_invalid_position) = pair
use <- it(
Expand All @@ -72,52 +62,43 @@ pub fn coerce_into_valid_number_string_tests() {
Error(SignAtInvalidPosition(sign_at_invalid_position)),
)
}),
]
|> list.concat,
),
describe(
"has_valid_decimal_position",
[
[#(".1", "0.1"), #("1.", "1.0")]
[#(".1", "0.1"), #("1.", "1.0")]
|> list.map(fn(pair) {
let #(input, output) = pair
use <- it("\"" <> input <> "\"")
input
|> coerce_into_valid_number_string
|> expect.to_equal(Ok(output))
}),
]
|> list.concat,
),
describe(
"has_invalid_decimal_position",
[
[".", "..", "0.0.", ".0.0"]
[".", "..", "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",
[
[
#("0", "0"),
#("0.0", "0.0"),
#("+1000", "+1000"),
#("-1000", "-1000"),
#(" 1000 ", "1000"),
#(" -1000 ", "-1000"),
#("1_000", "1000"),
#("1_000_000", "1000000"),
#("1_000_000.0", "1000000.0"),
#("1_000_000.000_1", "1000000.0001"),
#("1000.000_000", "1000.000000"),
]
#("0", "0"),
#("0.0", "0.0"),
#("+1000", "+1000"),
#("-1000", "-1000"),
#(" 1000 ", "1000"),
#(" -1000 ", "-1000"),
#("1_000", "1000"),
#("1_000_000", "1000000"),
#("1_000_000.0", "1000000.0"),
#("1_000_000.000_1", "1000000.0001"),
#("1000.000_000", "1000.000000"),
]
|> list.map(fn(pair) {
let #(input, output) = pair
use <- it("\"" <> input <> "\" -> \"" <> output <> "\"")
Expand All @@ -126,25 +107,20 @@ pub fn coerce_into_valid_number_string_tests() {
|> coerce_into_valid_number_string
|> expect.to_equal(Ok(output))
}),
]
|> list.concat,
),
describe(
"has_invalid_underscore_position",
[
[
"_", "_1000", "1000_", "+_1000", "-_1000", "1__000", "1_.000",
"1._000", "_1000.0", "1000.0_", "1000._0", "1000_.0", "1000_.",
]
"_", "_1000", "1000_", "+_1000", "-_1000", "1__000", "1_.000", "1._000",
"_1000.0", "1000.0_", "1000._0", "1000_.0", "1000_.",
]
|> list.map(fn(text) {
use <- it("\"" <> text <> "\"")

text
|> coerce_into_valid_number_string
|> expect.to_equal(Error(InvalidUnderscorePosition))
}),
]
|> list.concat,
),
])
}
17 changes: 7 additions & 10 deletions test/helpers_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import startest/expect

pub fn into_printable_text_tests() {
describe(
"should_be_printable_text_test",
"should_be_printable_text",
[
[
#("\t", "\\t"),
#("\n", "\\n"),
#("\r", "\\r"),
#("\f", "\\f"),
#("\t\nabc123\r", "\\t\\nabc123\\r"),
]
#("\t", "\\t"),
#("\n", "\\n"),
#("\r", "\\r"),
#("\f", "\\f"),
#("\t\nabc123\r", "\\t\\nabc123\\r"),
]
|> list.map(fn(pair) {
let #(input, output) = pair
use <- it("\"" <> output <> "\"")
input |> into_printable_text |> expect.to_equal(output)
}),
]
|> list.concat,
)
}
54 changes: 24 additions & 30 deletions test/to_float_parse_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import gleam/list
import lenient_parse
import parse_error.{
InvalidCharacter, InvalidDecimalPosition, InvalidUnderscorePosition,
WhitespaceOnlyOrEmptyString, parse_error_to_string,
WhitespaceOnlyOrEmptyString,
}
import startest.{describe, it}
import startest/expect
Expand All @@ -13,20 +13,19 @@ pub fn coerce_into_valid_number_string_tests() {
describe(
"should_coerce_to_float",
[
[
#("1.001", 1.001),
#("1.00", 1.0),
#("1.0", 1.0),
#("0.1", 0.1),
#("+123.321", 123.321),
#("-123.321", -123.321),
#("1", 1.0),
#("1.", 1.0),
#(".1", 0.1),
#("1_000_000.0", 1_000_000.0),
#(" 1 ", 1.0),
#(" 1.0 ", 1.0),
]
#("1.001", 1.001),
#("1.00", 1.0),
#("1.0", 1.0),
#("0.1", 0.1),
#("+123.321", 123.321),
#("-123.321", -123.321),
#("1", 1.0),
#("1.", 1.0),
#(".1", 0.1),
#("1_000_000.0", 1_000_000.0),
#(" 1 ", 1.0),
#(" 1.0 ", 1.0),
]
|> list.map(fn(pair) {
let #(input, output) = pair
let output_string = output |> float.to_string
Expand All @@ -36,34 +35,29 @@ pub fn coerce_into_valid_number_string_tests() {
|> lenient_parse.to_float
|> expect.to_equal(Ok(output))
}),
]
|> list.concat,
),
describe(
"should_not_coerce_to_float",
[
[
#("1_000__000.0", InvalidUnderscorePosition),
#("..1", InvalidDecimalPosition),
#("1..", InvalidDecimalPosition),
#(".1.", InvalidDecimalPosition),
#(".", InvalidDecimalPosition),
#("", WhitespaceOnlyOrEmptyString),
#(" ", WhitespaceOnlyOrEmptyString),
#("abc", InvalidCharacter("a")),
]
#("1_000__000.0", InvalidUnderscorePosition),
#("..1", InvalidDecimalPosition),
#("1..", InvalidDecimalPosition),
#(".1.", InvalidDecimalPosition),
#(".", InvalidDecimalPosition),
#("", WhitespaceOnlyOrEmptyString),
#(" ", WhitespaceOnlyOrEmptyString),
#("abc", InvalidCharacter("a")),
]
|> list.map(fn(pair) {
let #(input, error) = pair
let error_text = error |> parse_error_to_string
let error_text = error |> parse_error.to_string

use <- it("\"" <> input <> "\" -> " <> error_text)

input
|> lenient_parse.to_float
|> expect.to_equal(Error(error))
}),
]
|> list.concat,
),
])
}
38 changes: 16 additions & 22 deletions test/to_int_parse_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import gleam/list
import lenient_parse
import parse_error.{
GleamIntParseError, InvalidCharacter, InvalidUnderscorePosition,
WhitespaceOnlyOrEmptyString, parse_error_to_string,
WhitespaceOnlyOrEmptyString,
}
import startest.{describe, it}
import startest/expect
Expand All @@ -13,14 +13,13 @@ pub fn coerce_into_valid_number_string_tests() {
describe(
"should_coerce_to_int",
[
[
#("1", 1),
#("+123", 123),
#("0123", 123),
#("-123", -123),
#("1_000_000", 1_000_000),
#(" 1 ", 1),
]
#("1", 1),
#("+123", 123),
#("0123", 123),
#("-123", -123),
#("1_000_000", 1_000_000),
#(" 1 ", 1),
]
|> list.map(fn(pair) {
let #(input, output) = pair
let output_string = output |> int.to_string
Expand All @@ -30,32 +29,27 @@ pub fn coerce_into_valid_number_string_tests() {
|> lenient_parse.to_int
|> expect.to_equal(Ok(output))
}),
]
|> list.concat,
),
describe(
"should_not_coerce_to_int",
[
[
#("1_000__000", InvalidUnderscorePosition),
#("1.", GleamIntParseError),
#("1.0", GleamIntParseError),
#("", WhitespaceOnlyOrEmptyString),
#(" ", WhitespaceOnlyOrEmptyString),
#("abc", InvalidCharacter("a")),
]
#("1_000__000", InvalidUnderscorePosition),
#("1.", GleamIntParseError),
#("1.0", GleamIntParseError),
#("", WhitespaceOnlyOrEmptyString),
#(" ", WhitespaceOnlyOrEmptyString),
#("abc", InvalidCharacter("a")),
]
|> list.map(fn(pair) {
let #(input, error) = pair
let error_text = error |> parse_error_to_string
let error_text = error |> parse_error.to_string

use <- it("\"" <> input <> "\" -> " <> error_text)

input
|> lenient_parse.to_int
|> expect.to_equal(Error(error))
}),
]
|> list.concat,
),
])
}