forgery 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.travis.yml +13 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +27 -0
- data/LICENSE +1 -1
- data/README.markdown +9 -5
- data/Rakefile +9 -1
- data/forgery.gemspec +23 -0
- data/lib/forgery/dictionaries/company_names +0 -1
- data/lib/forgery/dictionaries/currency_descriptions +2 -2
- data/lib/forgery/file_reader.rb +1 -0
- data/lib/forgery/forgery/geo.rb +54 -0
- data/lib/forgery/forgery/internet.rb +7 -0
- data/lib/forgery/forgery/russian_tax.rb +57 -0
- data/lib/forgery/version.rb +1 -1
- data/spec/data/dictionaries/code_names +6 -0
- data/spec/data/dictionaries/female_first_names +1 -0
- data/spec/data/documents/mock_web_page.html +17 -0
- data/spec/data/documents/mock_xml_doc.xml +5 -0
- data/spec/dictionaries_spec.rb +35 -0
- data/spec/extensions/array_spec.rb +25 -0
- data/spec/extensions/range_spec.rb +31 -0
- data/spec/extensions/string_spec.rb +29 -0
- data/spec/file_reader_spec.rb +32 -0
- data/spec/forgery/address_spec.rb +84 -0
- data/spec/forgery/basic_spec.rb +179 -0
- data/spec/forgery/credit_card_spec.rb +68 -0
- data/spec/forgery/currency_spec.rb +15 -0
- data/spec/forgery/date_spec.rb +134 -0
- data/spec/forgery/internet_spec.rb +62 -0
- data/spec/forgery/lorem_ipsum_spec.rb +132 -0
- data/spec/forgery/monetary_spec.rb +14 -0
- data/spec/forgery/name_spec.rb +11 -0
- data/spec/forgery/personal_spec.rb +15 -0
- data/spec/forgery/russian_tax_spec.rb +81 -0
- data/spec/forgery/time_spec.rb +7 -0
- data/spec/forgery_spec.rb +63 -0
- data/spec/formats_spec.rb +35 -0
- data/spec/spec_helper.rb +39 -0
- metadata +101 -64
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Forgery::Internet do
|
4
|
+
describe ".user_name" do
|
5
|
+
it "should return a username that is lowercase" do
|
6
|
+
Forgery::Internet.user_name.should only_contain(Forgery::Basic::LOWER_ALPHA)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return a top level domain" do
|
11
|
+
Forgery.dictionaries[:top_level_domains].should include(Forgery::Internet.top_level_domain)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ".domain_name" do
|
15
|
+
it "should return a domain name that contains a lowercase company name" do
|
16
|
+
downcase_company_name = Forgery::Internet.domain_name.split('.').first
|
17
|
+
Forgery.dictionaries[:company_names].any?{ |cn| cn =~ /#{downcase_company_name}/i }
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return a domain name that contains a top level domain" do
|
21
|
+
Forgery.dictionaries[:top_level_domains].should include(Forgery::Internet.domain_name.split('.').last)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe ".email_address" do
|
26
|
+
it "should match the email format" do
|
27
|
+
Forgery::Internet.email_address.should match(/.+@.+\.(#{Forgery.dictionaries[:top_level_domains].join("|")})/)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ".cctld" do
|
32
|
+
it "should return a country-code top level domain" do
|
33
|
+
Forgery.dictionaries[:country_code_top_level_domains].should include(Forgery::Internet.cctld)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return the cctld in correct two-letter format" do
|
37
|
+
Forgery::Internet.cctld.should =~ /\A[a-z]{2}\Z/
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '.ip_v4' do
|
42
|
+
it 'should be 4 integers between 0 and 255 seprated by "."' do
|
43
|
+
val = Forgery::Internet.ip_v4
|
44
|
+
nums = val.split('.')
|
45
|
+
nums.should have(4).items
|
46
|
+
nums.each do |num|
|
47
|
+
num.should =~ /\d{1,3}/
|
48
|
+
num.to_i.should <= 255
|
49
|
+
num.to_i.should >= 0
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '.ip_v6' do
|
55
|
+
it 'should be a valid ipv6 address' do
|
56
|
+
val = Forgery::Internet.ip_v6
|
57
|
+
address = IPAddr.new(val)
|
58
|
+
address.ipv6?.should eq(true)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Forgery::LoremIpsum do
|
4
|
+
describe ".paragraphs" do
|
5
|
+
it "should return text" do
|
6
|
+
Forgery::LoremIpsum.paragraphs.should be_a_kind_of(String)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return text with 1 or more character" do
|
10
|
+
Forgery::LoremIpsum.paragraphs.size.should >= 1
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should default to returning 2 paragraphs separated by \\n\\n" do
|
14
|
+
Forgery::LoremIpsum.paragraphs.split("\n\n").size.should == 2
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should default to returning 3 sentences per paragraph" do
|
18
|
+
paragraphs = Forgery::LoremIpsum.paragraphs.split("\n\n")
|
19
|
+
paragraphs.each do |paragraph|
|
20
|
+
paragraph.scan(".").size.should == 3
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should wrap paragraphs in html paragraph tags when the :html option is true" do
|
25
|
+
paragraphs = Forgery::LoremIpsum.paragraphs(2, :html => true).split("\n\n")
|
26
|
+
paragraphs.each do |paragraph|
|
27
|
+
paragraph.should match(/<p>.+?<\/p>/)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should wrap paragraphs in specified wrapping text" do
|
32
|
+
paragraphs = Forgery::LoremIpsum.paragraphs(2, :wrap => {:start => "foo", :end => "bar"}).split("\n\n")
|
33
|
+
paragraphs.each do |paragraph|
|
34
|
+
paragraph.should match(/foo.+bar/)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should separate paragraphs with the specified string" do
|
39
|
+
Forgery::LoremIpsum.paragraphs(2, :separator => "foo").split("foo").size.should == 2
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe ".paragraph" do
|
44
|
+
it "should return text" do
|
45
|
+
Forgery::LoremIpsum.paragraph.should be_a_kind_of(String)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return text with 1 or more character" do
|
49
|
+
Forgery::LoremIpsum.paragraph.size.should >= 1
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return a single paragraph" do
|
53
|
+
Forgery::LoremIpsum.paragraph.scan("\n\n").size.should == 0
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe ".sentences" do
|
58
|
+
it "should return text" do
|
59
|
+
Forgery::LoremIpsum.sentences.should be_a_kind_of(String)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return text with 1 or more character" do
|
63
|
+
Forgery::LoremIpsum.sentences.size.should >= 1
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should return two sentences by default" do
|
67
|
+
Forgery::LoremIpsum.sentences.scan(".").size.should == 2
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should return as many sentences as you specify" do
|
71
|
+
Forgery::LoremIpsum.sentences(4).scan(".").size.should == 4
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe ".sentence" do
|
76
|
+
it "should return text" do
|
77
|
+
Forgery::LoremIpsum.sentence.should be_a_kind_of(String)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should return text with 1 or more character" do
|
81
|
+
Forgery::LoremIpsum.sentence.size.should >= 1
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should return a single sentence" do
|
85
|
+
Forgery::LoremIpsum.sentence.scan(".").size.should == 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe ".words" do
|
90
|
+
it "should return text" do
|
91
|
+
Forgery::LoremIpsum.words.should be_a_kind_of(String)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should return text with 1 or more character" do
|
95
|
+
Forgery::LoremIpsum.words.size.should >= 1
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should return a random set of words" do
|
99
|
+
Forgery::LoremIpsum.words(2, :random => true)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe ".word" do
|
104
|
+
it "should return text" do
|
105
|
+
Forgery::LoremIpsum.word.should be_a_kind_of(String)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should return text with 1 or more character" do
|
109
|
+
Forgery::LoremIpsum.word.size.should >= 1
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe ".characters" do
|
114
|
+
it "should return text" do
|
115
|
+
Forgery::LoremIpsum.characters.should be_a_kind_of(String)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should return text with 1 or more character" do
|
119
|
+
Forgery::LoremIpsum.characters.size.should >= 1
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe ".character" do
|
124
|
+
it "should return text" do
|
125
|
+
Forgery::LoremIpsum.character.should be_a_kind_of(String)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should return text with 1 or more character" do
|
129
|
+
Forgery::LoremIpsum.character.size.should >= 1
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'bigdecimal'
|
3
|
+
|
4
|
+
describe Forgery::Monetary do
|
5
|
+
|
6
|
+
it "should return random number string" do
|
7
|
+
Forgery(:monetary).money.should match(/^[\d+\.]+$/)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return random number respecting min and max parameters" do
|
11
|
+
BigDecimal.new(Forgery(:monetary).money({:min => 10, :max => 20})).should be_between(10, 20)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Forgery::Name do
|
4
|
+
it "should return a random first name" do
|
5
|
+
(Forgery.dictionaries[:male_first_names] + Forgery.dictionaries[:female_first_names]).should include(Forgery::Name.first_name)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return a random last name" do
|
9
|
+
Forgery.dictionaries[:last_names].should include(Forgery::Name.last_name)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Forgery::Personal do
|
4
|
+
describe '.gender' do
|
5
|
+
it 'should return male or female' do
|
6
|
+
Forgery.dictionaries[:genders].should include(Forgery::Personal.gender)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '.shirt_size' do
|
11
|
+
it 'should return a sane size' do
|
12
|
+
Forgery.dictionaries[:shirt_sizes].should include(Forgery::Personal.shirt_size)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Forgery::RussianTax do
|
4
|
+
|
5
|
+
it 'bik should be 9 symbols' do
|
6
|
+
Forgery::RussianTax.bik.length.should eq 9
|
7
|
+
end
|
8
|
+
|
9
|
+
it "bik should start with 04" do
|
10
|
+
Forgery::RussianTax.bik[0,2].should eq '04'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "bik should have valid last part" do
|
14
|
+
Forgery::RussianTax.bik[6,8].to_i.should be > 50
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'bik should be 20 symbols' do
|
18
|
+
Forgery::RussianTax.account_number.length.should eq 20
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'legal inn' do
|
22
|
+
let(:inn) { Forgery::RussianTax.inn({ :type =>:legal }) }
|
23
|
+
|
24
|
+
it 'legal inn should be 10 symbols' do
|
25
|
+
inn.length.should eq 10
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'leagl inn crc' do
|
29
|
+
mask = [2, 4, 10, 3, 5, 9, 4, 6, 8]
|
30
|
+
((0..(inn.length-2)).inject(0) {|crc, i| crc + inn[i].to_i*mask[i].to_i} % 11 % 10).should eq inn[9].chr.to_i
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'person inn' do
|
36
|
+
let(:inn) { Forgery::RussianTax.inn({ :type => :person }) }
|
37
|
+
|
38
|
+
it 'person inn should be 12 symbols' do
|
39
|
+
inn.length.should eq 12
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'person inn crc 10' do
|
43
|
+
mask = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
|
44
|
+
((0..(inn.length-3)).inject(0) {|crc, i| crc + inn[i].to_i*mask[i].to_i} % 11 % 10).should eq inn[10].chr.to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'person inn crc 11' do
|
48
|
+
mask = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
|
49
|
+
((0..(inn.length-2)).inject(0) {|crc, i| crc + inn[i].to_i*mask[i].to_i} % 11 % 10).should eq inn[11].chr.to_i
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'legal ogrn' do
|
55
|
+
let(:ogrn) { Forgery::RussianTax.ogrn({ :type => :legal }) }
|
56
|
+
|
57
|
+
it 'legal ogrn should be 13 symbols' do
|
58
|
+
ogrn.length.should eq 13
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'legal ogrn should have valid crc' do
|
62
|
+
(ogrn[0..-2].to_i%11%10).should eq ogrn[-1].chr.to_i
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'person ogrn' do
|
67
|
+
let(:ogrn) { Forgery::RussianTax.ogrn({ :type => :person }) }
|
68
|
+
|
69
|
+
it 'person ogrn should be 15 symbols' do
|
70
|
+
ogrn.length.should eq 15
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'person ogrn should have valid crc' do
|
74
|
+
(ogrn[0..-2].to_i%13%10).should eq ogrn[-1].chr.to_i
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
describe Forgery do
|
5
|
+
it "should load a dictionary when it is requested" do
|
6
|
+
Forgery.dictionaries.reset!
|
7
|
+
|
8
|
+
Forgery.dictionaries.should_not be_loaded(:colors)
|
9
|
+
|
10
|
+
Forgery.dictionaries[:colors]
|
11
|
+
|
12
|
+
Forgery.dictionaries.should be_loaded(:colors)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should load formats when it is requested" do
|
16
|
+
Forgery.formats.reset!
|
17
|
+
|
18
|
+
Forgery.formats.should_not be_loaded(:phone)
|
19
|
+
|
20
|
+
Forgery.formats[:phone]
|
21
|
+
|
22
|
+
Forgery.formats.should be_loaded(:phone)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should accept a symbol and return the appropriate forgery class" do
|
26
|
+
Forgery(:address).should == Forgery::Address
|
27
|
+
Forgery(:basic).should == Forgery::Basic
|
28
|
+
Forgery(:internet).should == Forgery::Internet
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should accept two symbols, finding the right class and calling the appropriate method" do
|
32
|
+
Forgery::Address.should_receive(:street_name)
|
33
|
+
Forgery(:address, :street_name)
|
34
|
+
|
35
|
+
Forgery::Name.should_receive(:full_name)
|
36
|
+
Forgery(:name, :full_name)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should accept two symbols and arguments, passing them along to the appropriate method" do
|
40
|
+
Forgery::LoremIpsum.should_receive(:text).with(:sentences, 2)
|
41
|
+
Forgery(:lorem_ipsum, :text, :sentences, 2)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return the rails root path as a string if Rails.root is defined" do
|
45
|
+
Rails = Object.new
|
46
|
+
Rails.stub!(:root).and_return(Pathname.new('/path/from/rails/dot/root'))
|
47
|
+
Forgery.rails_root.should == '/path/from/rails/dot/root'
|
48
|
+
Object.instance_eval { remove_const(:Rails) }
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return nil when Rails.root and Rails.root are not defined" do
|
52
|
+
Forgery.rails_root.should be_nil
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should not be a rails environment when there is not a rails_root" do
|
56
|
+
Forgery.rails?.should be_false
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should be a rails environment when there is a rails_root" do
|
60
|
+
Forgery.stub!(:rails?).and_return(true)
|
61
|
+
Forgery.rails?.should be_true
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Forgery::Formats do
|
4
|
+
it "should check if the dictionary is loaded" do
|
5
|
+
formats = Forgery::Formats.new
|
6
|
+
|
7
|
+
formats[:phone]
|
8
|
+
|
9
|
+
formats.should be_loaded(:phone)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should load a dictionary when called by the key" do
|
13
|
+
formats = Forgery::Formats.new
|
14
|
+
|
15
|
+
formats.reset!
|
16
|
+
|
17
|
+
formats.should_not be_loaded(:phone)
|
18
|
+
|
19
|
+
formats[:phone]
|
20
|
+
|
21
|
+
formats.should be_loaded(:phone)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should clear the loaded formats when calling reset!" do
|
25
|
+
formats = Forgery::Formats.new
|
26
|
+
|
27
|
+
formats[:phone]
|
28
|
+
|
29
|
+
formats.should be_loaded(:phone)
|
30
|
+
|
31
|
+
formats.reset!
|
32
|
+
|
33
|
+
formats.should_not be_loaded(:phone)
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'forgery'
|
7
|
+
require 'ipaddr'
|
8
|
+
|
9
|
+
ENV["TESTING_VIA_RSPEC"] = "true"
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class OnlyContain
|
16
|
+
def initialize(*expected)
|
17
|
+
@expected = [expected].flatten
|
18
|
+
end
|
19
|
+
|
20
|
+
def matches?(target)
|
21
|
+
@target = target.dup
|
22
|
+
@expected.each do |e|
|
23
|
+
target.gsub!(e, '')
|
24
|
+
end
|
25
|
+
target.size == 0
|
26
|
+
end
|
27
|
+
|
28
|
+
def failure_message
|
29
|
+
"expected #{@target} to only contain #{@expected.join(', ')}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def failure_message
|
33
|
+
"expected #{@target} to not only contain #{@expected.join(', ')}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def only_contain(*expected)
|
38
|
+
OnlyContain.new(expected)
|
39
|
+
end
|