sinatra-support 1.0.1

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.
data/test/test_date.rb ADDED
@@ -0,0 +1,102 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class DateTest < Test::Unit::TestCase
4
+ include Sinatra::DateForms::Helpers
5
+
6
+ test "day_choices returns 1 to 31" do
7
+ assert_equal 31, day_choices.size
8
+ assert_equal [1, 1], day_choices.first
9
+ assert_equal [31, 31], day_choices.last
10
+
11
+ day_choices.each do |label, value|
12
+ assert_equal label, value
13
+ end
14
+ end
15
+
16
+ describe "month choices" do
17
+ setup do
18
+ settings.stubs(:default_month_names).returns(Date::MONTHNAMES)
19
+ end
20
+
21
+ test "by default" do
22
+ assert_equal ["January", 1], month_choices[0]
23
+ assert_equal ["February", 2], month_choices[1]
24
+ assert_equal ["March", 3], month_choices[2]
25
+ assert_equal ["April", 4], month_choices[3]
26
+ assert_equal ["May", 5], month_choices[4]
27
+ assert_equal ["June", 6], month_choices[5]
28
+ assert_equal ["July", 7], month_choices[6]
29
+ assert_equal ["August", 8], month_choices[7]
30
+ assert_equal ["September", 9], month_choices[8]
31
+ assert_equal ["October", 10], month_choices[9]
32
+ assert_equal ["November", 11], month_choices[10]
33
+ assert_equal ["December", 12], month_choices[11]
34
+ end
35
+
36
+ test "given Date::ABBR_MONTHNAMES" do
37
+ month_choices = month_choices(Date::ABBR_MONTHNAMES)
38
+
39
+ assert_equal ["Jan", 1], month_choices[0]
40
+ assert_equal ["Feb", 2], month_choices[1]
41
+ assert_equal ["Mar", 3], month_choices[2]
42
+ assert_equal ["Apr", 4], month_choices[3]
43
+ assert_equal ["May", 5], month_choices[4]
44
+ assert_equal ["Jun", 6], month_choices[5]
45
+ assert_equal ["Jul", 7], month_choices[6]
46
+ assert_equal ["Aug", 8], month_choices[7]
47
+ assert_equal ["Sep", 9], month_choices[8]
48
+ assert_equal ["Oct", 10], month_choices[9]
49
+ assert_equal ["Nov", 11], month_choices[10]
50
+ assert_equal ["Dec", 12], month_choices[11]
51
+ end
52
+
53
+ test "when changing settings.default_month_names" do
54
+ settings.stubs(:default_month_names).at_least_once.returns(Date::ABBR_MONTHNAMES)
55
+
56
+ assert_equal ["Jan", 1], month_choices[0]
57
+ assert_equal ["Feb", 2], month_choices[1]
58
+ assert_equal ["Nov", 11], month_choices[10]
59
+ assert_equal ["Dec", 12], month_choices[11]
60
+ end
61
+ end
62
+
63
+ describe "year choices" do
64
+ setup do
65
+ settings.stubs(:default_year_loffset).returns(-60)
66
+ settings.stubs(:default_year_uoffset).returns(0)
67
+ end
68
+
69
+ test "by default" do
70
+ assert_equal 61, year_choices.size
71
+ year_choices.each do |label, value|
72
+ assert_equal label, value
73
+ end
74
+ end
75
+
76
+ test "allows to pass in a lower upper offset" do
77
+ assert_equal 1, year_choices(0, 0).size
78
+ assert_equal [Date.today.year, Date.today.year], year_choices(0, 0).first
79
+
80
+ Date.expects(:today).at_least_once.returns(Date.new(2010, 5, 5))
81
+ assert_equal [2005, 2005], year_choices(-5).first
82
+ assert_equal [2015, 2015], year_choices(0, 5).last
83
+ end
84
+
85
+ test "allows to start with a higher lower upper offset" do
86
+ Date.expects(:today).at_least_once.returns(Date.new(2010, 5, 5))
87
+ assert_equal [2010, 2010], year_choices(0, -50).first
88
+ assert_equal [1960, 1960], year_choices(0, -50).last
89
+ end
90
+
91
+ test "allows to customize via global settings" do
92
+ settings.expects(:default_year_loffset).at_least_once.returns(0)
93
+ settings.expects(:default_year_uoffset).at_least_once.returns(6)
94
+
95
+ year = Time.now.year
96
+ expected = (year..(year+6)).to_a
97
+ expected = expected.zip(expected)
98
+
99
+ assert_equal expected, year_choices
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class DateAppTest < Test::Unit::TestCase
4
+ include Sinatra::DateForms::Helpers
5
+ include Rack::Test::Methods
6
+
7
+ class App < Sinatra::Base
8
+ register Sinatra::DateForms
9
+ get('/years') { year_choices.to_s }
10
+ get('/months') { month_choices.to_s }
11
+ end
12
+
13
+ def app
14
+ App.new
15
+ end
16
+
17
+ test "returns the expected years" do
18
+ get '/years'
19
+
20
+ settings.stubs(:default_year_loffset).returns(-60)
21
+ settings.stubs(:default_year_uoffset).returns(0)
22
+
23
+ assert_equal year_choices.to_s, last_response.body
24
+ end
25
+
26
+ test "returns the expected months" do
27
+ get '/months'
28
+
29
+ settings.stubs(:default_month_names).returns(Date::MONTHNAMES)
30
+ assert_equal month_choices.to_s, last_response.body
31
+ end
32
+ end
@@ -0,0 +1,102 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class DateFormTest < Test::Unit::TestCase
4
+ include Sinatra::DateForms::Helpers
5
+
6
+ test "day_choices returns 1 to 31" do
7
+ assert_equal 31, day_choices.size
8
+ assert_equal [1, 1], day_choices.first
9
+ assert_equal [31, 31], day_choices.last
10
+
11
+ day_choices.each do |label, value|
12
+ assert_equal label, value
13
+ end
14
+ end
15
+
16
+ describe "month choices" do
17
+ setup do
18
+ settings.stubs(:default_month_names).returns(Date::MONTHNAMES)
19
+ end
20
+
21
+ test "by default" do
22
+ assert_equal ["January", 1], month_choices[0]
23
+ assert_equal ["February", 2], month_choices[1]
24
+ assert_equal ["March", 3], month_choices[2]
25
+ assert_equal ["April", 4], month_choices[3]
26
+ assert_equal ["May", 5], month_choices[4]
27
+ assert_equal ["June", 6], month_choices[5]
28
+ assert_equal ["July", 7], month_choices[6]
29
+ assert_equal ["August", 8], month_choices[7]
30
+ assert_equal ["September", 9], month_choices[8]
31
+ assert_equal ["October", 10], month_choices[9]
32
+ assert_equal ["November", 11], month_choices[10]
33
+ assert_equal ["December", 12], month_choices[11]
34
+ end
35
+
36
+ test "given Date::ABBR_MONTHNAMES" do
37
+ month_choices = month_choices(Date::ABBR_MONTHNAMES)
38
+
39
+ assert_equal ["Jan", 1], month_choices[0]
40
+ assert_equal ["Feb", 2], month_choices[1]
41
+ assert_equal ["Mar", 3], month_choices[2]
42
+ assert_equal ["Apr", 4], month_choices[3]
43
+ assert_equal ["May", 5], month_choices[4]
44
+ assert_equal ["Jun", 6], month_choices[5]
45
+ assert_equal ["Jul", 7], month_choices[6]
46
+ assert_equal ["Aug", 8], month_choices[7]
47
+ assert_equal ["Sep", 9], month_choices[8]
48
+ assert_equal ["Oct", 10], month_choices[9]
49
+ assert_equal ["Nov", 11], month_choices[10]
50
+ assert_equal ["Dec", 12], month_choices[11]
51
+ end
52
+
53
+ test "when changing settings.default_month_names" do
54
+ settings.stubs(:default_month_names).at_least_once.returns(Date::ABBR_MONTHNAMES)
55
+
56
+ assert_equal ["Jan", 1], month_choices[0]
57
+ assert_equal ["Feb", 2], month_choices[1]
58
+ assert_equal ["Nov", 11], month_choices[10]
59
+ assert_equal ["Dec", 12], month_choices[11]
60
+ end
61
+ end
62
+
63
+ describe "year choices" do
64
+ setup do
65
+ settings.stubs(:default_year_loffset).returns(-60)
66
+ settings.stubs(:default_year_uoffset).returns(0)
67
+ end
68
+
69
+ test "by default" do
70
+ assert_equal 61, year_choices.size
71
+ year_choices.each do |label, value|
72
+ assert_equal label, value
73
+ end
74
+ end
75
+
76
+ test "allows to pass in a lower upper offset" do
77
+ assert_equal 1, year_choices(0, 0).size
78
+ assert_equal [Date.today.year, Date.today.year], year_choices(0, 0).first
79
+
80
+ Date.expects(:today).at_least_once.returns(Date.new(2010, 5, 5))
81
+ assert_equal [2005, 2005], year_choices(-5).first
82
+ assert_equal [2015, 2015], year_choices(0, 5).last
83
+ end
84
+
85
+ test "allows to start with a higher lower upper offset" do
86
+ Date.expects(:today).at_least_once.returns(Date.new(2010, 5, 5))
87
+ assert_equal [2010, 2010], year_choices(0, -50).first
88
+ assert_equal [1960, 1960], year_choices(0, -50).last
89
+ end
90
+
91
+ test "allows to customize via global settings" do
92
+ settings.expects(:default_year_loffset).at_least_once.returns(0)
93
+ settings.expects(:default_year_uoffset).at_least_once.returns(6)
94
+
95
+ year = Time.now.year
96
+ expected = (year..(year+6)).to_a
97
+ expected = expected.zip(expected)
98
+
99
+ assert_equal expected, year_choices
100
+ end
101
+ end
102
+ end
data/test/test_html.rb ADDED
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class HtmlTest < Test::Unit::TestCase
4
+ include Sinatra::HtmlHelpers
5
+
6
+ describe "select_options" do
7
+ test "displays the pairs" do
8
+ html = select_options([['One', 1], ['Two', 2]])
9
+ doc = Nokogiri(%(<body>#{html}</body>))
10
+
11
+ assert_equal 'One', doc.search('option[value="1"]').text
12
+ assert_equal 'Two', doc.search('option[value="2"]').text
13
+ end
14
+
15
+ test "marks option as selected" do
16
+ html = select_options([['One', 1], ['Two', 2]], 1)
17
+ doc = Nokogiri(%(<body>#{html}</body>))
18
+
19
+ assert_equal 'One', doc.search('option[value="1"][selected]').text
20
+ assert_equal 'Two', doc.search('option[value="2"]').text
21
+ end
22
+
23
+ test "produces a prompt properly" do
24
+ html = select_options([['One', 1], ['Two', 2]], 1, "- Choose -")
25
+ doc = Nokogiri(%(<body>#{html}</body>))
26
+
27
+ assert_equal '- Choose -', doc.search('option[value=""]').text
28
+ assert_equal 'One', doc.search('option[value="1"][selected]').text
29
+ assert_equal 'Two', doc.search('option[value="2"]').text
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class HtmlHelpersTest < Test::Unit::TestCase
4
+ include Sinatra::HtmlHelpers
5
+
6
+ describe "tag" do
7
+ test "the basic no attr case" do
8
+ assert_equal '<div>Foobar</div>', tag('div', 'Foobar')
9
+ end
10
+
11
+ test "one attr case" do
12
+ assert_equal '<div class="baz">Foobar</div>',
13
+ tag('div', 'Foobar', :class => 'baz')
14
+ end
15
+
16
+ test "many attrs case" do
17
+ assert_equal '<div class="baz bar" style="display:none">Foobar</div>',
18
+ tag('div', 'Foobar', :class => 'baz bar', :style => 'display:none')
19
+ end
20
+
21
+ test "funky attrs case" do
22
+ assert_equal '<div class="baz &#39;bar&#39;" ' +
23
+ 'style="display:&quot;none&quot;">Foobar</div>',
24
+ tag('div', 'Foobar', :class => "baz 'bar'", :style => 'display:"none"')
25
+ end
26
+ end
27
+
28
+ test "h" do
29
+ assert_equal "&lt;Foo&gt;", h("<Foo>")
30
+ assert_equal "&lt;Foo bar=&#39;baz&#39;&gt;", h("<Foo bar='baz'>")
31
+ assert_equal "&lt;Foo bar=&quot;baz&quot;&gt;", h("<Foo bar=\"baz\">")
32
+ end
33
+ end
data/test/test_i18n.rb ADDED
@@ -0,0 +1,48 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class I18nTest < Test::Unit::TestCase
4
+ include Rack::Test::Methods
5
+
6
+ class App < Sinatra::Base
7
+ use Rack::Session::Cookie
8
+ register Sinatra::I18nSupport
9
+ load_locales I18nTest::fixture_path('i18n')
10
+
11
+ get('/get') { t('article.new') }
12
+ get('/use/:locale') { |locale| session[:locale] = locale }
13
+ get('/time') { l(Time.at(141415926)) }
14
+ get('/short') { l(Time.at(141415926), format: :short) }
15
+ end
16
+
17
+ def app
18
+ App.new
19
+ end
20
+
21
+ test "#t" do
22
+ get '/get'
23
+ assert last_response.body == 'New Article'
24
+ end
25
+
26
+ test "session[:locale]" do
27
+ get '/use/tl'
28
+ get '/get'
29
+ assert last_response.body == 'Bagong Artikulo'
30
+ end
31
+
32
+ test "#l" do
33
+ get '/time'
34
+ assert_equal 'Wed, 26. Jun 1974 02:12:06 +0800', last_response.body
35
+ end
36
+
37
+ test "#l in an alternate locale" do
38
+ get '/use/tl'
39
+ get '/time'
40
+ assert_equal 'Wed, 26. Hun 1974 02:12:06 +0800', last_response.body
41
+ end
42
+
43
+
44
+ test "#l short" do
45
+ get '/short'
46
+ assert_equal '26. Jun 02:12', last_response.body
47
+ end
48
+ end
@@ -0,0 +1,62 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class NumericTest < Test::Unit::TestCase
4
+ include Sinatra::Numeric::Helpers
5
+
6
+ describe "currency" do
7
+ setup do
8
+ settings.stubs(:default_currency_unit).returns('$')
9
+ settings.stubs(:default_currency_precision).returns(2)
10
+ settings.stubs(:default_currency_separator).returns(',')
11
+ end
12
+
13
+ test "nil" do
14
+ assert_nil currency(nil)
15
+ end
16
+
17
+ test "defaults" do
18
+ assert_equal "$ 10.00", currency(10)
19
+ end
20
+
21
+ test "with custom unit, precision" do
22
+ assert_equal "&pound; 10.0", currency(10, :unit => "&pound;", :precision => 1)
23
+ end
24
+
25
+ test "with 0 precision" do
26
+ assert_equal "$ 10", currency(10, :precision => 0)
27
+ end
28
+
29
+ test "separators" do
30
+ assert_equal "$ 100.00", currency(100)
31
+ assert_equal "$ 1,000.00", currency(1_000)
32
+ assert_equal "$ 10,000.00", currency(10_000)
33
+ assert_equal "$ 100,000.00", currency(100_000)
34
+ assert_equal "$ 1,000,000.00", currency(1_000_000)
35
+ end
36
+
37
+ test "specified defaults" do
38
+ settings.stubs(:default_currency_unit).returns('P')
39
+ settings.stubs(:default_currency_precision).returns(3)
40
+ settings.stubs(:default_currency_separator).returns(' ')
41
+
42
+ assert_equal "P 100.000", currency(100)
43
+ assert_equal "P 1 000.000", currency(1_000)
44
+ assert_equal "P 10 000.000", currency(10_000)
45
+ end
46
+ end
47
+
48
+ describe "percentage" do
49
+ test "nil" do
50
+ assert_nil percentage(nil)
51
+ end
52
+
53
+ test "various percentages" do
54
+ assert_equal '0.01%', percentage(0.01)
55
+ assert_equal '0.10%', percentage(0.10)
56
+ assert_equal '1%', percentage(1.00)
57
+ assert_equal '10.01%', percentage(10.01)
58
+ assert_equal '99.99%', percentage(99.99)
59
+ assert_equal '100%', percentage(100)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class OhmErrorTest < Test::Unit::TestCase
4
+ describe "errors_on" do
5
+ include Rack::Test::Methods
6
+
7
+ class App < Sinatra::Base
8
+ FORM = (<<-FORM).gsub(/^ {6}/, '')
9
+ - errors_on @post do |e|
10
+ - e.on [:email, :not_present], "Email is required."
11
+ - e.on [:name, :not_present], "Name is required."
12
+ FORM
13
+
14
+ helpers Sinatra::OhmErrorHelpers
15
+
16
+ class Post < Ohm::Model
17
+ attribute :email
18
+ attribute :name
19
+
20
+ def validate
21
+ assert_present :email
22
+ assert_present :name
23
+ end
24
+ end
25
+
26
+ get '/form' do
27
+ @post = Post.new
28
+ @post.valid?
29
+
30
+ haml FORM
31
+ end
32
+ end
33
+
34
+ def app
35
+ App.new
36
+ end
37
+
38
+ test "produces proper errors" do
39
+ get '/form'
40
+
41
+ doc = Nokogiri(last_response.body)
42
+
43
+ assert_equal 1, doc.search('div.errors').length
44
+
45
+ assert_equal 'Email is required.',
46
+ doc.search('div.errors > ul > li:first-child').text
47
+
48
+ assert_equal 'Name is required.',
49
+ doc.search('div.errors > ul > li:last-child').text
50
+ end
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-support
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Cyril David
9
+ - Rico Sta. Cruz
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-03-27 00:00:00 +08:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: sinatra
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: "1.0"
26
+ type: :runtime
27
+ version_requirements: *id001
28
+ - !ruby/object:Gem::Dependency
29
+ name: rack-test
30
+ prerelease: false
31
+ requirement: &id002 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: "0"
37
+ type: :development
38
+ version_requirements: *id002
39
+ - !ruby/object:Gem::Dependency
40
+ name: ohm
41
+ prerelease: false
42
+ requirement: &id003 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id003
50
+ - !ruby/object:Gem::Dependency
51
+ name: haml
52
+ prerelease: false
53
+ requirement: &id004 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id004
61
+ - !ruby/object:Gem::Dependency
62
+ name: mocha
63
+ prerelease: false
64
+ requirement: &id005 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ type: :development
71
+ version_requirements: *id005
72
+ - !ruby/object:Gem::Dependency
73
+ name: nokogiri
74
+ prerelease: false
75
+ requirement: &id006 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id006
83
+ - !ruby/object:Gem::Dependency
84
+ name: contest
85
+ prerelease: false
86
+ requirement: &id007 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ type: :development
93
+ version_requirements: *id007
94
+ description: Sinatra-support includes many helpers for forms, errors and many amazing things.
95
+ email:
96
+ - cyx.ucron@gmail.com
97
+ - rico@sinefunc.com
98
+ executables: []
99
+
100
+ extensions: []
101
+
102
+ extra_rdoc_files: []
103
+
104
+ files:
105
+ - lib/sinatra/support/compat-1.8.6.rb
106
+ - lib/sinatra/support/country.rb
107
+ - lib/sinatra/support/countryhelpers.rb
108
+ - lib/sinatra/support/csssupport.rb
109
+ - lib/sinatra/support/dateforms.rb
110
+ - lib/sinatra/support/errorhelpers.rb
111
+ - lib/sinatra/support/htmlhelpers.rb
112
+ - lib/sinatra/support/i18nsupport.rb
113
+ - lib/sinatra/support/ifhelpers.rb
114
+ - lib/sinatra/support/jssupport.rb
115
+ - lib/sinatra/support/numeric.rb
116
+ - lib/sinatra/support/ohmerrorhelpers.rb
117
+ - lib/sinatra/support/version.rb
118
+ - lib/sinatra/support.rb
119
+ - test/fixtures/i18n/en.yml
120
+ - test/fixtures/i18n/tl.yml
121
+ - test/helper.rb
122
+ - test/test_country.rb
123
+ - test/test_date.rb
124
+ - test/test_date_app.rb
125
+ - test/test_date_form.rb
126
+ - test/test_html.rb
127
+ - test/test_htmlhelpers.rb
128
+ - test/test_i18n.rb
129
+ - test/test_numeric.rb
130
+ - test/test_ohmerror.rb
131
+ - HISTORY.md
132
+ - Rakefile
133
+ has_rdoc: true
134
+ homepage: http://github.com/sinefunc/sinatra-support
135
+ licenses: []
136
+
137
+ post_install_message:
138
+ rdoc_options: []
139
+
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: "0"
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: "0"
154
+ requirements: []
155
+
156
+ rubyforge_project:
157
+ rubygems_version: 1.5.0
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: A gem with many essential helpers for creating web apps with Sinatra.
161
+ test_files: []
162
+