command_mapper 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/option_spec.rb CHANGED
@@ -41,6 +41,14 @@ describe CommandMapper::Option do
41
41
  end
42
42
  end
43
43
 
44
+ context "when value_in_flag: true is given" do
45
+ subject { described_class.new(flag, value_in_flag: true) }
46
+
47
+ it "#value_in_flag? must return true" do
48
+ expect(subject.value_in_flag?).to be(true)
49
+ end
50
+ end
51
+
44
52
  context "when given the repeats: true keyword argument" do
45
53
  subject { described_class.new(flag, repeats: true) }
46
54
 
@@ -177,6 +185,97 @@ describe CommandMapper::Option do
177
185
  end
178
186
  end
179
187
 
188
+ describe "#accepts_value?" do
189
+ context "when initialized with the value: keyword argument" do
190
+ context "and when value: is true" do
191
+ subject { described_class.new(flag, value: true) }
192
+
193
+ it "must return true" do
194
+ expect(subject.accepts_value?).to be(true)
195
+ end
196
+ end
197
+
198
+ context "and when value: is a Hash" do
199
+ let(:value_required) { true }
200
+ let(:value_type) { Types::KeyValue.new }
201
+ let(:value_kwargs) do
202
+ {
203
+ required: value_required,
204
+ type: value_type
205
+ }
206
+ end
207
+
208
+ subject { described_class.new(flag, value: value_kwargs) }
209
+
210
+ it "must return true" do
211
+ expect(subject.accepts_value?).to be(true)
212
+ end
213
+ end
214
+ end
215
+
216
+ context "when not initialied with the value: keyword argument" do
217
+ subject { described_class.new(flag) }
218
+
219
+ it "must return false" do
220
+ expect(subject.accepts_value?).to be(false)
221
+ end
222
+ end
223
+ end
224
+
225
+ describe "#equals?" do
226
+ context "when initialized with equals: true" do
227
+ subject { described_class.new(flag, value: true, equals: true) }
228
+
229
+ it "must return true" do
230
+ expect(subject.equals?).to be(true)
231
+ end
232
+ end
233
+
234
+ context "when not initialized with equals: true" do
235
+ subject { described_class.new(flag, value: true) }
236
+
237
+ it "must return nil" do
238
+ expect(subject.equals?).to be(nil)
239
+ end
240
+ end
241
+ end
242
+
243
+ describe "#repeats?" do
244
+ context "when initialized with repeats: true" do
245
+ subject { described_class.new(flag, value: true, repeats: true) }
246
+
247
+ it "must return true" do
248
+ expect(subject.repeats?).to be(true)
249
+ end
250
+ end
251
+
252
+ context "when not initialized with repeats: true" do
253
+ subject { described_class.new(flag, value: true) }
254
+
255
+ it "must return false" do
256
+ expect(subject.repeats?).to be(false)
257
+ end
258
+ end
259
+ end
260
+
261
+ describe "#value_in_flag?" do
262
+ context "when initialized with value_in_flag: true" do
263
+ subject { described_class.new(flag, value: true, value_in_flag: true) }
264
+
265
+ it "must return true" do
266
+ expect(subject.value_in_flag?).to be(true)
267
+ end
268
+ end
269
+
270
+ context "when not initialized with value_in_flag: true" do
271
+ subject { described_class.new(flag, value: true) }
272
+
273
+ it "must return nil" do
274
+ expect(subject.value_in_flag?).to be(nil)
275
+ end
276
+ end
277
+ end
278
+
180
279
  let(:flag) { "--opt" }
181
280
  let(:name) { "opt" }
182
281
 
@@ -199,11 +298,17 @@ describe CommandMapper::Option do
199
298
  }
200
299
  end
201
300
 
301
+ let(:equals) { nil }
302
+ let(:value_in_flag) { nil }
303
+
202
304
  subject do
203
305
  if accepts_value
204
306
  described_class.new(flag, name: name,
205
307
  value: value_kwargs,
206
- repeats: repeats)
308
+ repeats: repeats,
309
+ # formatting options
310
+ equals: equals,
311
+ value_in_flag: value_in_flag)
207
312
  else
208
313
  described_class.new(flag, name: name, repeats: repeats)
209
314
  end
@@ -516,6 +621,34 @@ describe CommandMapper::Option do
516
621
  )
517
622
  end
518
623
 
624
+ context "and #equals? is true" do
625
+ let(:equals) { true }
626
+
627
+ it "must return an argv of option flag=value Strings" do
628
+ expect(subject.argv(values)).to eq(
629
+ [
630
+ "#{flag}=#{values[0]}",
631
+ "#{flag}=#{values[1]}",
632
+ "#{flag}=#{values[2]}"
633
+ ]
634
+ )
635
+ end
636
+ end
637
+
638
+ context "and #value_in_flag? is true" do
639
+ let(:value_in_flag) { true }
640
+
641
+ it "must return an argv of option flag + value Strings" do
642
+ expect(subject.argv(values)).to eq(
643
+ [
644
+ "#{flag}#{values[0]}",
645
+ "#{flag}#{values[1]}",
646
+ "#{flag}#{values[2]}"
647
+ ]
648
+ )
649
+ end
650
+ end
651
+
519
652
  context "but one of the Array's elements is invalid" do
520
653
  let(:value) { ["foo", " ", "baz"] }
521
654
 
@@ -578,6 +711,32 @@ describe CommandMapper::Option do
578
711
  )
579
712
  end
580
713
 
714
+ context "and #equals? is true" do
715
+ let(:equals) { true }
716
+
717
+ it "must return an argv of option flag=value Strings" do
718
+ expect(subject.argv(values)).to eq(
719
+ [
720
+ "#{flag}=#{subject.value.format(values[0])}",
721
+ "#{flag}=#{subject.value.format(values[1])}"
722
+ ]
723
+ )
724
+ end
725
+ end
726
+
727
+ context "and #value_in_flag? is true" do
728
+ let(:value_in_flag) { true }
729
+
730
+ it "must return an argv of option flag + value Strings" do
731
+ expect(subject.argv(values)).to eq(
732
+ [
733
+ "#{flag}#{subject.value.format(values[0])}",
734
+ "#{flag}#{subject.value.format(values[1])}"
735
+ ]
736
+ )
737
+ end
738
+ end
739
+
581
740
  context "but one of the Hashes is empty" do
582
741
  let(:values) do
583
742
  [{"foo" => 1}, {}]
@@ -621,6 +780,22 @@ describe CommandMapper::Option do
621
780
  expect(subject.argv(value)).to eq([flag, value])
622
781
  end
623
782
 
783
+ context "and #equals? is true" do
784
+ let(:equals) { true }
785
+
786
+ it "must return an argv containing an option flag=value String" do
787
+ expect(subject.argv(value)).to eq(["#{flag}=#{value}"])
788
+ end
789
+ end
790
+
791
+ context "and #value_in_flag? is true" do
792
+ let(:value_in_flag) { true }
793
+
794
+ it "must return an argv containing an option flag + value String" do
795
+ expect(subject.argv(value)).to eq(["#{flag}#{value}"])
796
+ end
797
+ end
798
+
624
799
  context "but it's invalid" do
625
800
  let(:value) { " " }
626
801
 
@@ -656,6 +831,26 @@ describe CommandMapper::Option do
656
831
  )
657
832
  end
658
833
 
834
+ context "and #equals? is true" do
835
+ let(:equals) { true }
836
+
837
+ it "must return an argv containing an option flag=value String" do
838
+ expect(subject.argv(value)).to eq(
839
+ ["#{flag}=#{subject.value.format(value)}"]
840
+ )
841
+ end
842
+ end
843
+
844
+ context "and #value_in_flag? is true" do
845
+ let(:value_in_flag) { true }
846
+
847
+ it "must return an argv of option flag + value Strings" do
848
+ expect(subject.argv(value)).to eq(
849
+ ["#{flag}#{subject.value.format(value)}"]
850
+ )
851
+ end
852
+ end
853
+
659
854
  context "but it's empty" do
660
855
  let(:value) { {} }
661
856
 
@@ -704,6 +899,22 @@ describe CommandMapper::Option do
704
899
  expect(subject.argv(value)).to eq([flag, value])
705
900
  end
706
901
 
902
+ context "and #equals? is true" do
903
+ let(:equals) { true }
904
+
905
+ it "must return an argv containing an option flag=value String" do
906
+ expect(subject.argv(value)).to eq(["#{flag}=#{value}"])
907
+ end
908
+ end
909
+
910
+ context "and #value_in_flag? is true" do
911
+ let(:value_in_flag) { true }
912
+
913
+ it "must return an argv containing an option flag + value String" do
914
+ expect(subject.argv(value)).to eq(["#{flag}#{value}"])
915
+ end
916
+ end
917
+
707
918
  context "but it's invalid" do
708
919
  let(:value) { " " }
709
920
 
@@ -743,6 +954,26 @@ describe CommandMapper::Option do
743
954
  )
744
955
  end
745
956
 
957
+ context "and #equals? is true" do
958
+ let(:equals) { true }
959
+
960
+ it "must return an argv containing an option flag=value String" do
961
+ expect(subject.argv(value)).to eq(
962
+ ["#{flag}=#{subject.value.format(value)}"]
963
+ )
964
+ end
965
+ end
966
+
967
+ context "and #value_in_flag? is true" do
968
+ let(:value_in_flag) { true }
969
+
970
+ it "must return an argv containing an option flag + value String" do
971
+ expect(subject.argv(value)).to eq(
972
+ ["#{flag}#{subject.value.format(value)}"]
973
+ )
974
+ end
975
+ end
976
+
746
977
  context "but one of the Array's elements is nil" do
747
978
  let(:value) { ["foo", nil, "baz"] }
748
979
 
@@ -795,6 +1026,26 @@ describe CommandMapper::Option do
795
1026
  )
796
1027
  end
797
1028
 
1029
+ context "and #equals? is true" do
1030
+ let(:equals) { true }
1031
+
1032
+ it "must return an argv containing an option flag=value String" do
1033
+ expect(subject.argv(value)).to eq(
1034
+ ["#{flag}=#{subject.value.format(value)}"]
1035
+ )
1036
+ end
1037
+ end
1038
+
1039
+ context "and #value_in_flag? is true" do
1040
+ let(:value_in_flag) { true }
1041
+
1042
+ it "must return an argv of option flag + value Strings" do
1043
+ expect(subject.argv(value)).to eq(
1044
+ ["#{flag}#{subject.value.format(value)}"]
1045
+ )
1046
+ end
1047
+ end
1048
+
798
1049
  context "but it's empty" do
799
1050
  let(:value) { {} }
800
1051
 
@@ -14,6 +14,16 @@ describe CommandMapper::Types::Hex do
14
14
  expect(subject.leading_zero?).to be(true)
15
15
  end
16
16
  end
17
+
18
+ context "when initialized with range: ..." do
19
+ let(:range) { 1..10 }
20
+
21
+ subject { described_class.new(range: range) }
22
+
23
+ it "must set #range" do
24
+ expect(subject.range).to eq(range)
25
+ end
26
+ end
17
27
  end
18
28
 
19
29
  describe "#leading_zero?" do
@@ -69,12 +79,60 @@ describe CommandMapper::Types::Hex do
69
79
  expect(subject.validate(value)).to be(true)
70
80
  end
71
81
 
82
+ context "when initialized with range: ..." do
83
+ let(:range) { 2..16 }
84
+
85
+ subject { described_class.new(range: range) }
86
+
87
+ context "and the value is within the range of values" do
88
+ let(:value) { 'a' }
89
+
90
+ it "must return true" do
91
+ expect(subject.validate(value)).to be(true)
92
+ end
93
+ end
94
+
95
+ context "but the value is not within the range of values" do
96
+ let(:value) { '0' }
97
+
98
+ it "must return [false, \"unacceptable value (...)\"]" do
99
+ expect(subject.validate(value)).to eq(
100
+ [false, "unacceptable value (#{value.inspect})"]
101
+ )
102
+ end
103
+ end
104
+ end
105
+
72
106
  context "and the String begins with a '0x'" do
73
107
  let(:value) { "0xabcdef" }
74
108
 
75
109
  it "must return true" do
76
110
  expect(subject.validate(value)).to be(true)
77
111
  end
112
+
113
+ context "when initialized with range: ..." do
114
+ let(:range) { 2..16 }
115
+
116
+ subject { described_class.new(range: range) }
117
+
118
+ context "and the value is within the range of values" do
119
+ let(:value) { '0x4' }
120
+
121
+ it "must return true" do
122
+ expect(subject.validate(value)).to be(true)
123
+ end
124
+ end
125
+
126
+ context "but the value is not within the range of values" do
127
+ let(:value) { '0x0' }
128
+
129
+ it "must return [false, \"unacceptable value (...)\"]" do
130
+ expect(subject.validate(value)).to eq(
131
+ [false, "unacceptable value (#{value.inspect})"]
132
+ )
133
+ end
134
+ end
135
+ end
78
136
  end
79
137
 
80
138
  context "and the String contains a newline" do
@@ -88,7 +146,7 @@ describe CommandMapper::Types::Hex do
88
146
  end
89
147
  end
90
148
 
91
- context "but the String does not contain other characters" do
149
+ context "but the String contains other characters" do
92
150
  let(:value) { "foo" }
93
151
 
94
152
  it "must return [false, \"not in hex format (...)\"]" do
@@ -2,6 +2,24 @@ require 'spec_helper'
2
2
  require 'command_mapper/types/num'
3
3
 
4
4
  describe CommandMapper::Types::Num do
5
+ describe "#initialize" do
6
+ context "when initialized with no keyword arguments" do
7
+ it "must set #range to nil" do
8
+ expect(subject.range).to be(nil)
9
+ end
10
+ end
11
+
12
+ context "when initialized with range: ..." do
13
+ let(:range) { 1..10 }
14
+
15
+ subject { described_class.new(range: range) }
16
+
17
+ it "must set #range" do
18
+ expect(subject.range).to eq(range)
19
+ end
20
+ end
21
+ end
22
+
5
23
  describe "#validate" do
6
24
  context "when given an Integer" do
7
25
  let(:value) { 1 }
@@ -9,6 +27,30 @@ describe CommandMapper::Types::Num do
9
27
  it "must return true" do
10
28
  expect(subject.validate(value)).to be(true)
11
29
  end
30
+
31
+ context "when initialized with range: ..." do
32
+ let(:range) { 2..10 }
33
+
34
+ subject { described_class.new(range: range) }
35
+
36
+ context "and the value is within the range of values" do
37
+ let(:value) { 4 }
38
+
39
+ it "must return true" do
40
+ expect(subject.validate(value)).to be(true)
41
+ end
42
+ end
43
+
44
+ context "but the value is not within the range of values" do
45
+ let(:value) { 0 }
46
+
47
+ it "must return [false, \"unacceptable value (...)\"]" do
48
+ expect(subject.validate(value)).to eq(
49
+ [false, "unacceptable value (#{value.inspect})"]
50
+ )
51
+ end
52
+ end
53
+ end
12
54
  end
13
55
 
14
56
  context "when given a String" do
@@ -19,7 +61,31 @@ describe CommandMapper::Types::Num do
19
61
  expect(subject.validate(value)).to be(true)
20
62
  end
21
63
 
22
- context "and the String contains a newline" do
64
+ context "when initialized with range: ..." do
65
+ let(:range) { 2..10 }
66
+
67
+ subject { described_class.new(range: range) }
68
+
69
+ context "and the value is within the range of values" do
70
+ let(:value) { '4' }
71
+
72
+ it "must return true" do
73
+ expect(subject.validate(value)).to be(true)
74
+ end
75
+ end
76
+
77
+ context "but the value is not within the range of values" do
78
+ let(:value) { '0' }
79
+
80
+ it "must return [false, \"unacceptable value (...)\"]" do
81
+ expect(subject.validate(value)).to eq(
82
+ [false, "unacceptable value (#{value.inspect})"]
83
+ )
84
+ end
85
+ end
86
+ end
87
+
88
+ context "but the String contains a newline" do
23
89
  let(:value) { "01234\n56789" }
24
90
 
25
91
  it "must return [false, \"contains non-numeric characters (...)\"]" do
@@ -30,7 +96,7 @@ describe CommandMapper::Types::Num do
30
96
  end
31
97
  end
32
98
 
33
- context "and it contains non-digits" do
99
+ context "but it contains non-digits" do
34
100
  let(:value) { "12abc34" }
35
101
 
36
102
  it "must return [false, \"contains non-numeric characters (...)\"]" do
@@ -42,7 +108,7 @@ describe CommandMapper::Types::Num do
42
108
  end
43
109
 
44
110
  context "when given another type of Object" do
45
- context "but it defines a #to_i method" do
111
+ context "and it defines a #to_i method" do
46
112
  let(:value) { 1.0 }
47
113
 
48
114
  it "must return true" do
@@ -50,6 +116,30 @@ describe CommandMapper::Types::Num do
50
116
  end
51
117
  end
52
118
 
119
+ context "when initialized with range: ..." do
120
+ let(:range) { 2..10 }
121
+
122
+ subject { described_class.new(range: range) }
123
+
124
+ context "and the value is within the range of values" do
125
+ let(:value) { 4.0 }
126
+
127
+ it "must return true" do
128
+ expect(subject.validate(value)).to be(true)
129
+ end
130
+ end
131
+
132
+ context "but the value is not within the range of values" do
133
+ let(:value) { 0.0 }
134
+
135
+ it "must return [false, \"unacceptable value (...)\"]" do
136
+ expect(subject.validate(value)).to eq(
137
+ [false, "unacceptable value (#{value.inspect})"]
138
+ )
139
+ end
140
+ end
141
+ end
142
+
53
143
  context "but it does not define a #to_i method" do
54
144
  let(:value) { Object.new }
55
145
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Postmodern
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-29 00:00:00.000000000 Z
11
+ date: 2022-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -34,6 +34,7 @@ extra_rdoc_files:
34
34
  - LICENSE.txt
35
35
  - README.md
36
36
  files:
37
+ - ".document"
37
38
  - ".github/workflows/ruby.yml"
38
39
  - ".gitignore"
39
40
  - ".rspec"