Skip to content

Commit

Permalink
Merge pull request #1541 from sampersand/swesterman/23-09-22/make-tes…
Browse files Browse the repository at this point in the history
…t-types-basicobject

Remove all non-required methods from testing types
  • Loading branch information
soutaro authored Sep 29, 2023
2 parents 5f992e6 + 15d8a1c commit 63e3640
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 46 deletions.
3 changes: 3 additions & 0 deletions lib/rbs/test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "securerandom"
require "rbs/test/guaranteed"
require "rbs/test/observer"
require "rbs/test/spy"
require "rbs/test/errors"
Expand All @@ -23,6 +24,8 @@ module Test
METHODS = Kernel.instance_method(:methods)

class ArgumentsReturn
include Guaranteed::Inspect

attr_reader :arguments
attr_reader :exit_value
attr_reader :exit_type
Expand Down
31 changes: 31 additions & 0 deletions lib/rbs/test/guaranteed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module RBS
module Test
module Guaranteed
module Inspect
EQUAL = ::BasicObject.instance_method(:equal?)
INSPECT = ::Kernel.instance_method(:inspect)
private_constant :EQUAL, :INSPECT

module_function def guaranteed_inspect(obj)
obj.inspect
rescue NoMethodError => err
raise unless err.name == :inspect && EQUAL.bind_call(obj, err.receiver)
INSPECT.bind_call(obj)
end

def inspect
string = "<#{self.class.name}:"

instance_variables.each_with_index do |variable, index|
string.concat ', ' unless index.zero?
string.concat "#{variable}: #{guaranteed_inspect(variable)}"
end

string.concat '>'
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/rbs/test/type_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def zip_keyword_args(hash, fun)
end

def keyword?(value)
value.is_a?(Hash) && value.keys.all? {|key| key.is_a?(Symbol) }
Hash === value && value.each_key.all?(Symbol)
end

def zip_args(args, fun, &block)
Expand Down Expand Up @@ -310,7 +310,7 @@ def value(val, type)
when Types::Variable
true
when Types::Literal
val == type.literal
type.literal == val
when Types::Union
type.types.any? {|type| value(val, type) }
when Types::Intersection
Expand Down
5 changes: 3 additions & 2 deletions test/stdlib/Pathname_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,9 @@ def test_relative_path_from
Pathname('.'), :relative_path_from, Pathname('.')
assert_send_type '(String) -> Pathname',
Pathname('.'), :relative_path_from, '.'
assert_send_type '(ToStr) -> Pathname',
Pathname('.'), :relative_path_from, ToStr.new('.')

assert_send_type '(_ToStr) -> Pathname',
Pathname('.'), :relative_path_from, ToStr.new('.').__with_object_methods(:is_a?)
end

def test_rename
Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/ThreadQueue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_pop
q.pop(true)
q.pop(false)
q.pop(timeout: 0.2)
q.pop(timeout: ToF.new(0.1))
q.pop(timeout: ToF.new(0.1).__with_object_methods(:==))
end

def test_push
Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/ThreadSizedQueue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ def test_push
q.push(:a, true)
q.push(:a, false)
q.push(:a, timeout: 0.1)
q.push(:a, timeout: ToF.new(0.1))
q.push(:a, timeout: ToF.new(0.1).__with_object_methods(:==))
end
end
6 changes: 3 additions & 3 deletions test/stdlib/Warning_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_aref
refute_send_type "(Symbol) -> bool",
Warning, :[], :unknown_category

refute_send_type "(ToSym) -> bool",
refute_send_type "(_ToSym) -> bool",
Warning, :[], ToSym.new(WARNING_CATEGORIES.first)
end

Expand All @@ -29,7 +29,7 @@ def test_aset
refute_send_type "(Symbol, Rational) -> Rational",
Warning, :[]=, :unknown_category, 1r

refute_send_type "(ToSym, Rational) -> Rational",
refute_send_type "(_ToSym, Rational) -> Rational",
Warning, :[]=, ToSym.new(WARNING_CATEGORIES.first), 1r
end
end
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_warn
assert_send_type "(::String, category: nil) -> nil",
Warning, :warn, 'message', category: nil

refute_send_type "(::String, category: ToSym) -> nil",
refute_send_type "(::String, category: _ToSym) -> nil",
Warning, :warn, 'message', category: ToSym.new(WARNING_CATEGORIES.first)

refute_send_type "(::String, category: ::Symbol) -> nil",
Expand Down
48 changes: 34 additions & 14 deletions test/stdlib/json/JSON_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
require_relative "../test_helper"
require "json"

class JsonToStr
def initialize(value = "")
@value = value
end

def to_str
@value
end
end

class JsonToS
def initialize(value = "")
@value = value
end

def to_s
@value
end
end

class JSONSingletonTest < Test::Unit::TestCase
include TypeAssertions

Expand All @@ -9,26 +29,26 @@ class JSONSingletonTest < Test::Unit::TestCase

def test_aref
assert_send_type "(String) -> 42", JSON, :[], "42"
assert_send_type "(ToStr) -> 42", JSON, :[], ToStr.new("42")
assert_send_type "(_ToStr) -> 42", JSON, :[], JsonToStr.new("42")
assert_send_type "(ToJson) -> String", JSON, :[], ToJson.new
assert_send_type "(ToJson, indent: String) -> String", JSON, :[], ToJson.new, { indent: "\t" }
end

def test_create_id
JSON.create_id = ToS.new("json_class")
assert_send_type "() -> ToS", JSON, :create_id
JSON.create_id = JsonToS.new("json_class")
assert_send_type "() -> _ToS", JSON, :create_id
JSON.create_id = "json_class"
assert_send_type "() -> String", JSON, :create_id
end

def test_create_id_eq
assert_send_type "(ToS) -> ToS", JSON, :create_id=, ToS.new("json_class")
assert_send_type "(_ToS) -> _ToS", JSON, :create_id=, JsonToS.new("json_class")
assert_send_type "(String) -> String", JSON, :create_id=, "json_class"
end

def test_deep_const_get
assert_send_type "(String) -> String", JSON, :deep_const_get, "File::SEPARATOR"
assert_send_type "(ToS) -> String", JSON, :deep_const_get, ToS.new("File::SEPARATOR")
assert_send_type "(_ToS) -> String", JSON, :deep_const_get, JsonToS.new("File::SEPARATOR")
end

def test_dump
Expand Down Expand Up @@ -76,12 +96,12 @@ def test_generator=
def test_iconv
assert_send_type "(Encoding, Encoding, String) -> String", JSON, :iconv, Encoding::UTF_8, Encoding::UTF_16, "".encode(Encoding::UTF_16)
assert_send_type "(String, String, String) -> String", JSON, :iconv, 'UTF-8', 'UTF-16', "".encode(Encoding::UTF_16)
assert_send_type "(ToStr, ToStr, String) -> String", JSON, :iconv, ToStr.new('UTF-8'), ToStr.new('UTF-16'), "".encode(Encoding::UTF_16)
assert_send_type "(_ToStr, _ToStr, String) -> String", JSON, :iconv, JsonToStr.new('UTF-8'), JsonToStr.new('UTF-16'), "".encode(Encoding::UTF_16)
end

def test_load
assert_send_type "(String) -> 42", JSON, :load, "42"
assert_send_type "(ToStr) -> 42", JSON, :load, ToStr.new("42")
assert_send_type "(_ToStr) -> 42", JSON, :load, JsonToStr.new("42")
assert_send_type "(JsonToReadableIO) -> 42", JSON, :load, JsonToReadableIO.new
assert_send_type "(JsonRead) -> 42", JSON, :load, JsonRead.new
assert_send_type "(String, Proc) -> 42", JSON, :load, "42", proc { }
Expand All @@ -98,13 +118,13 @@ def test_load_default_options_eq

def test_parse
assert_send_type "(String) -> 42", JSON, :parse, "42"
assert_send_type "(ToStr) -> 42", JSON, :parse, ToStr.new("42")
assert_send_type "(_ToStr) -> 42", JSON, :parse, JsonToStr.new("42")
assert_send_type "(String, Hash[untyped, untyped]) -> 42", JSON, :parse, "42", { allow_nan: true }
end

def test_parse!
assert_send_type "(String) -> 42", JSON, :parse!, "42"
assert_send_type "(ToStr) -> 42", JSON, :parse!, ToStr.new("42")
assert_send_type "(_ToStr) -> 42", JSON, :parse!, JsonToStr.new("42")
assert_send_type "(String, Hash[untyped, untyped]) -> 42", JSON, :parse!, "42", { allow_nan: true }
end

Expand Down Expand Up @@ -132,7 +152,7 @@ def test_recurse_proc

def test_restore
assert_send_type "(String) -> 42", JSON, :restore, "42"
assert_send_type "(ToStr) -> 42", JSON, :restore, ToStr.new("42")
assert_send_type "(_ToStr) -> 42", JSON, :restore, JsonToStr.new("42")
assert_send_type "(JsonToReadableIO) -> 42", JSON, :restore, JsonToReadableIO.new
assert_send_type "(JsonRead) -> 42", JSON, :restore, JsonRead.new
assert_send_type "(String, Proc) -> 42", JSON, :restore, "42", proc { }
Expand Down Expand Up @@ -192,7 +212,7 @@ def test_generate

def test_load
assert_send_type "(String) -> 42", MyJSON.new, :load, "42"
assert_send_type "(ToStr) -> 42", MyJSON.new, :load, ToStr.new("42")
assert_send_type "(_ToStr) -> 42", MyJSON.new, :load, JsonToStr.new("42")
assert_send_type "(JsonToReadableIO) -> 42", MyJSON.new, :load, JsonToReadableIO.new
assert_send_type "(JsonRead) -> 42", MyJSON.new, :load, JsonRead.new
assert_send_type "(String, Proc) -> 42", MyJSON.new, :load, "42", proc { }
Expand All @@ -201,13 +221,13 @@ def test_load

def test_parse
assert_send_type "(String) -> 42", MyJSON.new, :parse, "42"
assert_send_type "(ToStr) -> 42", MyJSON.new, :parse, ToStr.new("42")
assert_send_type "(_ToStr) -> 42", MyJSON.new, :parse, JsonToStr.new("42")
assert_send_type "(String, Hash[untyped, untyped]) -> 42", MyJSON.new, :parse, "42", { allow_nan: true }
end

def test_parse!
assert_send_type "(String) -> 42", MyJSON.new, :parse!, "42"
assert_send_type "(ToStr) -> 42", MyJSON.new, :parse!, ToStr.new("42")
assert_send_type "(_ToStr) -> 42", MyJSON.new, :parse!, JsonToStr.new("42")
assert_send_type "(String, Hash[untyped, untyped]) -> 42", MyJSON.new, :parse!, "42", { allow_nan: true }
end

Expand All @@ -227,7 +247,7 @@ def test_recurse_proc

def test_restore
assert_send_type "(String) -> 42", MyJSON.new, :restore, "42"
assert_send_type "(ToStr) -> 42", MyJSON.new, :restore, ToStr.new("42")
assert_send_type "(_ToStr) -> 42", MyJSON.new, :restore, JsonToStr.new("42")
assert_send_type "(JsonToReadableIO) -> 42", MyJSON.new, :restore, JsonToReadableIO.new
assert_send_type "(JsonRead) -> 42", MyJSON.new, :restore, JsonRead.new
assert_send_type "(String, Proc) -> 42", MyJSON.new, :restore, "42", proc { }
Expand Down
12 changes: 6 additions & 6 deletions test/stdlib/rubygems/GemVersion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ class GemVersionSingletonTest < Test::Unit::TestCase
def test_correct?
assert_send_type "(String) -> bool",
Gem::Version, :correct?, "1.2.3"
assert_send_type "(ToS) -> bool",
Gem::Version, :correct?, ToS.new
assert_send_type "(_ToS) -> bool",
Gem::Version, :correct?, ToS.new.__with_object_methods(:respond_to?, :nil?)
end

def test_create
assert_send_type "(String) -> Gem::Version",
Gem::Version, :create, "1.2.3"
assert_send_type "(ToS) -> Gem::Version",
Gem::Version, :create, ToS.new("1.2.3")
assert_send_type "(_ToS) -> Gem::Version",
Gem::Version, :create, ToS.new("1.2.3").__with_object_methods(:respond_to?, :hash, :nil?, :is_a?)
assert_send_type "(Gem::Version) -> Gem::Version",
Gem::Version, :create, Gem::Version.new("1.2.3")
end

def test_new
assert_send_type "(String) -> Gem::Version",
Gem::Version, :new, "1.2.3"
assert_send_type "(ToS) -> Gem::Version",
Gem::Version, :new, ToS.new("1.2.3")
assert_send_type "(_ToS) -> Gem::Version",
Gem::Version, :new, ToS.new("1.2.3").__with_object_methods(:respond_to?, :hash, :nil?, :is_a?)
end
end

Expand Down
Loading

0 comments on commit 63e3640

Please sign in to comment.