Skip to content

Commit

Permalink
test: Prove booleans can be serialized and deserialized
Browse files Browse the repository at this point in the history
  • Loading branch information
maxveldink committed Mar 13, 2024
1 parent acd220f commit 4057969
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/typed/coercion/boolean_coercer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def coerce(field:, value:)
else
Failure.new(CoercionError.new)
end
rescue TypeError => e
rescue TypeError
Failure.new(CoercionError.new("Field type must be a T::Boolean."))
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/typed/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def validate(value)

sig { params(value: Value).returns(T::Boolean) }
def works_with?(value)
value.class == type || T.cast(type, T::Types::Base).valid?(value)
value.class == type || T.cast(type, T::Types::Base).valid?(value) # standard:disable Style/ClassEqualityComparison
rescue TypeError
false
end
Expand Down
11 changes: 11 additions & 0 deletions test/support/structs/city.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# typed: true

class City < T::Struct
include ActsAsComparable

const :name, String
const :capital, T::Boolean
end

NEW_YORK_CITY = City.new(name: "New York", capital: false)
DC_CITY = City.new(name: "DC", capital: true)
10 changes: 10 additions & 0 deletions test/support/structs/country.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# typed: true

require_relative "city"

class Country < T::Struct
const :name, String
const :cities, T::Array[City]
end

US_COUNTRY = Country.new(name: "US", cities: [NEW_YORK_CITY, DC_CITY])
14 changes: 14 additions & 0 deletions test/typed/hash_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def test_it_can_deep_serialize
assert_payload({name: "Alex", age: 31, ruby_rank: "pretty", job: {title: "Software Developer", salary: 1_000_000_00}}, result)
end

def test_with_boolean_it_can_serialize
result = Typed::HashSerializer.new(schema: Typed::Schema.from_struct(City)).serialize(NEW_YORK_CITY)

assert_success(result)
assert_payload({name: "New York", capital: false}, result)
end

def test_when_struct_given_is_not_of_target_type_returns_failure
result = @serializer.serialize(Job.new(title: "Testing", salary: 90_00))

Expand All @@ -53,6 +60,13 @@ def test_it_can_simple_deserialize_from_string_keys
assert_payload(MAX_PERSON, result)
end

def test_with_boolean_it_can_deserialize
result = Typed::HashSerializer.new(schema: Typed::Schema.from_struct(City)).deserialize({name: "New York", capital: false})

assert_success(result)
assert_payload(NEW_YORK_CITY, result)
end

def test_it_can_deserialize_with_nested_object
result = @serializer.deserialize({name: "Alex", age: 31, ruby_rank: RubyRank::Brilliant, job: {title: "Software Developer", salary: 1_000_000_00}})

Expand Down
14 changes: 14 additions & 0 deletions test/typed/json_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def test_it_can_serialize_with_nested_struct
assert_payload('{"name":"Alex","age":31,"ruby_rank":"pretty","job":{"title":"Software Developer","salary":100000000}}', result)
end

def test_with_boolean_it_can_serialize
result = Typed::JSONSerializer.new(schema: Typed::Schema.from_struct(City)).serialize(NEW_YORK_CITY)

assert_success(result)
assert_payload('{"name":"New York","capital":false}', result)
end

# Deserialize Tests

def test_it_can_simple_deserialize
Expand All @@ -32,6 +39,13 @@ def test_it_can_simple_deserialize
assert_payload(MAX_PERSON, result)
end

def test_with_boolean_it_can_deserialize
result = Typed::JSONSerializer.new(schema: Typed::Schema.from_struct(City)).deserialize('{"name":"New York","capital":false}')

assert_success(result)
assert_payload(NEW_YORK_CITY, result)
end

def test_it_can_deserialize_with_nested_object
result = @serializer.deserialize('{"name":"Alex","age":31,"ruby_rank":"pretty","job":{"title":"Software Developer","salary":100000000}}')

Expand Down

0 comments on commit 4057969

Please sign in to comment.