Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actions adore keyword arguments #174

Merged
merged 3 commits into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions lib/rake/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,7 @@ def execute(args=nil)
end
application.trace "** Execute #{name}" if application.options.trace
application.enhance_with_matching_rule(name) if @actions.empty?
@actions.each do |act|
case act.arity
when 1
act.call(self)
else
act.call(self, args)
end
end
@actions.each { |act| act.call(self, args) }
end

# Is this task needed?
Expand Down
30 changes: 30 additions & 0 deletions test/test_rake_task_with_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ def test_actions_of_various_arity_are_ok_with_args
assert_equal [:a, :b, :c, :d], notes
end


def test_actions_adore_keywords
# A brutish trick to avoid parsing. Remove it once support for 1.9 and 2.0 is dropped
# https://ci.appveyor.com/project/ruby/rake/build/1.0.301
skip 'Keywords aren\'t a feature in this version' if RUBY_VERSION =~ /^1|^2\.0/
eval <<-RUBY, binding, __FILE__, __LINE__+1
notes = []
t = task :t, [:reqr, :ovrd, :dflt] # required, overridden-optional, default-optional
verify = lambda do |name, expecteds, actuals|
notes << name
assert_equal expecteds.length, actuals.length
expecteds.zip(actuals) { |e, a| assert_equal e, a, "(TEST \#{name})" }
end

t.enhance { |dflt: 'd', **| verify.call :a, ['d'], [dflt] }
t.enhance { |ovrd: '-', **| verify.call :b, ['o'], [ovrd] }
t.enhance { |reqr: , **| verify.call :c, ['r'], [reqr] }

t.enhance { |t2, dflt: 'd', **| verify.call :d, [t,'d'], [t2,dflt] }
t.enhance { |t2, ovrd: 'd', **| verify.call :e, [t,'o'], [t2,ovrd] }
t.enhance { |t2, reqr: , **| verify.call :f, [t,'r'], [t2,reqr] }

t.enhance { |t2, dflt: 'd', reqr:, **| verify.call :g, [t,'d','r'], [t2,dflt,reqr] }
t.enhance { |t2, ovrd: '-', reqr:, **| verify.call :h, [t,'o','r'], [t2,ovrd,reqr] }

t.invoke('r', 'o')
assert_equal [*:a..:h], notes
RUBY
end

def test_arguments_are_passed_to_block
t = task(:t, :a, :b) { |tt, args|
assert_equal({ a: 1, b: 2 }, args.to_hash)
Expand Down