random_token 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Random Token
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/random_token.png)][gem]
4
+ [![Build Status](https://travis-ci.org/sibevin/random_token.png?branch=build)][travis]
5
+
6
+ [gem]: https://rubygems.org/gems/random_token
7
+ [travis]: https://travis-ci.org/sibevin/random_token
8
+
3
9
  A simple way to generate a random token.
4
10
 
5
11
  jjukoguwegcvclkuhljvsblrpfnd
@@ -17,163 +23,179 @@ A simple way to generate a random token.
17
23
 
18
24
  ### Basic
19
25
 
20
- Use "gen" method to create a random token with a given length.
26
+ Use "gen" method with a format string to create a random token.
27
+
28
+ RandomToken.gen("%8?")
29
+ # "6HRQZp8O"
30
+
31
+ RandomToken.gen("%4H-%4H-%4H-%4H")
32
+ # "FD77-2792-91A9-6CE3"
33
+
34
+ Or just give the token length.
21
35
 
22
36
  RandomToken.gen(8)
23
37
  # "iUEFxcG2"
24
38
 
25
39
  Some options can help to modify the token format.
26
40
 
27
- RandomToken.gen(20, :seed => :alphabet, :friendly => true, :case => :up)
41
+ RandomToken.gen(20, seed: :alphabet, friendly: true, case: :up)
42
+ # "YTHJHTXKSSXTPLARALRH"
43
+
44
+ Or just simplify as:
45
+
46
+ RandomToken.gen(20, s: :a, f: true, c: :u)
28
47
  # "YTHJHTXKSSXTPLARALRH"
29
48
 
30
- Use string format to create a random in the particular format. Please read "Format" below to get more details.
49
+ ### Format
50
+
51
+ You can embed random tokens in your own string using a format string. Here are some examples:
52
+
53
+ RandomToken.gen("%2A-%4n")
54
+ # "VS-6921"
55
+
56
+ RandomToken.genf("Your redeem code: %16?")
57
+ # "Your redeem code: 4xgRH2JaLxtR3dWK"
58
+
59
+ RandomToken.gen("Lucky Number = %n")
60
+ # "Lucky Number = 8"
61
+
62
+ The directive consists of a percent(%), length, and type.
63
+
64
+ %[length][type]
65
+
66
+ where
67
+
68
+ * length
69
+
70
+ > The length of random token you want to create. The default value is 1 if no length is given.
71
+
72
+ * type
73
+
74
+ > A - upper-case alphabets
75
+ > a - down-case alphabets
76
+ > n - numbers
77
+ > b - binaries
78
+ > o - octal digits
79
+ > H - upper-case hexadecimal digits
80
+ > h - down-case hexadecimal digits
81
+ > X - upper-case alphabets and numbers
82
+ > x - down-case alphabets and numbers
83
+ > ? - mixed-case alphabets and numbers
84
+ > % - show the % sign
85
+
86
+ `:seed`, `:friendly`, `:case` options are supported in the format string, and they would override the type behavior.
87
+
88
+ RandomToken.gen("%32X", seed: :a, friendly: true, case: :d)
89
+ # "ddbpeqhtmaenxsaksxrmeaawnwpkfess"
90
+
91
+ ### Length
92
+
93
+ You can just give a token length. For example:
94
+
95
+ RandomToken.gen(8)
96
+ # "iUEFxcG2"
97
+
98
+ The random token are created with mixed-case alphbets and numbers by default, same as the format string, you can use `:seed`, `:friendly`, `:case` options to modify the your token.
31
99
 
32
- RandomToken.gen("Give me a token %8?")
33
- # "Give me a token 6HRQZp8O"
100
+ ## Options
34
101
 
35
102
  ### Seed
36
103
 
37
- Use `:seed` option to customize the random characters.
104
+ Use `:s` (or `:seed`) option to customize the random characters. Here are available options:
38
105
 
39
106
  Use `:a` (or `:alphabet`, `:l`, `:letter`) to create a token with alphabets.
40
107
 
41
- RandomToken.gen(32, :seed => :a)
108
+ RandomToken.gen(32, s: :a)
42
109
  # "DTVbCnRldQnLAoERHAzjLOTJBTWoBGQb"
43
110
 
44
111
  Use `:n` (or `:number`, `10`, `1`) to create a token with numbers.
45
112
 
46
- RandomToken.gen(32, :seed => :n)
113
+ RandomToken.gen(32, s: :n)
47
114
  # "41107838709625922353782552721916"
48
115
 
49
116
  Use `:b` (or `:binary`, `2`) to create a binary token.
50
117
 
51
- RandomToken.gen(32, :seed => :b)
118
+ RandomToken.gen(32, s: :b)
52
119
  # "11101001110110110000101101010101"
53
120
 
54
121
  Use `:o` (or `:oct`, 8) to create a octal token.
55
122
 
56
- RandomToken.gen(32, :seed => :o)
123
+ RandomToken.gen(32, s: :o)
57
124
  # "57531726664723324643140173424423"
58
125
 
59
126
  Use `:h` (or `:hex`, 16) to create a hexadecimal token.
60
127
 
61
- RandomToken.gen(32, :seed => :h)
128
+ RandomToken.gen(32, s: :h)
62
129
  # "B7E1FD250D250008314496AE48EDE310"
63
130
 
64
131
  Use an `array` to customize random seeds.
65
132
 
66
- RandomToken.gen(32, :seed => ['a', 'b', 'c'])
133
+ RandomToken.gen(32, s: ['a', 'b', 'c'])
67
134
  # "cbbcbcabbbacbaaababcbcabcacbcacb"
68
135
 
69
136
  Use a `hash` to customize random seeds and their distribution. For example, the following hash would create a token with "a" and "b" where p("a") = 1/3 and p("b") = 2/3.
70
137
 
71
- RandomToken.gen(32, :seed => { 'a' => 1, 'b' => 2 })
138
+ RandomToken.gen(32, s: { 'a' => 1, 'b' => 2 })
72
139
  # "bbabbaaaabaabbbabbbbbbbbbaababbb"
73
140
 
74
- ### Friendly
141
+ ### Friendly & Mask
75
142
 
76
- Use `:friendly` option to remove the ambiguous characters, the default mask includes *1, I, l, i, 0, O, o, Q, D, C, c, G, 9, 6, U, u, V, v, E, F, M, N, 8, B*.
143
+ Use `:f` (or `:friendly`) option to remove the ambiguous characters, the default mask includes *1, I, l, i, 0, O, o, Q, D, C, c, G, 9, 6, U, u, V, v, E, F, M, N, 8, B*.
77
144
 
78
- RandomToken.gen(32, :friendly => true)
145
+ RandomToken.gen(32, f: true)
79
146
  # "fjx5WTb4wbPmbwb7b4szzY4szfrqtJLj"
80
147
 
81
- The default `:friendly` option is false. There is a convenient method "genf" using `:friendly => true` by default.
148
+ The default `:friendly` option is false. There is a convenient method "genf" using `friendly: true` by default.
82
149
 
83
150
  RandomToken.genf(32)
84
151
  # "kPafJh5gHAPJjYsssjW7yhthPr4Zg4t3"
85
152
 
86
153
  Friendly is not supported with some `:seed` options(`:b`, `:o`, `:h` and customized hash seeds).
87
154
 
88
- RandomToken.genf(32, :seed => :b)
155
+ RandomToken.genf(32, s: :b)
89
156
  # RandomToken::RandomTokenError: RandomToken::RandomTokenError
90
157
 
91
- Use `:mask` to customize your own friendly mask. Note that `:friendly` is true by default if `:mask` is given.
158
+ Use `:m` (or `:mask`) to customize your own friendly mask. Note that `:friendly` is true by default if `:mask` is given.
92
159
 
93
- RandomToken.gen(32, :mask => ['a', 'A', 'b', 'c'])
160
+ RandomToken.gen(32, m: ['a', 'A', 'b', 'c'])
94
161
  # "QlHhMpfrGnOykMS8tpfYrW0EnqvRsItw"
95
162
 
96
163
  ### Case
97
164
 
98
- Use `:case` option to modify the token case.
165
+ Use `:c` (or `:case`) option to modify the token case. Here are available options:
99
166
 
100
167
  Use `:u` (or `:up`) to create an upper-case token.
101
168
 
102
- RandomToken.gen(32, :case => :u)
169
+ RandomToken.gen(32, c: :u)
103
170
  # "YKNOAMC5FQZTMRS9U3SI4U9QWEMU7TLL"
104
171
 
105
172
  Use `:d` (or `:down`) to create a down-case token.
106
173
 
107
- RandomToken.gen(32, :case => :d)
174
+ RandomToken.gen(32, c: :d)
108
175
  # "8fvs6dfxko4buxcho23vz5jpxw9arj7t"
109
176
 
110
177
  Use `:m` (or `:mixed`) to create a mixed-case token. It is the default option.
111
178
 
112
- RandomToken.gen(32, :case => :m)
179
+ RandomToken.gen(32, c: :m)
113
180
  # "BjWRJjU2iKF2O1cKrWpnF1EzZHuoUCM5"
114
181
 
115
182
  Case is not supported with some `:seed` options(`:n`, `:b`, `:o` and customized hash seeds).
116
183
 
117
- RandomToken.gen(32, :seed => :n, :case => :u)
184
+ RandomToken.gen(32, s: :n, c: :u)
118
185
  # RandomToken::RandomTokenError: RandomToken::RandomTokenError
119
186
 
120
- With :hex seed, i.e, `:seed => :h`, the default case is changed to upper-case. You can override it by giving the `:case` option.
187
+ With :hex seed, i.e, `s: :h`, the default case is changed to upper-case. You can override it by giving the `:case` option.
121
188
 
122
- RandomToken.gen(32, :seed => :h, :case => :d)
189
+ RandomToken.gen(32, s: :h, c: :d)
123
190
  # "d331ce7dae87f3bb3dcb3975be9c430d"
124
191
 
125
- ### Format
126
-
127
- You can embed random tokens in your own string using the format feature. Here are some examples:
128
-
129
- RandomToken.gen("%4H-%4H-%4H-%4H")
130
- # "FD77-2792-91A9-6CE3"
131
-
132
- RandomToken.gen("%2A-%4n")
133
- # "VS-6921"
134
-
135
- RandomToken.genf("Your redeem code: %16?")
136
- # "Your redeem code: 4xgRH2JaLxtR3dWK"
137
-
138
- RandomToken.gen("Lucky Number = %n")
139
- # "Lucky Number = 8"
140
-
141
- The directive consists of a percent(%), length, and type.
142
-
143
- %[length][type]
144
-
145
- where
146
-
147
- * length
148
-
149
- > The length of random token you want to create. The default value is 1 if no length is given.
150
-
151
- * type
152
-
153
- > A - upper-case alphabets
154
- > a - down-case alphabets
155
- > n - numbers
156
- > b - binaries
157
- > o - octal digits
158
- > H - upper-case hexadecimal digits
159
- > h - down-case hexadecimal digits
160
- > X - upper-case alphabets and numbers
161
- > x - down-case alphabets and numbers
162
- > ? - mixed-case alphabets and numbers
163
- > % - show the % sign
164
-
165
- Note that `:seed`, `:friendly`, `:case` options are also supported in the format string, and they would override the type behavior.
166
-
167
- RandomToken.gen("%32X", :seed => :a, :friendly => true, :case => :d)
168
- # "ddbpeqhtmaenxsaksxrmeaawnwpkfess"
169
-
170
192
  ## Use Cases
171
193
 
172
194
  ### Case 1
173
195
 
174
196
  How to create a RPG random desert map?
175
197
 
176
- 1.upto(10).each { puts RandomToken.gen(28, :seed => { '.' => 1000, 'o' => 20, 'O' => 5, '_' => 1 }) }
198
+ 1.upto(10).each { puts RandomToken.gen(28, s: { '.' => 1000, 'o' => 20, 'O' => 5, '_' => 1 }) }
177
199
 
178
200
  ............................
179
201
  .......................O.o..
data/lib/random_token.rb CHANGED
@@ -58,7 +58,25 @@ module RandomToken
58
58
  }
59
59
  }
60
60
 
61
- # The directives used in the {RandomToken.strf} method.
61
+ # The supported options
62
+ SUPPORTED_OPTS = {
63
+ :seed => {
64
+ :abbr => :s
65
+ },
66
+ :case => {
67
+ :abbr => :c,
68
+ :value => [:up, :u, :down, :d, :mixed, :m]
69
+ },
70
+ :mask => {
71
+ :abbr => :m
72
+ },
73
+ :friendly => {
74
+ :abbr => :f,
75
+ :value => [true, false]
76
+ }
77
+ }
78
+
79
+ # The directives used in the format string
62
80
  STRF_ARG_MAP = {
63
81
  'A' => { :seed => :alphabet, :case => :up },
64
82
  'a' => { :seed => :alphabet, :case => :down },
@@ -73,7 +91,7 @@ module RandomToken
73
91
  }
74
92
 
75
93
  class << self
76
- # The major method to generate a random token. It would call {RandomToken.get} or {RandomToken.strf} according to the given arg.
94
+ # The major method to generate a random token.
77
95
  # @param arg [Fixnum, String]
78
96
  # To give a token length or the string format for generating token.
79
97
  # @param options [Hash]
@@ -84,15 +102,9 @@ module RandomToken
84
102
  # The generated token.
85
103
  # @raise [RandomTokenError]
86
104
  # Please see {RandomToken::RandomTokenError}
87
- # @see RandomToken.get
88
- # @see RandomToken.strf
89
105
  def gen(arg, options = {})
90
- if arg.class.name == 'Fixnum'
91
- get(arg, options)
92
- elsif arg.class.name == 'String'
93
- strf(arg, options)
94
- else
95
- raise RandomTokenError.new(:invalid_gen_arg, arg)
106
+ arg_dispatcher(arg, options) do |length, seeds|
107
+ (0...length).map{ seeds[rand(seeds.length)] }.join
96
108
  end
97
109
  end
98
110
 
@@ -105,26 +117,90 @@ module RandomToken
105
117
  gen(arg, { :friendly => true }.merge(options))
106
118
  end
107
119
 
108
- # To generate a random token with a given length.
109
- # @param length [Fixnum]
110
- # The token length.
120
+ # TODO: Add count feature
121
+ =begin
122
+ # Count the number of token combination with given arg and options.
123
+ # @param arg (see RandomToken.gen)
124
+ # @param options (see RandomToken.gen)
125
+ # @return [Fixnum] the number of combination
126
+ # @raise (see RandomToken.gen)
127
+ def count(arg, options = {})
128
+ arg_dispatcher(arg, options, true) do |length, seeds|
129
+ seeds.length ** length
130
+ end
131
+ end
132
+ =end
133
+
134
+ # An old method for downward compatibility and it should be discarded.
135
+ # Use "gen" instead.
136
+ # @param arg (see RandomToken.gen)
111
137
  # @param options (see RandomToken.gen)
112
138
  # @return (see RandomToken.gen)
113
139
  # @raise (see RandomToken.gen)
114
- def get(length, options = {})
115
- seeds = gen_seeds(options)[:seed]
116
- token = (0...length).map{ seeds[rand(seeds.length)] }.join
140
+ def get(arg, options = {})
141
+ gen(arg, options)
117
142
  end
118
143
 
119
- # To generate a random token with a string format. Please see {file:README.rdoc} to get more detail usage and examples
120
- # @param pattern [String]
121
- # The string format pattern.
144
+ # An old method for downward compatibility and it should be discarded.
145
+ # Use "gen" instead.
146
+ # @param arg (see RandomToken.gen)
122
147
  # @param options (see RandomToken.gen)
123
148
  # @return (see RandomToken.gen)
124
149
  # @raise (see RandomToken.gen)
125
- def strf(pattern, options = {})
150
+ def strf(arg, options = {})
151
+ gen(arg, options)
152
+ end
153
+
154
+ private
155
+
156
+ # Decide how to generate/count token according to the arg type.
157
+ def arg_dispatcher(arg, options, count = false)
158
+ options = check_opt(options)
159
+ unless block_given?
160
+ raise RandomTokenError.new(
161
+ "No block is given when calling arg_dispatcher.")
162
+ end
163
+ if arg.is_a?(Fixnum)
164
+ run_by_length(arg, options) do |length, seeds|
165
+ yield(length, seeds)
166
+ end
167
+ elsif arg.is_a?(String)
168
+ result = run_by_pattern(arg, options) do |length, seeds|
169
+ yield(length, seeds)
170
+ end
171
+ return result.join
172
+ # TODO: Add count feature
173
+ =begin
174
+ if count
175
+ return result.delete_if { |x| x.is_a?(String) }.inject(0, :*)
176
+ else
177
+ return result.join
178
+ end
179
+ =end
180
+ else
181
+ raise RandomTokenError.new(:invalid_gen_arg, arg)
182
+ end
183
+ end
184
+
185
+ # Generate/count token with a given length.
186
+ def run_by_length(length, options)
187
+ if block_given?
188
+ seeds = gen_seeds(options)[:seed]
189
+ yield(length, seeds)
190
+ else
191
+ raise RandomTokenError.new(
192
+ "No block is given when calling run_by_length.")
193
+ end
194
+ end
195
+
196
+ # Generate/count token with a format string.
197
+ def run_by_pattern(pattern, options = {})
198
+ unless block_given?
199
+ raise RandomTokenError.new(
200
+ "No block is given when calling run_by_pattern.")
201
+ end
126
202
  in_arg = false
127
- result = ''
203
+ result = []
128
204
  length = ''
129
205
  pattern.split(//).each do |x|
130
206
  if x == '%'
@@ -143,8 +219,8 @@ module RandomToken
143
219
  end
144
220
  elsif STRF_ARG_MAP.keys.include?(x)
145
221
  if in_arg
146
- result << self.get((length == "") ? 1 : length.to_i,
147
- STRF_ARG_MAP[x].merge(options))
222
+ seeds = gen_seeds(STRF_ARG_MAP[x].merge(options))[:seed]
223
+ result << yield((length == "") ? 1 : length.to_i, seeds)
148
224
  length = ''
149
225
  in_arg = false
150
226
  else
@@ -161,9 +237,21 @@ module RandomToken
161
237
  result
162
238
  end
163
239
 
164
- private
240
+ # Check options
241
+ def check_opt(opts)
242
+ SUPPORTED_OPTS.each do |key, value|
243
+ if opts[key] && opts.keys.include?(value[:abbr])
244
+ raise RandomTokenError.new(:duplicated_option, opts)
245
+ end
246
+ opts.merge!(key => opts[value[:abbr]]) if opts[value[:abbr]]
247
+ if value[:value] && opts[key] && !value[:value].include?(opts[key])
248
+ raise RandomTokenError.new(:invalid_option_value, opts)
249
+ end
250
+ end
251
+ opts
252
+ end
165
253
 
166
- # To generate seeds according to the :seed options
254
+ # Generate seeds according to the :seed options
167
255
  def gen_seeds(opt)
168
256
  opt_seed = opt[:seed]
169
257
  default_opt = SEED_TYPES[:default]
@@ -181,15 +269,17 @@ module RandomToken
181
269
  return seed_modifier(default_opt.merge(:case => :keep).merge(opt))
182
270
  elsif opt_seed.is_a?(Hash)
183
271
  seeds = opt_seed.to_a.map {|s| (s.first * s.last).split(//)}.flatten
184
- return seed_modifier(default_opt.merge(opt).merge(:seed => seeds,
185
- :support_case => false,
186
- :support_friendly => false ))
272
+ return seed_modifier(default_opt.
273
+ merge(opt).
274
+ merge(:seed => seeds,
275
+ :support_case => false,
276
+ :support_friendly => false ))
187
277
  else
188
278
  raise RandomTokenError.new(:unknown_seed, opt_seed)
189
279
  end
190
280
  end
191
281
 
192
- # To modify seeds according to the :case, :friendly and :mask options
282
+ # Modify seeds according to the :case, :friendly and :mask options
193
283
  def seed_modifier(opt)
194
284
  if opt[:support_case]
195
285
  case_opt = opt[:case] || opt[:default_case]
@@ -32,7 +32,15 @@ module RandomToken
32
32
  :mask_remove_all_seeds => {
33
33
  :value => 7,
34
34
  :msg => "The friendly mask removes all seeds."
35
- }
35
+ },
36
+ :duplicated_option => {
37
+ :value => 8,
38
+ :msg => "The same options are given."
39
+ },
40
+ :invalid_option_value => {
41
+ :value => 9,
42
+ :msg => "The given option value is invalid."
43
+ },
36
44
  }
37
45
 
38
46
  attr_reader :code, :value, :msg, :info
@@ -1,3 +1,3 @@
1
1
  module RandomToken
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -0,0 +1,59 @@
1
+ require "test/unit"
2
+ require "random_token"
3
+
4
+ class TestPattern < Test::Unit::TestCase
5
+ def test_gen_should_create_a_random_with_alphabets
6
+ length = 10000
7
+ pattern = "===%#{length}A===%#{length}a===Aa==="
8
+ token = RandomToken.gen(pattern)
9
+ assert_match(/^===[A-Z]{#{length}}===[a-z]{#{length}}===Aa===$/, token)
10
+ end
11
+
12
+ def test_gen_should_create_a_random_with_numbers
13
+ length = 10000
14
+ pattern = "===%#{length}n===n==="
15
+ token = RandomToken.gen(pattern)
16
+ assert_match(/^===[0-9]{#{length}}===n===$/, token)
17
+ end
18
+
19
+ def test_gen_should_create_a_random_with_0_and_1
20
+ length = 10000
21
+ pattern = "===%#{length}b===b==="
22
+ token = RandomToken.gen(pattern)
23
+ assert_match(/^===[0-1]{#{length}}===b===$/, token)
24
+ end
25
+
26
+ def test_gen_should_create_a_random_with_octal_digits
27
+ length = 10000
28
+ pattern = "===%#{length}o===o==="
29
+ token = RandomToken.gen(pattern)
30
+ assert_match(/^===[0-8]{#{length}}===o===$/, token)
31
+ end
32
+
33
+ def test_gen_should_create_a_random_with_hexadecimal_digits
34
+ length = 10000
35
+ pattern = "===%#{length}h===%#{length}H===hH==="
36
+ token = RandomToken.gen(pattern)
37
+ assert_match(/^===[0-9a-f]{#{length}}===[0-9A-F]{#{length}}===hH===$/, token)
38
+ end
39
+
40
+ def test_gen_should_create_a_random_with_alphabets_and_numbers
41
+ length = 10000
42
+ pattern = "===%#{length}x===%#{length}X===xX==="
43
+ token = RandomToken.gen(pattern)
44
+ assert_match(/^===[0-9a-z]{#{length}}===[0-9A-Z]{#{length}}===xX===$/, token)
45
+ end
46
+
47
+ def test_gen_should_create_a_random_with_given_format
48
+ length = 10000
49
+ pattern = "===%#{length}?===?==="
50
+ token = RandomToken.gen(pattern)
51
+ assert_match(/^===.{#{length}}===\?===$/, token)
52
+ end
53
+
54
+ def test_gen_should_raise_an_exception_if_the_given_pattern_is_invalid
55
+ pattern = "===%123==="
56
+ e = assert_raise(RandomToken::RandomTokenError) { RandomToken.gen(pattern) }
57
+ assert(e.code == :invalid_strf_pattern)
58
+ end
59
+ end
@@ -0,0 +1,11 @@
1
+ require "test/unit"
2
+ require "random_token"
3
+
4
+ class TestPercent < Test::Unit::TestCase
5
+ def test_gen_should_create_a_random_with_percent_signs
6
+ length = 10000
7
+ pattern = "===%#{length}%===%%==="
8
+ token = RandomToken.gen(pattern)
9
+ assert_match(/^===%{#{length}}===%===$/, token)
10
+ end
11
+ end
@@ -40,6 +40,7 @@ class TestCase < Test::Unit::TestCase
40
40
  length = 10000
41
41
  seeds = ['a', 'B', 'c', 'D', 'e', 'F', 'g', 'H', 'i', 'J', 'k', 'L', 'm', 'N']
42
42
  token = RandomToken.get(length, :seed => seeds)
43
- assert_match(/^#{"[#{Regexp.escape(seeds.join)}]"}*$/, token)
43
+ targets = "[#{Regexp.escape(seeds.join)}]"
44
+ assert_match(/^#{targets}*$/, token)
44
45
  end
45
46
  end
@@ -63,6 +63,7 @@ class TestSeed < Test::Unit::TestCase
63
63
  '[', ']', '\\', '|', ';', ':', '\'', '"', ',', '<',
64
64
  '.', '>', '/', '?', '`', '~']
65
65
  token = RandomToken.get(length, :seed => seeds)
66
- assert_match(/^#{"[#{Regexp.escape(seeds.join)}]"}*$/, token)
66
+ targets = "[#{Regexp.escape(seeds.join)}]"
67
+ assert_match(/^#{targets}*$/, token)
67
68
  end
68
69
  end
@@ -0,0 +1,54 @@
1
+ require "test/unit"
2
+ require "random_token"
3
+
4
+ class TestCase < Test::Unit::TestCase
5
+ def test_gen_should_create_a_random_with_upper_case_alphabets_or_numbers
6
+ length = 10000
7
+ token = RandomToken.gen(length, :case => :up)
8
+ assert_match(/^[A-Z0-9]{#{length}}$/, token)
9
+ token = RandomToken.gen(length, :case => :u)
10
+ assert_match(/^[A-Z0-9]{#{length}}$/, token)
11
+ token = RandomToken.gen(length, :c => :up)
12
+ assert_match(/^[A-Z0-9]{#{length}}$/, token)
13
+ token = RandomToken.gen(length, :c => :u)
14
+ assert_match(/^[A-Z0-9]{#{length}}$/, token)
15
+ end
16
+
17
+ def test_gen_should_create_a_random_with_lower_case_alphabets_or_numbers
18
+ length = 10000
19
+ token = RandomToken.gen(length, :case => :down)
20
+ assert_match(/^[a-z0-9]{#{length}}$/, token)
21
+ token = RandomToken.gen(length, :case => :d)
22
+ assert_match(/^[a-z0-9]{#{length}}$/, token)
23
+ token = RandomToken.gen(length, :c => :down)
24
+ assert_match(/^[a-z0-9]{#{length}}$/, token)
25
+ token = RandomToken.gen(length, :c => :d)
26
+ assert_match(/^[a-z0-9]{#{length}}$/, token)
27
+ end
28
+
29
+ def test_gen_should_not_support_case_feature_when_creating_binray_random
30
+ length = 10000
31
+ e = assert_raise(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :b, :case => :up) }
32
+ assert(e.code == :not_support_case)
33
+ end
34
+
35
+ def test_gen_should_not_support_case_feature_when_creating_octal_random
36
+ length = 10000
37
+ e = assert_raise(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :o, :case => :up) }
38
+ assert(e.code == :not_support_case)
39
+ end
40
+
41
+ def test_gen_should_use_upper_case_by_default_when_creating_hexdecimal_random
42
+ length = 10000
43
+ token = RandomToken.gen(length, :seed => :h)
44
+ assert_match(/^[0-9A-F]*$/, token)
45
+ end
46
+
47
+ def test_gen_should_keep_case_by_default_when_creating_random_with_given_seeds
48
+ length = 10000
49
+ seeds = ['a', 'B', 'c', 'D', 'e', 'F', 'g', 'H', 'i', 'J', 'k', 'L', 'm', 'N']
50
+ token = RandomToken.gen(length, :seed => seeds)
51
+ targets = "[#{Regexp.escape(seeds.join)}]"
52
+ assert_match(/^#{targets}*$/, token)
53
+ end
54
+ end
@@ -0,0 +1,37 @@
1
+ require "test/unit"
2
+ require "random_token"
3
+
4
+ class TestFriendly < Test::Unit::TestCase
5
+ def test_gen_should_not_create_a_random_including_masked_alphabets_or_numbers
6
+ length = 10000
7
+ token = RandomToken.gen(length, :friendly => true)
8
+ assert((RandomToken::MASK - token.split(//).uniq) == RandomToken::MASK)
9
+ token = RandomToken.gen(length, :f => true)
10
+ assert((RandomToken::MASK - token.split(//).uniq) == RandomToken::MASK)
11
+ end
12
+
13
+ def test_gen_should_not_support_friendly_feature_when_creating_binary_random
14
+ length = 10000
15
+ e = assert_raise(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :b, :friendly => true) }
16
+ assert(e.code == :not_support_friendly)
17
+ end
18
+
19
+ def test_gen_should_not_support_friendly_feature_when_creating_octal_random
20
+ length = 10000
21
+ e = assert_raise(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :o, :friendly => true) }
22
+ assert(e.code == :not_support_friendly)
23
+ end
24
+
25
+ def test_gen_should_not_support_friendly_feature_when_creating_hexadecimal_random
26
+ length = 10000
27
+ e = assert_raise(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :h, :friendly => true) }
28
+ assert(e.code == :not_support_friendly)
29
+ end
30
+
31
+ def test_gen_should_support_friendly_feature_when_creating_random_with_given_seeds
32
+ length = 10000
33
+ seeds = ['a','b','c','d','e','0','1','2','3','4']
34
+ token = RandomToken.gen(length, :seed => seeds, :friendly => true)
35
+ assert((token.split(//).uniq - RandomToken::MASK) == token.split(//).uniq)
36
+ end
37
+ end
@@ -0,0 +1,36 @@
1
+ require "test/unit"
2
+ require "random_token"
3
+
4
+ class TestHashSeed < Test::Unit::TestCase
5
+ def test_gen_should_create_a_random_with_the_given_seeds
6
+ length = 10000
7
+ token = RandomToken.gen(length, :seed => { 'a' => 1, 'b' => 2, 'c' => 3 })
8
+ assert_match(/^[a-c]{#{length}}$/, token)
9
+ end
10
+
11
+ def test_gen_should_create_a_random_with_the_given_distribution_of_seeds
12
+ length = 1000000
13
+ token = RandomToken.gen(length, :seed => { 'a' => 1, 'b' => 2, 'c' => 3 })
14
+ a_count = token.count('a')
15
+ b_count = token.count('b')
16
+ c_count = token.count('c')
17
+ assert_in_delta(b_count.to_f/(2*a_count), 1, 0.01)
18
+ assert_in_delta(c_count.to_f/(3*a_count), 1, 0.01)
19
+ end
20
+
21
+ def test_gen_should_not_support_case_feature_when_given_the_hash_seed
22
+ length = 10000
23
+ e = assert_raise(RandomToken::RandomTokenError) {
24
+ RandomToken.gen(length, :seed => { 'a' => 1, 'b' => 2, 'c' => 3 }, :case => :up)
25
+ }
26
+ assert(e.code == :not_support_case)
27
+ end
28
+
29
+ def test_gen_should_not_support_friendly_feature_when_given_the_hash_seed
30
+ length = 10000
31
+ e = assert_raise(RandomToken::RandomTokenError) {
32
+ RandomToken.gen(length, :seed => { 'a' => 1, 'b' => 2, 'c' => 3 }, :friendly => true)
33
+ }
34
+ assert(e.code == :not_support_friendly)
35
+ end
36
+ end
@@ -0,0 +1,10 @@
1
+ require "test/unit"
2
+ require "random_token"
3
+
4
+ class TestLength < Test::Unit::TestCase
5
+ def test_gen_should_create_a_given_length_random_string
6
+ length = 8
7
+ token = RandomToken.gen(length)
8
+ assert_match(/^.{#{length}}$/, token)
9
+ end
10
+ end
@@ -0,0 +1,30 @@
1
+ require "test/unit"
2
+ require "random_token"
3
+
4
+ class TestMask < Test::Unit::TestCase
5
+ def test_gen_should_use_given_mask
6
+ length = 10000
7
+ mask = ['a','b','c','d','e','0','1','2','3','4']
8
+ token = RandomToken.gen(length, :mask => mask)
9
+ assert((token.split(//).uniq - mask) == token.split(//).uniq)
10
+ token = RandomToken.gen(length, :m => mask)
11
+ assert((token.split(//).uniq - mask) == token.split(//).uniq)
12
+ end
13
+
14
+ def test_gen_should_raise_an_exception_if_mask_is_given_but_friendly_option_is_false
15
+ length = 10000
16
+ mask = ['a','b','c','d','e','0','1','2','3','4']
17
+ e = assert_raise(RandomToken::RandomTokenError) { RandomToken.gen(length, :mask => mask,
18
+ :friendly => false) }
19
+ assert(e.code == :false_friendly_with_given_mask)
20
+ end
21
+
22
+ def test_gen_should_raise_an_exception_if_mask_remove_all_seeds
23
+ length = 10000
24
+ seed = ['a']
25
+ mask = ['a']
26
+ e = assert_raise(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => seed,
27
+ :mask => mask) }
28
+ assert(e.code == :mask_remove_all_seeds)
29
+ end
30
+ end
@@ -0,0 +1,106 @@
1
+ require "test/unit"
2
+ require "random_token"
3
+
4
+ class TestSeed < Test::Unit::TestCase
5
+ def test_gen_should_create_a_random_with_alphabets_only
6
+ length = 10000
7
+ token = RandomToken.gen(length, :seed => :a)
8
+ assert_match(/^[A-Za-z]*$/, token)
9
+ token = RandomToken.gen(length, :seed => :alphabet)
10
+ assert_match(/^[A-Za-z]*$/, token)
11
+ token = RandomToken.gen(length, :seed => :l)
12
+ assert_match(/^[A-Za-z]*$/, token)
13
+ token = RandomToken.gen(length, :seed => :letter)
14
+ assert_match(/^[A-Za-z]*$/, token)
15
+ token = RandomToken.gen(length, :s => :a)
16
+ assert_match(/^[A-Za-z]*$/, token)
17
+ token = RandomToken.gen(length, :s => :alphabet)
18
+ assert_match(/^[A-Za-z]*$/, token)
19
+ token = RandomToken.gen(length, :s => :l)
20
+ assert_match(/^[A-Za-z]*$/, token)
21
+ token = RandomToken.gen(length, :s => :letter)
22
+ assert_match(/^[A-Za-z]*$/, token)
23
+ end
24
+
25
+ def test_gen_should_create_a_random_with_numbers_only
26
+ length = 10000
27
+ token = RandomToken.gen(length, :seed => :n)
28
+ assert_match(/^[0-9]*$/, token)
29
+ token = RandomToken.gen(length, :seed => :number)
30
+ assert_match(/^[0-9]*$/, token)
31
+ token = RandomToken.gen(length, :seed => 1)
32
+ assert_match(/^[0-9]*$/, token)
33
+ token = RandomToken.gen(length, :seed => 10)
34
+ assert_match(/^[0-9]*$/, token)
35
+ token = RandomToken.gen(length, :s => :n)
36
+ assert_match(/^[0-9]*$/, token)
37
+ token = RandomToken.gen(length, :s => :number)
38
+ assert_match(/^[0-9]*$/, token)
39
+ token = RandomToken.gen(length, :s => 1)
40
+ assert_match(/^[0-9]*$/, token)
41
+ token = RandomToken.gen(length, :s => 10)
42
+ assert_match(/^[0-9]*$/, token)
43
+ end
44
+
45
+ def test_gen_should_create_a_random_with_0_and_1_only
46
+ length = 10000
47
+ token = RandomToken.gen(length, :seed => :b)
48
+ assert_match(/^[01]*$/, token)
49
+ token = RandomToken.gen(length, :seed => :binary)
50
+ assert_match(/^[01]*$/, token)
51
+ token = RandomToken.gen(length, :seed => 2)
52
+ assert_match(/^[01]*$/, token)
53
+ token = RandomToken.gen(length, :s => :b)
54
+ assert_match(/^[01]*$/, token)
55
+ token = RandomToken.gen(length, :s => :binary)
56
+ assert_match(/^[01]*$/, token)
57
+ token = RandomToken.gen(length, :s => 2)
58
+ assert_match(/^[01]*$/, token)
59
+ end
60
+
61
+ def test_gen_should_create_a_random_with_octal_digits
62
+ length = 10000
63
+ token = RandomToken.gen(length, :seed => :o)
64
+ assert_match(/^[0-7]*$/, token)
65
+ token = RandomToken.gen(length, :seed => :oct)
66
+ assert_match(/^[0-7]*$/, token)
67
+ token = RandomToken.gen(length, :seed => 8)
68
+ assert_match(/^[0-7]*$/, token)
69
+ token = RandomToken.gen(length, :s => :o)
70
+ assert_match(/^[0-7]*$/, token)
71
+ token = RandomToken.gen(length, :s => :oct)
72
+ assert_match(/^[0-7]*$/, token)
73
+ token = RandomToken.gen(length, :s => 8)
74
+ assert_match(/^[0-7]*$/, token)
75
+ end
76
+
77
+ def test_gen_should_create_a_random_with_hexadecimal_digits
78
+ length = 10000
79
+ token = RandomToken.gen(length, :seed => :h)
80
+ assert_match(/^[0-9A-F]*$/, token)
81
+ token = RandomToken.gen(length, :seed => :hex)
82
+ assert_match(/^[0-9A-F]*$/, token)
83
+ token = RandomToken.gen(length, :seed => 16)
84
+ assert_match(/^[0-9A-F]*$/, token)
85
+ token = RandomToken.gen(length, :s => :h)
86
+ assert_match(/^[0-9A-F]*$/, token)
87
+ token = RandomToken.gen(length, :s => :hex)
88
+ assert_match(/^[0-9A-F]*$/, token)
89
+ token = RandomToken.gen(length, :s => 16)
90
+ assert_match(/^[0-9A-F]*$/, token)
91
+ end
92
+
93
+ def test_gen_should_create_a_random_with_given_seeds
94
+ length = 10000
95
+ seeds = ['a', 'b', 'c', '1', '2', '3', '!', '@', '#', '$', '%',
96
+ '^', '&', '*', '(', ')', '-', '_', '=', '+', '{', '}',
97
+ '[', ']', '\\', '|', ';', ':', '\'', '"', ',', '<',
98
+ '.', '>', '/', '?', '`', '~']
99
+ token = RandomToken.gen(length, :seed => seeds)
100
+ targets = "[#{Regexp.escape(seeds.join)}]"
101
+ assert_match(/^#{targets}*$/, token)
102
+ token = RandomToken.gen(length, :s => seeds)
103
+ targets = "[#{Regexp.escape(seeds.join)}]"
104
+ assert_match(/^#{targets}*$/, token)
105
+ end
106
+ end
data/test/test_all.rb CHANGED
@@ -3,6 +3,6 @@ require "test/unit"
3
3
  $LOAD_PATH.unshift("#{File.dirname(__FILE__)}")
4
4
  require "test_helper"
5
5
  require "random_token"
6
+ require "test_gen"
6
7
  require "test_get"
7
8
  require "test_strf"
8
- require "test_gen"
data/test/test_gen.rb CHANGED
@@ -3,10 +3,29 @@ require "test/unit"
3
3
  $LOAD_PATH.unshift("#{File.dirname(__FILE__)}")
4
4
  require "test_helper"
5
5
  require "random_token"
6
+ require "length/test_length"
7
+ require "length/test_case"
8
+ require "length/test_friendly"
9
+ require "length/test_seed"
10
+ require "length/test_mask"
11
+ require "length/test_hash_seed"
12
+ require "format/test_pattern"
13
+ require "format/test_percent"
6
14
 
7
15
  class TestGen < Test::Unit::TestCase
8
16
  def test_gen_should_raise_an_exception_if_the_given_arg_is_invalid
9
17
  e = assert_raise(RandomToken::RandomTokenError) { RandomToken.gen(nil) }
10
18
  assert(e.code == :invalid_gen_arg)
11
19
  end
20
+
21
+ def test_gen_should_raise_an_exception_if_the_given_options_are_duplicated
22
+ e = assert_raise(RandomToken::RandomTokenError) { RandomToken.gen(8, :s => :a, :seed => :o) }
23
+ assert(e.code == :duplicated_option)
24
+ end
25
+
26
+ def test_gen_should_raise_an_exception_if_the_given_option_values_are_invalid
27
+ invalid_value = 'invalid_value'
28
+ e = assert_raise(RandomToken::RandomTokenError) { RandomToken.gen(8, :c => invalid_value) }
29
+ assert(e.code == :invalid_option_value)
30
+ end
12
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random_token
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-28 00:00:00.000000000 Z
12
+ date: 2013-10-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple way to generate a random token.
15
15
  email:
@@ -24,12 +24,20 @@ files:
24
24
  - lib/random_token/random_token_error.rb
25
25
  - lib/random_token/version.rb
26
26
  - random_token.gemspec
27
+ - test/format/test_pattern.rb
28
+ - test/format/test_percent.rb
27
29
  - test/get/test_case.rb
28
30
  - test/get/test_friendly.rb
29
31
  - test/get/test_hash_seed.rb
30
32
  - test/get/test_length.rb
31
33
  - test/get/test_mask.rb
32
34
  - test/get/test_seed.rb
35
+ - test/length/test_case.rb
36
+ - test/length/test_friendly.rb
37
+ - test/length/test_hash_seed.rb
38
+ - test/length/test_length.rb
39
+ - test/length/test_mask.rb
40
+ - test/length/test_seed.rb
33
41
  - test/strf/test_pattern.rb
34
42
  - test/strf/test_percent.rb
35
43
  - test/test_all.rb
@@ -58,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
66
  version: '0'
59
67
  requirements: []
60
68
  rubyforge_project:
61
- rubygems_version: 1.8.25
69
+ rubygems_version: 1.8.24
62
70
  signing_key:
63
71
  specification_version: 3
64
72
  summary: ! 'Use "gen" method to create a random token with a given length. RandomToken.gen(8)
@@ -67,12 +75,20 @@ summary: ! 'Use "gen" method to create a random token with a given length. Rand
67
75
  string format to create a random in the particular format. RandomToken.gen("Give
68
76
  me a token %8?") # "Give me a token 6HRQZp8O" Please see "README" to get more details.'
69
77
  test_files:
78
+ - test/format/test_pattern.rb
79
+ - test/format/test_percent.rb
70
80
  - test/get/test_case.rb
71
81
  - test/get/test_friendly.rb
72
82
  - test/get/test_hash_seed.rb
73
83
  - test/get/test_length.rb
74
84
  - test/get/test_mask.rb
75
85
  - test/get/test_seed.rb
86
+ - test/length/test_case.rb
87
+ - test/length/test_friendly.rb
88
+ - test/length/test_hash_seed.rb
89
+ - test/length/test_length.rb
90
+ - test/length/test_mask.rb
91
+ - test/length/test_seed.rb
76
92
  - test/strf/test_pattern.rb
77
93
  - test/strf/test_percent.rb
78
94
  - test/test_all.rb
@@ -80,4 +96,3 @@ test_files:
80
96
  - test/test_get.rb
81
97
  - test/test_helper.rb
82
98
  - test/test_strf.rb
83
- has_rdoc: