sitemap_generator 2.2.1 → 3.0.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.
Files changed (59) hide show
  1. data/Gemfile +9 -24
  2. data/Gemfile.lock +23 -58
  3. data/README.md +56 -75
  4. data/Rakefile +29 -117
  5. data/VERSION +1 -1
  6. data/lib/sitemap_generator.rb +24 -8
  7. data/lib/sitemap_generator/application.rb +31 -4
  8. data/lib/sitemap_generator/builder.rb +0 -6
  9. data/lib/sitemap_generator/builder/sitemap_file.rb +16 -6
  10. data/lib/sitemap_generator/builder/sitemap_index_file.rb +4 -3
  11. data/lib/sitemap_generator/builder/sitemap_index_url.rb +1 -1
  12. data/lib/sitemap_generator/builder/sitemap_url.rb +6 -8
  13. data/lib/sitemap_generator/core_ext.rb +3 -0
  14. data/lib/sitemap_generator/core_ext/big_decimal.rb +45 -0
  15. data/lib/sitemap_generator/core_ext/numeric.rb +48 -0
  16. data/lib/sitemap_generator/helpers/number_helper.rb +237 -0
  17. data/lib/sitemap_generator/interpreter.rb +1 -1
  18. data/lib/sitemap_generator/link_set.rb +39 -18
  19. data/lib/sitemap_generator/railtie.rb +2 -2
  20. data/lib/sitemap_generator/sitemap_namer.rb +1 -1
  21. data/lib/sitemap_generator/tasks.rb +53 -1
  22. data/lib/sitemap_generator/utilities.rb +107 -1
  23. data/lib/tasks/sitemap_generator_tasks.rake +1 -0
  24. data/spec/blueprint.rb +15 -0
  25. data/spec/files/sitemap.create.rb +12 -0
  26. data/spec/files/sitemap.deprecated.rb +13 -0
  27. data/spec/files/sitemap.groups.rb +37 -0
  28. data/spec/sitemap_generator/application_spec.rb +69 -0
  29. data/spec/sitemap_generator/builder/sitemap_file_spec.rb +77 -0
  30. data/spec/sitemap_generator/builder/sitemap_index_file_spec.rb +38 -0
  31. data/spec/sitemap_generator/builder/sitemap_index_url_spec.rb +16 -0
  32. data/spec/sitemap_generator/builder/sitemap_url_spec.rb +152 -0
  33. data/spec/sitemap_generator/core_ext/bigdecimal_spec.rb +20 -0
  34. data/spec/sitemap_generator/core_ext/numeric_spec.rb +43 -0
  35. data/spec/sitemap_generator/geo_sitemap_spec.rb +30 -0
  36. data/spec/sitemap_generator/helpers/number_helper_spec.rb +191 -0
  37. data/spec/sitemap_generator/interpreter_spec.rb +24 -0
  38. data/spec/sitemap_generator/link_set_spec.rb +606 -0
  39. data/spec/sitemap_generator/news_sitemap_spec.rb +42 -0
  40. data/spec/sitemap_generator/sitemap_generator_spec.rb +232 -0
  41. data/spec/sitemap_generator/sitemap_groups_spec.rb +133 -0
  42. data/spec/sitemap_generator/sitemap_location_spec.rb +124 -0
  43. data/spec/sitemap_generator/sitemap_namer_spec.rb +61 -0
  44. data/spec/sitemap_generator/templates_spec.rb +24 -0
  45. data/spec/sitemap_generator/utilities/existence_spec.rb +26 -0
  46. data/spec/sitemap_generator/utilities/hash_spec.rb +57 -0
  47. data/spec/sitemap_generator/utilities/rounding_spec.rb +31 -0
  48. data/spec/sitemap_generator/utilities_spec.rb +50 -0
  49. data/spec/sitemap_generator/video_sitemap_spec.rb +103 -0
  50. data/spec/spec_helper.rb +20 -0
  51. data/spec/support/file_macros.rb +39 -0
  52. data/spec/support/schemas/siteindex.xsd +73 -0
  53. data/spec/support/schemas/sitemap-geo.xsd +41 -0
  54. data/spec/support/schemas/sitemap-news.xsd +159 -0
  55. data/spec/support/schemas/sitemap-video.xsd +409 -0
  56. data/spec/support/schemas/sitemap.xsd +115 -0
  57. data/spec/support/xml_macros.rb +55 -0
  58. metadata +141 -122
  59. data/tasks/sitemap_generator_tasks.rake +0 -43
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'SitemapGenerator::Builder::SitemapFile' do
4
+ before :each do
5
+ @loc = SitemapGenerator::SitemapLocation.new(:namer => SitemapGenerator::SitemapNamer.new(:sitemap), :public_path => 'tmp/', :sitemaps_path => 'test/', :host => 'http://example.com/')
6
+ @s = SitemapGenerator::Builder::SitemapFile.new(@loc)
7
+ end
8
+
9
+ it "should have a default namer" do
10
+ @s = SitemapGenerator::Builder::SitemapFile.new
11
+ @s.location.filename.should == 'sitemap1.xml.gz'
12
+ end
13
+
14
+ it "should return the name of the sitemap file" do
15
+ @s.location.filename.should == 'sitemap1.xml.gz'
16
+ end
17
+
18
+ it "should return the URL" do
19
+ @s.location.url.should == 'http://example.com/test/sitemap1.xml.gz'
20
+ end
21
+
22
+ it "should return the path" do
23
+ @s.location.path.should == File.expand_path('tmp/test/sitemap1.xml.gz')
24
+ end
25
+
26
+ it "should be empty" do
27
+ @s.empty?.should be_true
28
+ @s.link_count.should == 0
29
+ end
30
+
31
+ it "should not be finalized" do
32
+ @s.finalized?.should be_false
33
+ end
34
+
35
+ it "should increment the namer after finalizing" do
36
+ @s.finalize!
37
+ @s.location.filename.should_not == @s.location.namer.to_s
38
+ end
39
+
40
+ it "should raise if no default host is set" do
41
+ lambda { SitemapGenerator::Builder::SitemapFile.new.location.url }.should raise_error(SitemapGenerator::SitemapError)
42
+ end
43
+
44
+ describe "lastmod" do
45
+ it "should be the file last modified time" do
46
+ lastmod = (Time.now - 1209600)
47
+ File.expects(:mtime).with(@s.location.path).returns(lastmod)
48
+ @s.lastmod.should == lastmod
49
+ end
50
+
51
+ it "should be nil if the file DNE" do
52
+ File.expects(:mtime).raises(Errno::ENOENT)
53
+ @s.lastmod.should be_nil
54
+ end
55
+ end
56
+
57
+ describe "new" do
58
+ before :each do
59
+ @orig_s = @s
60
+ @s = @s.new
61
+ end
62
+
63
+ it "should inherit the same options" do
64
+ # The name is the same because the original sitemap was not finalized
65
+ @s.location.url.should == 'http://example.com/test/sitemap1.xml.gz'
66
+ @s.location.path.should == File.expand_path('tmp/test/sitemap1.xml.gz')
67
+ end
68
+
69
+ it "should not share the same location instance" do
70
+ @s.location.should_not be(@orig_s.location)
71
+ end
72
+
73
+ it "should inherit the same namer instance" do
74
+ @s.location.namer.should == @orig_s.location.namer
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'SitemapGenerator::Builder::SitemapIndexFile' do
4
+ before :each do
5
+ @loc = SitemapGenerator::SitemapLocation.new(:filename => 'sitemap_index.xml.gz', :public_path => '/public/', :sitemaps_path => 'test/', :host => 'http://example.com/')
6
+ @s = SitemapGenerator::Builder::SitemapIndexFile.new(@loc)
7
+ end
8
+
9
+ it "should return the URL" do
10
+ @s.location.url.should == 'http://example.com/test/sitemap_index.xml.gz'
11
+ end
12
+
13
+ it "should return the path" do
14
+ @s.location.path.should == '/public/test/sitemap_index.xml.gz'
15
+ end
16
+
17
+ it "should be empty" do
18
+ @s.empty?.should be_true
19
+ @s.link_count.should == 0
20
+ end
21
+
22
+ it "should not have a last modification data" do
23
+ @s.lastmod.should be_nil
24
+ end
25
+
26
+ it "should not be finalized" do
27
+ @s.finalized?.should be_false
28
+ end
29
+
30
+ it "filename should default to sitemap_index" do
31
+ @s.location.filename.should == 'sitemap_index.xml.gz'
32
+ end
33
+
34
+ it "should have a default namer" do
35
+ @s = SitemapGenerator::Builder::SitemapIndexFile.new
36
+ @s.location.filename.should == 'sitemap_index.xml.gz'
37
+ end
38
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe SitemapGenerator::Builder::SitemapIndexUrl do
4
+ before :all do
5
+ @s = SitemapGenerator::Builder::SitemapIndexFile.new(
6
+ :sitemaps_path => 'sitemaps/',
7
+ :host => 'http://test.com',
8
+ :filename => 'sitemap_index.xml.gz'
9
+ )
10
+ end
11
+
12
+ it "should return the correct url" do
13
+ @u = SitemapGenerator::Builder::SitemapUrl.new(@s)
14
+ @u[:loc].should == 'http://test.com/sitemaps/sitemap_index.xml.gz'
15
+ end
16
+ end
@@ -0,0 +1,152 @@
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::SitemapNamer.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/sitemap1.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 not fail if invalid characters are used in the URL" do
71
+ special = ':$&+,;:=?@'
72
+ url = SitemapGenerator::Builder::SitemapUrl.new("/#{special}", :host => "http://example.com/#{special}/")
73
+ url[:loc].should == "http://example.com/#{special}/#{special}"
74
+ end
75
+
76
+ describe "w3c_date" do
77
+ it "should convert dates and times to W3C format" do
78
+ url = new_url
79
+ url.send(:w3c_date, Date.new(0)).should == '0000-01-01'
80
+ url.send(:w3c_date, Time.at(0).utc).should == '1970-01-01T00:00:00+00:00'
81
+ url.send(:w3c_date, DateTime.new(0)).should == '0000-01-01T00:00:00+00:00'
82
+ end
83
+
84
+ it "should return strings unmodified" do
85
+ new_url.send(:w3c_date, '2010-01-01').should == '2010-01-01'
86
+ end
87
+
88
+ it "should try to convert to utc" do
89
+ time = Time.at(0)
90
+ time.expects(:respond_to?).times(2).returns(false, true) # iso8601, utc
91
+ new_url.send(:w3c_date, time).should == '1970-01-01T00:00:00+00:00'
92
+ end
93
+
94
+ it "should include timezone for objects which do not respond to iso8601 or utc" do
95
+ time = Time.at(0)
96
+ time.expects(:respond_to?).times(2).returns(false, false) # iso8601, utc
97
+ time.expects(:strftime).times(2).returns('+0800', '1970-01-01T00:00:00')
98
+ new_url.send(:w3c_date, time).should == '1970-01-01T00:00:00+08:00'
99
+ end
100
+ end
101
+
102
+ describe "yes_or_no" do
103
+ it "should recognize truthy values" do
104
+ new_url.send(:yes_or_no, 1).should == 'yes'
105
+ new_url.send(:yes_or_no, 0).should == 'yes'
106
+ new_url.send(:yes_or_no, 'yes').should == 'yes'
107
+ new_url.send(:yes_or_no, 'Yes').should == 'yes'
108
+ new_url.send(:yes_or_no, 'YES').should == 'yes'
109
+ new_url.send(:yes_or_no, true).should == 'yes'
110
+ new_url.send(:yes_or_no, Object.new).should == 'yes'
111
+ end
112
+
113
+ it "should recognize falsy values" do
114
+ new_url.send(:yes_or_no, nil).should == 'no'
115
+ new_url.send(:yes_or_no, 'no').should == 'no'
116
+ new_url.send(:yes_or_no, 'No').should == 'no'
117
+ new_url.send(:yes_or_no, 'NO').should == 'no'
118
+ new_url.send(:yes_or_no, false).should == 'no'
119
+ end
120
+
121
+ it "should raise on unrecognized strings" do
122
+ lambda { new_url.send(:yes_or_no, 'dunno') }.should raise_error(ArgumentError)
123
+ lambda { new_url.send(:yes_or_no, 'yessir') }.should raise_error(ArgumentError)
124
+ end
125
+ end
126
+
127
+ describe "yes_or_no_with_default" do
128
+ it "should use the default if the value is nil" do
129
+ url = new_url
130
+ url.expects(:yes_or_no).with(true).returns('surely')
131
+ url.send(:yes_or_no_with_default, nil, true).should == 'surely'
132
+ end
133
+
134
+ it "should use the value if it is not nil" do
135
+ url = new_url
136
+ url.expects(:yes_or_no).with('surely').returns('absolutely')
137
+ url.send(:yes_or_no_with_default, 'surely', true).should == 'absolutely'
138
+ end
139
+ end
140
+
141
+ describe "format_float" do
142
+ it "should not modify if a string" do
143
+ new_url.send(:format_float, '0.4').should == '0.4'
144
+ end
145
+
146
+ it "should round to one decimal place" do
147
+ url = new_url
148
+ url.send(:format_float, 0.499999).should == '0.5'
149
+ url.send(:format_float, 3.444444).should == '3.4'
150
+ end
151
+ end
152
+ 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,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='http://www.google.com/geo/schemas/sitemap/1.0'>#{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, 'http://www.google.com/geo/schemas/sitemap/1.0', 'sitemap-geo')
29
+ end
30
+ end
@@ -0,0 +1,191 @@
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
+ number_with_precision(9.995, :precision => 2).should == "9.99"
58
+ number_with_precision(10.995, :precision => 2).should == "11.00"
59
+ end
60
+
61
+ it "should number_with_precision_with_custom_delimiter_and_separator" do
62
+ number_with_precision(31.825, :precision => 2, :separator => ',').should == '31,83'
63
+ number_with_precision(1231.825, :precision => 2, :separator => ',', :delimiter => '.').should == '1.231,83'
64
+ end
65
+
66
+ it "should number_with_precision_with_significant_digits" do
67
+ number_with_precision(123987, :precision => 3, :significant => true).should == "124000"
68
+ number_with_precision(123987876, :precision => 2, :significant => true ).should == "120000000"
69
+ number_with_precision("43523", :precision => 1, :significant => true ).should == "40000"
70
+ number_with_precision(9775, :precision => 4, :significant => true ).should == "9775"
71
+ number_with_precision(5.3923, :precision => 2, :significant => true ).should == "5.4"
72
+ number_with_precision(5.3923, :precision => 1, :significant => true ).should == "5"
73
+ number_with_precision(1.232, :precision => 1, :significant => true ).should == "1"
74
+ number_with_precision(7, :precision => 1, :significant => true ).should == "7"
75
+ number_with_precision(1, :precision => 1, :significant => true ).should == "1"
76
+ number_with_precision(52.7923, :precision => 2, :significant => true ).should == "53"
77
+ number_with_precision(9775, :precision => 6, :significant => true ).should == "9775.00"
78
+ number_with_precision(5.3929, :precision => 7, :significant => true ).should == "5.392900"
79
+ number_with_precision(0, :precision => 2, :significant => true ).should == "0.0"
80
+ number_with_precision(0, :precision => 1, :significant => true ).should == "0"
81
+ number_with_precision(0.0001, :precision => 1, :significant => true ).should == "0.0001"
82
+ number_with_precision(0.0001, :precision => 3, :significant => true ).should == "0.000100"
83
+ number_with_precision(0.0001111, :precision => 1, :significant => true ).should == "0.0001"
84
+ number_with_precision(9.995, :precision => 3, :significant => true).should == "10.0"
85
+ number_with_precision(9.994, :precision => 3, :significant => true).should == "9.99"
86
+ number_with_precision(10.995, :precision => 3, :significant => true).should == "11.0"
87
+ end
88
+
89
+ it "should number_with_precision_with_strip_insignificant_zeros" do
90
+ number_with_precision(9775.43, :precision => 4, :strip_insignificant_zeros => true ).should == "9775.43"
91
+ number_with_precision(9775.2, :precision => 6, :significant => true, :strip_insignificant_zeros => true ).should == "9775.2"
92
+ number_with_precision(0, :precision => 6, :significant => true, :strip_insignificant_zeros => true ).should == "0"
93
+ end
94
+
95
+ it "should number_with_precision_with_significant_true_and_zero_precision" do
96
+ # Zero precision with significant is a mistake (would always return zero),
97
+ # so we treat it as if significant was false (increases backwards compatibily for number_to_human_size)
98
+ number_with_precision(123.987, :precision => 0, :significant => true).should == "124"
99
+ number_with_precision(12, :precision => 0, :significant => true ).should == "12"
100
+ number_with_precision("12.3", :precision => 0, :significant => true ).should == "12"
101
+ end
102
+
103
+ it "should number_to_human_size" do
104
+ number_to_human_size(0).should == '0 Bytes'
105
+ number_to_human_size(1).should == '1 Byte'
106
+ number_to_human_size(3.14159265).should == '3 Bytes'
107
+ number_to_human_size(123.0).should == '123 Bytes'
108
+ number_to_human_size(123).should == '123 Bytes'
109
+ number_to_human_size(1234).should == '1.21 KB'
110
+ number_to_human_size(12345).should == '12.1 KB'
111
+ number_to_human_size(1234567).should == '1.18 MB'
112
+ number_to_human_size(1234567890).should == '1.15 GB'
113
+ number_to_human_size(1234567890123).should == '1.12 TB'
114
+ number_to_human_size(terabytes(1026)).should == '1030 TB'
115
+ number_to_human_size(kilobytes(444)).should == '444 KB'
116
+ number_to_human_size(megabytes(1023)).should == '1020 MB'
117
+ number_to_human_size(terabytes(3)).should == '3 TB'
118
+ number_to_human_size(1234567, :precision => 2).should == '1.2 MB'
119
+ number_to_human_size(3.14159265, :precision => 4).should == '3 Bytes'
120
+ number_to_human_size('123').should == '123 Bytes'
121
+ number_to_human_size(kilobytes(1.0123), :precision => 2).should == '1 KB'
122
+ number_to_human_size(kilobytes(1.0100), :precision => 4).should == '1.01 KB'
123
+ number_to_human_size(kilobytes(10.000), :precision => 4).should == '10 KB'
124
+ number_to_human_size(1.1).should == '1 Byte'
125
+ number_to_human_size(10).should == '10 Bytes'
126
+ end
127
+
128
+ it "should number_to_human_size_with_options_hash" do
129
+ number_to_human_size(1234567, :precision => 2).should == '1.2 MB'
130
+ number_to_human_size(3.14159265, :precision => 4).should == '3 Bytes'
131
+ number_to_human_size(kilobytes(1.0123), :precision => 2).should == '1 KB'
132
+ number_to_human_size(kilobytes(1.0100), :precision => 4).should == '1.01 KB'
133
+ number_to_human_size(kilobytes(10.000), :precision => 4).should == '10 KB'
134
+ number_to_human_size(1234567890123, :precision => 1).should == '1 TB'
135
+ number_to_human_size(524288000, :precision=>3).should == '500 MB'
136
+ number_to_human_size(9961472, :precision=>0).should == '10 MB'
137
+ number_to_human_size(41010, :precision => 1).should == '40 KB'
138
+ number_to_human_size(41100, :precision => 2).should == '40 KB'
139
+ number_to_human_size(kilobytes(1.0123), :precision => 2, :strip_insignificant_zeros => false).should == '1.0 KB'
140
+ number_to_human_size(kilobytes(1.0123), :precision => 3, :significant => false).should == '1.012 KB'
141
+ number_to_human_size(kilobytes(1.0123), :precision => 0, :significant => true) #ignores significant it precision is 0.should == '1 KB'
142
+ end
143
+
144
+ it "should number_to_human_size_with_custom_delimiter_and_separator" do
145
+ number_to_human_size(kilobytes(1.0123), :precision => 3, :separator => ',') .should == '1,01 KB'
146
+ number_to_human_size(kilobytes(1.0100), :precision => 4, :separator => ',') .should == '1,01 KB'
147
+ number_to_human_size(terabytes(1000.1), :precision => 5, :delimiter => '.', :separator => ',') .should == '1.000,1 TB'
148
+ end
149
+
150
+ it "should number_helpers_should_return_nil_when_given_nil" do
151
+ number_with_delimiter(nil).should be_nil
152
+ number_with_precision(nil).should be_nil
153
+ number_to_human_size(nil).should be_nil
154
+ end
155
+
156
+ it "should number_helpers_should_return_non_numeric_param_unchanged" do
157
+ number_with_delimiter("x").should == "x"
158
+ number_with_precision("x.").should == "x."
159
+ number_with_precision("x").should == "x"
160
+ number_to_human_size('x').should == "x"
161
+ end
162
+
163
+ it "should number_helpers_should_raise_error_if_invalid_when_specified" do
164
+ lambda do
165
+ number_to_human_size("x", :raise => true)
166
+ end.should raise_error(SitemapGenerator::Helpers::NumberHelper::InvalidNumberError)
167
+ begin
168
+ number_to_human_size("x", :raise => true)
169
+ rescue SitemapGenerator::Helpers::NumberHelper::InvalidNumberError => e
170
+ e.number.should == "x"
171
+ end
172
+
173
+ lambda do
174
+ number_with_precision("x", :raise => true)
175
+ end.should raise_error(SitemapGenerator::Helpers::NumberHelper::InvalidNumberError)
176
+ begin
177
+ number_with_precision("x", :raise => true)
178
+ rescue SitemapGenerator::Helpers::NumberHelper::InvalidNumberError => e
179
+ e.number.should == "x"
180
+ end
181
+
182
+ lambda do
183
+ number_with_delimiter("x", :raise => true)
184
+ end.should raise_error(SitemapGenerator::Helpers::NumberHelper::InvalidNumberError)
185
+ begin
186
+ number_with_delimiter("x", :raise => true)
187
+ rescue SitemapGenerator::Helpers::NumberHelper::InvalidNumberError => e
188
+ e.number.should == "x"
189
+ end
190
+ end
191
+ end