2004-11-24 01:04:44 +00:00
require 'test/unit'
require File . dirname ( __FILE__ ) + '/../../lib/action_view/helpers/form_options_helper'
2005-02-23 15:05:04 +00:00
class MockTimeZone
2005-02-23 12:54:58 +00:00
attr_reader :name
def initialize ( name )
@name = name
end
def self . all
[ " A " , " B " , " C " , " D " , " E " ] . map { | s | new s }
end
def == ( z )
z && @name == z . name
end
def to_s
@name
end
end
2005-02-23 15:05:04 +00:00
ActionView :: Helpers :: FormOptionsHelper :: TimeZone = MockTimeZone
2004-11-24 01:04:44 +00:00
class FormOptionsHelperTest < Test :: Unit :: TestCase
include ActionView :: Helpers :: FormOptionsHelper
old_verbose , $VERBOSE = $VERBOSE , nil
Post = Struct . new ( 'Post' , :title , :author_name , :body , :secret , :written_on , :category , :origin )
Continent = Struct . new ( 'Continent' , :continent_name , :countries )
Country = Struct . new ( 'Country' , :country_id , :country_name )
2005-02-23 12:54:58 +00:00
Firm = Struct . new ( 'Firm' , :time_zone )
2004-11-24 01:04:44 +00:00
$VERBOSE = old_verbose
def test_collection_options
@posts = [
Post . new ( " <Abe> went home " , " <Abe> " , " To a little house " , " shh! " ) ,
Post . new ( " Babe went home " , " Babe " , " To a little house " , " shh! " ) ,
Post . new ( " Cabe went home " , " Cabe " , " To a little house " , " shh! " )
]
assert_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" >Babe went home</option> \n <option value= \" Cabe \" >Cabe went home</option> " ,
options_from_collection_for_select ( @posts , " author_name " , " title " )
)
end
2005-03-06 11:50:41 +00:00
2004-11-24 01:04:44 +00:00
def test_collection_options_with_preselected_value
@posts = [
Post . new ( " <Abe> went home " , " <Abe> " , " To a little house " , " shh! " ) ,
Post . new ( " Babe went home " , " Babe " , " To a little house " , " shh! " ) ,
Post . new ( " Cabe went home " , " Cabe " , " To a little house " , " shh! " )
]
assert_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" selected= \" selected \" >Babe went home</option> \n <option value= \" Cabe \" >Cabe went home</option> " ,
options_from_collection_for_select ( @posts , " author_name " , " title " , " Babe " )
)
end
def test_collection_options_with_preselected_value_array
@posts = [
Post . new ( " <Abe> went home " , " <Abe> " , " To a little house " , " shh! " ) ,
Post . new ( " Babe went home " , " Babe " , " To a little house " , " shh! " ) ,
Post . new ( " Cabe went home " , " Cabe " , " To a little house " , " shh! " )
]
assert_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" selected= \" selected \" >Babe went home</option> \n <option value= \" Cabe \" selected= \" selected \" >Cabe went home</option> " ,
options_from_collection_for_select ( @posts , " author_name " , " title " , [ " Babe " , " Cabe " ] )
)
end
def test_array_options_for_select
assert_equal (
2005-03-06 11:50:41 +00:00
" <option><Denmark></option> \n <option>USA</option> \n <option>Sweden</option> " ,
2004-11-24 01:04:44 +00:00
options_for_select ( [ " <Denmark> " , " USA " , " Sweden " ] )
)
end
def test_array_options_for_select_with_selection
assert_equal (
2005-03-06 11:50:41 +00:00
" <option>Denmark</option> \n <option selected= \" selected \" ><USA></option> \n <option>Sweden</option> " ,
2004-11-24 01:04:44 +00:00
options_for_select ( [ " Denmark " , " <USA> " , " Sweden " ] , " <USA> " )
)
end
def test_array_options_for_select_with_selection_array
assert_equal (
" <option>Denmark</option> \n <option selected= \" selected \" ><USA></option> \n <option selected= \" selected \" >Sweden</option> " ,
options_for_select ( [ " Denmark " , " <USA> " , " Sweden " ] , [ " <USA> " , " Sweden " ] )
)
end
def test_hash_options_for_select
assert_equal (
2005-03-06 11:50:41 +00:00
" <option value= \" <Kroner> \" ><DKR></option> \n <option value= \" Dollar \" >$</option> " ,
2004-11-24 01:04:44 +00:00
options_for_select ( { " $ " = > " Dollar " , " <DKR> " = > " <Kroner> " } )
)
end
def test_hash_options_for_select_with_selection
assert_equal (
2005-03-06 11:50:41 +00:00
" <option value= \" <Kroner> \" ><DKR></option> \n <option value= \" Dollar \" selected= \" selected \" >$</option> " ,
2004-11-24 01:04:44 +00:00
options_for_select ( { " $ " = > " Dollar " , " <DKR> " = > " <Kroner> " } , " Dollar " )
)
end
def test_hash_options_for_select_with_selection
assert_equal (
2005-03-06 11:50:41 +00:00
" <option value= \" <Kroner> \" selected= \" selected \" ><DKR></option> \n <option value= \" Dollar \" selected= \" selected \" >$</option> " ,
2004-11-24 01:04:44 +00:00
options_for_select ( { " $ " = > " Dollar " , " <DKR> " = > " <Kroner> " } , [ " Dollar " , " <Kroner> " ] )
)
end
def test_html_option_groups_from_collection
@continents = [
Continent . new ( " <Africa> " , [ Country . new ( " <sa> " , " <South Africa> " ) , Country . new ( " so " , " Somalia " ) ] ) ,
Continent . new ( " Europe " , [ Country . new ( " dk " , " Denmark " ) , Country . new ( " ie " , " Ireland " ) ] )
]
assert_equal (
" <optgroup label= \" <Africa> \" ><option value= \" <sa> \" ><South Africa></option> \n <option value= \" so \" >Somalia</option></optgroup><optgroup label= \" Europe \" ><option value= \" dk \" selected= \" selected \" >Denmark</option> \n <option value= \" ie \" >Ireland</option></optgroup> " ,
option_groups_from_collection_for_select ( @continents , " countries " , " continent_name " , " country_id " , " country_name " , " dk " )
)
end
2005-02-23 12:54:58 +00:00
def test_time_zone_options_no_parms
opts = time_zone_options_for_select
2005-02-24 11:50:59 +00:00
assert_equal " <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" >D</option> \n " +
" <option value= \" E \" >E</option> " ,
2005-02-23 12:54:58 +00:00
opts
end
def test_time_zone_options_with_selected
2005-02-24 11:50:59 +00:00
opts = time_zone_options_for_select ( " D " )
assert_equal " <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " ,
2005-02-23 12:54:58 +00:00
opts
end
def test_time_zone_options_with_unknown_selected
2005-02-24 11:50:59 +00:00
opts = time_zone_options_for_select ( " K " )
assert_equal " <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" >D</option> \n " +
" <option value= \" E \" >E</option> " ,
2005-02-23 12:54:58 +00:00
opts
end
def test_time_zone_options_with_priority_zones
zones = [ TimeZone . new ( " B " ) , TimeZone . new ( " E " ) ]
opts = time_zone_options_for_select ( nil , zones )
2005-02-24 11:50:59 +00:00
assert_equal " <option value= \" B \" >B</option> \n " +
" <option value= \" E \" >E</option> " +
2005-02-23 12:54:58 +00:00
" <option>-------------</option> \n " +
2005-02-24 11:50:59 +00:00
" <option value= \" A \" >A</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" >D</option> " ,
2005-02-23 12:54:58 +00:00
opts
end
def test_time_zone_options_with_selected_priority_zones
zones = [ TimeZone . new ( " B " ) , TimeZone . new ( " E " ) ]
2005-02-24 11:50:59 +00:00
opts = time_zone_options_for_select ( " E " , zones )
assert_equal " <option value= \" B \" >B</option> \n " +
" <option value= \" E \" selected= \" selected \" >E</option> " +
2005-02-23 12:54:58 +00:00
" <option>-------------</option> \n " +
2005-02-24 11:50:59 +00:00
" <option value= \" A \" >A</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" >D</option> " ,
2005-02-23 12:54:58 +00:00
opts
end
def test_time_zone_options_with_unselected_priority_zones
zones = [ TimeZone . new ( " B " ) , TimeZone . new ( " E " ) ]
2005-02-24 11:50:59 +00:00
opts = time_zone_options_for_select ( " C " , zones )
assert_equal " <option value= \" B \" >B</option> \n " +
" <option value= \" E \" >E</option> " +
2005-02-23 12:54:58 +00:00
" <option>-------------</option> \n " +
2005-02-24 11:50:59 +00:00
" <option value= \" A \" >A</option> \n " +
" <option value= \" C \" selected= \" selected \" >C</option> \n " +
" <option value= \" D \" >D</option> " ,
2005-02-23 12:54:58 +00:00
opts
end
2004-11-24 01:04:44 +00:00
def test_select
@post = Post . new
@post . category = " <mus> "
assert_equal (
2005-03-06 11:50:41 +00:00
" <select id= \" post_category \" name= \" post[category] \" ><option>abe</option> \n <option selected= \" selected \" ><mus></option> \n <option>hest</option></select> " ,
2004-11-24 01:04:44 +00:00
select ( " post " , " category " , %w( abe <mus> hest ) )
)
end
def test_select_with_blank
@post = Post . new
@post . category = " <mus> "
assert_equal (
2005-03-06 11:50:41 +00:00
" <select id= \" post_category \" name= \" post[category] \" ><option></option> \n <option>abe</option> \n <option selected= \" selected \" ><mus></option> \n <option>hest</option></select> " ,
2004-11-24 01:04:44 +00:00
select ( " post " , " category " , %w( abe <mus> hest ) , :include_blank = > true )
)
end
def test_collection_select
@posts = [
Post . new ( " <Abe> went home " , " <Abe> " , " To a little house " , " shh! " ) ,
Post . new ( " Babe went home " , " Babe " , " To a little house " , " shh! " ) ,
Post . new ( " Cabe went home " , " Cabe " , " To a little house " , " shh! " )
]
@post = Post . new
@post . author_name = " Babe "
assert_equal (
2005-03-06 11:50:41 +00:00
" <select id= \" post_author_name \" name= \" post[author_name] \" ><option value= \" <Abe> \" ><Abe></option> \n <option value= \" Babe \" selected= \" selected \" >Babe</option> \n <option value= \" Cabe \" >Cabe</option></select> " ,
2004-11-24 01:04:44 +00:00
collection_select ( " post " , " author_name " , @posts , " author_name " , " author_name " )
)
end
def test_collection_select_with_blank_and_style
@posts = [
Post . new ( " <Abe> went home " , " <Abe> " , " To a little house " , " shh! " ) ,
Post . new ( " Babe went home " , " Babe " , " To a little house " , " shh! " ) ,
Post . new ( " Cabe went home " , " Cabe " , " To a little house " , " shh! " )
]
@post = Post . new
@post . author_name = " Babe "
assert_equal (
2005-03-06 11:50:41 +00:00
" <select id= \" post_author_name \" name= \" post[author_name] \" style= \" width: 200px \" ><option></option> \n <option value= \" <Abe> \" ><Abe></option> \n <option value= \" Babe \" selected= \" selected \" >Babe</option> \n <option value= \" Cabe \" >Cabe</option></select> " ,
2004-11-24 01:04:44 +00:00
collection_select ( " post " , " author_name " , @posts , " author_name " , " author_name " , { :include_blank = > true } , " style " = > " width: 200px " )
)
end
def test_country_select
@post = Post . new
@post . origin = " Denmark "
assert_equal (
2005-02-19 23:21:08 +00:00
" <select id= \" post_origin \" name= \" post[origin] \" ><option>Afghanistan</option> \n <option>Albania</option> \n <option>Algeria</option> \n <option>American Samoa</option> \n <option>Andorra</option> \n <option>Angola</option> \n <option>Anguilla</option> \n <option>Antarctica</option> \n <option>Antigua And Barbuda</option> \n <option>Argentina</option> \n <option>Armenia</option> \n <option>Aruba</option> \n <option>Australia</option> \n <option>Austria</option> \n <option>Azerbaijan</option> \n <option>Bahamas</option> \n <option>Bahrain</option> \n <option>Bangladesh</option> \n <option>Barbados</option> \n <option>Belarus</option> \n <option>Belgium</option> \n <option>Belize</option> \n <option>Benin</option> \n <option>Bermuda</option> \n <option>Bhutan</option> \n <option>Bolivia</option> \n <option>Bosnia and Herzegowina</option> \n <option>Botswana</option> \n <option>Bouvet Island</option> \n <option>Brazil</option> \n <option>British Indian Ocean Territory</option> \n <option>Brunei Darussalam</option> \n <option>Bulgaria</option> \n <option>Burkina Faso</option> \n <option>Burma</option> \n <option>Burundi</option> \n <option>Cambodia</option> \n <option>Cameroon</option> \n <option>Canada</option> \n <option>Cape Verde</option> \n <option>Cayman Islands</option> \n <option>Central African Republic</option> \n <option>Chad</option> \n <option>Chile</option> \n <option>China</option> \n <option>Christmas Island</option> \n <option>Cocos (Keeling) Islands</option> \n <option>Colombia</option> \n <option>Comoros</option> \n <option>Congo</option> \n <option>Congo, the Democratic Republic of the</option> \n <option>Cook Islands</option> \n <option>Costa Rica</option> \n <option>Cote d'Ivoire</option> \n <option>Croatia</option> \n <option>Cyprus</option> \n <option>Czech Republic</option> \n <option selected= \" selected \" >Denmark</option> \n <option>Djibouti</option> \n <option>Dominica</option> \n <option>Dominican Republic</option> \n <option>East Timor</option> \n <option>Ecuador</option> \n <option>Egypt</option> \n <option>El Salvador</option> \n <option>England</option> \n <option>Equatorial Guinea</option> \n <option>Eritrea</option> \n <option>Espana</option> \n <option>Estonia</option> \n <option>Ethiopia</option> \n <option>Falkland Islands</option> \n <option>Faroe Islands</option> \n <option>Fiji</option> \n <option>Finland</option> \n <option>France</option> \n <option>French Guiana</option> \n <option>French Polynesia</option> \n <option>French Southern Territories</option> \n <option>Gabon</option> \n <option>Gambia</option> \n <option>Georgia</option> \n <option>Germany</option> \n <option>Ghana</option> \n <option>Gibraltar</option> \n <option>Great Britain</option> \n <option>Greece</option> \n <option>Greenland</option> \n <option>Grenada</option> \n <option>Guadeloupe</option> \n <option>Guam</option> \n <option>Guatemala</option> \n <option>Guinea</option> \n <option>Guinea-Bissau</option> \n <option>Guyana</option> \n <option>Haiti</option> \n <option>Heard and Mc Donald Islands</option> \n <option>Honduras</option> \n <option>Hong Kong</option> \n <option>Hungary</option> \n <option>Iceland</option> \n <option>India</option> \n <option>Indonesia</option> \n <option>Ireland</option> \n <option>Israel</option> \n <option>Italy</option> \n <option>Iran</option> \n <option>Irak</option> \n <option>Jamaica</option> \n <option>Japan</option> \n <option>Jordan</option> \n <option>Kazakhstan</option> \n <option>Kenya</option> \n <option>Kiribati</option> \n <option>Korea, Republic of</option> \n <option>Korea (South)</option> \n <option>Kuwait</option> \n <option>Kyrgyzstan</option> \n <option>Lao People's Democratic Republic</option> \n <option>Latvia</option> \n <option>Lebanon</option> \n <option>Lesotho</option> \n <option>Liberia</option> \n <option>Liechtenstein</option> \n <option>Lithuania</option> \n <option>Luxembourg</option> \n <option>Macau</option> \n <option>Macedonia</option> \n <option>Madagascar</option> \n <option>Malawi</option> \n <option>Malaysia</option> \n <option>Maldives</option> \n <option>Mali</option> \n <option>Malta</option> \n <option>Marshall Islands</option> \n <option>Martinique</option> \n <option>Mauritania</option> \n <option>Mauritius</option> \n <option>Mayotte</option> \n <option>Mexico</option> \n <option>Micronesia, Federated
2004-11-24 01:04:44 +00:00
country_select ( " post " , " origin " )
)
end
2005-02-23 12:54:58 +00:00
def test_time_zone_select
2005-02-24 11:50:59 +00:00
@firm = Firm . new ( " D " )
2005-02-23 12:54:58 +00:00
html = time_zone_select ( " firm " , " time_zone " )
assert_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
2005-02-24 11:50:59 +00:00
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
2005-02-23 12:54:58 +00:00
" </select> " ,
html
end
def test_time_zone_select_with_blank
2005-02-24 11:50:59 +00:00
@firm = Firm . new ( " D " )
2005-02-23 12:54:58 +00:00
html = time_zone_select ( " firm " , " time_zone " , nil , :include_blank = > true )
assert_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
" <option></option> \n " +
2005-02-24 11:50:59 +00:00
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
2005-02-23 12:54:58 +00:00
" </select> " ,
html
end
def test_time_zone_select_with_style
2005-02-24 11:50:59 +00:00
@firm = Firm . new ( " D " )
2005-02-23 12:54:58 +00:00
html = time_zone_select ( " firm " , " time_zone " , nil , { } ,
" style " = > " color: red " )
assert_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" style= \" color: red \" > " +
2005-02-24 11:50:59 +00:00
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
2005-02-23 12:54:58 +00:00
" </select> " ,
html
2005-03-06 11:50:41 +00:00
assert_equal html , time_zone_select ( " firm " , " time_zone " , nil , { } ,
:style = > " color: red " )
2005-02-23 12:54:58 +00:00
end
def test_time_zone_select_with_blank_and_style
2005-02-24 11:50:59 +00:00
@firm = Firm . new ( " D " )
2005-02-23 12:54:58 +00:00
html = time_zone_select ( " firm " , " time_zone " , nil ,
{ :include_blank = > true } , " style " = > " color: red " )
assert_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" style= \" color: red \" > " +
" <option></option> \n " +
2005-02-24 11:50:59 +00:00
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
2005-02-23 12:54:58 +00:00
" </select> " ,
html
2005-03-06 11:50:41 +00:00
assert_equal html , time_zone_select ( " firm " , " time_zone " , nil ,
{ :include_blank = > true } , :style = > " color: red " )
2005-02-23 12:54:58 +00:00
end
def test_time_zone_select_with_priority_zones
2005-02-24 11:50:59 +00:00
@firm = Firm . new ( " D " )
2005-02-23 12:54:58 +00:00
zones = [ TimeZone . new ( " A " ) , TimeZone . new ( " D " ) ]
html = time_zone_select ( " firm " , " time_zone " , zones )
assert_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
2005-02-24 11:50:59 +00:00
" <option value= \" A \" >A</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> " +
2005-02-23 12:54:58 +00:00
" <option>-------------</option> \n " +
2005-02-24 11:50:59 +00:00
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" E \" >E</option> " +
2005-02-23 12:54:58 +00:00
" </select> " ,
html
end
2004-11-24 01:04:44 +00:00
end