Skip to content

Commit

Permalink
Merge pull request #174 from JoshCheek/actions_adore_keywords
Browse files Browse the repository at this point in the history
Actions adore keyword arguments
  • Loading branch information
hsbt authored Dec 2, 2016
2 parents 5985610 + 7d664a0 commit 4dc6630
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
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

0 comments on commit 4dc6630

Please # to comment.