ftbpro_sitemap_generator 5.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +35 -0
- data/MIT-LICENSE +20 -0
- data/README.md +1139 -0
- data/Rakefile +43 -0
- data/VERSION +1 -0
- data/lib/capistrano/sitemap_generator.rb +1 -0
- data/lib/capistrano/tasks/sitemap_generator.cap +36 -0
- data/lib/sitemap_generator.rb +85 -0
- data/lib/sitemap_generator/adapters.rb +0 -0
- data/lib/sitemap_generator/adapters/file_adapter.rb +43 -0
- data/lib/sitemap_generator/adapters/fog_adapter.rb +28 -0
- data/lib/sitemap_generator/adapters/s3_adapter.rb +41 -0
- data/lib/sitemap_generator/adapters/wave_adapter.rb +21 -0
- data/lib/sitemap_generator/application.rb +49 -0
- data/lib/sitemap_generator/builder.rb +8 -0
- data/lib/sitemap_generator/builder/sitemap_file.rb +172 -0
- data/lib/sitemap_generator/builder/sitemap_index_file.rb +149 -0
- data/lib/sitemap_generator/builder/sitemap_index_url.rb +28 -0
- data/lib/sitemap_generator/builder/sitemap_url.rb +250 -0
- data/lib/sitemap_generator/core_ext.rb +3 -0
- data/lib/sitemap_generator/core_ext/big_decimal.rb +45 -0
- data/lib/sitemap_generator/core_ext/numeric.rb +48 -0
- data/lib/sitemap_generator/helpers/number_helper.rb +237 -0
- data/lib/sitemap_generator/interpreter.rb +80 -0
- data/lib/sitemap_generator/link_set.rb +677 -0
- data/lib/sitemap_generator/railtie.rb +7 -0
- data/lib/sitemap_generator/sitemap_location.rb +192 -0
- data/lib/sitemap_generator/sitemap_namer.rb +75 -0
- data/lib/sitemap_generator/tasks.rb +53 -0
- data/lib/sitemap_generator/templates.rb +41 -0
- data/lib/sitemap_generator/utilities.rb +181 -0
- data/lib/tasks/sitemap_generator_tasks.rake +1 -0
- data/rails/install.rb +2 -0
- data/rails/uninstall.rb +2 -0
- data/spec/blueprint.rb +15 -0
- data/spec/files/sitemap.create.rb +12 -0
- data/spec/files/sitemap.groups.rb +49 -0
- data/spec/sitemap_generator/adapters/s3_adapter_spec.rb +23 -0
- data/spec/sitemap_generator/alternate_sitemap_spec.rb +79 -0
- data/spec/sitemap_generator/application_spec.rb +69 -0
- data/spec/sitemap_generator/builder/sitemap_file_spec.rb +110 -0
- data/spec/sitemap_generator/builder/sitemap_index_file_spec.rb +124 -0
- data/spec/sitemap_generator/builder/sitemap_index_url_spec.rb +28 -0
- data/spec/sitemap_generator/builder/sitemap_url_spec.rb +186 -0
- data/spec/sitemap_generator/core_ext/bigdecimal_spec.rb +20 -0
- data/spec/sitemap_generator/core_ext/numeric_spec.rb +43 -0
- data/spec/sitemap_generator/file_adaptor_spec.rb +20 -0
- data/spec/sitemap_generator/geo_sitemap_spec.rb +30 -0
- data/spec/sitemap_generator/helpers/number_helper_spec.rb +196 -0
- data/spec/sitemap_generator/interpreter_spec.rb +90 -0
- data/spec/sitemap_generator/link_set_spec.rb +864 -0
- data/spec/sitemap_generator/mobile_sitemap_spec.rb +27 -0
- data/spec/sitemap_generator/news_sitemap_spec.rb +42 -0
- data/spec/sitemap_generator/pagemap_sitemap_spec.rb +57 -0
- data/spec/sitemap_generator/sitemap_generator_spec.rb +582 -0
- data/spec/sitemap_generator/sitemap_groups_spec.rb +144 -0
- data/spec/sitemap_generator/sitemap_location_spec.rb +210 -0
- data/spec/sitemap_generator/sitemap_namer_spec.rb +96 -0
- data/spec/sitemap_generator/templates_spec.rb +24 -0
- data/spec/sitemap_generator/utilities/existence_spec.rb +26 -0
- data/spec/sitemap_generator/utilities/hash_spec.rb +57 -0
- data/spec/sitemap_generator/utilities/rounding_spec.rb +31 -0
- data/spec/sitemap_generator/utilities_spec.rb +101 -0
- data/spec/sitemap_generator/video_sitemap_spec.rb +117 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/support/file_macros.rb +39 -0
- data/spec/support/schemas/siteindex.xsd +73 -0
- data/spec/support/schemas/sitemap-geo.xsd +41 -0
- data/spec/support/schemas/sitemap-mobile.xsd +32 -0
- data/spec/support/schemas/sitemap-news.xsd +159 -0
- data/spec/support/schemas/sitemap-pagemap.xsd +97 -0
- data/spec/support/schemas/sitemap-video.xsd +643 -0
- data/spec/support/schemas/sitemap.xsd +115 -0
- data/spec/support/xml_macros.rb +67 -0
- data/templates/sitemap.rb +27 -0
- metadata +226 -0
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SitemapGenerator::Builder::SitemapIndexUrl do
|
4
|
+
let(:index) {
|
5
|
+
SitemapGenerator::Builder::SitemapIndexFile.new(
|
6
|
+
:sitemaps_path => 'sitemaps/',
|
7
|
+
:host => 'http://test.com',
|
8
|
+
:filename => 'sitemap_index.xml.gz'
|
9
|
+
)
|
10
|
+
}
|
11
|
+
let(:url) { SitemapGenerator::Builder::SitemapUrl.new(index) }
|
12
|
+
|
13
|
+
it "should return the correct url" do
|
14
|
+
url[:loc].should == 'http://test.com/sitemaps/sitemap_index.xml.gz'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should use the host from the index" do
|
18
|
+
host = 'http://myexample.com'
|
19
|
+
index.location.expects(:host).returns(host)
|
20
|
+
url[:host].should == host
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should use the public path for the link" do
|
24
|
+
path = '/path'
|
25
|
+
index.location.expects(:path_in_public).returns(path)
|
26
|
+
url[:loc].should == 'http://test.com/path'
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,186 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SitemapGenerator::Builder::SitemapUrl do
|
4
|
+
let(:loc) {
|
5
|
+
SitemapGenerator::SitemapLocation.new(
|
6
|
+
:sitemaps_path => 'sitemaps/',
|
7
|
+
:public_path => '/public',
|
8
|
+
:host => 'http://test.com',
|
9
|
+
:namer => SitemapGenerator::SimpleNamer.new(:sitemap)
|
10
|
+
)}
|
11
|
+
let(:sitemap_file) { SitemapGenerator::Builder::SitemapFile.new(loc) }
|
12
|
+
|
13
|
+
def new_url(*args)
|
14
|
+
if args.empty?
|
15
|
+
args = ['/home', { :host => 'http://example.com' }]
|
16
|
+
end
|
17
|
+
SitemapGenerator::Builder::SitemapUrl.new(*args)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should build urls for sitemap files" do
|
21
|
+
url = SitemapGenerator::Builder::SitemapUrl.new(sitemap_file)
|
22
|
+
url[:loc].should == 'http://test.com/sitemaps/sitemap.xml.gz'
|
23
|
+
end
|
24
|
+
|
25
|
+
it "lastmod should default to the last modified date for sitemap files" do
|
26
|
+
lastmod = (Time.now - 1209600)
|
27
|
+
sitemap_file.expects(:lastmod).returns(lastmod)
|
28
|
+
url = SitemapGenerator::Builder::SitemapUrl.new(sitemap_file)
|
29
|
+
url[:lastmod].should == lastmod
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should support subdirectory routing" do
|
33
|
+
url = SitemapGenerator::Builder::SitemapUrl.new('/profile', :host => 'http://example.com/subdir/')
|
34
|
+
url[:loc].should == 'http://example.com/subdir/profile'
|
35
|
+
url = SitemapGenerator::Builder::SitemapUrl.new('profile', :host => 'http://example.com/subdir/')
|
36
|
+
url[:loc].should == 'http://example.com/subdir/profile'
|
37
|
+
url = SitemapGenerator::Builder::SitemapUrl.new('/deep/profile/', :host => 'http://example.com/subdir/')
|
38
|
+
url[:loc].should == 'http://example.com/subdir/deep/profile/'
|
39
|
+
url = SitemapGenerator::Builder::SitemapUrl.new('/deep/profile', :host => 'http://example.com/subdir')
|
40
|
+
url[:loc].should == 'http://example.com/subdir/deep/profile'
|
41
|
+
url = SitemapGenerator::Builder::SitemapUrl.new('deep/profile', :host => 'http://example.com/subdir')
|
42
|
+
url[:loc].should == 'http://example.com/subdir/deep/profile'
|
43
|
+
url = SitemapGenerator::Builder::SitemapUrl.new('deep/profile/', :host => 'http://example.com/subdir/')
|
44
|
+
url[:loc].should == 'http://example.com/subdir/deep/profile/'
|
45
|
+
url = SitemapGenerator::Builder::SitemapUrl.new('/', :host => 'http://example.com/subdir/')
|
46
|
+
url[:loc].should == 'http://example.com/subdir/'
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not fail on a nil path segment" do
|
50
|
+
lambda do
|
51
|
+
SitemapGenerator::Builder::SitemapUrl.new(nil, :host => 'http://example.com')[:loc].should == 'http://example.com'
|
52
|
+
end.should_not raise_error
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should support a :videos option" do
|
56
|
+
loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :videos => [1,2,3])
|
57
|
+
loc[:videos].should == [1,2,3]
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should support a singular :video option" do
|
61
|
+
loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :video => 1)
|
62
|
+
loc[:videos].should == [1]
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should support an array :video option" do
|
66
|
+
loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :video => [1,2], :videos => [3,4])
|
67
|
+
loc[:videos].should == [3,4,1,2]
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should support a :alternates option" do
|
71
|
+
loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :alternates => [1,2,3])
|
72
|
+
loc[:alternates].should == [1,2,3]
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should support a singular :alternate option" do
|
76
|
+
loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :alternate => 1)
|
77
|
+
loc[:alternates].should == [1]
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should support an array :alternate option" do
|
81
|
+
loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :alternate => [1,2], :alternates => [3,4])
|
82
|
+
loc[:alternates].should == [3,4,1,2]
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should not fail if invalid characters are used in the URL" do
|
86
|
+
special = ':$&+,;:=?@'
|
87
|
+
url = SitemapGenerator::Builder::SitemapUrl.new("/#{special}", :host => "http://example.com/#{special}/")
|
88
|
+
url[:loc].should == "http://example.com/#{special}/#{special}"
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "w3c_date" do
|
92
|
+
it "should convert dates and times to W3C format" do
|
93
|
+
url = new_url
|
94
|
+
url.send(:w3c_date, Date.new(0)).should == '0000-01-01'
|
95
|
+
url.send(:w3c_date, Time.at(0).utc).should == '1970-01-01T00:00:00+00:00'
|
96
|
+
url.send(:w3c_date, DateTime.new(0)).should == '0000-01-01T00:00:00+00:00'
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should return strings unmodified" do
|
100
|
+
new_url.send(:w3c_date, '2010-01-01').should == '2010-01-01'
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should try to convert to utc" do
|
104
|
+
time = Time.at(0)
|
105
|
+
time.expects(:respond_to?).times(2).returns(false, true) # iso8601, utc
|
106
|
+
new_url.send(:w3c_date, time).should == '1970-01-01T00:00:00+00:00'
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should include timezone for objects which do not respond to iso8601 or utc" do
|
110
|
+
time = Time.at(0)
|
111
|
+
time.expects(:respond_to?).times(2).returns(false, false) # iso8601, utc
|
112
|
+
time.expects(:strftime).times(2).returns('+0800', '1970-01-01T00:00:00')
|
113
|
+
new_url.send(:w3c_date, time).should == '1970-01-01T00:00:00+08:00'
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should support integers" do
|
117
|
+
new_url.send(:w3c_date, Time.at(0).to_i).should == '1970-01-01T00:00:00+00:00'
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "yes_or_no" do
|
122
|
+
it "should recognize truthy values" do
|
123
|
+
new_url.send(:yes_or_no, 1).should == 'yes'
|
124
|
+
new_url.send(:yes_or_no, 0).should == 'yes'
|
125
|
+
new_url.send(:yes_or_no, 'yes').should == 'yes'
|
126
|
+
new_url.send(:yes_or_no, 'Yes').should == 'yes'
|
127
|
+
new_url.send(:yes_or_no, 'YES').should == 'yes'
|
128
|
+
new_url.send(:yes_or_no, true).should == 'yes'
|
129
|
+
new_url.send(:yes_or_no, Object.new).should == 'yes'
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should recognize falsy values" do
|
133
|
+
new_url.send(:yes_or_no, nil).should == 'no'
|
134
|
+
new_url.send(:yes_or_no, 'no').should == 'no'
|
135
|
+
new_url.send(:yes_or_no, 'No').should == 'no'
|
136
|
+
new_url.send(:yes_or_no, 'NO').should == 'no'
|
137
|
+
new_url.send(:yes_or_no, false).should == 'no'
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should raise on unrecognized strings" do
|
141
|
+
lambda { new_url.send(:yes_or_no, 'dunno') }.should raise_error(ArgumentError)
|
142
|
+
lambda { new_url.send(:yes_or_no, 'yessir') }.should raise_error(ArgumentError)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe "yes_or_no_with_default" do
|
147
|
+
it "should use the default if the value is nil" do
|
148
|
+
url = new_url
|
149
|
+
url.expects(:yes_or_no).with(true).returns('surely')
|
150
|
+
url.send(:yes_or_no_with_default, nil, true).should == 'surely'
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should use the value if it is not nil" do
|
154
|
+
url = new_url
|
155
|
+
url.expects(:yes_or_no).with('surely').returns('absolutely')
|
156
|
+
url.send(:yes_or_no_with_default, 'surely', true).should == 'absolutely'
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
describe "format_float" do
|
161
|
+
it "should not modify if a string" do
|
162
|
+
new_url.send(:format_float, '0.4').should == '0.4'
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should round to one decimal place" do
|
166
|
+
url = new_url
|
167
|
+
url.send(:format_float, 0.499999).should == '0.5'
|
168
|
+
url.send(:format_float, 3.444444).should == '3.4'
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
describe "expires" do
|
173
|
+
let(:url) { SitemapGenerator::Builder::SitemapUrl.new('/path', :host => 'http://example.com', :expires => time) }
|
174
|
+
let(:time) { Time.at(0).utc }
|
175
|
+
|
176
|
+
it "should include the option" do
|
177
|
+
url[:expires].should == time
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should format it and include it in the XML" do
|
181
|
+
xml = url.to_xml
|
182
|
+
doc = Nokogiri::XML("<root xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:xhtml='http://www.w3.org/1999/xhtml'>#{xml}</root>")
|
183
|
+
doc.css('url expires').text.should == url.send(:w3c_date, time)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'bigdecimal'
|
3
|
+
|
4
|
+
describe SitemapGenerator::BigDecimal do
|
5
|
+
describe "to_yaml" do
|
6
|
+
it "should serialize correctly" do
|
7
|
+
SitemapGenerator::BigDecimal.new('100000.30020320320000000000000000000000000000001').to_yaml.should =~ /^--- 100000\.30020320320000000000000000000000000000001\n/
|
8
|
+
SitemapGenerator::BigDecimal.new('Infinity').to_yaml.should =~ /^--- \.Inf\n/
|
9
|
+
SitemapGenerator::BigDecimal.new('NaN').to_yaml.should =~ /^--- \.NaN\n/
|
10
|
+
SitemapGenerator::BigDecimal.new('-Infinity').to_yaml.should =~ /^--- -\.Inf\n/
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "to_d" do
|
15
|
+
it "should convert correctly" do
|
16
|
+
bd = SitemapGenerator::BigDecimal.new '10'
|
17
|
+
bd.to_d.should == bd
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SitemapGenerator::Numeric do
|
4
|
+
def numeric(size)
|
5
|
+
SitemapGenerator::Numeric.new(size)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "bytes" do
|
9
|
+
it "should define equality of different units" do
|
10
|
+
relationships = {
|
11
|
+
numeric( 1024).bytes => numeric( 1).kilobyte,
|
12
|
+
numeric( 1024).kilobytes => numeric( 1).megabyte,
|
13
|
+
numeric(3584.0).kilobytes => numeric(3.5).megabytes,
|
14
|
+
numeric(3584.0).megabytes => numeric(3.5).gigabytes,
|
15
|
+
numeric(1).kilobyte ** 4 => numeric( 1).terabyte,
|
16
|
+
numeric(1024).kilobytes + numeric(2).megabytes => numeric(3).megabytes,
|
17
|
+
numeric( 2).gigabytes / 4 => numeric(512).megabytes,
|
18
|
+
numeric(256).megabytes * 20 +numeric( 5).gigabytes => numeric(10).gigabytes,
|
19
|
+
numeric(1).kilobyte ** 5 => numeric(1).petabyte,
|
20
|
+
numeric(1).kilobyte ** 6 => numeric(1).exabyte
|
21
|
+
}
|
22
|
+
|
23
|
+
relationships.each do |left, right|
|
24
|
+
left.should == right
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should represent units as bytes" do
|
29
|
+
numeric(3).megabytes.should == 3145728
|
30
|
+
numeric(3).megabyte .should == 3145728
|
31
|
+
numeric(3).kilobytes.should == 3072
|
32
|
+
numeric(3).kilobyte .should == 3072
|
33
|
+
numeric(3).gigabytes.should == 3221225472
|
34
|
+
numeric(3).gigabyte .should == 3221225472
|
35
|
+
numeric(3).terabytes.should == 3298534883328
|
36
|
+
numeric(3).terabyte .should == 3298534883328
|
37
|
+
numeric(3).petabytes.should == 3377699720527872
|
38
|
+
numeric(3).petabyte .should == 3377699720527872
|
39
|
+
numeric(3).exabytes .should == 3458764513820540928
|
40
|
+
numeric(3).exabyte .should == 3458764513820540928
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "SitemapGenerator::FileAdapter" do
|
4
|
+
let(:location) { SitemapGenerator::SitemapLocation.new }
|
5
|
+
let(:adapter) { SitemapGenerator::FileAdapter.new }
|
6
|
+
|
7
|
+
describe "write" do
|
8
|
+
it "should gzip contents if filename ends in .gz" do
|
9
|
+
adapter.expects(:gzip).once
|
10
|
+
location.stubs(:filename).returns('sitemap.xml.gz')
|
11
|
+
adapter.write(location, 'data')
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should not gzip contents if filename does not end in .gz" do
|
15
|
+
adapter.expects(:plain).once
|
16
|
+
location.stubs(:filename).returns('sitemap.xml')
|
17
|
+
adapter.write(location, 'data')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "SitemapGenerator" do
|
4
|
+
|
5
|
+
it "should add the geo sitemap element" do
|
6
|
+
loc = 'http://www.example.com/geo_page.kml'
|
7
|
+
format = 'kml'
|
8
|
+
|
9
|
+
geo_xml_fragment = SitemapGenerator::Builder::SitemapUrl.new('geo_page.kml',
|
10
|
+
:host => 'http://www.example.com',
|
11
|
+
:geo => {
|
12
|
+
:format => format
|
13
|
+
}
|
14
|
+
).to_xml
|
15
|
+
|
16
|
+
# Check that the options were parsed correctly
|
17
|
+
doc = Nokogiri::XML.parse("<root xmlns:geo='#{SitemapGenerator::SCHEMAS['geo']}'>#{geo_xml_fragment}</root>")
|
18
|
+
url = doc.at_xpath("//url")
|
19
|
+
url.should_not be_nil
|
20
|
+
url.at_xpath("loc").text.should == loc
|
21
|
+
|
22
|
+
geo = url.at_xpath("geo:geo")
|
23
|
+
geo.should_not be_nil
|
24
|
+
geo.at_xpath("geo:format").text.should == format
|
25
|
+
|
26
|
+
# Google's documentation and published schema don't match some valid elements may
|
27
|
+
# not validate.
|
28
|
+
xml_fragment_should_validate_against_schema(geo, 'sitemap-geo', 'xmlns:geo' => SitemapGenerator::SCHEMAS['geo'])
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,196 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sitemap_generator/helpers/number_helper'
|
3
|
+
|
4
|
+
def kilobytes(number)
|
5
|
+
number * 1024
|
6
|
+
end
|
7
|
+
|
8
|
+
def megabytes(number)
|
9
|
+
kilobytes(number) * 1024
|
10
|
+
end
|
11
|
+
|
12
|
+
def gigabytes(number)
|
13
|
+
megabytes(number) * 1024
|
14
|
+
end
|
15
|
+
|
16
|
+
def terabytes(number)
|
17
|
+
gigabytes(number) * 1024
|
18
|
+
end
|
19
|
+
|
20
|
+
describe SitemapGenerator::Helpers::NumberHelper do
|
21
|
+
include SitemapGenerator::Helpers::NumberHelper
|
22
|
+
|
23
|
+
it "should number_with_delimiter" do
|
24
|
+
number_with_delimiter(12345678).should == "12,345,678"
|
25
|
+
number_with_delimiter(0).should == "0"
|
26
|
+
number_with_delimiter(123).should == "123"
|
27
|
+
number_with_delimiter(123456).should == "123,456"
|
28
|
+
number_with_delimiter(123456.78).should == "123,456.78"
|
29
|
+
number_with_delimiter(123456.789).should == "123,456.789"
|
30
|
+
number_with_delimiter(123456.78901).should == "123,456.78901"
|
31
|
+
number_with_delimiter(123456789.78901).should == "123,456,789.78901"
|
32
|
+
number_with_delimiter(0.78901).should == "0.78901"
|
33
|
+
number_with_delimiter("123456.78").should == "123,456.78"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should number_with_delimiter_with_options_hash" do
|
37
|
+
number_with_delimiter(12345678, :delimiter => ' ').should == '12 345 678'
|
38
|
+
number_with_delimiter(12345678.05, :separator => '-').should == '12,345,678-05'
|
39
|
+
number_with_delimiter(12345678.05, :separator => ',', :delimiter => '.').should == '12.345.678,05'
|
40
|
+
number_with_delimiter(12345678.05, :delimiter => '.', :separator => ',').should == '12.345.678,05'
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should number_with_precision" do
|
44
|
+
number_with_precision(-111.2346).should == "-111.235"
|
45
|
+
number_with_precision(111.2346).should == "111.235"
|
46
|
+
number_with_precision(31.825, :precision => 2).should == "31.83"
|
47
|
+
number_with_precision(111.2346, :precision => 2).should == "111.23"
|
48
|
+
number_with_precision(111, :precision => 2).should == "111.00"
|
49
|
+
number_with_precision("111.2346").should == "111.235"
|
50
|
+
number_with_precision("31.825", :precision => 2).should == "31.83"
|
51
|
+
number_with_precision((32.6751 * 100.00), :precision => 0).should == "3268"
|
52
|
+
number_with_precision(111.50, :precision => 0).should == "112"
|
53
|
+
number_with_precision(1234567891.50, :precision => 0).should == "1234567892"
|
54
|
+
number_with_precision(0, :precision => 0).should == "0"
|
55
|
+
number_with_precision(0.001, :precision => 5).should == "0.00100"
|
56
|
+
number_with_precision(0.00111, :precision => 3).should == "0.001"
|
57
|
+
# Odd difference between Ruby versions
|
58
|
+
if RUBY_VERSION < '1.9.3'
|
59
|
+
number_with_precision(9.995, :precision => 2).should == "9.99"
|
60
|
+
else
|
61
|
+
number_with_precision(9.995, :precision => 2).should == "10.00"
|
62
|
+
end
|
63
|
+
number_with_precision(10.995, :precision => 2).should == "11.00"
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should number_with_precision_with_custom_delimiter_and_separator" do
|
67
|
+
number_with_precision(31.825, :precision => 2, :separator => ',').should == '31,83'
|
68
|
+
number_with_precision(1231.825, :precision => 2, :separator => ',', :delimiter => '.').should == '1.231,83'
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should number_with_precision_with_significant_digits" do
|
72
|
+
number_with_precision(123987, :precision => 3, :significant => true).should == "124000"
|
73
|
+
number_with_precision(123987876, :precision => 2, :significant => true ).should == "120000000"
|
74
|
+
number_with_precision("43523", :precision => 1, :significant => true ).should == "40000"
|
75
|
+
number_with_precision(9775, :precision => 4, :significant => true ).should == "9775"
|
76
|
+
number_with_precision(5.3923, :precision => 2, :significant => true ).should == "5.4"
|
77
|
+
number_with_precision(5.3923, :precision => 1, :significant => true ).should == "5"
|
78
|
+
number_with_precision(1.232, :precision => 1, :significant => true ).should == "1"
|
79
|
+
number_with_precision(7, :precision => 1, :significant => true ).should == "7"
|
80
|
+
number_with_precision(1, :precision => 1, :significant => true ).should == "1"
|
81
|
+
number_with_precision(52.7923, :precision => 2, :significant => true ).should == "53"
|
82
|
+
number_with_precision(9775, :precision => 6, :significant => true ).should == "9775.00"
|
83
|
+
number_with_precision(5.3929, :precision => 7, :significant => true ).should == "5.392900"
|
84
|
+
number_with_precision(0, :precision => 2, :significant => true ).should == "0.0"
|
85
|
+
number_with_precision(0, :precision => 1, :significant => true ).should == "0"
|
86
|
+
number_with_precision(0.0001, :precision => 1, :significant => true ).should == "0.0001"
|
87
|
+
number_with_precision(0.0001, :precision => 3, :significant => true ).should == "0.000100"
|
88
|
+
number_with_precision(0.0001111, :precision => 1, :significant => true ).should == "0.0001"
|
89
|
+
number_with_precision(9.995, :precision => 3, :significant => true).should == "10.0"
|
90
|
+
number_with_precision(9.994, :precision => 3, :significant => true).should == "9.99"
|
91
|
+
number_with_precision(10.995, :precision => 3, :significant => true).should == "11.0"
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should number_with_precision_with_strip_insignificant_zeros" do
|
95
|
+
number_with_precision(9775.43, :precision => 4, :strip_insignificant_zeros => true ).should == "9775.43"
|
96
|
+
number_with_precision(9775.2, :precision => 6, :significant => true, :strip_insignificant_zeros => true ).should == "9775.2"
|
97
|
+
number_with_precision(0, :precision => 6, :significant => true, :strip_insignificant_zeros => true ).should == "0"
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should number_with_precision_with_significant_true_and_zero_precision" do
|
101
|
+
# Zero precision with significant is a mistake (would always return zero),
|
102
|
+
# so we treat it as if significant was false (increases backwards compatibily for number_to_human_size)
|
103
|
+
number_with_precision(123.987, :precision => 0, :significant => true).should == "124"
|
104
|
+
number_with_precision(12, :precision => 0, :significant => true ).should == "12"
|
105
|
+
number_with_precision("12.3", :precision => 0, :significant => true ).should == "12"
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should number_to_human_size" do
|
109
|
+
number_to_human_size(0).should == '0 Bytes'
|
110
|
+
number_to_human_size(1).should == '1 Byte'
|
111
|
+
number_to_human_size(3.14159265).should == '3 Bytes'
|
112
|
+
number_to_human_size(123.0).should == '123 Bytes'
|
113
|
+
number_to_human_size(123).should == '123 Bytes'
|
114
|
+
number_to_human_size(1234).should == '1.21 KB'
|
115
|
+
number_to_human_size(12345).should == '12.1 KB'
|
116
|
+
number_to_human_size(1234567).should == '1.18 MB'
|
117
|
+
number_to_human_size(1234567890).should == '1.15 GB'
|
118
|
+
number_to_human_size(1234567890123).should == '1.12 TB'
|
119
|
+
number_to_human_size(terabytes(1026)).should == '1030 TB'
|
120
|
+
number_to_human_size(kilobytes(444)).should == '444 KB'
|
121
|
+
number_to_human_size(megabytes(1023)).should == '1020 MB'
|
122
|
+
number_to_human_size(terabytes(3)).should == '3 TB'
|
123
|
+
number_to_human_size(1234567, :precision => 2).should == '1.2 MB'
|
124
|
+
number_to_human_size(3.14159265, :precision => 4).should == '3 Bytes'
|
125
|
+
number_to_human_size('123').should == '123 Bytes'
|
126
|
+
number_to_human_size(kilobytes(1.0123), :precision => 2).should == '1 KB'
|
127
|
+
number_to_human_size(kilobytes(1.0100), :precision => 4).should == '1.01 KB'
|
128
|
+
number_to_human_size(kilobytes(10.000), :precision => 4).should == '10 KB'
|
129
|
+
number_to_human_size(1.1).should == '1 Byte'
|
130
|
+
number_to_human_size(10).should == '10 Bytes'
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should number_to_human_size_with_options_hash" do
|
134
|
+
number_to_human_size(1234567, :precision => 2).should == '1.2 MB'
|
135
|
+
number_to_human_size(3.14159265, :precision => 4).should == '3 Bytes'
|
136
|
+
number_to_human_size(kilobytes(1.0123), :precision => 2).should == '1 KB'
|
137
|
+
number_to_human_size(kilobytes(1.0100), :precision => 4).should == '1.01 KB'
|
138
|
+
number_to_human_size(kilobytes(10.000), :precision => 4).should == '10 KB'
|
139
|
+
number_to_human_size(1234567890123, :precision => 1).should == '1 TB'
|
140
|
+
number_to_human_size(524288000, :precision=>3).should == '500 MB'
|
141
|
+
number_to_human_size(9961472, :precision=>0).should == '10 MB'
|
142
|
+
number_to_human_size(41010, :precision => 1).should == '40 KB'
|
143
|
+
number_to_human_size(41100, :precision => 2).should == '40 KB'
|
144
|
+
number_to_human_size(kilobytes(1.0123), :precision => 2, :strip_insignificant_zeros => false).should == '1.0 KB'
|
145
|
+
number_to_human_size(kilobytes(1.0123), :precision => 3, :significant => false).should == '1.012 KB'
|
146
|
+
number_to_human_size(kilobytes(1.0123), :precision => 0, :significant => true) #ignores significant it precision is 0.should == '1 KB'
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should number_to_human_size_with_custom_delimiter_and_separator" do
|
150
|
+
number_to_human_size(kilobytes(1.0123), :precision => 3, :separator => ',') .should == '1,01 KB'
|
151
|
+
number_to_human_size(kilobytes(1.0100), :precision => 4, :separator => ',') .should == '1,01 KB'
|
152
|
+
number_to_human_size(terabytes(1000.1), :precision => 5, :delimiter => '.', :separator => ',') .should == '1.000,1 TB'
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should number_helpers_should_return_nil_when_given_nil" do
|
156
|
+
number_with_delimiter(nil).should be_nil
|
157
|
+
number_with_precision(nil).should be_nil
|
158
|
+
number_to_human_size(nil).should be_nil
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should number_helpers_should_return_non_numeric_param_unchanged" do
|
162
|
+
number_with_delimiter("x").should == "x"
|
163
|
+
number_with_precision("x.").should == "x."
|
164
|
+
number_with_precision("x").should == "x"
|
165
|
+
number_to_human_size('x').should == "x"
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should number_helpers_should_raise_error_if_invalid_when_specified" do
|
169
|
+
lambda do
|
170
|
+
number_to_human_size("x", :raise => true)
|
171
|
+
end.should raise_error(SitemapGenerator::Helpers::NumberHelper::InvalidNumberError)
|
172
|
+
begin
|
173
|
+
number_to_human_size("x", :raise => true)
|
174
|
+
rescue SitemapGenerator::Helpers::NumberHelper::InvalidNumberError => e
|
175
|
+
e.number.should == "x"
|
176
|
+
end
|
177
|
+
|
178
|
+
lambda do
|
179
|
+
number_with_precision("x", :raise => true)
|
180
|
+
end.should raise_error(SitemapGenerator::Helpers::NumberHelper::InvalidNumberError)
|
181
|
+
begin
|
182
|
+
number_with_precision("x", :raise => true)
|
183
|
+
rescue SitemapGenerator::Helpers::NumberHelper::InvalidNumberError => e
|
184
|
+
e.number.should == "x"
|
185
|
+
end
|
186
|
+
|
187
|
+
lambda do
|
188
|
+
number_with_delimiter("x", :raise => true)
|
189
|
+
end.should raise_error(SitemapGenerator::Helpers::NumberHelper::InvalidNumberError)
|
190
|
+
begin
|
191
|
+
number_with_delimiter("x", :raise => true)
|
192
|
+
rescue SitemapGenerator::Helpers::NumberHelper::InvalidNumberError => e
|
193
|
+
e.number.should == "x"
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|