Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/context_dev/internal/type/array_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def self.[](...) = new(...)
# @param other [Object]
#
# @return [Boolean]
def ===(other) = other.is_a?(Array) && other.all?(item_type)
def ===(other) = other.is_a?(Array) && other.all? { (nilable? && _1.nil?) || item_type === _1 }

# @api public
#
Expand Down
2 changes: 2 additions & 0 deletions lib/context_dev/internal/type/hash_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def ===(other)
case [key, val]
in [Symbol | String, ^type]
true
in [Symbol | String, nil] if nilable?
true
else
false
end
Expand Down
27 changes: 27 additions & 0 deletions test/context_dev/internal/type/base_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -725,3 +725,30 @@ def test_meta_retrieval
assert_equal({doc: "dawg"}, ub)
end
end

class ContextDev::Test::NilableCollectionCaseEqualityTest < Minitest::Test
def test_array_of_nilable_case_equality
arr_type = ContextDev::Internal::Type::ArrayOf[String, nil?: true]
assert_operator(arr_type, :===, ["hello", nil])
assert_operator(arr_type, :===, [nil])
assert_operator(arr_type, :===, ["hello", "world"])
refute_operator(arr_type, :===, ["hello", 123])

non_nilable = ContextDev::Internal::Type::ArrayOf[String]
assert_operator(non_nilable, :===, ["hello", "world"])
refute_operator(non_nilable, :===, ["hello", nil])
end

def test_hash_of_nilable_case_equality
hash_type = ContextDev::Internal::Type::HashOf[String, nil?: true]
assert_operator(hash_type, :===, {a: "hello", b: nil})
assert_operator(hash_type, :===, {"a" => "hello", "b" => nil})
assert_operator(hash_type, :===, {a: nil})
assert_operator(hash_type, :===, {a: "hello"})
refute_operator(hash_type, :===, {a: 123})

non_nilable = ContextDev::Internal::Type::HashOf[String]
assert_operator(non_nilable, :===, {a: "hello", b: "world"})
refute_operator(non_nilable, :===, {a: "hello", b: nil})
end
end