Skip to content

Commit

Permalink
Use qcheck 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mooreryan authored and lpil committed Mar 3, 2025
1 parent 75e6bb7 commit 9073700
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 78 deletions.
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ gleam_stdlib = ">= 0.44.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
qcheck = ">= 0.0.7 and < 1.0.0"
qcheck = ">= 1.0.0 and < 2.0.0"
simplifile = ">= 2.2.0 and < 3.0.0"
gleam_regexp = ">= 1.0.0 and < 2.0.0"
prng = ">= 4.0.1 and < 5.0.0"
10 changes: 5 additions & 5 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ packages = [
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
{ name = "gleam_bitwise", version = "1.3.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "B36E1D3188D7F594C7FD4F43D0D2CE17561DE896202017548578B16FE1FE9EFC" },
{ name = "gleam_regexp", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "A3655FDD288571E90EE9C4009B719FEF59FA16AFCDF3952A76A125AF23CF1592" },
{ name = "gleam_stdlib", version = "0.51.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "14AFA8D3DDD7045203D422715DBB822D1725992A31DF35A08D97389014B74B68" },
{ name = "gleam_regexp", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "7F5E0C0BBEB3C58E57C9CB05FA9002F970C85AD4A63BA1E55CBCB35C15809179" },
{ name = "gleam_stdlib", version = "0.55.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "32D8F4AE03771516950047813A9E359249BD9FBA5C33463FDB7B953D6F8E896B" },
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "gleeunit", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "0E6C83834BA65EDCAAF4FE4FB94AC697D9262D83E6F58A750D63C9F6C8A9D9FF" },
{ name = "prng", version = "4.0.1", build_tools = ["gleam"], requirements = ["gleam_bitwise", "gleam_stdlib", "gleam_yielder"], otp_app = "prng", source = "hex", outer_checksum = "695AB70E4BE713042062E901975FC08D1EC725B85B808D4786A14C406ADFBCF1" },
{ name = "qcheck", version = "0.0.7", build_tools = ["gleam"], requirements = ["exception", "gleam_regexp", "gleam_stdlib", "gleam_yielder", "prng"], otp_app = "qcheck", source = "hex", outer_checksum = "2363A970BB2E82623023154ED09EE48E3005229A869DA423B18468EA066A4CA9" },
{ name = "qcheck", version = "1.0.0", build_tools = ["gleam"], requirements = ["exception", "gleam_regexp", "gleam_stdlib", "gleam_yielder", "prng"], otp_app = "qcheck", source = "hex", outer_checksum = "6DAE7925E350480CE813F80D07AC4B9BAB25360F0D63EC98C5742D8456C9A9A1" },
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
]

Expand All @@ -19,5 +19,5 @@ gleam_regexp = { version = ">= 1.0.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
prng = { version = ">= 4.0.1 and < 5.0.0" }
qcheck = { version = ">= 0.0.7 and < 1.0.0" }
qcheck = { version = ">= 1.0.0 and < 2.0.0" }
simplifile = { version = ">= 2.2.0 and < 3.0.0" }
48 changes: 26 additions & 22 deletions test/gleam/time/duration_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import qcheck

pub fn add_property_0_test() {
use #(x, y) <- qcheck.given(qcheck.tuple2(
qcheck.int_uniform(),
qcheck.int_uniform(),
qcheck.uniform_int(),
qcheck.uniform_int(),
))
let expected = duration.nanoseconds(x + y)
let actual = duration.nanoseconds(x) |> duration.add(duration.nanoseconds(y))
expected == actual
should.equal(expected, actual)
}

pub fn add_property_1_test() {
use #(x, y) <- qcheck.given(qcheck.tuple2(
qcheck.int_uniform(),
qcheck.int_uniform(),
qcheck.uniform_int(),
qcheck.uniform_int(),
))
let expected = duration.seconds(x + y)
let actual = duration.seconds(x) |> duration.add(duration.seconds(y))
expected == actual
should.equal(expected, actual)
}

pub fn add_0_test() {
Expand Down Expand Up @@ -122,58 +122,62 @@ pub fn to_seconds_4_test() {

pub fn compare_property_0_test() {
use #(x, y) <- qcheck.given(qcheck.tuple2(
qcheck.int_uniform_inclusive(0, 999_999),
qcheck.int_uniform_inclusive(0, 999_999),
qcheck.bounded_int(0, 999_999),
qcheck.bounded_int(0, 999_999),
))
// Durations of seconds
let tx = duration.seconds(x)
let ty = duration.seconds(y)
duration.compare(tx, ty) == int.compare(x, y)
should.equal(duration.compare(tx, ty), int.compare(x, y))
}

pub fn compare_property_1_test() {
use #(x, y) <- qcheck.given(qcheck.tuple2(
qcheck.int_uniform_inclusive(0, 999_999),
qcheck.int_uniform_inclusive(0, 999_999),
qcheck.bounded_int(0, 999_999),
qcheck.bounded_int(0, 999_999),
))
// Durations of nanoseconds
let tx = duration.nanoseconds(x)
let ty = duration.nanoseconds(y)
duration.compare(tx, ty) == int.compare(x, y)
should.equal(duration.compare(tx, ty), int.compare(x, y))
}

pub fn compare_property_2_test() {
use x <- qcheck.given(qcheck.int_uniform())
use x <- qcheck.given(qcheck.uniform_int())
let tx = duration.nanoseconds(x)
duration.compare(tx, tx) == order.Eq
should.equal(duration.compare(tx, tx), order.Eq)
}

pub fn compare_property_3_test() {
use #(x, y) <- qcheck.given(qcheck.tuple2(
qcheck.int_uniform_inclusive(0, 999_999),
qcheck.int_uniform_inclusive(0, 999_999),
qcheck.bounded_int(0, 999_999),
qcheck.bounded_int(0, 999_999),
))
let tx = duration.nanoseconds(x)
// It doesn't matter if a duration is positive or negative, it's the amount
// of time spanned that matters.
//
// Second durations
duration.compare(tx, duration.seconds(y))
== duration.compare(tx, duration.seconds(0 - y))
should.equal(
duration.compare(tx, duration.seconds(y)),
duration.compare(tx, duration.seconds(0 - y)),
)
}

pub fn compare_property_4_test() {
use #(x, y) <- qcheck.given(qcheck.tuple2(
qcheck.int_uniform_inclusive(0, 999_999),
qcheck.int_uniform_inclusive(0, 999_999),
qcheck.bounded_int(0, 999_999),
qcheck.bounded_int(0, 999_999),
))
let tx = duration.nanoseconds(x)
// It doesn't matter if a duration is positive or negative, it's the amount
// of time spanned that matters.
//
// Nanosecond durations
duration.compare(tx, duration.nanoseconds(y))
== duration.compare(tx, duration.nanoseconds(y * -1))
should.equal(
duration.compare(tx, duration.nanoseconds(y)),
duration.compare(tx, duration.nanoseconds(y * -1)),
)
}

pub fn compare_0_test() {
Expand Down
60 changes: 30 additions & 30 deletions test/gleam/time/generators.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import gleam/string
import gleam/time/timestamp
import qcheck

/// Generate timestamps representing instants in the range `0000-01-01T00:00:00Z`
/// Generate timestamps representing instants in the range `0000-01-01T00:00:00Z`
/// to `9999-12-31T23:59:59.999999999Z`.
///
///
pub fn timestamp() {
// prng can only generate good integers in the range
// prng can only generate good integers in the range
// [-2_147_483_648, 2_147_483_647]
//
//
// So we must get to the range we need by generating the values in parts, then
// adding them together.
// adding them together.
//
// The smallest number of milliseconds we need to generate:
// > d=new Date("0000-01-01T00:00:00"); d.getTime()
Expand All @@ -22,21 +22,21 @@ pub fn timestamp() {
//
// The largest number of milliseconds without leap second we need to generate:
// > d=new Date("9999-12-31T23:59:59"); d.getTime()
// 253_402_318_799_000 ms
// 253_402_318_799_000 ms
// 253_402_318_799 s
//

let megasecond_generator = {
use second <- qcheck.map(qcheck.int_uniform_inclusive(-62_167, 253_402))
use second <- qcheck.map(qcheck.bounded_int(-62_167, 253_402))
second * 1_000_000
}

let second_generator = qcheck.int_uniform_inclusive(-201_438, 318_799)
let second_generator = qcheck.bounded_int(-201_438, 318_799)

use megasecond, second, nanosecond <- qcheck.map3(
g1: megasecond_generator,
g2: second_generator,
g3: qcheck.int_uniform_inclusive(0, 999_999_999),
megasecond_generator,
second_generator,
qcheck.bounded_int(0, 999_999_999),
)
let total_seconds = megasecond + second

Expand All @@ -52,9 +52,9 @@ pub fn rfc3339(
avoid_erlang_errors avoid_erlang_errors: Bool,
) -> qcheck.Generator(String) {
use full_date, t, full_time <- qcheck.map3(
g1: full_date_generator(),
g2: t_generator(),
g3: full_time_generator(with_leap_second, second_fraction_spec),
full_date_generator(),
t_generator(),
full_time_generator(with_leap_second, second_fraction_spec),
)
let date_time = full_date <> t <> full_time

Expand Down Expand Up @@ -140,16 +140,16 @@ fn is_leap_year(year_input: String) -> Bool {
}

fn t_generator() {
qcheck.from_generators([qcheck.return("T"), qcheck.return("t")])
qcheck.from_generators(qcheck.constant("T"), [qcheck.constant("t")])
}

fn full_time_generator(
with_leap_second with_leap_second: Bool,
second_fraction_spec second_fraction_spec: SecondFractionSpec,
) -> qcheck.Generator(String) {
use partial_time, time_offset <- qcheck.map2(
g1: partial_time_generator(with_leap_second, second_fraction_spec),
g2: time_offset_generator(),
partial_time_generator(with_leap_second, second_fraction_spec),
time_offset_generator(),
)
partial_time <> time_offset
}
Expand All @@ -158,7 +158,7 @@ fn partial_time_generator(
with_leap_second with_leap_second: Bool,
second_fraction_spec second_fraction_spec: SecondFractionSpec,
) -> qcheck.Generator(String) {
qcheck.return({
qcheck.constant({
use time_hour <- qcheck.parameter
use time_minute <- qcheck.parameter
use time_second <- qcheck.parameter
Expand All @@ -174,7 +174,7 @@ fn partial_time_generator(
|> qcheck.apply(time_minute_generator())
|> qcheck.apply(time_second_generator(with_leap_second))
|> qcheck.apply(
qcheck.option(time_second_fraction_generator(second_fraction_spec)),
qcheck.option_from(time_second_fraction_generator(second_fraction_spec)),
)
}

Expand All @@ -201,7 +201,7 @@ fn zero_padded_digits_generator(
from min: Int,
to max: Int,
) -> qcheck.Generator(String) {
use n <- qcheck.map(qcheck.int_uniform_inclusive(min, max))
use n <- qcheck.map(qcheck.bounded_int(min, max))
int.to_string(n) |> string.pad_start(to: length, with: "0")
}

Expand All @@ -223,39 +223,39 @@ fn time_second_fraction_generator(
}

fn one_or_more_digits_generator() -> qcheck.Generator(String) {
qcheck.string_non_empty_from(qcheck.char_digit())
qcheck.non_empty_string_from(qcheck.ascii_digit_codepoint())
}

fn digits_generator(
min_count min_count: Int,
max_count max_count: Int,
) -> qcheck.Generator(String) {
qcheck.string_generic(
qcheck.char_digit(),
qcheck.int_uniform_inclusive(min_count, max_count),
qcheck.generic_string(
qcheck.ascii_digit_codepoint(),
qcheck.bounded_int(min_count, max_count),
)
}

fn time_offset_generator() -> qcheck.Generator(String) {
qcheck.from_generators([z_generator(), time_numoffset_generator()])
qcheck.from_generators(z_generator(), [time_numoffset_generator()])
}

fn z_generator() {
qcheck.from_generators([qcheck.return("Z"), qcheck.return("z")])
qcheck.from_generators(qcheck.constant("Z"), [qcheck.constant("z")])
}

fn time_numoffset_generator() -> qcheck.Generator(String) {
use plus_or_minus, time_hour, time_minute <- qcheck.map3(
g1: plus_or_minus_generator(),
g2: time_hour_generator(),
g3: time_minute_generator(),
plus_or_minus_generator(),
time_hour_generator(),
time_minute_generator(),
)

plus_or_minus <> time_hour <> ":" <> time_minute
}

fn plus_or_minus_generator() -> qcheck.Generator(String) {
qcheck.from_generators([qcheck.return("+"), qcheck.return("-")])
qcheck.from_generators(qcheck.constant("+"), [qcheck.constant("-")])
}

fn unwrap_optional_string(optional_string: option.Option(String)) -> String {
Expand Down
Loading

0 comments on commit 9073700

Please sign in to comment.