Skip to content

Instantly share code, notes, and snippets.

@walf443
Forked from anonymous/gist:25484
Created November 16, 2008 13:51
Show Gist options
  • Save walf443/25485 to your computer and use it in GitHub Desktop.
Save walf443/25485 to your computer and use it in GitHub Desktop.
==> randexp_spec.rb <==
begin
require 'spec/fixture'
require 'randexp'
describe "randexp" do
with_fixtures :template => :expect do
it do |template,expect|
warn template
10.times do
result = /#{template}/.gen
warn result
result.should =~ /#{template}/
end
end
set_fixtures([
[ { "[0-9]{1}" => [[[0..9], 1]] }, 'numeric'],
[ { "[a-z]{1}" => [[['a'..'z'], 1]] }, 'alpha_downcase'],
[ { "[A-Z]{1}" => [[['A'..'Z'], 1]] }, 'alpha_upcase'],
[ { "[あ-お]{1}" => [[['あ'..'お'], 1]] }, 'non-ascii'],
[ { "[0-9a-zA-Z]{1}" => [[[0..9, 'a'..'z', 'A'..'Z'], 1]] }, 'multi set'],
[ { "[0-9]{10}" => [[[0..9], 10]] }, 'multi count'],
[ { "[0-9]{1}[a-z]{5}" => [[[0..9], 1], [['a'..'z'], 5]] }, 'multi expression'],
[ { "[0-9]{1,5}" => [[[0..9], 1..5]] }, 'multi count'],
[ { "[0-9]?" => [[[0..9], 0..1]] }, 'question'],
[ { '[abc]{1}' => [[['a', 'b', 'c'], 1]] }, 'non-range set' ],
[ { '[\w]{1}' => [[[ 'a'..'z', 'A'..'Z', 0..9, '_' ] , 1]]}, 'non-range set ( \w )' ],
[ { '[\d]{1}' => [[[ 0..9 ] , 1]] }, 'non-range set ( \d )' ],
[ { '[abc0-9]{1}' => [[['a', 'b', 'c', 0..9 ], 1]] }, 'mixed set' ],
[ { '[0-9あいう]{1}' => [[[0..9, 'あ', 'い', 'う' ], 1]] }, 'mixed set with non-ascii' ],
[ { "abc" => ['abc'] }, 'with non-random expression'],
[ { "-_" => ['-_'] }, 'with non-random expression'],
[ { "あいう" => ['あいう'] }, 'with non-random expression'],
[ { '\d' => [[[0..9], 1]] }, 'with non-set expression ( \d )'],
[ { '\d?' => [[[0..9], 0..1]] }, 'with non-set expression ( \d? )'],
[ { '\w' => [[['a'..'z', 'A'..'Z', 0..9, '_'], 1]] }, 'with non-set expression ( \w )'],
[ { '.' => [[['a'..'z', 'A'..'Z', 0..9, '_'], 1]] }, 'with non-set expression ( . )'],
[ { 'hoge\?' => ['hoge', '?'] }, 'with non-set expression ( escaped ? )'],
[ { 'hoge\[' => ['hoge', '['] }, 'with non-set expression ( escaped [ )'],
[ { 'hoge\]' => ['hoge', ']'] }, 'with non-set expression ( escaped ] )'],
[ { 'hoge\{' => ['hoge', '{'] }, 'with non-set expression ( escaped { )'],
[ { 'hoge\}' => ['hoge', '}'] }, 'with non-set expression ( escaped } )'],
[ { 'hoge\*' => ['hoge', '*'] }, 'with non-set expression ( escaped * )'],
[ { 'hoge\+' => ['hoge', '+'] }, 'with non-set expression ( escaped + )'],
[ { 'hoge\^' => ['hoge', '^'] }, 'with non-set expression ( escaped ^ )'],
[ { 'hoge\$' => ['hoge', '$'] }, 'with non-set expression ( escaped $ )'],
[ { 'hoge\\\\' => ['hoge', '\\'] }, 'with non-set expression ( escaped \ )'],
[ { 'hoge\.' => ['hoge', '.'] }, 'with non-set expression ( escaped . )'],
[ { 'hoge\|' => ['hoge', '|'] }, 'with non-set expression ( escaped | )'],
[ { 'hoge\(' => ['hoge', '('] }, 'with non-set expression ( escaped ( )'],
[ { 'hoge\)' => ['hoge', ')'] }, 'with non-set expression ( escaped ) )'],
[ { 'hoge\[' => ['hoge', '['] }, 'with non-set expression ( escaped [ )'],
[ { '\d{2}' => [[[0..9], 2]] }, 'with non-set expression ( \d )'],
[ { '\w{2}' => [[['a'..'z', 'A'..'Z', 0..9, '_'], 2]] }, 'with non-set expression ( \w )'],
[ { '.{2}' => [[['a'..'z', 'A'..'Z', 0..9, '_'], 2]] }, 'with non-set expression ( . )'],
[ { '\d{3}-\d{4}-\d{4}' => [[[0..9], 3], '-', [[0..9], 4], '-', [[0..9], 4]] }, 'with non-set expression mixed'],
[ { "abc[0-9]{1}" => ['abc', [[0..9], 1]] }, 'mixed with non-random expression in head'],
[ { "__[0-9]{1}" => ['__', [[0..9], 1]] }, 'mixed with non-random expression in head (symbol)'],
[ { "あいう[0-9]{1}" => ['あいう', [[0..9], 1]] }, 'mixed with non-random expression in head (non-ascii)'],
[ { "[0-9]{1}abc" => [[[0..9], 1], 'abc'] }, 'mixed with non-random expression in tail'],
[ { "[0-9]{1}_-" => [[[0..9], 1], '_-'] }, 'mixed with non-random expression in tail (symbol)'],
[ { "[0-9]{1}あいう" => [[[0..9], 1], 'あいう'] }, 'mixed with non-random expression in tail (non-ascii)'],
[ { "[0-9]{1}abc[a-z]{5}" => [[[0..9], 1], 'abc', [['a'..'z'], 5]] }, 'mixed with non-random expression in middle'],
[ { "[0-9]{1}__[a-z]{5}" => [[[0..9], 1], '__', [['a'..'z'], 5]] }, 'mixed with non-random expression in middle (symbol)'],
[ { "[0-9]{1}あいう[a-z]{5}" => [[[0..9], 1], 'あいう', [['a'..'z'], 5]] }, 'mixed with non-random expression in middle (non-ascii)'],
])
end
end
rescue LoadError => e
warn "this test reuired rspec-fixture."
end
==> result.txt <==
[0-9a-zA-Z]{1}
[0-9a-zA-Z]
[0-9a-zA-Z]
[0-9a-zA-Z]
[0-9a-zA-Z]
[0-9a-zA-Z]
[0-9a-zA-Z]
[0-9a-zA-Z]
[0-9a-zA-Z]
[0-9a-zA-Z]
[0-9a-zA-Z]
.[0-9]{1}_-
[0-9]_-
F__[0-9]{1}
__[0-9]
Fhoge\^
hoge^
hoge^
hoge^
hoge^
hoge^
hoge^
hoge^
hoge^
hoge^
hoge^
.abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
.hoge\)
hoge)
hoge)
hoge)
hoge)
hoge)
hoge)
hoge)
hoge)
hoge)
hoge)
.hoge\$
hoge$
hoge$
hoge$
hoge$
hoge$
hoge$
hoge$
hoge$
hoge$
hoge$
.\d?
2
5
7
3
.hoge\|
F[0-9]{1}abc[a-z]{5}
[0-9]abc[a-z]]]]]
F[abc]{1}
[abc]
[abc]
[abc]
[abc]
[abc]
[abc]
[abc]
[abc]
[abc]
[abc]
.[0-9]{1}abc
[0-9]abc
F[0-9]{1}あいう[a-z]{5}
[0-9]あいう[a-z]]]]]
F\d{2}
35
69
95
87
22
42
73
44
95
56
.[abc0-9]{1}
[abc0-9]
[abc0-9]
[abc0-9]
[abc0-9]
[abc0-9]
[abc0-9]
[abc0-9]
[abc0-9]
[abc0-9]
[abc0-9]
.hoge\(
hoge(
hoge(
hoge(
hoge(
hoge(
hoge(
hoge(
hoge(
hoge(
hoge(
.\w{2}
ni
Gi
by
Io
Hu
ut
di
if
Ao
Wa
.\w
b
i
l
o
S
t
F
c
P
d
.hoge\*
F.{2}
..
..
..
..
..
..
..
..
..
..
.hoge\]
hoge]
hoge]
hoge]
hoge]
hoge]
hoge]
hoge]
hoge]
hoge]
hoge]
.-_
-_
-_
-_
-_
-_
-_
-_
-_
-_
-_
.[A-Z]{1}
[A-Z]
[A-Z]
[A-Z]
[A-Z]
[A-Z]
[A-Z]
[A-Z]
[A-Z]
[A-Z]
[A-Z]
.[\w]{1}
[q]
[h]
[N]
[A]
[P]
[Z]
[W]
[y]
[g]
[m]
.hoge\}
hoge}
hoge}
hoge}
hoge}
hoge}
hoge}
hoge}
hoge}
hoge}
hoge}
.[0-9]?
Fhoge\.
hoge.
hoge.
hoge.
hoge.
hoge.
hoge.
hoge.
hoge.
hoge.
hoge.
.あいう[0-9]{1}
あいう[0-9]
F[0-9]{1}[a-z]{5}
[0-9][a-z]]]]]
F\d{3}-\d{4}-\d{4}
782-3051-3906
906-9774-6604
149-2431-6183
900-3081-9095
927-3749-0959
565-3694-6586
615-3013-2953
652-6310-1977
116-8914-9348
157-9939-9484
.[0-9]{1}あいう
[0-9]あいう
F.
.
.
.
.
.
.
.
.
.
.
.[0-9]{1}
[0-9]
[0-9]
[0-9]
[0-9]
[0-9]
[0-9]
[0-9]
[0-9]
[0-9]
[0-9]
.[0-9]{10}
[0-9]]]]]]]]]]
F[0-9あいう]{1}
[0-9あいう]
[0-9あいう]
[0-9あいう]
[0-9あいう]
[0-9あいう]
[0-9あいう]
[0-9あいう]
[0-9あいう]
[0-9あいう]
[0-9あいう]
.[a-z]{1}
[a-z]
[a-z]
[a-z]
[a-z]
[a-z]
[a-z]
[a-z]
[a-z]
[a-z]
[a-z]
.hoge\+
F[あ-お]{1}
[あ-お]
[あ-お]
[あ-お]
[あ-お]
[あ-お]
[あ-お]
[あ-お]
[あ-お]
[あ-お]
[あ-お]
.[0-9]{1}__[a-z]{5}
[0-9]__[a-z]]]]]
Fhoge\?
Fhoge\{
hoge{
hoge{
hoge{
hoge{
hoge{
hoge{
hoge{
hoge{
hoge{
hoge{
.\d
2
0
7
6
1
9
2
0
7
9
.abc[0-9]{1}
abc[0-9]
Fあいう
あいう
あいう
あいう
あいう
あいう
あいう
あいう
あいう
あいう
あいう
.hoge\[
hoge[
hoge[
hoge[
hoge[
hoge[
hoge[
hoge[
hoge[
hoge[
hoge[
.hoge\\
hoge\
hoge\
hoge\
hoge\
hoge\
hoge\
hoge\
hoge\
hoge\
hoge\
.[\d]{1}
[3]
[3]
[4]
[4]
[0]
[0]
[3]
[2]
[5]
[4]
.[0-9]{1,5}
[0-9]]]]
[0-9]]]]]
[0-9]]]]
[0-9]]]]
[0-9]
[0-9]]]]
[0-9]]
[0-9]]]
[0-9]]]]]
[0-9]]]]]
.hoge\[
hoge[
hoge[
hoge[
hoge[
hoge[
hoge[
hoge[
hoge[
hoge[
hoge[
.
1)
'randexp mixed with non-random expression in tail (symbol)' FAILED
expected: /[0-9]{1}_-/,
got: "[0-9]_-" (using =~)
randexp_spec.rb:12:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
2)
'randexp mixed with non-random expression in head (symbol)' FAILED
expected: /__[0-9]{1}/,
got: "__[0-9]" (using =~)
randexp_spec.rb:12:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
3)
NoMethodError in 'randexp with non-set expression ( escaped | )'
undefined method `first' for nil:NilClass
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/parser.rb:82:in `intersection'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/parser.rb:16:in `[]'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp.rb:5:in `initialize'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/core_ext/regexp.rb:3:in `new'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/core_ext/regexp.rb:3:in `gen'
randexp_spec.rb:10:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
4)
'randexp mixed with non-random expression in middle' FAILED
expected: /[0-9]{1}abc[a-z]{5}/,
got: "[0-9]abc[a-z]]]]]" (using =~)
randexp_spec.rb:12:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
5)
'randexp mixed with non-random expression in tail' FAILED
expected: /[0-9]{1}abc/,
got: "[0-9]abc" (using =~)
randexp_spec.rb:12:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
6)
'randexp mixed with non-random expression in middle (non-ascii)' FAILED
expected: /[0-9]{1}あいう[a-z]{5}/,
got: "[0-9]あいう[a-z]]]]]" (using =~)
randexp_spec.rb:12:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
7)
RuntimeError in 'randexp with non-set expression ( escaped * )'
Sorry, "\*" is too vague, try setting a range: "\{0,3}"
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:28:in `literal'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:12:in `quantify'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `union'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `map'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `union'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `[]'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp.rb:9:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/core_ext/regexp.rb:3:in `gen'
randexp_spec.rb:10:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
8)
TypeError in 'randexp question'
can't convert String into Integer
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:26:in `*'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:26:in `literal'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:12:in `quantify'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `union'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `map'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `union'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `[]'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp.rb:9:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/core_ext/regexp.rb:3:in `gen'
randexp_spec.rb:10:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
9)
'randexp mixed with non-random expression in head (non-ascii)' FAILED
expected: /あいう[0-9]{1}/,
got: "あいう[0-9]" (using =~)
randexp_spec.rb:12:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
10)
'randexp multi expression' FAILED
expected: /[0-9]{1}[a-z]{5}/,
got: "[0-9][a-z]]]]]" (using =~)
randexp_spec.rb:12:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
11)
'randexp mixed with non-random expression in tail (non-ascii)' FAILED
expected: /[0-9]{1}あいう/,
got: "[0-9]あいう" (using =~)
randexp_spec.rb:12:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
12)
'randexp multi count' FAILED
expected: /[0-9]{10}/,
got: "[0-9]]]]]]]]]]" (using =~)
randexp_spec.rb:12:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
13)
RuntimeError in 'randexp with non-set expression ( escaped + )'
Sorry, "\+" is too vague, try setting a range: "\{1,3}"
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:27:in `literal'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:12:in `quantify'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `union'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `map'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `union'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `[]'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp.rb:9:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/core_ext/regexp.rb:3:in `gen'
randexp_spec.rb:10:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
14)
'randexp mixed with non-random expression in middle (symbol)' FAILED
expected: /[0-9]{1}__[a-z]{5}/,
got: "[0-9]__[a-z]]]]]" (using =~)
randexp_spec.rb:12:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
15)
TypeError in 'randexp with non-set expression ( escaped ? )'
can't convert String into Integer
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:26:in `*'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:26:in `literal'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:12:in `quantify'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `union'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `map'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:53:in `union'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `send'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/reducer.rb:4:in `[]'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp.rb:9:in `reduce'
/Library/Ruby/Gems/gems/randexp-0.1.4/lib/randexp/core_ext/regexp.rb:3:in `gen'
randexp_spec.rb:10:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
16)
'randexp mixed with non-random expression in head' FAILED
expected: /abc[0-9]{1}/,
got: "abc[0-9]" (using =~)
randexp_spec.rb:12:
randexp_spec.rb:9:in `times'
randexp_spec.rb:9:
randexp_spec.rb:5:
Finished in 0.680561 seconds
49 examples, 16 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment