|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class RBS::SingletonTypeTest < Test::Unit::TestCase |
| 4 | + include TestHelper |
| 5 | + |
| 6 | + Parser = RBS::Parser |
| 7 | + Buffer = RBS::Buffer |
| 8 | + Types = RBS::Types |
| 9 | + TypeName = RBS::TypeName |
| 10 | + Namespace = RBS::Namespace |
| 11 | + |
| 12 | + def test_singleton_type_with_arguments |
| 13 | + Parser.parse_type("singleton(Array)[String]").yield_self do |type| |
| 14 | + assert_instance_of Types::ClassSingleton, type |
| 15 | + assert_equal TypeName.new(namespace: Namespace.empty, name: :Array), type.name |
| 16 | + assert_equal 1, type.args.size |
| 17 | + assert_instance_of Types::ClassInstance, type.args[0] |
| 18 | + assert_equal TypeName.new(namespace: Namespace.empty, name: :String), type.args[0].name |
| 19 | + assert_equal "singleton(Array)[String]", type.location.source |
| 20 | + end |
| 21 | + |
| 22 | + Parser.parse_type("singleton(Hash)[Symbol, Integer]").yield_self do |type| |
| 23 | + assert_instance_of Types::ClassSingleton, type |
| 24 | + assert_equal TypeName.new(namespace: Namespace.empty, name: :Hash), type.name |
| 25 | + assert_equal 2, type.args.size |
| 26 | + assert_instance_of Types::ClassInstance, type.args[0] |
| 27 | + assert_instance_of Types::ClassInstance, type.args[1] |
| 28 | + assert_equal TypeName.new(namespace: Namespace.empty, name: :Symbol), type.args[0].name |
| 29 | + assert_equal TypeName.new(namespace: Namespace.empty, name: :Integer), type.args[1].name |
| 30 | + assert_equal "singleton(Hash)[Symbol, Integer]", type.location.source |
| 31 | + end |
| 32 | + |
| 33 | + Parser.parse_type("singleton(::Foo::Bar)[Baz]").yield_self do |type| |
| 34 | + assert_instance_of Types::ClassSingleton, type |
| 35 | + assert_equal TypeName.new(namespace: Namespace.parse("::Foo"), name: :Bar), type.name |
| 36 | + assert_equal 1, type.args.size |
| 37 | + assert_instance_of Types::ClassInstance, type.args[0] |
| 38 | + assert_equal TypeName.new(namespace: Namespace.empty, name: :Baz), type.args[0].name |
| 39 | + assert_equal "singleton(::Foo::Bar)[Baz]", type.location.source |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + def test_singleton_type_equality |
| 44 | + type1 = parse_type("singleton(Array)[String]") |
| 45 | + type2 = parse_type("singleton(Array)[String]") |
| 46 | + type3 = parse_type("singleton(Array)[Integer]") |
| 47 | + type4 = parse_type("singleton(Hash)[String]") |
| 48 | + |
| 49 | + assert_equal type1, type2 |
| 50 | + refute_equal type1, type3 |
| 51 | + refute_equal type1, type4 |
| 52 | + end |
| 53 | + |
| 54 | + def test_singleton_type_hash |
| 55 | + type1 = parse_type("singleton(Array)[String]") |
| 56 | + type2 = parse_type("singleton(Array)[String]") |
| 57 | + type3 = parse_type("singleton(Array)[Integer]") |
| 58 | + |
| 59 | + assert_equal type1.hash, type2.hash |
| 60 | + refute_equal type1.hash, type3.hash |
| 61 | + end |
| 62 | + |
| 63 | + def test_singleton_type_sub |
| 64 | + type = parse_type("singleton(Array)[T]", variables: [:T]) |
| 65 | + subst = RBS::Substitution.build([:T], [parse_type("String")]) |
| 66 | + |
| 67 | + result = type.sub(subst) |
| 68 | + assert_instance_of Types::ClassSingleton, result |
| 69 | + assert_equal TypeName.new(namespace: Namespace.empty, name: :Array), result.name |
| 70 | + assert_equal 1, result.args.size |
| 71 | + assert_instance_of Types::ClassInstance, result.args[0] |
| 72 | + assert_equal TypeName.new(namespace: Namespace.empty, name: :String), result.args[0].name |
| 73 | + end |
| 74 | + |
| 75 | + def test_singleton_type_map_type_name |
| 76 | + type = parse_type("singleton(Array)[String]") |
| 77 | + |
| 78 | + mapped = type.map_type_name do |name, _, _| |
| 79 | + TypeName.new(namespace: Namespace.empty, name: :List) |
| 80 | + end |
| 81 | + |
| 82 | + assert_instance_of Types::ClassSingleton, mapped |
| 83 | + assert_equal TypeName.new(namespace: Namespace.empty, name: :List), mapped.name |
| 84 | + assert_equal 1, mapped.args.size |
| 85 | + assert_instance_of Types::ClassInstance, mapped.args[0] |
| 86 | + assert_equal TypeName.new(namespace: Namespace.empty, name: :List), mapped.args[0].name |
| 87 | + end |
| 88 | +end |
0 commit comments