Class: RBS::Test::TypeCheck
- Inherits:
-
Object
- Object
- RBS::Test::TypeCheck
- Defined in:
- lib/rbs/test/type_check.rb
Constant Summary collapse
- DEFAULT_SAMPLE_SIZE =
100
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#sample_size ⇒ Object
readonly
Returns the value of attribute sample_size.
-
#self_class ⇒ Object
readonly
Returns the value of attribute self_class.
Instance Method Summary collapse
- #args(method_name, method_type, fun, call, errors, type_error:, argument_error:) ⇒ Object
-
#initialize(self_class:, builder:, sample_size:) ⇒ TypeCheck
constructor
A new instance of TypeCheck.
- #keyword?(value) ⇒ Boolean
- #method_call(method_name, method_type, call, errors:) ⇒ Object
- #overloaded_call(method, method_name, call, errors:) ⇒ Object
- #return(method_name, method_type, fun, call, errors, return_error:) ⇒ Object
- #sample(array) ⇒ Object
- #value(val, type) ⇒ Object
- #zip_args(args, fun, &block) ⇒ Object
- #zip_keyword_args(hash, fun) ⇒ Object
Constructor Details
#initialize(self_class:, builder:, sample_size:) ⇒ TypeCheck
Returns a new instance of TypeCheck.
10 11 12 13 14 |
# File 'lib/rbs/test/type_check.rb', line 10 def initialize(self_class:, builder:, sample_size:) @self_class = self_class @builder = builder @sample_size = sample_size end |
Instance Attribute Details
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
5 6 7 |
# File 'lib/rbs/test/type_check.rb', line 5 def builder @builder end |
#sample_size ⇒ Object (readonly)
Returns the value of attribute sample_size.
6 7 8 |
# File 'lib/rbs/test/type_check.rb', line 6 def sample_size @sample_size end |
#self_class ⇒ Object (readonly)
Returns the value of attribute self_class.
4 5 6 |
# File 'lib/rbs/test/type_check.rb', line 4 def self_class @self_class end |
Instance Method Details
#args(method_name, method_type, fun, call, errors, type_error:, argument_error:) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rbs/test/type_check.rb', line 68 def args(method_name, method_type, fun, call, errors, type_error:, argument_error:) test = zip_args(call.arguments, fun) do |val, param| unless self.value(val, param.type) errors << type_error.new(klass: self_class, method_name: method_name, method_type: method_type, param: param, value: val) end end unless test errors << argument_error.new(klass: self_class, method_name: method_name, method_type: method_type) end end |
#keyword?(value) ⇒ Boolean
127 128 129 |
# File 'lib/rbs/test/type_check.rb', line 127 def keyword?(value) value.is_a?(Hash) && value.keys.all? {|key| key.is_a?(Symbol) } end |
#method_call(method_name, method_type, call, errors:) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rbs/test/type_check.rb', line 40 def method_call(method_name, method_type, call, errors:) args(method_name, method_type, method_type.type, call.method_call, errors, type_error: Errors::ArgumentTypeError, argument_error: Errors::ArgumentError) self.return(method_name, method_type, method_type.type, call.method_call, errors, return_error: Errors::ReturnTypeError) if method_type.block case when !call.block_calls.empty? call.block_calls.each do |block_call| args(method_name, method_type, method_type.block.type, block_call, errors, type_error: Errors::BlockArgumentTypeError, argument_error: Errors::BlockArgumentError) self.return(method_name, method_type, method_type.block.type, block_call, errors, return_error: Errors::BlockReturnTypeError) end when !call.block_given # Block is not given if method_type.block.required errors << Errors::MissingBlockError.new(klass: self_class, method_name: method_name, method_type: method_type) end else # Block is given, but not yielded end else if call.block_given errors << Errors::UnexpectedBlockError.new(klass: self_class, method_name: method_name, method_type: method_type) end end errors end |
#overloaded_call(method, method_name, call, errors:) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rbs/test/type_check.rb', line 16 def overloaded_call(method, method_name, call, errors:) es = method.method_types.map do |method_type| es = method_call(method_name, method_type, call, errors: []) if es.empty? return errors else es end end if es.size == 1 errors.push(*es[0]) else errors << Errors::UnresolvedOverloadingError.new( klass: self_class, method_name: method_name, method_types: method.method_types ) end errors end |
#return(method_name, method_type, fun, call, errors, return_error:) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rbs/test/type_check.rb', line 86 def return(method_name, method_type, fun, call, errors, return_error:) if call.return? unless value(call.return_value, fun.return_type) errors << return_error.new(klass: self_class, method_name: method_name, method_type: method_type, type: fun.return_type, value: call.return_value) end end end |
#sample(array) ⇒ Object
182 183 184 |
# File 'lib/rbs/test/type_check.rb', line 182 def sample(array) sample_size && (array.size > sample_size) ? array.sample(sample_size) : array end |
#value(val, type) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/rbs/test/type_check.rb', line 186 def value(val, type) case type when Types::Bases::Any true when Types::Bases::Bool true when Types::Bases::Top true when Types::Bases::Bottom false when Types::Bases::Void true when Types::Bases::Self Test.call(val, IS_AP, self_class) when Types::Bases::Nil Test.call(val, IS_AP, ::NilClass) when Types::Bases::Class Test.call(val, IS_AP, Class) when Types::Bases::Instance Test.call(val, IS_AP, self_class) when Types::ClassInstance klass = Object.const_get(type.name.to_s) case when klass == ::Array Test.call(val, IS_AP, klass) && sample(val).yield_self do |val| val.all? {|v| value(v, type.args[0]) } end when klass == ::Hash Test.call(val, IS_AP, klass) && sample(val.keys).yield_self do |keys| values = val.values_at(*keys) keys.all? {|key| value(key, type.args[0]) } && values.all? {|v| value(v, type.args[1]) } end when klass == ::Range Test.call(val, IS_AP, klass) && value(val.begin, type.args[0]) && value(val.end, type.args[0]) when klass == ::Enumerator if Test.call(val, IS_AP, klass) case val.size when Float::INFINITY values = [] ret = self val.lazy.take(10).each do |*args| values << args nil end else values = [] ret = val.each do |*args| values << args nil end end sample(values).all? do |v| if v.size == 1 # Only one block argument. value(v[0], type.args[0]) || value(v, type.args[0]) else value(v, type.args[0]) end end && if ret.equal?(self) type.args[1].is_a?(Types::Bases::Bottom) else value(ret, type.args[1]) end end else Test.call(val, IS_AP, klass) end when Types::ClassSingleton klass = Object.const_get(type.name.to_s) val == klass when Types::Interface methods = Set.new(Test.call(val, METHODS)) if (definition = builder.build_interface(type.name)) definition.methods.each_key.all? do |method_name| methods.member?(method_name) end end when Types::Variable true when Types::Literal val == type.literal when Types::Union type.types.any? {|type| value(val, type) } when Types::Intersection type.types.all? {|type| value(val, type) } when Types::Optional Test.call(val, IS_AP, ::NilClass) || value(val, type.type) when Types::Alias value(val, builder.(type.name)) when Types::Tuple Test.call(val, IS_AP, ::Array) && type.types.map.with_index {|ty, index| value(val[index], ty) }.all? when Types::Record Test::call(val, IS_AP, ::Hash) when Types::Proc Test::call(val, IS_AP, ::Proc) else false end end |
#zip_args(args, fun, &block) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rbs/test/type_check.rb', line 131 def zip_args(args, fun, &block) case when args.empty? if fun.required_positionals.empty? && fun.trailing_positionals.empty? && fun.required_keywords.empty? true else false end when !fun.required_positionals.empty? yield_self do param, fun_ = fun.drop_head yield(args.first, param) zip_args(args.drop(1), fun_, &block) end when fun.has_keyword? yield_self do hash = args.last if keyword?(hash) zip_keyword_args(hash, fun, &block) && zip_args(args.take(args.size - 1), fun.update(required_keywords: {}, optional_keywords: {}, rest_keywords: nil), &block) else fun.required_keywords.empty? && zip_args(args, fun.update(required_keywords: {}, optional_keywords: {}, rest_keywords: nil), &block) end end when !fun.trailing_positionals.empty? yield_self do param, fun_ = fun.drop_tail yield(args.last, param) zip_args(args.take(args.size - 1), fun_, &block) end when !fun.optional_positionals.empty? yield_self do param, fun_ = fun.drop_head yield(args.first, param) zip_args(args.drop(1), fun_, &block) end when fun.rest_positionals yield_self do yield(args.first, fun.rest_positionals) zip_args(args.drop(1), fun, &block) end else false end end |
#zip_keyword_args(hash, fun) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rbs/test/type_check.rb', line 98 def zip_keyword_args(hash, fun) fun.required_keywords.each do |name, param| if hash.key?(name) yield(hash[name], param) else return false end end fun.optional_keywords.each do |name, param| if hash.key?(name) yield(hash[name], param) end end hash.each do |name, value| next if fun.required_keywords.key?(name) next if fun.optional_keywords.key?(name) if fun.rest_keywords yield value, fun.rest_keywords else return false end end true end |