Skip to content

Commit

Permalink
Replace Example class with pattern match
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Mar 20, 2024
1 parent 255554a commit 0cc073e
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions test/many_data/test_snapshots.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,44 @@

# https://github.com/kachick/ruby-ulid/issues/89
class TestSnapshots < Test::Unit::TestCase
Example = Data.define(:integer, :timestamp, :randomness, :to_time, :inspect, :uuidish, :uuidv4, :octets)

toml = PerfectTOML.load_file("#{__dir__}/fixtures/snapshots_2024-01-10_07-59.toml", symbolize_names: true)
EXAMPLES = toml.to_h do |id, table|
encoded = id.id2name
[encoded, [encoded, Example.new(**table)]]
[encoded, [encoded, table]]
end
raise 'looks like misloading' unless EXAMPLES.size > 1000

def assert_example(ulid, example)
assert_equal(example.integer, ulid.to_i)
assert_equal(example.inspect, ulid.inspect)
assert_equal(example.timestamp, ulid.timestamp)
assert_equal(example.randomness, ulid.randomness)
assert_equal(example.uuidish, ulid.to_uuidish)
assert_equal(example.uuidv4, ulid.to_uuidv4(force: true))
assert_equal(example.to_time, ulid.to_time)
assert_equal(example.octets, ulid.octets)
case example
in { integer:, timestamp:, randomness:, to_time:, inspect:, uuidish:, uuidv4:, octets: }
assert_equal(integer, ulid.to_i)
assert_equal(inspect, ulid.inspect)
assert_equal(timestamp, ulid.timestamp)
assert_equal(randomness, ulid.randomness)
assert_equal(uuidish, ulid.to_uuidish)
assert_equal(uuidv4, ulid.to_uuidv4(force: true))
assert_equal(to_time, ulid.to_time)
assert_equal(octets, ulid.octets)
else
raise(ArgumentError, 'given example is unknown format')
end
end

data(EXAMPLES)
def test_decoders(ee)
encoded, example = *ee
ulid_parsed = ULID.parse(encoded)
ulid_from_integer = ULID.from_integer(example.integer)
ulid_from_uuidv4 = ULID.from_uuidish(ulid_parsed.to_uuidish)

case example
in { integer:, uuidish: }
ulid_from_integer = ULID.from_integer(integer)
ulid_from_uuidish = ULID.from_uuidish(uuidish)
else
raise(ArgumentError, 'given example is unknown format')
end

assert_equal(ulid_parsed, ulid_from_integer)
assert_equal(ulid_parsed, ulid_from_uuidv4)
assert_equal(ulid_parsed, ulid_from_uuidish)

assert_equal(encoded, ulid_parsed.to_s)
assert do
Expand Down

0 comments on commit 0cc073e

Please sign in to comment.