Class: TestStringRandom
- Defined in:
- lib/shokkenki/term/ruby-string-random/test/test_strrand.rb
Instance Method Summary collapse
- #setup ⇒ Object
-
#test_initialize ⇒ Object
StringRandom itself.
-
#test_method_respond ⇒ Object
StringRandom methods.
-
#test_random_pattern ⇒ Object
StringRandom#random_pattern.
-
#test_random_pattern_builtin ⇒ Object
StringRandom#random_pattern.
-
#test_random_pattern_invalid ⇒ Object
StringRandom#random_pattern.
-
#test_random_regex ⇒ Object
StringRandom#random_regex.
-
#test_random_regex_invalid ⇒ Object
StringRandom#random_regex.
-
#test_singleton_random_regex ⇒ Object
StringRandom.random_regex.
-
#test_singleton_random_string ⇒ Object
StringRandom.random_string.
Instance Method Details
#setup ⇒ Object
12 13 14 |
# File 'lib/shokkenki/term/ruby-string-random/test/test_strrand.rb', line 12 def setup @string_random = StringRandom.new end |
#test_initialize ⇒ Object
StringRandom itself
17 18 19 20 |
# File 'lib/shokkenki/term/ruby-string-random/test/test_strrand.rb', line 17 def test_initialize assert_not_nil(@string_random) assert_instance_of(StringRandom, @string_random) end |
#test_method_respond ⇒ Object
StringRandom methods
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/shokkenki/term/ruby-string-random/test/test_strrand.rb', line 23 def test_method_respond # instance methods assert_respond_to(@string_random, :[]) assert_respond_to(@string_random, :[]=) assert_respond_to(@string_random, :random_regex) assert_respond_to(@string_random, :random_pattern) # singleton methods assert_respond_to(StringRandom, :random_regex) assert_respond_to(StringRandom, :random_string) end |
#test_random_pattern ⇒ Object
StringRandom#random_pattern
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/shokkenki/term/ruby-string-random/test/test_strrand.rb', line 77 def test_random_pattern assert_equal(0, @string_random.random_pattern('').length) patterns = { 'x' => ['a'], 'y' => ['b'], 'z' => ['c'] } patterns.each_pair do |key, pattern| @string_random[key] = pattern end assert_equal('abc', @string_random.random_pattern('xyz')) target = patterns.keys result = @string_random.random_pattern(target) assert_equal(target.size, result.size) target.each_with_index do |pattern, i| assert_equal(@string_random[pattern][0], result[i]) end end |
#test_random_pattern_builtin ⇒ Object
StringRandom#random_pattern
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/shokkenki/term/ruby-string-random/test/test_strrand.rb', line 100 def test_random_pattern_builtin ['C', 'c', 'n', '!', '.', 's', 'b'].each do |val| assert_not_nil(@string_random[val]) end range = ('A'..'Z').to_a range.each_with_index do |val, i| assert_equal(val, @string_random['C'][i]) end # modify built-in pattern @string_random['C'] = ['n'] assert_equal('n', @string_random.random_pattern('C')) # undefined behavior count = 0 patterns = { 'X' => ('A'..'Z'), 'Y' => [['foo,', 'bar'], [['baz']]], 'Z' => [true, false], '' => 'hogehoge' # no raise } patterns.each_pair do |key, pattern| begin @string_random[key] = pattern @string_random.random_pattern(key) rescue Exception count += 1 end end assert_equal(patterns.keys.size - 1, count) # No pollute other object @other = StringRandom.new assert_not_equal('n', @other.random_pattern('C')) end |
#test_random_pattern_invalid ⇒ Object
StringRandom#random_pattern
138 139 140 141 142 |
# File 'lib/shokkenki/term/ruby-string-random/test/test_strrand.rb', line 138 def test_random_pattern_invalid ['A', 'CZ1s', 'Hoge', '\n'].each do |pattern| assert_raise(RuntimeError) { @string_random.random_pattern(pattern) } end end |
#test_random_regex ⇒ Object
StringRandom#random_regex
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/shokkenki/term/ruby-string-random/test/test_strrand.rb', line 36 def test_random_regex patterns = ['\d\d\d', '\w\w\w', '[ABC][abc]', '[012][345]', '...', '[a-z][0-9]', '[aw-zX][123]', '[a-z]{5}', '0{80}', '[a-f][nprt]\d{3}', '\t\n\r\f\a\e', '\S\S\S', '\s\s\s', '\w{5,10}', '\w?', '\w+', '\w*', ''] patterns.each do |pattern| result = @string_random.random_regex(pattern) assert_match(/#{pattern}/, result, "#{result} is invalid: pattern #{pattern}") end result = @string_random.random_regex(patterns) assert_equal(patterns.size, result.size) end |
#test_random_regex_invalid ⇒ Object
StringRandom#random_regex
66 67 68 69 70 71 72 73 74 |
# File 'lib/shokkenki/term/ruby-string-random/test/test_strrand.rb', line 66 def test_random_regex_invalid patterns = ['[a-z]{a}', '0{,,}', '9{1,z}'] patterns.each do |pattern| assert_raise(RuntimeError, "Non expected: #{pattern}") { @string_random.random_regex(pattern) } end end |
#test_singleton_random_regex ⇒ Object
StringRandom.random_regex
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/shokkenki/term/ruby-string-random/test/test_strrand.rb', line 145 def test_singleton_random_regex patterns = ['\d\d\d', '\w\w\w', '[ABC][abc]', '[012][345]', '...', '[a-z][0-9]', '[aw-zX][123]', '[a-z]{5}', '0{80}', '[a-f][nprt]\d{3}', '\t\n\r\f\a\e', '\S\S\S', '\s\s\s', '\w{5,10}', '\w?', '\w+', '\w*', ''] patterns.each do |pattern| result = StringRandom.random_regex(pattern) assert_match(/#{pattern}/, result, "#{result} is invalid: pattern #{pattern}") end end |
#test_singleton_random_string ⇒ Object
StringRandom.random_string
172 173 174 175 176 177 178 179 |
# File 'lib/shokkenki/term/ruby-string-random/test/test_strrand.rb', line 172 def test_singleton_random_string assert_match(/\d/, StringRandom.random_string('n')) assert_raise(RuntimeError) { StringRandom.random_string('0') } # with optional lists assert_equal('abc', StringRandom.random_string('012', ['a'], ['b'], ['c'])) assert_match(/[abc][def]/, StringRandom.random_string('01', ['a', 'b', 'c'], ['d', 'e', 'f'])) end |