chars 0.2.0 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,220 +1,323 @@
1
- require 'chars/chars'
2
-
3
1
  require 'spec_helper'
2
+ require 'chars/char_set'
4
3
 
5
4
  describe Chars::CharSet do
6
- before(:all) do
7
- @integer_range = (0x41..0x43)
8
- @string_range = ('A'..'Z')
9
- @integers = @integer_range.to_a
10
- @strings = @string_range.to_a
5
+ let(:integer_range) { (0x41..0x43) }
6
+ let(:string_range) { ('A'..'Z') }
7
+ let(:integers) { integer_range.to_a }
8
+ let(:strings) { string_range.to_a }
11
9
 
12
- @char_set = Chars::CharSet.new(*@strings)
13
- end
10
+ subject { described_class.new(*strings) }
14
11
 
15
- it "may be created with String arguments" do
16
- @chars = Chars::CharSet.new(*@strings)
12
+ describe "#initialize" do
13
+ context "when given multiple String arguments" do
14
+ subject { described_class.new(*strings) }
17
15
 
18
- @strings.each do |s|
19
- @chars.include_char?(s).should == true
16
+ it "must populate the char set with the String's chars" do
17
+ expect(strings.all? { |s| subject.include_char?(s) }).to be(true)
18
+ end
20
19
  end
21
- end
22
20
 
23
- it "may be created with an Array of Strings" do
24
- @chars = Chars::CharSet.new(@strings)
21
+ context "when given an Array of Strings" do
22
+ subject { described_class.new(strings) }
25
23
 
26
- @strings.each do |s|
27
- @chars.include_char?(s).should == true
24
+ it "must populate the char set with the String's chars" do
25
+ expect(strings.all? { |s| subject.include_char?(s) }).to be(true)
26
+ end
28
27
  end
29
- end
30
28
 
31
- it "may be created with a Range of Strings" do
32
- @chars = Chars::CharSet.new(@string_range)
29
+ context "when given a Range of Strings" do
30
+ subject { described_class.new(string_range) }
33
31
 
34
- @strings.each do |s|
35
- @chars.include_char?(s).should == true
32
+ it "must populate the char set by enumerating over the String's chars" do
33
+ expect(strings.all? { |s| subject.include_char?(s) }).to be(true)
34
+ end
36
35
  end
37
- end
38
36
 
39
- it "may be created with Integer arguments" do
40
- @chars = Chars::CharSet.new(*@integers)
37
+ context "when given multiple Integer arguments" do
38
+ subject { described_class.new(*integers) }
41
39
 
42
- @integers.each do |i|
43
- @chars.include?(i).should == true
40
+ it "must populate the char set using the Integers as bytes" do
41
+ expect(integers.all? { |i| subject.include?(i) }).to be(true)
42
+ end
44
43
  end
45
- end
46
44
 
47
- it "may be created with an Array of Integers" do
48
- @chars = Chars::CharSet.new(@integers)
45
+ context "when given an Array of Integers" do
46
+ subject { described_class.new(integers) }
49
47
 
50
- @integers.each do |i|
51
- @chars.include?(i).should == true
48
+ it "must populate the char set using the Integers as bytes" do
49
+ expect(integers.all? { |i| subject.include?(i) }).to be(true)
50
+ end
52
51
  end
53
- end
54
52
 
55
- it "may be created with a Range of Integers" do
56
- @chars = Chars::CharSet.new(@integer_range)
53
+ context "when given a Range of Integers" do
54
+ subject { described_class.new(integer_range) }
57
55
 
58
- @integers.each do |i|
59
- @chars.include?(i).should == true
56
+ it "must populate the char set by enumerating over the Integer's bytes" do
57
+ expect(integers.all? { |i| subject.include?(i) }).to be(true)
58
+ end
60
59
  end
61
60
  end
62
61
 
63
- it "should include Strings" do
64
- @char_set.include_char?('A').should == true
62
+ describe "#include_char?" do
63
+ it "should include Strings" do
64
+ expect(subject.include_char?('A')).to be(true)
65
+ end
65
66
  end
66
67
 
67
- it "should include Integers" do
68
- @char_set.include?(0x41).should == true
68
+ describe "#include?" do
69
+ it "should include Integers" do
70
+ expect(subject).to include(0x41)
71
+ end
69
72
  end
70
73
 
71
- it "should be able to select bytes" do
72
- @sub_chars = @char_set.select_bytes { |c| c <= 0x42 }
74
+ describe "#select_bytes" do
75
+ it "should be able to select bytes" do
76
+ sub_set = subject.select_bytes { |c| c <= 0x42 }
73
77
 
74
- @sub_chars.should == [0x41, 0x42]
78
+ expect(sub_set).to be == [0x41, 0x42]
79
+ end
75
80
  end
76
81
 
77
- it "should be able to select chars" do
78
- @sub_chars = @char_set.select_chars { |c| c <= 'B' }
82
+ describe "#select_chars" do
83
+ it "should be able to select chars" do
84
+ sub_set = subject.select_chars { |c| c <= 'B' }
79
85
 
80
- @sub_chars.should == ['A', 'B']
86
+ expect(sub_set).to be == ['A', 'B']
87
+ end
81
88
  end
82
89
 
83
- it "should return a random byte" do
84
- @char_set.include?(@char_set.random_byte).should == true
90
+ describe "#random_byte" do
91
+ it "should return a random byte" do
92
+ expect(subject).to include(subject.random_byte)
93
+ end
85
94
  end
86
95
 
87
- it "should return a random char" do
88
- @char_set.include_char?(@char_set.random_char).should == true
96
+ describe "#random_char" do
97
+ it "should return a random char" do
98
+ expect(subject.include_char?(subject.random_char)).to be(true)
99
+ end
89
100
  end
90
101
 
91
- it "should iterate over n random bytes" do
92
- @char_set.each_random_byte(10) do |b|
93
- @char_set.include?(b).should == true
102
+ describe "#each_random_byte" do
103
+ it "should iterate over n random bytes" do
104
+ expect(subject.each_random_byte(10).all? { |b|
105
+ subject.include?(b)
106
+ }).to be(true)
94
107
  end
95
108
  end
96
109
 
97
- it "should iterate over n random chars" do
98
- @char_set.each_random_char(10) do |c|
99
- @char_set.include_char?(c).should == true
110
+ describe "#each_random_char" do
111
+ it "should iterate over n random chars" do
112
+ expect(subject.each_random_char(10).all? { |c|
113
+ subject.include_char?(c)
114
+ }).to be(true)
100
115
  end
101
116
  end
102
117
 
103
- it "should return a random Array of bytes" do
104
- bytes = @char_set.random_bytes(10)
118
+ describe "#random_bytes" do
119
+ it "should return a random Array of bytes" do
120
+ random_bytes = subject.random_bytes(10)
105
121
 
106
- bytes.each do |b|
107
- @char_set.include?(b).should == true
122
+ expect(random_bytes.all? { |b| subject.include?(b) }).to be(true)
108
123
  end
109
- end
110
124
 
111
- it "should return a random Array of chars" do
112
- chars = @char_set.random_chars(10)
125
+ context "with a range of lengths" do
126
+ it "should return a random Array of bytes with a varying length" do
127
+ random_bytes = subject.random_bytes(5..10)
113
128
 
114
- chars.each do |c|
115
- @char_set.include_char?(c).should == true
129
+ expect(random_bytes.length).to be_between(5, 10)
130
+ expect(random_bytes.all? { |b| subject.include?(b) }).to be(true)
131
+ end
116
132
  end
117
133
  end
118
134
 
119
- it "should return a random Array of bytes with a varying length" do
120
- bytes = @char_set.random_bytes(5..10)
135
+ describe "#random_chars" do
136
+ it "should return a random Array of chars" do
137
+ random_chars = subject.random_chars(10)
121
138
 
122
- bytes.length.between?(5, 10).should == true
123
- bytes.each do |b|
124
- @char_set.include?(b).should == true
139
+ expect(random_chars.all? { |c| subject.include_char?(c) }).to be(true)
125
140
  end
126
- end
127
141
 
128
- it "should return a random Array of chars with a varying length" do
129
- chars = @char_set.random_chars(5..10)
142
+ context "with a range of lengths" do
143
+ it "should return a random Array of chars with a varying length" do
144
+ random_chars = subject.random_chars(5..10)
130
145
 
131
- chars.length.between?(5, 10).should == true
132
- chars.each do |c|
133
- @char_set.include_char?(c).should == true
146
+ expect(random_chars.length).to be_between(5, 10)
147
+ expect(random_chars.all? { |c| subject.include_char?(c) }).to be(true)
148
+ end
134
149
  end
135
150
  end
136
151
 
137
- it "should return a random String of chars" do
138
- @char_set.random_string(10).each_byte do |b|
139
- @char_set.include?(b).should == true
152
+ describe "#random_string" do
153
+ it "should return a random String of chars" do
154
+ random_string = subject.random_string(10)
155
+
156
+ expect(random_string.chars.all? { |b|
157
+ subject.include_char?(b)
158
+ }).to be(true)
140
159
  end
141
- end
142
160
 
143
- it "should return a random String of chars with a varying length" do
144
- string = @char_set.random_string(5..10)
161
+ context "with a range of lengths" do
162
+ it "should return a random String of chars with a varying length" do
163
+ random_string = subject.random_string(5..10)
145
164
 
146
- string.length.between?(5, 10)
147
- string.each_byte do |b|
148
- @char_set.include?(b).should == true
165
+ expect(random_string.length).to be_between(5, 10)
166
+ expect(random_string.chars.all? { |b|
167
+ subject.include_char?(b)
168
+ }).to be(true)
169
+ end
149
170
  end
150
171
  end
151
172
 
152
- it "should return a random Array of unique bytes" do
153
- bytes = @char_set.random_distinct_bytes(10)
154
- bytes.uniq.length.should == bytes.length
155
- bytes.each do |b|
156
- @char_set.include?(b).should == true
173
+ describe "#random_distinct_bytes" do
174
+ it "should return a random Array of unique bytes" do
175
+ random_bytes = subject.random_distinct_bytes(10)
176
+
177
+ expect(random_bytes.uniq).to be == random_bytes
178
+ expect(random_bytes.all? { |b| subject.include_byte?(b) }).to be(true)
157
179
  end
158
- end
159
180
 
160
- it "should return a random Array of unique chars" do
161
- chars = @char_set.random_distinct_chars(10)
162
- chars.uniq.length.should == chars.length
163
- chars.each do |c|
164
- @char_set.include_char?(c).should == true
181
+ context "with a Range of lengths" do
182
+ it "should return a random Array of unique bytes with a varying length" do
183
+ random_bytes = subject.random_distinct_bytes(5..10)
184
+
185
+ expect(random_bytes.uniq).to be == random_bytes
186
+ expect(random_bytes.length).to be_between(5, 10)
187
+ expect(random_bytes.all? { |b| subject.include_byte?(b) }).to be(true)
188
+ end
165
189
  end
166
190
  end
167
191
 
168
- it "should return a random Array of unique bytes with a varying length" do
169
- bytes = @char_set.random_distinct_bytes(5..10)
170
- bytes.uniq.length.should == bytes.length
171
- bytes.length.between?(5, 10).should == true
172
- bytes.each do |b|
173
- @char_set.include?(b).should == true
192
+ describe "#random_distinct_chars" do
193
+ it "should return a random Array of unique chars" do
194
+ random_chars = subject.random_distinct_chars(10)
195
+
196
+ expect(random_chars.uniq).to be == random_chars
197
+ expect(random_chars.all? { |c| subject.include_char?(c) }).to be(true)
174
198
  end
175
- end
176
199
 
177
- it "should return a random Array of unique chars with a varying length" do
178
- chars = @char_set.random_distinct_chars(5..10)
179
- chars.uniq.length.should == chars.length
180
- chars.length.between?(5, 10).should == true
181
- chars.each do |c|
182
- @char_set.include_char?(c).should == true
200
+ context "with a range of lengths" do
201
+ it "should return a random Array of unique chars with a varying length" do
202
+ random_chars = subject.random_distinct_chars(5..10)
203
+
204
+ expect(random_chars.uniq).to be == random_chars
205
+ expect(random_chars.length).to be_between(5, 10)
206
+ expect(random_chars.all? { |c| subject.include_char?(c) }).to be(true)
207
+ end
183
208
  end
184
209
  end
185
210
 
186
-
187
- it "should be able to be compared with another set of chars" do
188
- (@char_set == Chars::CharSet[('A'..'Z')]).should == true
211
+ describe "#==" do
212
+ it "should be able to be compared with another set of chars" do
213
+ expect(subject).to be == described_class['A'..'Z']
214
+ end
189
215
  end
190
216
 
191
- it "should be able to be unioned with another set of chars" do
192
- super_set = (@char_set | Chars::CharSet['D'])
217
+ describe "#|" do
218
+ it "should be able to be unioned with another set of chars" do
219
+ super_set = (subject | described_class['D'])
193
220
 
194
- super_set.class.should == Chars::CharSet
195
- super_set.should == Chars::CharSet[('A'..'Z'), 'D']
221
+ expect(super_set).to be_kind_of(described_class)
222
+ expect(super_set).to be == described_class['A'..'Z', 'D']
223
+ end
196
224
  end
197
225
 
198
- it "should be able to be removed from another set of chars" do
199
- sub_set = (@char_set - Chars::CharSet['B'])
226
+ describe "#-" do
227
+ it "should be able to be removed from another set of chars" do
228
+ sub_set = (subject - described_class['B'])
200
229
 
201
- sub_set.class.should == Chars::CharSet
202
- sub_set.subset?(@char_set).should == true
230
+ expect(sub_set).to be_kind_of(described_class)
231
+ expect(sub_set).to be_subset(subject)
232
+ end
203
233
  end
204
234
 
205
- it "should find one sub-string from a String belonging to the char set" do
206
- @char_set.strings_in("AAAA").should == ["AAAA"]
207
- end
235
+ describe "#strings_in" do
236
+ subject { described_class.new(['A', 'B', 'C']) }
237
+
238
+ let(:string) { "AAAA....BBBB....CCCC...." }
239
+
240
+ context "when given a block" do
241
+ it "should find sub-strings from a String belonging to the char set" do
242
+ expect { |b|
243
+ subject.strings_in(string,&b)
244
+ }.to yield_successive_args(
245
+ "AAAA",
246
+ "BBBB",
247
+ "CCCC"
248
+ )
249
+ end
250
+
251
+ it "must find sub-strings of a minimum length of 4" do
252
+ expect { |b|
253
+ subject.strings_in("A...BBB...CCCC",&b)
254
+ }.to yield_successive_args("CCCC")
255
+ end
256
+
257
+ context "and when the whole string matches the char set" do
258
+ it "should find one sub-string from a String belonging to the char set" do
259
+ expect { |b|
260
+ subject.strings_in("AAAA",&b)
261
+ }.to yield_successive_args("AAAA")
262
+ end
263
+ end
264
+
265
+ context "when the :length option is given" do
266
+ it "must find sub-strings of the given minimum length" do
267
+ expect { |b|
268
+ subject.strings_in("AAAA...BBBB...CCCCC", :length => 5, &b)
269
+ }.to yield_successive_args("CCCCC")
270
+ end
271
+ end
272
+
273
+ context "and when the block takes two arguments" do
274
+ it "must yield the sub-strings and their indexes" do
275
+ yielded_args = []
276
+
277
+ subject.strings_in(string) do |substring,index|
278
+ yielded_args << [substring, index]
279
+ end
280
+
281
+ expect(yielded_args).to eq(
282
+ [
283
+ ['AAAA', string.index('AAAA')],
284
+ ['BBBB', string.index('BBBB')],
285
+ ['CCCC', string.index('CCCC')]
286
+ ]
287
+ )
288
+ end
289
+ end
290
+ end
208
291
 
209
- it "should find sub-strings from a String belonging to the char set" do
210
- @char_set.strings_in("AAAA!B!CCCCCC").should == [
211
- "AAAA",
212
- "CCCCCC"
213
- ]
292
+ context "when no block is given" do
293
+ it "must return an Array of sub-strings" do
294
+ expect(subject.strings_in(string)).to eq(
295
+ [
296
+ "AAAA",
297
+ "BBBB",
298
+ "CCCC"
299
+ ]
300
+ )
301
+ end
302
+
303
+ context "when given the :offset option" do
304
+ it "must return a Hash of the substrings and their indexes" do
305
+ expect(subject.strings_in(string, :offsets => true)).to eq(
306
+ {
307
+ "AAAA" => string.index("AAAA"),
308
+ "BBBB" => string.index("BBBB"),
309
+ "CCCC" => string.index("CCCC")
310
+ }
311
+ )
312
+ end
313
+ end
314
+ end
214
315
  end
215
316
 
216
- it "should determine if a String is made up of the characters from the char set" do
217
- (@char_set === "AABCBAA").should == true
218
- (@char_set === "AA!!EE").should_not == true
317
+ describe "#===" do
318
+ it "should determine if a String is made up of the characters from the char set" do
319
+ expect(subject).to be === "AABCBAA"
320
+ expect(subject).to_not be === "AA!!EE"
321
+ end
219
322
  end
220
323
  end
data/spec/chars_spec.rb CHANGED
@@ -1,115 +1,56 @@
1
- require 'chars/chars'
2
-
3
1
  require 'spec_helper'
2
+ require 'chars/chars'
4
3
 
5
4
  describe Chars do
6
- before(:all) do
7
- @numeric_string = Chars.numeric.random_string(10)
8
- @octal_string = Chars.octal.random_string(10)
9
- @uppercase_hex_string = Chars.uppercase_hexadecimal.random_string(10)
10
- @lowercase_hex_string = Chars.lowercase_hexadecimal.random_string(10)
11
- @hex_string = Chars.hexadecimal.random_string(10)
12
- @uppercase_alpha_string = Chars.uppercase_alpha.random_string(10)
13
- @lowercase_alpha_string = Chars.lowercase_alpha.random_string(10)
14
- @alpha_string = Chars.alpha.random_string(10)
15
- @alpha_numeric_string = Chars.alpha_numeric.random_string(10)
16
- @space_string = Chars.space.random_string(10)
17
- @punctuation_string = Chars.punctuation.random_string(10)
18
- @symbols_string = Chars.symbols.random_string(10)
19
- @control_string = Chars.control.random_string(10)
20
- @signed_ascii_string = Chars.signed_ascii.random_string(10)
21
- @ascii_string = Chars.ascii.random_string(10)
22
- @visible_string = Chars.visible.random_string(10)
23
- end
24
-
25
5
  it "should provide a numeric CharSet" do
26
- @numeric_string.length.should == 10
27
- @numeric_string.each_byte do |b|
28
- Chars::NUMERIC.include?(b).should == true
29
- end
6
+ expect(described_class::NUMERIC).to be =~ '0123456789'
30
7
  end
31
8
 
32
9
  it "should provide an octal CharSet" do
33
- @octal_string.length.should == 10
34
- @octal_string.each_byte do |b|
35
- Chars::OCTAL.include?(b).should == true
36
- end
10
+ expect(described_class::OCTAL).to be =~ "01234567"
37
11
  end
38
12
 
39
13
  it "should provide an upper-case hexadecimal CharSet" do
40
- @uppercase_hex_string.length.should == 10
41
- @uppercase_hex_string.each_byte do |b|
42
- Chars::UPPERCASE_HEXADECIMAL.include?(b).should == true
43
- end
14
+ expect(described_class::UPPERCASE_HEXADECIMAL).to be =~ "0123456789ABCDEF"
44
15
  end
45
16
 
46
17
  it "should provide a lower-case hexadecimal CharSet" do
47
- @lowercase_hex_string.length.should == 10
48
- @lowercase_hex_string.each_byte do |b|
49
- Chars::LOWERCASE_HEXADECIMAL.include?(b).should == true
50
- end
18
+ expect(described_class::LOWERCASE_HEXADECIMAL).to be =~ "0123456789abcdef"
51
19
  end
52
20
 
53
21
  it "should provide a hexadecimal CharSet" do
54
- @hex_string.length.should == 10
55
- @hex_string.each_byte do |b|
56
- Chars::HEXADECIMAL.include?(b).should == true
57
- end
22
+ expect(described_class::HEXADECIMAL).to be =~ "0123456789ABCDEFabcdef"
58
23
  end
59
24
 
60
25
  it "should provide an upper-case alpha CharSet" do
61
- @uppercase_alpha_string.length.should == 10
62
- @uppercase_alpha_string.each_byte do |b|
63
- Chars::UPPERCASE_ALPHA.include?(b).should == true
64
- end
26
+ expect(described_class::UPPERCASE_ALPHA).to be =~ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
65
27
  end
66
28
 
67
29
  it "should provide a lower-case alpha CharSet" do
68
- @lowercase_alpha_string.length.should == 10
69
- @lowercase_alpha_string.each_byte do |b|
70
- Chars::LOWERCASE_ALPHA.include?(b).should == true
71
- end
30
+ expect(described_class::LOWERCASE_ALPHA).to be =~ "abcdefghijklmnopqrstuvwxyz"
72
31
  end
73
32
 
74
33
  it "should provide an alpha CharSet" do
75
- @alpha_string.length.should == 10
76
- @alpha_string.each_byte do |b|
77
- Chars::ALPHA.include?(b).should == true
78
- end
34
+ expect(described_class::ALPHA).to be =~ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
79
35
  end
80
36
 
81
37
  it "should provide an alpha-numeric CharSet" do
82
- @alpha_numeric_string.length.should == 10
83
- @alpha_numeric_string.each_byte do |b|
84
- Chars::ALPHA_NUMERIC.include?(b).should == true
85
- end
38
+ expect(described_class::ALPHA_NUMERIC).to be =~ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
86
39
  end
87
40
 
88
41
  it "should provide a visible CharSet" do
89
- @visible_string.length.should == 10
90
- @visible_string.each_byte do |b|
91
- Chars::VISIBLE.include?(b).should == true
92
- end
42
+ expect(described_class::VISIBLE).to be =~ "!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
93
43
  end
94
44
 
95
45
  it "should provide a space CharSet" do
96
- @space_string.length.should == 10
97
- @space_string.each_byte do |b|
98
- Chars::SPACE.include?(b).should == true
99
- end
46
+ expect(described_class::SPACE).to be =~ "\t\n\v\f\r "
100
47
  end
101
48
 
102
49
  it "should provide a punctuation CharSet" do
103
- @punctuation_string.length.should == 10
104
- @punctuation_string.each_byte do |b|
105
- Chars::PUNCTUATION.include?(b).should == true
106
- end
50
+ expect(described_class::PUNCTUATION).to be =~ " !\"'(),-.:;?[]`{}~"
107
51
  end
108
52
 
109
53
  it "should provide a symbols CharSet" do
110
- @symbols_string.length.should == 10
111
- @symbols_string.each_byte do |b|
112
- Chars::SYMBOLS.include?(b).should == true
113
- end
54
+ expect(described_class::SYMBOLS).to be =~ " !\"\#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
114
55
  end
115
56
  end