forked from rails/thor
/
task_spec.rb
83 lines (73 loc) · 2.77 KB
/
task_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe Thor::Task do
def task(options={})
options.each do |key, value|
options[key] = Thor::Option.parse(key, value)
end
@task ||= Thor::Task.new(:can_has, "I can has cheezburger", "I can has cheezburger\nLots and lots of it", "can_has", options)
end
describe "#formatted_usage" do
it "includes namespace within usage" do
Object.stub!(:namespace).and_return("foo")
class << Object
public :namespace
end
Object.stub!(:arguments).and_return([])
Object.stub!(:parent_commands).and_return([])
task(:bar => :required).formatted_usage(Object).should == "foo:can_has --bar=BAR"
end
it "removes default from namespace" do
Object.stub!(:namespace).and_return("default:foo")
class << Object
public :namespace
end
Object.stub!(:arguments).and_return([])
Object.stub!(:parent_commands).and_return([])
task(:bar => :required).formatted_usage(Object).should == ":foo:can_has --bar=BAR"
end
it "injects arguments into usage" do
Object.stub!(:namespace).and_return("foo")
class << Object
public :namespace
end
Object.stub!(:arguments).and_return([ Thor::Argument.new(:bar, nil, true, :string) ])
Object.stub!(:parent_commands).and_return([])
task(:foo => :required).formatted_usage(Object).should == "foo:can_has BAR --foo=FOO"
end
end
describe "#dynamic" do
it "creates a dynamic task with the given name" do
Thor::DynamicTask.new('task').name.should == 'task'
Thor::DynamicTask.new('task').description.should == 'A dynamically-generated task'
Thor::DynamicTask.new('task').usage.should == 'task'
Thor::DynamicTask.new('task').options.should == {}
end
it "does not invoke an existing method" do
mock = mock()
mock.class.should_receive(:handle_no_task_error).with("to_s")
Thor::DynamicTask.new('to_s').run(mock)
end
end
describe "#dup" do
it "dup options hash" do
task = Thor::Task.new("can_has", nil, nil, nil, :foo => true, :bar => :required)
task.dup.options.delete(:foo)
task.options[:foo].should be
end
end
describe "#run" do
it "runs a task by calling a method in the given instance" do
mock = mock()
mock.should_receive(:public_methods).and_return([:can_has])
mock.should_receive(:send).with("can_has", 1, 2, 3)
task.run(mock, [1, 2, 3])
end
it "raises an error if the method to be invoked is private" do
mock = mock()
mock.should_receive(:private_methods).and_return([:can_has])
mock.should_receive(:public_methods).and_return([])
mock.class.should_receive(:handle_no_task_error).with("can_has")
task.run(mock)
end
end
end