Skip to content

Commit

Permalink
Merge pull request #1600 from sampersand/swesterman/23-11-07/add-test…
Browse files Browse the repository at this point in the history
…-harness-utils

updated test harness utils
  • Loading branch information
soutaro authored Nov 22, 2023
2 parents d92e90e + 300fe48 commit 854be0b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 25 deletions.
2 changes: 1 addition & 1 deletion test/stdlib/Dir_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions test/stdlib/Exception_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/stdlib/Marshal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/stdlib/Signal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/stdlib/UnboundMethod_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 48 additions & 11 deletions test/stdlib/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 854be0b

Please sign in to comment.