/
command_spec.rb
145 lines (127 loc) · 4.27 KB
/
command_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/../spec_helper'
module Termtter
describe 'Command#initialize' do
it 'requires the name element in the argument hash' do
lambda { Command.new(:nama => 1) }.should raise_error(ArgumentError)
lambda { Command.new(:name => 1) }.should_not raise_error(ArgumentError)
end
it 'does not destroy the argument hash' do
hash = {
:name => 1,
:exec => 3
}
Command.new hash
hash.should eql(hash)
hash[:name].should == 1
hash[:exec].should == 3
hash[:exec_proc].should be_nil
end
end
describe Command do
before do
params = {
:name => 'update',
:aliases => ['u', 'up'],
:exec_proc => lambda {|arg| arg },
:completion_proc => lambda {|command, arg| %w[complete1 complete2] },
:help => ['update,u TEXT', 'test command']
}
@command = Command.new(params)
end
describe '#pattern' do
it 'returns command regex' do
@command.pattern.
should == /^((update|u|up)|(update|u|up)\s+(.*?))\s*$/
end
end
it 'is given name as String or Symbol' do
Command.new(:name => 'foo').name.should == :foo
Command.new(:name => :foo).name.should == :foo
end
it 'returns name' do
@command.name.should == :update
end
it 'returns aliases' do
@command.aliases.should == [:u, :up]
end
it 'returns commands' do
@command.commands.should == [:update, :u, :up]
end
it 'returns help' do
@command.help.should == ['update,u TEXT', 'test command']
end
it 'returns candidates for completion' do
# complement
[
['upd', ['update']],
[' upd', []],
[' upd ', []],
['update a', ['complete1', 'complete2']],
[' update ', []],
['u foo', ['complete1', 'complete2']],
].each do |input, comp|
@command.complement(input).should == comp
end
end
it 'returns empty array as candidates when competition_proc is nil'do
command = Command.new(:name => :foo)
command.complement('foo bar').should == []
end
it 'returns command_info when call method "match?"' do
[
['update', true],
['up', true],
['u', true],
['update ', true],
[' update ', false],
['update foo', true],
[' update foo', false],
[' update foo ', false],
['u foo', true],
['up foo', true],
['upd foo', false],
['upd foo', false],
].each do |input, result|
@command.match?(input).should == result
end
end
it 'calls exec_proc when call method "call"' do
@command.call('foo', 'test', 'foo test').should == 'test'
@command.call('foo', ' test', 'foo test').should == ' test'
@command.call('foo', ' test ', 'foo test ').should == ' test '
@command.call('foo', 'test test', 'foo test test').should == 'test test'
end
it 'raises ArgumentError at call' do
lambda { @command.call('foo', nil, 'foo') }.
should_not raise_error(ArgumentError)
lambda { @command.call('foo', 'foo', 'foo') }.
should_not raise_error(ArgumentError)
lambda { @command.call('foo', false, 'foo') }.
should raise_error(ArgumentError)
lambda { @command.call('foo', Array.new, 'foo') }.
should raise_error(ArgumentError)
end
describe '#alias=' do
it 'wraps aliases=' do
a = :ujihisa
@command.should_receive(:aliases=).with([a])
@command.alias = a
end
end
describe '.split_command_line' do
it 'splits from a command line string to the command name and the arg' do
Command.split_command_line('test foo bar').
should == ['test', 'foo bar']
Command.split_command_line('test foo bar').
should == ['test', 'foo bar']
Command.split_command_line('test foo bar').
should == ['test', 'foo bar']
Command.split_command_line(' test foo bar').
should == ['test', 'foo bar']
Command.split_command_line(' test foo bar ').
should == ['test', 'foo bar']
end
end
end
end