Skip to content

Commit

Permalink
Add build float tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephTLyons committed Nov 16, 2024
1 parent 0b84984 commit f9fb3bf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/lenient_parse/internal/build.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ pub fn float_value(
is_positive is_positive: Bool,
whole_digits whole_digits: Queue(Int),
fractional_digits fractional_digits: Queue(Int),
exponent exponent: Int,
scale_factor scale_factor: Int,
) -> Float {
let #(whole_digits, fractional_digits) =
scale.queues(whole_digits, fractional_digits, exponent)
scale.queues(whole_digits, fractional_digits, scale_factor)
let fractional_digits_length = fractional_digits |> queue.length
let #(all_digits, _) =
scale.queues(whole_digits, fractional_digits, fractional_digits_length)
Expand Down
2 changes: 1 addition & 1 deletion src/lenient_parse/internal/parser.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn parse_float(tokens tokens: List(Token)) -> Result(Float, ParseError) {
is_positive: is_positive,
whole_digits: whole_digits,
fractional_digits: fractional_digits,
exponent: exponent,
scale_factor: exponent,
))
}
}
Expand Down
55 changes: 55 additions & 0 deletions test/build_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import gleam/queue
import lenient_parse/internal/build
import startest/expect

pub fn build_float_value_whole_number_test() {
let a = queue.from_list([1])
let b = queue.from_list([])

build.float_value(
is_positive: True,
whole_digits: a,
fractional_digits: b,
scale_factor: 0,
)
|> expect.to_equal(1.0)
}

pub fn build_float_value_whole_number_with_zero_fraction_test() {
let a = queue.from_list([1])
let b = queue.from_list([0])

build.float_value(
is_positive: True,
whole_digits: a,
fractional_digits: b,
scale_factor: 0,
)
|> expect.to_equal(1.0)
}

pub fn build_float_value_fractional_only_test() {
let a = queue.from_list([])
let b = queue.from_list([1])

build.float_value(
is_positive: True,
whole_digits: a,
fractional_digits: b,
scale_factor: 0,
)
|> expect.to_equal(0.1)
}

pub fn build_float_value_zero_whole_with_fraction_test() {
let a = queue.from_list([0])
let b = queue.from_list([1])

build.float_value(
is_positive: True,
whole_digits: a,
fractional_digits: b,
scale_factor: 0,
)
|> expect.to_equal(0.1)
}

0 comments on commit f9fb3bf

Please sign in to comment.