chars 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,59 +2,63 @@ require 'chars/char_set'
2
2
 
3
3
  module Chars
4
4
  # The numeric decimal character set
5
- NUMERIC = CharSet.new('0'..'9')
5
+ NUMERIC = CharSet['0'..'9']
6
6
 
7
7
  # The octal character set
8
- OCTAL = CharSet.new('0'..'7')
8
+ OCTAL = CharSet['0'..'7']
9
9
 
10
10
  # The upper-case hexadecimal character set
11
- UPPERCASE_HEXADECIMAL = NUMERIC + ('A'..'F')
11
+ UPPERCASE_HEXADECIMAL = NUMERIC | CharSet['A'..'F']
12
12
 
13
13
  # The lower-case hexadecimal character set
14
- LOWERCASE_HEXADECIMAL = NUMERIC + ('a'..'f')
14
+ LOWERCASE_HEXADECIMAL = NUMERIC | CharSet['a'..'f']
15
15
 
16
16
  # The hexadecimal character set
17
- HEXADECIMAL = UPPERCASE_HEXADECIMAL + LOWERCASE_HEXADECIMAL
17
+ HEXADECIMAL = UPPERCASE_HEXADECIMAL | LOWERCASE_HEXADECIMAL
18
18
 
19
19
  # The upper-case alpha character set
20
- UPPERCASE_ALPHA = CharSet.new('A'..'Z')
20
+ UPPERCASE_ALPHA = CharSet['A'..'Z']
21
21
 
22
22
  # The lower-case alpha character set
23
- LOWERCASE_ALPHA = CharSet.new('a'..'z')
23
+ LOWERCASE_ALPHA = CharSet['a'..'z']
24
24
 
25
25
  # The alpha character set
26
- ALPHA = UPPERCASE_ALPHA + LOWERCASE_ALPHA
26
+ ALPHA = UPPERCASE_ALPHA | LOWERCASE_ALPHA
27
27
 
28
28
  # The alpha-numeric character set
29
- ALPHA_NUMERIC = ALPHA + NUMERIC
29
+ ALPHA_NUMERIC = ALPHA | NUMERIC
30
30
 
31
31
  # The punctuation character set
32
- PUNCTUATION = CharSet.new(' ', '\'', '"', '`', ',', ';', ':', '~', '-',
33
- '(', ')', '[', ']', '{', '}', '.', '?', '!')
32
+ PUNCTUATION = CharSet[' ', '\'', '"', '`', ',', ';', ':', '~', '-',
33
+ '(', ')', '[', ']', '{', '}', '.', '?', '!']
34
34
 
35
35
  # The symbolic character set
36
- SYMBOLS = PUNCTUATION + ['@', '#', '$', '%', '^', '&', '*', '_', '+',
37
- '=', '|', '\\', '<', '>', '/']
36
+ SYMBOLS = PUNCTUATION | CharSet[
37
+ '@', '#', '$', '%', '^', '&', '*', '_', '+',
38
+ '=', '|', '\\', '<', '>', '/'
39
+ ]
38
40
 
39
41
  # The space character set
40
42
  SPACE = CharSet.new(' ', "\f", "\n", "\r", "\t", "\v")
41
43
 
42
44
  # The set of printable characters (not including spaces)
43
- VISIBLE = ALPHA_NUMERIC + ['\'', '"', '`', ',', ';', ':', '~', '-',
44
- '(', ')', '[', ']', '{', '}', '.', '?', '!', '@', '#', '$',
45
- '%', '^', '&', '*', '_', '+', '=', '|', '\\', '<', '>', '/']
45
+ VISIBLE = ALPHA_NUMERIC | CharSet[
46
+ '\'', '"', '`', ',', ';', ':', '~', '-',
47
+ '(', ')', '[', ']', '{', '}', '.', '?', '!', '@', '#', '$',
48
+ '%', '^', '&', '*', '_', '+', '=', '|', '\\', '<', '>', '/'
49
+ ]
46
50
 
47
51
  # The set of printable characters (including spaces)
48
- PRINTABLE = ALPHA_NUMERIC + PUNCTUATION + SYMBOLS + SPACE
52
+ PRINTABLE = ALPHA_NUMERIC | PUNCTUATION | SYMBOLS | SPACE
49
53
 
50
54
  # The control-char character set
51
- CONTROL = CharSet.new(0..0x1f, 0x7f)
55
+ CONTROL = CharSet[0..0x1f, 0x7f]
52
56
 
53
57
  # The signed ASCII character set
54
- SIGNED_ASCII = CharSet.new(0..0x7f)
58
+ SIGNED_ASCII = CharSet[0..0x7f]
55
59
 
56
60
  # The full 8-bit character set
57
- ASCII = CharSet.new(0..0xff)
61
+ ASCII = CharSet[0..0xff]
58
62
 
59
63
  #
60
64
  # The decimal-digit character set.
@@ -11,7 +11,7 @@ class Integer
11
11
  # @see Chars.numeric
12
12
  #
13
13
  def numeric?
14
- Chars::NUMERIC.include?(self)
14
+ Chars::NUMERIC.include_byte?(self)
15
15
  end
16
16
 
17
17
  #
@@ -23,7 +23,7 @@ class Integer
23
23
  # @see Chars.octal
24
24
  #
25
25
  def octal?
26
- Chars::OCTAL.include?(self)
26
+ Chars::OCTAL.include_byte?(self)
27
27
  end
28
28
 
29
29
  #
@@ -37,7 +37,7 @@ class Integer
37
37
  # @see Chars.uppercase_hexadecimal
38
38
  #
39
39
  def uppercase_hex?
40
- Chars::UPPERCASE_HEXADECIMAL.include?(self)
40
+ Chars::UPPERCASE_HEXADECIMAL.include_byte?(self)
41
41
  end
42
42
 
43
43
  #
@@ -51,7 +51,7 @@ class Integer
51
51
  # @see Chars.lowercase_hexadecimal
52
52
  #
53
53
  def lowercase_hex?
54
- Chars::LOWERCASE_HEXADECIMAL.include?(self)
54
+ Chars::LOWERCASE_HEXADECIMAL.include_byte?(self)
55
55
  end
56
56
 
57
57
  #
@@ -63,7 +63,7 @@ class Integer
63
63
  # @see Chars.hexadecimal
64
64
  #
65
65
  def hex?
66
- Chars::HEXADECIMAL.include?(self)
66
+ Chars::HEXADECIMAL.include_byte?(self)
67
67
  end
68
68
 
69
69
  #
@@ -77,7 +77,7 @@ class Integer
77
77
  # @see Chars.uppercase_alpha
78
78
  #
79
79
  def uppercase_alpha?
80
- Chars::UPPERCASE_ALPHA.include?(self)
80
+ Chars::UPPERCASE_ALPHA.include_byte?(self)
81
81
  end
82
82
 
83
83
  #
@@ -91,7 +91,7 @@ class Integer
91
91
  # @see Chars.lowercase_alpha
92
92
  #
93
93
  def lowercase_alpha?
94
- Chars::LOWERCASE_ALPHA.include?(self)
94
+ Chars::LOWERCASE_ALPHA.include_byte?(self)
95
95
  end
96
96
 
97
97
  #
@@ -103,7 +103,7 @@ class Integer
103
103
  # @see Chars.alpha
104
104
  #
105
105
  def alpha?
106
- Chars::ALPHA.include?(self)
106
+ Chars::ALPHA.include_byte?(self)
107
107
  end
108
108
 
109
109
  #
@@ -115,7 +115,7 @@ class Integer
115
115
  # @see Chars.alpha_numeric
116
116
  #
117
117
  def alpha_numeric?
118
- Chars::ALPHA_NUMERIC.include?(self)
118
+ Chars::ALPHA_NUMERIC.include_byte?(self)
119
119
  end
120
120
 
121
121
  #
@@ -127,7 +127,7 @@ class Integer
127
127
  # @see Chars.punctuation
128
128
  #
129
129
  def punctuation?
130
- Chars::PUNCTUATION.include?(self)
130
+ Chars::PUNCTUATION.include_byte?(self)
131
131
  end
132
132
 
133
133
  #
@@ -139,7 +139,7 @@ class Integer
139
139
  # @see Chars.symbols
140
140
  #
141
141
  def symbolic?
142
- Chars::SYMBOLS.include?(self)
142
+ Chars::SYMBOLS.include_byte?(self)
143
143
  end
144
144
 
145
145
  #
@@ -151,7 +151,7 @@ class Integer
151
151
  # @see Chars.space
152
152
  #
153
153
  def space?
154
- Chars::SPACE.include?(self)
154
+ Chars::SPACE.include_byte?(self)
155
155
  end
156
156
 
157
157
  #
@@ -163,7 +163,7 @@ class Integer
163
163
  # @see Chars.visible
164
164
  #
165
165
  def visible?
166
- Chars::VISIBLE.include?(self)
166
+ Chars::VISIBLE.include_byte?(self)
167
167
  end
168
168
 
169
169
  #
@@ -175,7 +175,7 @@ class Integer
175
175
  # @see Chars.printable
176
176
  #
177
177
  def printable?
178
- Chars::PRINTABLE.include?(self)
178
+ Chars::PRINTABLE.include_byte?(self)
179
179
  end
180
180
 
181
181
  #
@@ -188,7 +188,7 @@ class Integer
188
188
  # @see Chars.control
189
189
  #
190
190
  def control?
191
- Chars::CONTROL.include?(self)
191
+ Chars::CONTROL.include_byte?(self)
192
192
  end
193
193
 
194
194
  #
@@ -200,7 +200,7 @@ class Integer
200
200
  # @see Chars.signed_ascii
201
201
  #
202
202
  def signed_ascii?
203
- Chars::SIGNED_ASCII.include?(self)
203
+ Chars::SIGNED_ASCII.include_byte?(self)
204
204
  end
205
205
 
206
206
  #
@@ -212,7 +212,7 @@ class Integer
212
212
  # @see Chars.ascii
213
213
  #
214
214
  def ascii?
215
- Chars::ASCII.include?(self)
215
+ Chars::ASCII.include_byte?(self)
216
216
  end
217
217
 
218
218
  end
@@ -1,4 +1,4 @@
1
1
  module Chars
2
2
  # Chars version
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.1'
4
4
  end
@@ -1,6 +1,5 @@
1
- require 'chars/chars'
2
-
3
1
  require 'spec_helper'
2
+ require 'chars/chars'
4
3
 
5
4
  describe Chars::CharSet do
6
5
  before(:all) do
@@ -9,54 +8,44 @@ describe Chars::CharSet do
9
8
  @integers = @integer_range.to_a
10
9
  @strings = @string_range.to_a
11
10
 
12
- @char_set = Chars::CharSet.new(*@strings)
11
+ @char_set = described_class.new(*@strings)
13
12
  end
14
13
 
15
- it "may be created with String arguments" do
16
- @chars = Chars::CharSet.new(*@strings)
14
+ describe "#initialize" do
15
+ it "may be created with String arguments" do
16
+ @chars = described_class.new(*@strings)
17
17
 
18
- @strings.each do |s|
19
- @chars.include_char?(s).should == true
18
+ @strings.all? { |s| @chars.include_char?(s) }.should == true
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
+ it "may be created with an Array of Strings" do
22
+ @chars = described_class.new(@strings)
25
23
 
26
- @strings.each do |s|
27
- @chars.include_char?(s).should == true
24
+ @strings.all? { |s| @chars.include_char?(s) }.should == true
28
25
  end
29
- end
30
26
 
31
- it "may be created with a Range of Strings" do
32
- @chars = Chars::CharSet.new(@string_range)
27
+ it "may be created with a Range of Strings" do
28
+ @chars = described_class.new(@string_range)
33
29
 
34
- @strings.each do |s|
35
- @chars.include_char?(s).should == true
30
+ @strings.all? { |s| @chars.include_char?(s) }.should == true
36
31
  end
37
- end
38
32
 
39
- it "may be created with Integer arguments" do
40
- @chars = Chars::CharSet.new(*@integers)
33
+ it "may be created with Integer arguments" do
34
+ @chars = described_class.new(*@integers)
41
35
 
42
- @integers.each do |i|
43
- @chars.include?(i).should == true
36
+ @integers.all? { |i| @chars.include?(i) }.should == true
44
37
  end
45
- end
46
38
 
47
- it "may be created with an Array of Integers" do
48
- @chars = Chars::CharSet.new(@integers)
39
+ it "may be created with an Array of Integers" do
40
+ @chars = described_class.new(@integers)
49
41
 
50
- @integers.each do |i|
51
- @chars.include?(i).should == true
42
+ @integers.all? { |i| @chars.include?(i) }.should == true
52
43
  end
53
- end
54
44
 
55
- it "may be created with a Range of Integers" do
56
- @chars = Chars::CharSet.new(@integer_range)
45
+ it "may be created with a Range of Integers" do
46
+ @chars = described_class.new(@integer_range)
57
47
 
58
- @integers.each do |i|
59
- @chars.include?(i).should == true
48
+ @integers.all? { |i| @chars.include?(i) }.should == true
60
49
  end
61
50
  end
62
51
 
@@ -65,7 +54,7 @@ describe Chars::CharSet do
65
54
  end
66
55
 
67
56
  it "should include Integers" do
68
- @char_set.include?(0x41).should == true
57
+ @char_set.should include(0x41)
69
58
  end
70
59
 
71
60
  it "should be able to select bytes" do
@@ -81,7 +70,7 @@ describe Chars::CharSet do
81
70
  end
82
71
 
83
72
  it "should return a random byte" do
84
- @char_set.include?(@char_set.random_byte).should == true
73
+ @char_set.should include(@char_set.random_byte)
85
74
  end
86
75
 
87
76
  it "should return a random char" do
@@ -89,117 +78,102 @@ describe Chars::CharSet do
89
78
  end
90
79
 
91
80
  it "should iterate over n random bytes" do
92
- @char_set.each_random_byte(10) do |b|
93
- @char_set.include?(b).should == true
94
- end
81
+ @char_set.each_random_byte(10).all? { |b|
82
+ @char_set.include?(b)
83
+ }.should == true
95
84
  end
96
85
 
97
86
  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
100
- end
87
+ @char_set.each_random_char(10).all? { |c|
88
+ @char_set.include_char?(c)
89
+ }.should == true
101
90
  end
102
91
 
103
92
  it "should return a random Array of bytes" do
104
93
  bytes = @char_set.random_bytes(10)
105
94
 
106
- bytes.each do |b|
107
- @char_set.include?(b).should == true
108
- end
95
+ bytes.all? { |b| @char_set.include?(b) }.should == true
109
96
  end
110
97
 
111
98
  it "should return a random Array of chars" do
112
99
  chars = @char_set.random_chars(10)
113
100
 
114
- chars.each do |c|
115
- @char_set.include_char?(c).should == true
116
- end
101
+ chars.all? { |c| @char_set.include_char?(c) }.should == true
117
102
  end
118
103
 
119
104
  it "should return a random Array of bytes with a varying length" do
120
105
  bytes = @char_set.random_bytes(5..10)
121
106
 
122
- bytes.length.between?(5, 10).should == true
123
- bytes.each do |b|
124
- @char_set.include?(b).should == true
125
- end
107
+ bytes.length.should be_between(5, 10)
108
+ bytes.all? { |b| @char_set.include?(b) }.should == true
126
109
  end
127
110
 
128
111
  it "should return a random Array of chars with a varying length" do
129
112
  chars = @char_set.random_chars(5..10)
130
113
 
131
- chars.length.between?(5, 10).should == true
132
- chars.each do |c|
133
- @char_set.include_char?(c).should == true
134
- end
114
+ chars.length.should be_between(5, 10)
115
+ chars.all? { |c| @char_set.include_char?(c) }.should == true
135
116
  end
136
117
 
137
118
  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
140
- end
119
+ string = @char_set.random_string(10)
120
+
121
+ string.chars.all? { |b| @char_set.include_char?(b) }.should == true
141
122
  end
142
123
 
143
124
  it "should return a random String of chars with a varying length" do
144
125
  string = @char_set.random_string(5..10)
145
126
 
146
- string.length.between?(5, 10)
147
- string.each_byte do |b|
148
- @char_set.include?(b).should == true
149
- end
127
+ string.length.should be_between(5, 10)
128
+ string.chars.all? { |b| @char_set.include_char?(b) }.should == true
150
129
  end
151
130
 
152
131
  it "should return a random Array of unique bytes" do
153
132
  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
157
- end
133
+
134
+ bytes.uniq.should == bytes
135
+ bytes.all? { |b| @char_set.include?(b) }.should == true
158
136
  end
159
137
 
160
138
  it "should return a random Array of unique chars" do
161
139
  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
165
- end
140
+
141
+ chars.uniq.should == chars
142
+ chars.all? { |c| @char_set.include_char?(c) }.should == true
166
143
  end
167
144
 
168
145
  it "should return a random Array of unique bytes with a varying length" do
169
146
  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
174
- end
147
+
148
+ bytes.uniq.should == bytes
149
+ bytes.length.should be_between(5, 10)
150
+ bytes.all? { |b| @char_set.include?(b) }.should == true
175
151
  end
176
152
 
177
153
  it "should return a random Array of unique chars with a varying length" do
178
154
  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
183
- end
184
- end
185
155
 
156
+ chars.uniq.should == chars
157
+ chars.length.should be_between(5, 10)
158
+ chars.all? { |c| @char_set.include_char?(c) }.should == true
159
+ end
186
160
 
187
161
  it "should be able to be compared with another set of chars" do
188
- (@char_set == Chars::CharSet[('A'..'Z')]).should == true
162
+ @char_set.should == described_class['A'..'Z']
189
163
  end
190
164
 
191
165
  it "should be able to be unioned with another set of chars" do
192
- super_set = (@char_set | Chars::CharSet['D'])
166
+ super_set = (@char_set | described_class['D'])
193
167
 
194
- super_set.class.should == Chars::CharSet
195
- super_set.should == Chars::CharSet[('A'..'Z'), 'D']
168
+ super_set.class.should == described_class
169
+ super_set.should == described_class['A'..'Z', 'D']
196
170
  end
197
171
 
198
172
  it "should be able to be removed from another set of chars" do
199
- sub_set = (@char_set - Chars::CharSet['B'])
173
+ sub_set = (@char_set - described_class['B'])
200
174
 
201
- sub_set.class.should == Chars::CharSet
202
- sub_set.subset?(@char_set).should == true
175
+ sub_set.class.should == described_class
176
+ sub_set.should be_subset(@char_set)
203
177
  end
204
178
 
205
179
  it "should find one sub-string from a String belonging to the char set" do
@@ -214,7 +188,7 @@ describe Chars::CharSet do
214
188
  end
215
189
 
216
190
  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
191
+ @char_set.should === "AABCBAA"
192
+ @char_set.should_not === "AA!!EE"
219
193
  end
220
194
  end