From 300fe48fc8c93808e93e1737b0e54e99298ded1c Mon Sep 17 00:00:00 2001 From: SamW Date: Tue, 7 Nov 2023 13:58:54 -0800 Subject: [PATCH] updated test harness utils --- test/stdlib/Dir_test.rb | 2 +- test/stdlib/Exception_test.rb | 12 +++---- test/stdlib/Marshal_test.rb | 6 ++-- test/stdlib/Signal_test.rb | 4 +-- test/stdlib/UnboundMethod_test.rb | 4 +-- test/stdlib/test_helper.rb | 59 +++++++++++++++++++++++++------ 6 files changed, 62 insertions(+), 25 deletions(-) diff --git a/test/stdlib/Dir_test.rb b/test/stdlib/Dir_test.rb index 5eae341d1..6c956ac8c 100644 --- a/test/stdlib/Dir_test.rb +++ b/test/stdlib/Dir_test.rb @@ -97,7 +97,7 @@ def test_entries end def test_exist? - with_path.chain(with_io).each do |path| + with_path.and(with_io) do |path| assert_send_type "(::path | ::io) -> bool", Dir, :exist?, path end diff --git a/test/stdlib/Exception_test.rb b/test/stdlib/Exception_test.rb index 89e05c510..2507e6beb 100644 --- a/test/stdlib/Exception_test.rb +++ b/test/stdlib/Exception_test.rb @@ -16,7 +16,7 @@ def test_exception assert_send_type '() -> ExceptionSingletonTest::MyException', MyException, :exception - with_string.chain([1r]).each do |message| + with_string.and(1r) do |message| assert_send_type '(string | _ToS) -> ExceptionSingletonTest::MyException', MyException, :exception, message end @@ -83,7 +83,7 @@ def test_detailed_message assert_send_type '() -> String', INSTANCE, :detailed_message - [true, false, nil].each do |highlight| + with_bool.and_nil do |highlight| assert_send_type '(highlight: bool?) -> String', INSTANCE, :detailed_message, highlight: highlight assert_send_type '(highlight: bool?, **untyped) -> String', @@ -99,14 +99,14 @@ def test_exception assert_send_type '(self) -> self', INSTANCE, :exception, INSTANCE - with_string.chain([Object.new, 1r]).each do |message| + with_string.and(Object.new, 1r) do |message| assert_send_type '(string | _ToS) -> ExceptionInstanceTest::MyException', MyException.new, :exception, message end end def test_initialize - with_string.chain([Object.new, 1r]).each do |message| + with_string.and(Object.new, 1r) do |message| assert_send_type '(string | _ToS) -> self', Exception.allocate, :initialize, message end @@ -144,11 +144,11 @@ def test_full_message assert_send_type '() -> String', INSTANCE, :full_message - [true, false, nil].each do |highlight| + with_bool.and_nil do |highlight| assert_send_type '(highlight: bool?) -> String', INSTANCE, :full_message, highlight: highlight - with_string('top').chain(with_string('bottom'), [:top, :bottom, nil]).each do |order| + with_string('top').and(nil, with_string('bottom'), :top, :bottom) do |order| assert_send_type '(highlight: bool?, order: (:top | :bottom | string)?) -> String', INSTANCE, :full_message, highlight: highlight, order: order end diff --git a/test/stdlib/Marshal_test.rb b/test/stdlib/Marshal_test.rb index bfd626f10..fbb6fe01c 100644 --- a/test/stdlib/Marshal_test.rb +++ b/test/stdlib/Marshal_test.rb @@ -26,7 +26,7 @@ def test_dump assert_send_type '(untyped, Writer) -> Writer', Marshal, :dump, obj, writer - with_int.chain([nil]).each do |limit| + with_int.and_nil do |limit| assert_send_type '(untyped, Writer, int?) -> Writer', Marshal, :dump, obj, writer, limit end @@ -69,7 +69,7 @@ def result_proc.call(loaded) 1r end Marshal, meth, source, result_proc source.reset! - [nil, :yep, true, "hello"].each do |freeze| + with_boolish do |freeze| assert_send_type '(string | Marshal::_Source, freeze: boolish) -> untyped', Marshal, meth, source, freeze: freeze source.reset! @@ -102,7 +102,7 @@ def test_dump assert_send_type '(untyped, Writer) -> Writer', Marshal, :dump, obj, writer - with_int.chain([nil]).each do |limit| + with_int.and_nil do |limit| assert_send_type '(untyped, Writer, int?) -> Writer', Marshal, :dump, obj, writer, limit end diff --git a/test/stdlib/Signal_test.rb b/test/stdlib/Signal_test.rb index e803c27ed..054c84ba7 100644 --- a/test/stdlib/Signal_test.rb +++ b/test/stdlib/Signal_test.rb @@ -25,11 +25,11 @@ def test_signame def test_trap old_usr2 = trap(:USR2, nil) - with_interned(:USR2).chain([Signal.list['USR2']]).each do |signal| + with_interned(:USR2).and(Signal.list['USR2']) do |signal| assert_send_type '(Integer | ::interned) { (Integer) -> void } -> Signal::trap_command', Signal, :trap, signal do |n| end - with_string('').chain([true, false, nil, Class.new{def call(x)end}.new]).each do |command| + with_string('').and(with_bool, nil, Class.new { def call(x) end }.new) do |command| assert_send_type '(Integer | ::interned, Signal::trap_command) -> Signal::trap_command', Signal, :trap, signal, command end diff --git a/test/stdlib/UnboundMethod_test.rb b/test/stdlib/UnboundMethod_test.rb index 1b137584c..4b2ad2c2b 100644 --- a/test/stdlib/UnboundMethod_test.rb +++ b/test/stdlib/UnboundMethod_test.rb @@ -17,14 +17,14 @@ def no_kwargs(**nil) end end def test_eq - [UMETH, proc{}, Object.new, nil].each do |other| + with_untyped.and(UMETH) do |other| assert_send_type '(untyped) -> bool', UMETH, :==, other end end def test_eql? - [UMETH, proc{}, Object.new, nil].each do |other| + with_untyped.and(UMETH) do |other| assert_send_type '(untyped) -> bool', UMETH, :eql?, other end diff --git a/test/stdlib/test_helper.rb b/test/stdlib/test_helper.rb index 0851d5fbe..42d2acb9e 100644 --- a/test/stdlib/test_helper.rb +++ b/test/stdlib/test_helper.rb @@ -168,66 +168,103 @@ def if_ruby31(&block) end module WithAliases + class WithEnum + include Enumerable + + def initialize(enum) = @enum = enum + + def each(&block) = @enum.each(&block) + + def and_nil(&block) + self.and(nil, &block) + end + + def and(*args, &block) + return WithEnum.new to_enum(__method__, args) unless block_given? + each(&block) + args.each do |arg| + if WithEnum === arg + arg.each(&block) + else + block.call(arg) + end + end + end + end + def with_int(value = 3) - return to_enum(__method__, value) unless block_given? + return WithEnum.new to_enum(__method__, value) unless block_given? yield value yield ToInt.new(value) end def with_float(value = 0.1) - return to_enum(__method__, value) unless block_given? + return WithEnum.new to_enum(__method__, value) unless block_given? yield value yield ToF.new(value) end - def with_string(value = "") - return to_enum(__method__, value) unless block_given? + def with_string(value = '') + return WithEnum.new to_enum(__method__, value) unless block_given? yield value yield ToStr.new(value) end def with_array(*elements) - return to_enum(__method__, *elements) unless block_given? + return WithEnum.new to_enum(__method__, *elements) unless block_given? yield elements yield ToArray.new(*elements) end def with_hash(hash = {}) - return to_enum(__method__, hash) unless block_given? + return WithEnum.new to_enum(__method__, hash) unless block_given? yield hash yield ToHash.new(hash) end def with_io(io = $stdout) - return to_enum(__method__, io) unless block_given? + return WithEnum.new to_enum(__method__, io) unless block_given? yield io yield ToIO.new(io) end def with_path(path = "/tmp/foo.txt", &block) - return to_enum(__method__, path) unless block_given? + return WithEnum.new to_enum(__method__, path) unless block_given? with_string(path, &block) block.call ToPath.new(path) end def with_encoding(encoding = Encoding::UTF_8, &block) - return to_enum(__method__, encoding) unless block_given? + return WithEnum.new to_enum(__method__, encoding) unless block_given? block.call encoding with_string(encoding.to_s, &block) end def with_interned(value = :&, &block) - return to_enum(__method__, value) unless block_given? + return WithEnum.new to_enum(__method__, value) unless block_given? with_string(value.to_s, &block) block.call value.to_sym end -end + def with_bool + return WithEnum.new to_enum(__method__) unless block_given? + yield true + yield false + end + + def with_boolish(&block) + return WithEnum.new to_enum(__method__) unless block_given? + with_bool(&block) + [nil, 1, Object.new, BlankSlate.new, "hello, world!"].each(&block) + end + + alias with_untyped with_boolish +end module TypeAssertions module ClassMethods attr_reader :target