tty 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,227 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Shell::Question, '#ask' do
6
+
7
+ let(:input) { StringIO.new }
8
+ let(:output) { StringIO.new }
9
+ let(:shell) { TTY::Shell.new(input, output) }
10
+
11
+ context 'with default' do
12
+ it 'uses default value' do
13
+ name = 'Anonymous'
14
+ q = shell.ask("What is your name?").default(name)
15
+ answer = q.read
16
+ expect(answer).to eql name
17
+ end
18
+
19
+ it 'uses default value in block' do
20
+ name = 'Anonymous'
21
+ q = shell.ask "What is your name?" do
22
+ default name
23
+ end
24
+ answer = q.read
25
+ expect(answer).to eql name
26
+ end
27
+ end
28
+
29
+ context 'with argument' do
30
+ it 'requires value to be present with helper' do
31
+ input << ''
32
+ input.rewind
33
+ q = shell.ask("What is your name?").argument(:required)
34
+ expect { q.read }.to raise_error(ArgumentError)
35
+ end
36
+
37
+ it 'requires value to be present with option' do
38
+ input << ''
39
+ input.rewind
40
+ q = shell.ask("What is your name?", :required => true)
41
+ expect { q.read }.to raise_error(ArgumentError)
42
+ end
43
+ end
44
+
45
+ context 'with validation' do
46
+ it 'fails to validate input' do
47
+ input << 'piotrmurach'
48
+ input.rewind
49
+ q = shell.ask("What is your username?").validate(/^[^\.]+\.[^\.]+/)
50
+ expect { q.read_string }.to raise_error(ArgumentError)
51
+ end
52
+
53
+ it 'validates input with regex' do
54
+ input << 'piotr.murach'
55
+ input.rewind
56
+ q = shell.ask("What is your username?").validate(/^[^\.]+\.[^\.]+/)
57
+ expect(q.read_string).to eql 'piotr.murach'
58
+ end
59
+
60
+ it 'validates input in block' do
61
+ input << 'piotr.murach'
62
+ input.rewind
63
+ q = shell.ask("What is your username?").validate { |arg| arg =~ /^[^\.]+\.[^\.]+/ }
64
+ expect(q.read_string).to eql 'piotr.murach'
65
+ end
66
+ end
67
+
68
+ context 'with valid choice' do
69
+ let(:cards) { %w[ club diamond spade heart ] }
70
+
71
+ it 'reads valid optios with helper' do
72
+ input << 'club'
73
+ input.rewind
74
+ q = shell.ask("What is your card suit sir?").valid(cards)
75
+ expect(q.read_choice).to eql 'club'
76
+ end
77
+
78
+ it 'reads valid options with option hash' do
79
+ input << 'club'
80
+ input.rewind
81
+ q = shell.ask("What is your card suit sir?", :valid => cards)
82
+ expect(q.read_choice).to eql 'club'
83
+ end
84
+
85
+ it 'reads invalid option' do
86
+ input << 'clover'
87
+ input.rewind
88
+ q = shell.ask("What is your card suit sir?").valid(cards)
89
+ expect { q.read_choice }.to raise_error(TTY::InvalidArgument)
90
+ end
91
+
92
+ it 'needs argument' do
93
+ input << ''
94
+ input.rewind
95
+ q = shell.ask("What is your card suit sir?").valid(cards)
96
+ expect { q.read_choice }.to raise_error(TTY::ArgumentRequired)
97
+ end
98
+
99
+ it 'reads with default' do
100
+ input << ''
101
+ input.rewind
102
+ q = shell.ask("What is your card suit sir?").valid(cards).default('club')
103
+ expect(q.read_choice).to eql 'club'
104
+ end
105
+ end
106
+
107
+ context 'with modification' do
108
+ it 'preserves answer for unkown modification' do
109
+ input << 'piotr'
110
+ input.rewind
111
+ q = shell.ask("What is your name?").modify(:none)
112
+ expect(q.read_string).to eql 'piotr'
113
+ end
114
+
115
+ it 'converts to upper case' do
116
+ input << 'piotr'
117
+ input.rewind
118
+ q = shell.ask("What is your name?").modify(:upcase)
119
+ expect(q.read_string).to eql 'PIOTR'
120
+ end
121
+
122
+ it 'trims whitespace' do
123
+ input << " Some white\t space\t \there! \n"
124
+ input.rewind
125
+ q = shell.ask("Enter some text: ").modify(:trim)
126
+ expect(q.read_string).to eql "Some white\t space\t \there!"
127
+ end
128
+
129
+ it 'collapses whitespace' do
130
+ input << " Some white\t space\t \there! \n"
131
+ input.rewind
132
+ q = shell.ask("Enter some text: ").modify(:collapse)
133
+ expect(q.read_string).to eql " Some white space here! "
134
+ end
135
+
136
+ it 'strips and collapses whitespace' do
137
+ input << " Some white\t space\t \there! \n"
138
+ input.rewind
139
+ q = shell.ask("Enter some text: ").modify(:strip, :collapse)
140
+ expect(q.read_string).to eql "Some white space here!"
141
+ end
142
+ end
143
+
144
+ context 'with type' do
145
+ it 'reads string' do
146
+ name = "Piotr"
147
+ input << name
148
+ input.rewind
149
+ q = shell.ask("What is your name?")
150
+ answer = q.read_string
151
+ expect(answer).to eql name
152
+ end
153
+
154
+ it 'reads integer' do
155
+ input << 5
156
+ input.rewind
157
+ q = shell.ask("What temperature?")
158
+ answer = q.read_int
159
+ expect(answer).to eql 5
160
+ end
161
+
162
+ it 'reads float' do
163
+ input << '6.0'
164
+ input.rewind
165
+ q = shell.ask("How tall are you?")
166
+ answer = q.read_float
167
+ expect(answer).to eql 6.0
168
+ end
169
+
170
+ it 'fails to read boolean' do
171
+ input << 'invalid'
172
+ input.rewind
173
+ q = shell.ask("Do you read books?")
174
+ expect { q.read_bool }.to raise_error(ArgumentError)
175
+ end
176
+
177
+ it 'reads negative boolean' do
178
+ input << 'No'
179
+ input.rewind
180
+ q = shell.ask("Do you read books?")
181
+ answer = q.read_bool
182
+ expect(answer).to eql false
183
+ end
184
+
185
+ it 'reads positive boolean' do
186
+ input << 'Yes'
187
+ input.rewind
188
+ q = shell.ask("Do you read books?")
189
+ answer = q.read_bool
190
+ expect(answer).to eql true
191
+ end
192
+ end
193
+
194
+ context 'with multiple line' do
195
+ it 'reads multiple lines' do
196
+ input << "First line\nSecond line\nThird line"
197
+ input.rewind
198
+ q = shell.ask("Provide description?")
199
+ expect(q.read_multiple).to eql "First line\nSecond line\nThird line"
200
+ end
201
+ end
202
+
203
+ context 'with email' do
204
+ it 'reads valid email' do
205
+ input << "piotr@example.com"
206
+ input.rewind
207
+ q = shell.ask("What is your email?")
208
+ expect(q.read_email).to eql "piotr@example.com"
209
+ end
210
+
211
+ it 'fails to read invalid email' do
212
+ input << "this will@neverwork"
213
+ input.rewind
214
+ q = shell.ask("What is your email?")
215
+ expect { q.read_email }.to raise_error(TTY::InvalidArgument)
216
+ end
217
+
218
+ it 'asks again' do
219
+ input << "this will@neverwork\nthis.will@example.com"
220
+ input.rewind
221
+ q = shell.ask("What is your email?").on_error(:retry)
222
+ expect(q.read_email).to eql "this.will@example.com"
223
+ expect(output.string).to eql "What is your email?\nWhat is your email?\n"
224
+ end
225
+ end
226
+
227
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Shell::Question::Modifier, '#apply_to' do
6
+ let(:instance) { described_class.new modifiers }
7
+ let(:string) { "Text to be modified"}
8
+
9
+ subject { instance.apply_to string }
10
+
11
+ context 'when no modifiers specified' do
12
+ let(:modifiers) { [] }
13
+
14
+ it { should == string }
15
+ end
16
+
17
+ context 'when modifiers specified' do
18
+ let(:modifiers) { [:down, :capitalize] }
19
+
20
+ it 'applies letter case modifications' do
21
+ described_class.should_receive(:letter_case).with(modifiers, string)
22
+ subject
23
+ end
24
+
25
+ it 'applies whitespace modifications' do
26
+ described_class.should_receive(:whitespace).with(modifiers, string)
27
+ subject
28
+ end
29
+ end
30
+ end # apply_to
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Shell::Question::Modifier, '#letter_case' do
6
+ let(:string) { 'text to modify' }
7
+
8
+ subject { described_class.letter_case modifier, string}
9
+
10
+ context 'when upper case' do
11
+ let(:modifier) { :up }
12
+
13
+ it { should == 'TEXT TO MODIFY' }
14
+ end
15
+
16
+ context 'when lower case' do
17
+ let(:modifier) { :down }
18
+
19
+ it { should == 'text to modify'}
20
+ end
21
+
22
+ context 'when capitalize' do
23
+ let(:modifier) { :capitalize }
24
+
25
+ it { should == 'Text to modify'}
26
+ end
27
+ end # lettercase
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Shell::Question::Modifier, '#whitespace' do
6
+ let(:string) { " text\t \n to\t modify\r\n" }
7
+
8
+ subject { described_class.whitespace modifier, string}
9
+
10
+ context 'when stripping whitespace' do
11
+ let(:modifier) { :trim }
12
+
13
+ it { should == "text\t \n to\t modify" }
14
+ end
15
+
16
+ context 'when chomping whitespace' do
17
+ let(:modifier) { :chomp }
18
+
19
+ it { should == " text\t \n to\t modify" }
20
+ end
21
+
22
+ context 'when capitalize' do
23
+ let(:modifier) { :collapse }
24
+
25
+ it { should == " text to modify " }
26
+ end
27
+
28
+ context 'when removing whitespace' do
29
+ let(:modifier) { :remove }
30
+
31
+ it { should == "texttomodify" }
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Shell::Question::Validation, '#coerce' do
6
+ let(:validation) { "^[^\.]+\.[^\.]+" }
7
+ let(:instance) { described_class.new }
8
+
9
+ subject { instance.coerce validation }
10
+
11
+ it { should be_kind_of Regexp }
12
+
13
+ context 'when proc' do
14
+ let(:validation) { lambda { "^[^\.]+\.[^\.]+" } }
15
+
16
+ it { should be_kind_of Proc }
17
+ end
18
+
19
+ context 'when unkown type' do
20
+ let(:validation) { Object.new }
21
+
22
+ it { expect { subject }.to raise_error(TTY::ValidationCoercion) }
23
+ end
24
+
25
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Shell::Question::Validation, '#valid_value?' do
6
+ let(:validation) { /^[^\.]+\.[^\.]+/ }
7
+ let(:instance) { described_class.new validation }
8
+
9
+ subject { instance.valid_value?(value) }
10
+
11
+ context '' do
12
+ let(:value) { nil }
13
+
14
+ it { should be_false }
15
+ end
16
+
17
+ context 'when the value matches validation' do
18
+ let(:value) { 'piotr.murach' }
19
+
20
+ it { should be_nil }
21
+ end
22
+
23
+ context 'when the value is not matching validation' do
24
+ let(:value) { 'piotrmurach' }
25
+
26
+ it { expect { subject }.to raise_error(TTY::InvalidArgument) }
27
+ end
28
+ end
@@ -0,0 +1,64 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Shell, '#say' do
6
+ let(:input) { StringIO.new }
7
+ let(:output) { StringIO.new }
8
+
9
+ subject(:shell) { TTY::Shell.new(input, output) }
10
+
11
+ after { output.rewind }
12
+
13
+ it 'prints an empty message' do
14
+ shell.say ""
15
+ expect(output.string).to eql ""
16
+ end
17
+
18
+ context 'with new line' do
19
+ it 'prints a message with newline' do
20
+ shell.say "Hell yeah!\n"
21
+ expect(output.string).to eql "Hell yeah!\n"
22
+ end
23
+
24
+ it 'prints a message with implicit newline' do
25
+ shell.say "Hell yeah!\n"
26
+ expect(output.string).to eql "Hell yeah!\n"
27
+ end
28
+
29
+ it 'prints a message with newline within text' do
30
+ shell.say "Hell\n yeah!"
31
+ expect(output.string).to eql "Hell\n yeah!\n"
32
+ end
33
+
34
+ it 'prints a message with newline within text and blank space' do
35
+ shell.say "Hell\n yeah! "
36
+ expect(output.string).to eql "Hell\n yeah! "
37
+ end
38
+
39
+ it 'prints a message without newline' do
40
+ shell.say "Hell yeah!", :newline => false
41
+ expect(output.string).to eql "Hell yeah!"
42
+ end
43
+ end
44
+
45
+ context 'with tab or space' do
46
+ it 'prints ' do
47
+ shell.say "Hell yeah!\t"
48
+ expect(output.string).to eql "Hell yeah!\t"
49
+ end
50
+ end
51
+
52
+ context 'with color' do
53
+ it 'prints message with ansi color' do
54
+ shell.say "Hell yeah!", :color => :green
55
+ expect(output.string).to eql "\e[32mHell yeah!\e[0m\n"
56
+ end
57
+
58
+ it 'prints message with ansi color without newline' do
59
+ shell.say "Hell yeah! ", :color => :green
60
+ expect(output.string).to eql "\e[32mHell yeah! \e[0m"
61
+ end
62
+ end
63
+
64
+ end # say
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Shell::Statement, '#new' do
6
+ let(:input) { StringIO.new }
7
+ let(:output) { StringIO.new }
8
+ let(:shell) { TTY::Shell.new(input, output) }
9
+
10
+ subject(:statement) { described_class.new }
11
+
12
+ its(:newline) { should be_true }
13
+
14
+ its(:color) { should be_nil }
15
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Shell, '#warn' do
6
+ let(:input) { StringIO.new }
7
+ let(:output) { StringIO.new }
8
+
9
+ subject(:shell) { TTY::Shell.new(input, output) }
10
+
11
+ after { output.rewind }
12
+
13
+ it 'displays one message' do
14
+ shell.warn "Careful young apprentice!"
15
+ expect(output.string).to eql "\e[33mCareful young apprentice!\e[0m\n"
16
+ end
17
+
18
+ it 'displays many messages' do
19
+ shell.warn "Careful there!", "It's dangerous!"
20
+ expect(output.string).to eql "\e[33mCareful there!\e[0m\n\e[33mIt's dangerous!\e[0m\n"
21
+ end
22
+
23
+ it 'displays message with option' do
24
+ shell.warn "Careful young apprentice!", :newline => false
25
+ expect(output.string).to eql "\e[33mCareful young apprentice!\e[0m"
26
+ end
27
+
28
+ end # warn
@@ -8,7 +8,6 @@ describe TTY::Table, '#renderer' do
8
8
 
9
9
  before do
10
10
  TTY::Table.renderer = basic_renderer
11
- TTY::Color.stub(:color?).and_return false
12
11
  end
13
12
 
14
13
  after do
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal::Color, '#code' do
6
+ let(:string) { "This is a \e[1m\e[34mbold blue text\e[0m" }
7
+
8
+ it 'finds single code' do
9
+ subject.code(:black).should == ["\e[30m"]
10
+ end
11
+
12
+ it 'finds more than one code' do
13
+ subject.code(:black, :green).should == ["\e[30m", "\e[32m"]
14
+ end
15
+
16
+ it "doesn't find code" do
17
+ expect { subject.code(:unkown) }.to raise_error(ArgumentError)
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal::Color, '#remove' do
6
+ let(:string) { "This is a \e[1m\e[34mbold blue text\e[0m" }
7
+
8
+ it 'remove color from string' do
9
+ subject.remove(string).should == "This is a bold blue text"
10
+ end
11
+
12
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal::Color, '#set' do
6
+ let(:string) { 'string' }
7
+
8
+ it 'applies green text to string' do
9
+ subject.set(string, :green).should == "\e[32m#{string}\e[0m"
10
+ end
11
+
12
+ it 'applies red text background to string' do
13
+ subject.set(string, :on_red).should == "\e[41m#{string}\e[0m"
14
+ end
15
+
16
+ it 'applies style and color to string' do
17
+ text = subject.set(string, :bold, :green)
18
+ text.should == "\e[1m\e[32m#{string}\e[0m"
19
+ end
20
+
21
+ it 'applies style, color and background to string' do
22
+ text = subject.set(string, :bold, :green, :on_blue)
23
+ text.should == "\e[1m\e[32m\e[44m#{string}\e[0m"
24
+ end
25
+
26
+ it 'errors for unkown color' do
27
+ expect { subject.set(string, :crimson) }.to raise_error(ArgumentError)
28
+ end
29
+
30
+ end
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal, '#color' do
6
+
7
+ it { should respond_to(:color) }
8
+
9
+ its(:color) { should be_kind_of TTY::Terminal::Color}
10
+
11
+ it 'delegates color handling' do
12
+ string = 'text'
13
+ subject.color.set(string, :red).should == "\e[31m#{string}\e[0m"
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal, '#home' do
6
+
7
+ before { ENV.stub!(:[]) }
8
+
9
+ subject(:terminal) { described_class.new.home }
10
+
11
+ after { terminal.instance_variable_set(:@home, nil) }
12
+
13
+ it 'expands user home path if HOME environemnt not set' do
14
+ File.stub!(:expand_path).and_return('/home/user')
15
+ expect(terminal).to eql('/home/user')
16
+ end
17
+
18
+ it 'defaults to user HOME environment' do
19
+ ENV.stub!(:[]).with('HOME').and_return('/home/user')
20
+ expect(terminal).to eq('/home/user')
21
+ end
22
+
23
+ context 'when failed to expand' do
24
+ before { File.should_receive(:expand_path).and_raise(RuntimeError) }
25
+
26
+ it 'returns C:/ on windows' do
27
+ TTY::System.stub(:windows?).and_return true
28
+ expect(terminal).to eql("C:/")
29
+ end
30
+
31
+ it 'returns root on unix' do
32
+ TTY::System.stub(:windows?).and_return false
33
+ expect(terminal).to eql("/")
34
+ end
35
+ end
36
+
37
+ end
@@ -7,7 +7,5 @@ begin
7
7
  end
8
8
  end
9
9
  rescue LoadError
10
- task :reek do
11
- warn "Reek is not available. In order to run reek, you must: gem install reek"
12
- end
10
+ warn "Reek is not available. In order to run reek, you must: gem install reek"
13
11
  end