-
Notifications
You must be signed in to change notification settings - Fork 0
/
with_keywords_when_needed_spec.rb
48 lines (39 loc) · 1.46 KB
/
with_keywords_when_needed_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'rspec/support/with_keywords_when_needed'
module RSpec::Support
RSpec.describe "WithKeywordsWhenNeeded" do
describe ".class_exec" do
extend RubyFeatures
let(:klass) do
Class.new do
def self.check_argument(argument)
raise ArgumentError unless argument == 42
end
end
end
def run(klass, *args, &block)
WithKeywordsWhenNeeded.class_exec(klass, *args, &block)
end
it "will run a block without keyword arguments" do
run(klass, 42) { |arg| check_argument(arg) }
end
it "will run a block with a hash without keyword arguments" do
run(klass, "value" => 42) { |arg| check_argument(arg["value"]) }
end
it "will run a block with optional keyword arguments when none are provided", :if => kw_args_supported? do
binding.eval(<<-CODE, __FILE__, __LINE__)
run(klass, 42) { |arg, val: nil| check_argument(arg) }
CODE
end
it "will run a block with optional keyword arguments when they are provided", :if => required_kw_args_supported? do
binding.eval(<<-CODE, __FILE__, __LINE__)
run(klass, val: 42) { |val: nil| check_argument(val) }
CODE
end
it "will run a block with required keyword arguments", :if => required_kw_args_supported? do
binding.eval(<<-CODE, __FILE__, __LINE__)
run(klass, val: 42) { |val:| check_argument(val) }
CODE
end
end
end
end