Skip to content

Commit

Permalink
test: Add tests for JSONSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
maxveldink committed Mar 5, 2024
1 parent 6a79889 commit 706c297
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions test/typed/json_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@ def test_it_can_simple_serialize
assert_equal('{"name":"Max","age":29}', @serializer.serialize(max))
end

def test_it_can_serialize_with_nested_struct
hank = Person.new(name: "Hank", age: 38, job: Job.new(title: "Software Developer", salary: 90_000_00))

assert_equal('{"name":"Hank","age":38,"job":{"title":"Software Developer","salary":9000000}}', @serializer.serialize(hank))
end

# Deserialize Tests

def test_it_can_simple_deserialize
max_json = '{"name": "Max", "age": 29}'
hank_json = '{"name":"Hank","age":38,"job":{"title":"Software Developer","salary":9000000}}'

result = @serializer.deserialize(max_json)
result = @serializer.deserialize(hank_json)

assert_success(result)
assert_payload(Person.new(name: "Hank", age: 38, job: Job.new(title: "Software Developer", salary: 90_000_00)), result)
end

def test_it_can_deserialize_with_nested_object
hank_json = '{"name":"Hank","age":38,"job":{"title":"Software Developer","salary":9000000}}'

result = @serializer.deserialize(hank_json)

assert_success(result)
assert_payload(Person.new(name: "Max", age: 29), result)
assert_payload(Person.new(name: "Hank", age: 38, job: Job.new(title: "Software Developer", salary: 90_000_00)), result)
end

def test_it_reports_on_parse_errors_on_deserialize
Expand Down

0 comments on commit 706c297

Please sign in to comment.