chars 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ChangeLog.md +7 -0
- data/Gemfile +1 -0
- data/LICENSE.txt +1 -1
- data/README.md +76 -48
- data/Rakefile +1 -1
- data/chars.gemspec +1 -0
- data/gemspec.yml +8 -2
- data/lib/chars/char_set.rb +68 -110
- data/lib/chars/chars.rb +54 -18
- data/lib/chars/extensions/integer.rb +34 -17
- data/lib/chars/extensions/string.rb +34 -17
- data/lib/chars/version.rb +1 -1
- data/spec/char_set_spec.rb +200 -91
- metadata +13 -9
data/spec/char_set_spec.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'chars/
|
2
|
+
require 'chars/char_set'
|
3
3
|
|
4
4
|
describe Chars::CharSet do
|
5
5
|
let(:integer_range) { (0x41..0x43) }
|
@@ -10,205 +10,314 @@ describe Chars::CharSet do
|
|
10
10
|
subject { described_class.new(*strings) }
|
11
11
|
|
12
12
|
describe "#initialize" do
|
13
|
-
|
14
|
-
|
13
|
+
context "when given multiple String arguments" do
|
14
|
+
subject { described_class.new(*strings) }
|
15
15
|
|
16
|
-
|
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
|
17
19
|
end
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
+
context "when given an Array of Strings" do
|
22
|
+
subject { described_class.new(strings) }
|
21
23
|
|
22
|
-
|
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
|
23
27
|
end
|
24
28
|
|
25
|
-
|
26
|
-
|
29
|
+
context "when given a Range of Strings" do
|
30
|
+
subject { described_class.new(string_range) }
|
27
31
|
|
28
|
-
|
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
|
29
35
|
end
|
30
36
|
|
31
|
-
|
32
|
-
|
37
|
+
context "when given multiple Integer arguments" do
|
38
|
+
subject { described_class.new(*integers) }
|
33
39
|
|
34
|
-
|
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
|
35
43
|
end
|
36
44
|
|
37
|
-
|
38
|
-
|
45
|
+
context "when given an Array of Integers" do
|
46
|
+
subject { described_class.new(integers) }
|
39
47
|
|
40
|
-
|
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
|
41
51
|
end
|
42
52
|
|
43
|
-
|
44
|
-
|
53
|
+
context "when given a Range of Integers" do
|
54
|
+
subject { described_class.new(integer_range) }
|
45
55
|
|
46
|
-
|
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
|
47
59
|
end
|
48
60
|
end
|
49
61
|
|
50
|
-
|
51
|
-
|
62
|
+
describe "#include_char?" do
|
63
|
+
it "should include Strings" do
|
64
|
+
expect(subject.include_char?('A')).to be(true)
|
65
|
+
end
|
52
66
|
end
|
53
67
|
|
54
|
-
|
55
|
-
|
68
|
+
describe "#include?" do
|
69
|
+
it "should include Integers" do
|
70
|
+
expect(subject).to include(0x41)
|
71
|
+
end
|
56
72
|
end
|
57
73
|
|
58
|
-
|
59
|
-
|
74
|
+
describe "#select_bytes" do
|
75
|
+
it "should be able to select bytes" do
|
76
|
+
sub_set = subject.select_bytes { |c| c <= 0x42 }
|
60
77
|
|
61
|
-
|
78
|
+
expect(sub_set).to be == [0x41, 0x42]
|
79
|
+
end
|
62
80
|
end
|
63
81
|
|
64
|
-
|
65
|
-
|
82
|
+
describe "#select_chars" do
|
83
|
+
it "should be able to select chars" do
|
84
|
+
sub_set = subject.select_chars { |c| c <= 'B' }
|
66
85
|
|
67
|
-
|
86
|
+
expect(sub_set).to be == ['A', 'B']
|
87
|
+
end
|
68
88
|
end
|
69
89
|
|
70
|
-
|
71
|
-
|
90
|
+
describe "#random_byte" do
|
91
|
+
it "should return a random byte" do
|
92
|
+
expect(subject).to include(subject.random_byte)
|
93
|
+
end
|
72
94
|
end
|
73
95
|
|
74
|
-
|
75
|
-
|
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
|
76
100
|
end
|
77
101
|
|
78
|
-
|
79
|
-
|
80
|
-
subject.
|
81
|
-
|
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)
|
107
|
+
end
|
82
108
|
end
|
83
109
|
|
84
|
-
|
85
|
-
|
86
|
-
subject.
|
87
|
-
|
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)
|
115
|
+
end
|
88
116
|
end
|
89
117
|
|
90
118
|
describe "#random_bytes" do
|
91
119
|
it "should return a random Array of bytes" do
|
92
|
-
|
120
|
+
random_bytes = subject.random_bytes(10)
|
93
121
|
|
94
|
-
expect(
|
122
|
+
expect(random_bytes.all? { |b| subject.include?(b) }).to be(true)
|
95
123
|
end
|
96
124
|
|
97
125
|
context "with a range of lengths" do
|
98
126
|
it "should return a random Array of bytes with a varying length" do
|
99
|
-
|
127
|
+
random_bytes = subject.random_bytes(5..10)
|
100
128
|
|
101
|
-
expect(
|
102
|
-
expect(
|
129
|
+
expect(random_bytes.length).to be_between(5, 10)
|
130
|
+
expect(random_bytes.all? { |b| subject.include?(b) }).to be(true)
|
103
131
|
end
|
104
132
|
end
|
105
133
|
end
|
106
134
|
|
107
135
|
describe "#random_chars" do
|
108
136
|
it "should return a random Array of chars" do
|
109
|
-
|
137
|
+
random_chars = subject.random_chars(10)
|
110
138
|
|
111
|
-
expect(
|
139
|
+
expect(random_chars.all? { |c| subject.include_char?(c) }).to be(true)
|
112
140
|
end
|
113
141
|
|
114
142
|
context "with a range of lengths" do
|
115
143
|
it "should return a random Array of chars with a varying length" do
|
116
|
-
|
144
|
+
random_chars = subject.random_chars(5..10)
|
117
145
|
|
118
|
-
expect(
|
119
|
-
expect(
|
146
|
+
expect(random_chars.length).to be_between(5, 10)
|
147
|
+
expect(random_chars.all? { |c| subject.include_char?(c) }).to be(true)
|
120
148
|
end
|
121
149
|
end
|
122
150
|
end
|
123
151
|
|
124
152
|
describe "#random_string" do
|
125
153
|
it "should return a random String of chars" do
|
126
|
-
|
154
|
+
random_string = subject.random_string(10)
|
127
155
|
|
128
|
-
expect(
|
156
|
+
expect(random_string.chars.all? { |b|
|
157
|
+
subject.include_char?(b)
|
158
|
+
}).to be(true)
|
129
159
|
end
|
130
160
|
|
131
161
|
context "with a range of lengths" do
|
132
162
|
it "should return a random String of chars with a varying length" do
|
133
|
-
|
163
|
+
random_string = subject.random_string(5..10)
|
134
164
|
|
135
|
-
expect(
|
136
|
-
expect(
|
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)
|
137
169
|
end
|
138
170
|
end
|
139
171
|
end
|
140
172
|
|
141
173
|
describe "#random_distinct_bytes" do
|
142
174
|
it "should return a random Array of unique bytes" do
|
143
|
-
|
175
|
+
random_bytes = subject.random_distinct_bytes(10)
|
144
176
|
|
145
|
-
expect(
|
146
|
-
expect(
|
177
|
+
expect(random_bytes.uniq).to be == random_bytes
|
178
|
+
expect(random_bytes.all? { |b| subject.include_byte?(b) }).to be(true)
|
147
179
|
end
|
148
180
|
|
149
|
-
context "with a
|
181
|
+
context "with a Range of lengths" do
|
150
182
|
it "should return a random Array of unique bytes with a varying length" do
|
151
|
-
|
183
|
+
random_bytes = subject.random_distinct_bytes(5..10)
|
152
184
|
|
153
|
-
expect(
|
154
|
-
expect(
|
155
|
-
expect(
|
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)
|
156
188
|
end
|
157
189
|
end
|
158
190
|
end
|
159
191
|
|
160
192
|
describe "#random_distinct_chars" do
|
161
193
|
it "should return a random Array of unique chars" do
|
162
|
-
|
194
|
+
random_chars = subject.random_distinct_chars(10)
|
163
195
|
|
164
|
-
expect(
|
165
|
-
expect(
|
196
|
+
expect(random_chars.uniq).to be == random_chars
|
197
|
+
expect(random_chars.all? { |c| subject.include_char?(c) }).to be(true)
|
166
198
|
end
|
167
199
|
|
168
200
|
context "with a range of lengths" do
|
169
201
|
it "should return a random Array of unique chars with a varying length" do
|
170
|
-
|
202
|
+
random_chars = subject.random_distinct_chars(5..10)
|
171
203
|
|
172
|
-
expect(
|
173
|
-
expect(
|
174
|
-
expect(
|
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)
|
175
207
|
end
|
176
208
|
end
|
177
209
|
end
|
178
210
|
|
179
|
-
|
180
|
-
|
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
|
181
215
|
end
|
182
216
|
|
183
|
-
|
184
|
-
|
217
|
+
describe "#|" do
|
218
|
+
it "should be able to be unioned with another set of chars" do
|
219
|
+
super_set = (subject | described_class['D'])
|
185
220
|
|
186
|
-
|
187
|
-
|
221
|
+
expect(super_set).to be_kind_of(described_class)
|
222
|
+
expect(super_set).to be == described_class['A'..'Z', 'D']
|
223
|
+
end
|
188
224
|
end
|
189
225
|
|
190
|
-
|
191
|
-
|
226
|
+
describe "#-" do
|
227
|
+
it "should be able to be removed from another set of chars" do
|
228
|
+
sub_set = (subject - described_class['B'])
|
192
229
|
|
193
|
-
|
194
|
-
|
230
|
+
expect(sub_set).to be_kind_of(described_class)
|
231
|
+
expect(sub_set).to be_subset(subject)
|
232
|
+
end
|
195
233
|
end
|
196
234
|
|
197
235
|
describe "#strings_in" do
|
198
|
-
|
199
|
-
|
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
|
200
290
|
end
|
201
291
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
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
|
207
314
|
end
|
208
315
|
end
|
209
316
|
|
210
|
-
|
211
|
-
|
212
|
-
|
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
|
213
322
|
end
|
214
323
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chars
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Postmodern
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -61,11 +61,15 @@ files:
|
|
61
61
|
- spec/extensions/integer_spec.rb
|
62
62
|
- spec/extensions/string_spec.rb
|
63
63
|
- spec/spec_helper.rb
|
64
|
-
homepage: https://github.com/postmodern/chars#readme
|
64
|
+
homepage: https://github.com/postmodern/chars.rb#readme
|
65
65
|
licenses:
|
66
66
|
- MIT
|
67
|
-
metadata:
|
68
|
-
|
67
|
+
metadata:
|
68
|
+
documentation_uri: https://rubydoc.info/gems/chars
|
69
|
+
source_code_uri: https://github.com/postmodern/chars.rb
|
70
|
+
bug_tracker_uri: https://github.com/postmodern/chars.rb/issues
|
71
|
+
changelog_uri: https://github.com/postmodern/chars.rb/blob/master/ChangeLog.md
|
72
|
+
post_install_message:
|
69
73
|
rdoc_options: []
|
70
74
|
require_paths:
|
71
75
|
- lib
|
@@ -73,15 +77,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
77
|
requirements:
|
74
78
|
- - ">="
|
75
79
|
- !ruby/object:Gem::Version
|
76
|
-
version:
|
80
|
+
version: 2.0.0
|
77
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
82
|
requirements:
|
79
83
|
- - ">="
|
80
84
|
- !ruby/object:Gem::Version
|
81
85
|
version: '0'
|
82
86
|
requirements: []
|
83
|
-
rubygems_version: 3.2.
|
84
|
-
signing_key:
|
87
|
+
rubygems_version: 3.2.22
|
88
|
+
signing_key:
|
85
89
|
specification_version: 4
|
86
90
|
summary: A Ruby library for working with various character sets
|
87
91
|
test_files: []
|