factoryform 0.1.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.
- data/History.txt +20 -0
- data/README.rdoc +2 -0
- data/Rakefile +41 -0
- data/VERSION +1 -0
- data/factoryform.gemspec +144 -0
- data/init.rb +2 -0
- data/lib/factoryform.rb +25 -0
- data/lib/factoryform/boolean.rb +17 -0
- data/lib/factoryform/currency.rb +7 -0
- data/lib/factoryform/date_answer.rb +10 -0
- data/lib/factoryform/descriptive_content.rb +17 -0
- data/lib/factoryform/email.rb +11 -0
- data/lib/factoryform/field.rb +29 -0
- data/lib/factoryform/form.rb +127 -0
- data/lib/factoryform/image_data.rb +13 -0
- data/lib/factoryform/long_answer.rb +12 -0
- data/lib/factoryform/multiple_choice.rb +39 -0
- data/lib/factoryform/number.rb +11 -0
- data/lib/factoryform/rating.rb +23 -0
- data/lib/factoryform/short_answer.rb +11 -0
- data/lib/factoryform/text_data.rb +11 -0
- data/lib/factoryform/time_answer.rb +11 -0
- data/lib/factoryform/url.rb +10 -0
- data/lib/factoryform/utils.rb +13 -0
- data/rdoc/classes/Array.html +171 -0
- data/rdoc/classes/FactoryForm.html +192 -0
- data/rdoc/classes/FactoryForm/Boolean.html +166 -0
- data/rdoc/classes/FactoryForm/Currency.html +113 -0
- data/rdoc/classes/FactoryForm/DateAnswer.html +149 -0
- data/rdoc/classes/FactoryForm/DescriptiveContent.html +173 -0
- data/rdoc/classes/FactoryForm/DuplicateIDException.html +117 -0
- data/rdoc/classes/FactoryForm/Email.html +150 -0
- data/rdoc/classes/FactoryForm/Field.html +205 -0
- data/rdoc/classes/FactoryForm/Form.html +477 -0
- data/rdoc/classes/FactoryForm/ImageData.html +178 -0
- data/rdoc/classes/FactoryForm/LongAnswer.html +162 -0
- data/rdoc/classes/FactoryForm/MultipleChoice.html +241 -0
- data/rdoc/classes/FactoryForm/Number.html +149 -0
- data/rdoc/classes/FactoryForm/ParameterExpectedException.html +118 -0
- data/rdoc/classes/FactoryForm/Rating.html +188 -0
- data/rdoc/classes/FactoryForm/ShortAnswer.html +150 -0
- data/rdoc/classes/FactoryForm/TextData.html +173 -0
- data/rdoc/classes/FactoryForm/TimeAnswer.html +149 -0
- data/rdoc/classes/FactoryForm/Url.html +149 -0
- data/rdoc/created.rid +1 -0
- data/rdoc/files/README_rdoc.html +108 -0
- data/rdoc/files/lib/factoryform/boolean_rb.html +101 -0
- data/rdoc/files/lib/factoryform/currency_rb.html +101 -0
- data/rdoc/files/lib/factoryform/date_answer_rb.html +101 -0
- data/rdoc/files/lib/factoryform/descriptive_content_rb.html +101 -0
- data/rdoc/files/lib/factoryform/email_rb.html +101 -0
- data/rdoc/files/lib/factoryform/field_rb.html +101 -0
- data/rdoc/files/lib/factoryform/form_rb.html +111 -0
- data/rdoc/files/lib/factoryform/image_data_rb.html +101 -0
- data/rdoc/files/lib/factoryform/long_answer_rb.html +101 -0
- data/rdoc/files/lib/factoryform/multiple_choice_rb.html +101 -0
- data/rdoc/files/lib/factoryform/number_rb.html +101 -0
- data/rdoc/files/lib/factoryform/rating_rb.html +101 -0
- data/rdoc/files/lib/factoryform/short_answer_rb.html +101 -0
- data/rdoc/files/lib/factoryform/text_data_rb.html +101 -0
- data/rdoc/files/lib/factoryform/time_answer_rb.html +101 -0
- data/rdoc/files/lib/factoryform/url_rb.html +101 -0
- data/rdoc/files/lib/factoryform/utils_rb.html +101 -0
- data/rdoc/files/lib/factoryform_rb.html +101 -0
- data/rdoc/fr_class_index.html +46 -0
- data/rdoc/fr_file_index.html +45 -0
- data/rdoc/fr_method_index.html +55 -0
- data/rdoc/index.html +26 -0
- data/rdoc/rdoc-style.css +208 -0
- data/test/boolean_test.rb +10 -0
- data/test/currency_test.rb +9 -0
- data/test/date_answer_test.rb +10 -0
- data/test/descriptive_content_test.rb +14 -0
- data/test/field_test.rb +19 -0
- data/test/form_test.rb +166 -0
- data/test/image_data_test.rb +18 -0
- data/test/long_answer_test.rb +54 -0
- data/test/multiple_choice_test.rb +61 -0
- data/test/number_test.rb +11 -0
- data/test/rating_test.rb +21 -0
- data/test/short_answer_test.rb +83 -0
- data/test/test_helper.rb +2 -0
- data/test/text_data_test.rb +17 -0
- data/test/time_answer_test.rb +10 -0
- data/test/url_test.rb +11 -0
- metadata +197 -0
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class BooleanTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_should_create_boolean_answer_field
|
6
|
+
bool = FactoryForm::Boolean.new(:id => "fav-color", :label => "Choose your favourite color")
|
7
|
+
assert_equal [true, false], bool.values
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DateAnswerTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_should_create_date_answer_field
|
6
|
+
date = FactoryForm::DateAnswer.new(:id => "date_1", :label => "Give date of establishment")
|
7
|
+
assert_equal "date", date.validation_format
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DescriptiveContentTest < Test::Unit::TestCase
|
4
|
+
def test_should_create_descriptive_content_field_with_minimum_params_passed
|
5
|
+
dc = FactoryForm::DescriptiveContent.new(:id => "field_3", :label => "Personal email address")
|
6
|
+
assert_equal "Personal email address",dc.label
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_should_raise_exception_unless_required_params_passed
|
11
|
+
# Raise expected argument exception as ID,label are required
|
12
|
+
assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::DescriptiveContent.new() }
|
13
|
+
end
|
14
|
+
end
|
data/test/field_test.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FieldTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_should_check_of_having_validation
|
6
|
+
sa1 = FactoryForm::ShortAnswer.new(:id => "email_1", :label => "Personal email address")
|
7
|
+
la1 = FactoryForm::LongAnswer.new(:id => "comments", :label => "Provide your comments below")
|
8
|
+
|
9
|
+
# By default, no validation for different formats unless explicitly specified
|
10
|
+
assert_equal "general",sa1.validation_format
|
11
|
+
|
12
|
+
# By default, answer for this field is optional unless explicitly specified as required
|
13
|
+
assert_equal true,sa1.required
|
14
|
+
|
15
|
+
# By default, no uniqueness of answer for this field unless explicitly specified as unique
|
16
|
+
assert_equal false,sa1.unique
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/test/form_test.rb
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FormTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_should_create_form
|
6
|
+
# Create short answer and long answer objects
|
7
|
+
sa = FactoryForm::ShortAnswer.new(:id => "email_1",:label => "Personal email address" )
|
8
|
+
la = FactoryForm::LongAnswer.new(:id => "comment_1",:label => "Provide your comments below")
|
9
|
+
form = FactoryForm::Form.new()
|
10
|
+
|
11
|
+
# Add short answer and long answer objects
|
12
|
+
form.add(sa)
|
13
|
+
form.add(la)
|
14
|
+
|
15
|
+
assert_equal 2,form.fields.length
|
16
|
+
|
17
|
+
# Create form object with title
|
18
|
+
form2 = FactoryForm::Form.new({:title => "Data collection"})
|
19
|
+
assert_equal "Data collection", form2.title
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_should_list_all_the_fields
|
24
|
+
sa = FactoryForm::ShortAnswer.new(:id => "url_1",:label => "Personal web address",
|
25
|
+
:options => {
|
26
|
+
:hint => "It should be his/her personal web address",
|
27
|
+
:validation_format => "url",
|
28
|
+
:required => true,
|
29
|
+
:unique => false
|
30
|
+
}
|
31
|
+
)
|
32
|
+
|
33
|
+
la = FactoryForm::LongAnswer.new(:id => "comment_1",:label => "Provide your comments below",
|
34
|
+
:options => {
|
35
|
+
:hint => "Up to a maximum of 250 characters",
|
36
|
+
:required => false,
|
37
|
+
:unique => true,
|
38
|
+
:min_chars => 50,
|
39
|
+
:rows => 6,
|
40
|
+
:cols => 5
|
41
|
+
}
|
42
|
+
)
|
43
|
+
form = FactoryForm::Form.new()
|
44
|
+
form.add(sa)
|
45
|
+
form.add(la)
|
46
|
+
|
47
|
+
# Get the list of all fields in the form
|
48
|
+
list = form.fields
|
49
|
+
|
50
|
+
assert_equal true,list.first.required
|
51
|
+
assert_equal "url", list.first.validation_format
|
52
|
+
|
53
|
+
assert_equal true,list.last.required
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_should_get_field
|
58
|
+
form = create_form
|
59
|
+
assert_equal 3, form.fields.length
|
60
|
+
|
61
|
+
# Get email field
|
62
|
+
field_to_delete = form.get(:id => "email_1")
|
63
|
+
assert_instance_of FactoryForm::ShortAnswer,field_to_delete
|
64
|
+
|
65
|
+
# Delete email field
|
66
|
+
form.remove(field_to_delete)
|
67
|
+
|
68
|
+
assert_equal 2, form.fields.length
|
69
|
+
assert_instance_of FactoryForm::LongAnswer, form.fields.first
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_should_re_order_fields
|
74
|
+
form = create_form
|
75
|
+
field1 = form.get(:id => "email_1")
|
76
|
+
field2 = form.get(:id => "comment_1")
|
77
|
+
field3 = form.get(:id => "name_1")
|
78
|
+
|
79
|
+
assert_equal 0,form.fields.index(field1)
|
80
|
+
assert_equal 1,form.fields.index(field2)
|
81
|
+
|
82
|
+
# Move email field down
|
83
|
+
form.move_down(field1)
|
84
|
+
|
85
|
+
assert_equal 1,form.fields.index(field1)
|
86
|
+
assert_equal 0,form.fields.index(field2)
|
87
|
+
|
88
|
+
# Move comment field up
|
89
|
+
form.move_up(field3)
|
90
|
+
|
91
|
+
assert_equal 0,form.fields.index(field2)
|
92
|
+
assert_equal 1,form.fields.index(field3)
|
93
|
+
assert_equal 2,form.fields.index(field1)
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_should_change_to_yml
|
98
|
+
form = create_form
|
99
|
+
changed_yml = form.to_yml
|
100
|
+
# TODO: FIND BETTER WAY FOR TESTING .yml CONTENT-TYPE
|
101
|
+
assert_kind_of(String, changed_yml)
|
102
|
+
|
103
|
+
# Create short answer and long answer objects
|
104
|
+
sa = FactoryForm::ShortAnswer.new(:id => "email_1",:label => "Personal email address" )
|
105
|
+
la = FactoryForm::LongAnswer.new(:id => "comment_1",:label => "Provide your comments below")
|
106
|
+
td = FactoryForm::TextData.new(:id => "name_1", :label => "Person's name")
|
107
|
+
img = FactoryForm::ImageData.new(:id => "photo_1", :label => "Photo of person")
|
108
|
+
form = FactoryForm::Form.new(:title => "Survey form")
|
109
|
+
|
110
|
+
# Add short answer and long answer objects
|
111
|
+
form.add(sa)
|
112
|
+
form.add(la)
|
113
|
+
form.add(td)
|
114
|
+
form.add(img)
|
115
|
+
assert_instance_of FactoryForm::ImageData, form.fields.last
|
116
|
+
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_should_change_position_in_yml_string
|
121
|
+
form = create_form
|
122
|
+
field1 = form.get(:id => "email_1")
|
123
|
+
field2 = form.get(:id => "comment_1")
|
124
|
+
changed_yml = form.to_yml
|
125
|
+
|
126
|
+
# Convert to hash for making sure that yaml string is also changed
|
127
|
+
before_hash = YAML::load(changed_yml)
|
128
|
+
assert_instance_of FactoryForm::ShortAnswer, before_hash.fields.first
|
129
|
+
|
130
|
+
# Move email field down
|
131
|
+
form.move_down(field1)
|
132
|
+
after_hash = YAML::load(form.to_yml)
|
133
|
+
assert_instance_of FactoryForm::LongAnswer, after_hash.fields.first
|
134
|
+
|
135
|
+
form.add(FactoryForm::TextData.new(:id => "my-name", :label => "my name"))
|
136
|
+
assert_instance_of FactoryForm::TextData, after_hash.fields.last
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_should_change_to_xml
|
141
|
+
form = create_form
|
142
|
+
changed_xml = form.to_xml
|
143
|
+
# TODO: FIND BETTER WAY FOR TESTING .xml CONTENT-TYPE
|
144
|
+
assert_kind_of(String, changed_xml)
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_should_change_to_json
|
148
|
+
form = create_form
|
149
|
+
changed_json = form.to_json
|
150
|
+
|
151
|
+
# TODO: FIND BETTER WAY FOR TESTING json CONTENT-TYPE
|
152
|
+
assert_kind_of(String, changed_json)
|
153
|
+
end
|
154
|
+
|
155
|
+
private
|
156
|
+
def create_form
|
157
|
+
sa = FactoryForm::ShortAnswer.new(:id => "email_1", :label => "Personal email address" )
|
158
|
+
la = FactoryForm::LongAnswer.new(:id => "comment_1", :label => "Provide your comments below")
|
159
|
+
td = FactoryForm::TextData.new(:id => "name_1", :label => "Person's name")
|
160
|
+
form = FactoryForm::Form.new()
|
161
|
+
form.add(sa)
|
162
|
+
form.add(la)
|
163
|
+
form.add(td)
|
164
|
+
return form
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ImageDataTest < Test::Unit::TestCase
|
4
|
+
def test_should_create_image_data_field_with_minimum_params_passed
|
5
|
+
td = FactoryForm::ImageData.new(:id => "photo_1", :label => "Photo of child")
|
6
|
+
assert_equal "Photo of child",td.label
|
7
|
+
|
8
|
+
# By default alt attribute is same as field id
|
9
|
+
assert_equal "Photo of child",td.alt
|
10
|
+
assert_equal "image_data",td.field_type
|
11
|
+
assert_equal true,td.required
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_should_raise_exception_unless_required_params_passed
|
15
|
+
# Raise expected argument exception as ID,label are required
|
16
|
+
assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::ImageData.new() }
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class LongAnswerTest < Test::Unit::TestCase
|
4
|
+
def test_should_create_long_answer_field
|
5
|
+
la = FactoryForm::LongAnswer.new(:id => "comments", :label => "Provide your comments below")
|
6
|
+
|
7
|
+
# Long answer text should not have any validation format
|
8
|
+
assert_equal "general", la.validation_format
|
9
|
+
|
10
|
+
la.hint = "Up to a maximum of 250 characters"
|
11
|
+
assert_equal "Up to a maximum of 250 characters", la.hint
|
12
|
+
|
13
|
+
# By default, answer for this field is optional unless explicitly specified as required
|
14
|
+
assert_equal true,la.required
|
15
|
+
|
16
|
+
# By default, no uniqueness of answer for this field unless explicitly specified as unique
|
17
|
+
assert_equal false,la.unique
|
18
|
+
|
19
|
+
# Set answer for this field as required
|
20
|
+
la.required = true
|
21
|
+
|
22
|
+
# Set answer for this field to be unique
|
23
|
+
la.unique = true
|
24
|
+
|
25
|
+
# Should have required attribute to be true
|
26
|
+
assert_equal true, la.required
|
27
|
+
|
28
|
+
# Should have unique answers
|
29
|
+
assert_equal true, la.unique
|
30
|
+
|
31
|
+
# Should have 500 min_chars
|
32
|
+
assert_equal 500, la.min_chars
|
33
|
+
|
34
|
+
# Pass min_chars
|
35
|
+
la2 = FactoryForm::LongAnswer.new(:id => "comments", :label => "Provide your comments below", :options => {:min_chars => 700 })
|
36
|
+
assert_equal 700, la2.min_chars
|
37
|
+
|
38
|
+
# Field type
|
39
|
+
assert_equal "long_answer", la.field_type
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_should_not_create_long_answer_field_if_ID_label_are_missing
|
44
|
+
# Raise expected argument exception as ID,label are required
|
45
|
+
assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::LongAnswer.new() }
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_should_set_minimum_number_of_characters
|
49
|
+
la = FactoryForm::LongAnswer.new(:id => "comments", :label => "Provide your comments below")
|
50
|
+
la.min_chars = 500
|
51
|
+
assert_equal 500,la.min_chars
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class MultipleChoiceTest < Test::Unit::TestCase
|
4
|
+
def test_should_create_multiple_choice_answer_field
|
5
|
+
|
6
|
+
# Multiple choice should have value attributes passed as well
|
7
|
+
assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color") }
|
8
|
+
|
9
|
+
mc = FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color",
|
10
|
+
:values => ["Red color","Green color"]
|
11
|
+
)
|
12
|
+
|
13
|
+
# There should be at least two values for this field
|
14
|
+
assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color") }
|
15
|
+
assert_equal 2, mc.values.length
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_should_add_remove_new_option
|
20
|
+
mc = FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color",
|
21
|
+
:values => ["Red color","Green color"]
|
22
|
+
)
|
23
|
+
assert_equal "Green color", mc.values.last
|
24
|
+
assert_equal 2, mc.values.length
|
25
|
+
|
26
|
+
# Add other option values
|
27
|
+
mc.add_option("Blue Color")
|
28
|
+
assert_equal "Blue Color", mc.values.last
|
29
|
+
assert_equal 3, mc.values.length
|
30
|
+
|
31
|
+
mc.remove_option("Blue Color")
|
32
|
+
assert_equal 2, mc.values.length
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_should_support_different_field_types
|
37
|
+
mc = FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color",
|
38
|
+
:values => ["Red color","Green color"]
|
39
|
+
)
|
40
|
+
assert_equal "radio_button", mc.field_type
|
41
|
+
|
42
|
+
mc = FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color",
|
43
|
+
:values => ["Red color","Green color"],
|
44
|
+
:field_type => "check_box"
|
45
|
+
)
|
46
|
+
assert_equal "check_box", mc.field_type
|
47
|
+
|
48
|
+
# Should not allow field types other than "select_box", "check_box", "radio_button"
|
49
|
+
assert_raise(ArgumentError){ FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color",
|
50
|
+
:values => ["Red color","Green color"],
|
51
|
+
:field_type => "list"
|
52
|
+
)
|
53
|
+
}
|
54
|
+
|
55
|
+
# Change field type to select_box
|
56
|
+
mc.field_type = "select_box"
|
57
|
+
assert_equal "select_box", mc.field_type
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
data/test/number_test.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class NumberTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_should_create_number_answer_field
|
6
|
+
number = FactoryForm::Number.new(:id => "number_1", :label => "Give phone number")
|
7
|
+
assert_equal "number", number.validation_format
|
8
|
+
assert_equal "short_answer", number.field_type
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
data/test/rating_test.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class RatingTest < Test::Unit::TestCase
|
4
|
+
def test_should_create_rating_answer_field
|
5
|
+
|
6
|
+
# Rating answer field should have scale attributes passed as well
|
7
|
+
assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::Rating.new(:id => "rating_1", :label => "Choose your ranking" ) }
|
8
|
+
|
9
|
+
# Create rating answer field providing scales
|
10
|
+
rating = FactoryForm::Rating.new(:id => "rating_1", :label => "Choose your ranking" , :scale => "5" )
|
11
|
+
assert_equal 5, rating.scale
|
12
|
+
assert_equal "", rating.from_label
|
13
|
+
|
14
|
+
# Create rating answer field providing from and to label
|
15
|
+
rating = FactoryForm::Rating.new(:id => "rating_1", :label => "How would you rank this website, on the basis of easiness of UI ?" , :scale => 5, :from_label => "bad", :to_label => "good" )
|
16
|
+
assert_equal "bad", rating.from_label
|
17
|
+
assert_equal "good", rating.to_label
|
18
|
+
assert_equal "rating", rating.field_type
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ShortAnswerTest < Test::Unit::TestCase
|
4
|
+
def test_should_create_short_answer_field_with_minimum_params_passed
|
5
|
+
sa1 = FactoryForm::ShortAnswer.new(:id => "email_1", :label => "Personal email address")
|
6
|
+
|
7
|
+
# By default, no validation for different formats unless explicitly specified
|
8
|
+
assert_equal "general",sa1.validation_format
|
9
|
+
|
10
|
+
# By default, answer for this field is optional unless explicitly specified as required
|
11
|
+
assert_equal true,sa1.required
|
12
|
+
|
13
|
+
# By default, no uniqueness of answer for this field unless explicitly specified as unique
|
14
|
+
assert_equal false,sa1.unique
|
15
|
+
|
16
|
+
# Set validation format as email
|
17
|
+
sa1.validation_format = "email"
|
18
|
+
|
19
|
+
# Should have email format as validation_format
|
20
|
+
assert_equal "email", sa1.validation_format
|
21
|
+
|
22
|
+
# Set answer for this field as required
|
23
|
+
sa1.required = true
|
24
|
+
|
25
|
+
# Set answer for this field to be unique
|
26
|
+
sa1.unique = true
|
27
|
+
|
28
|
+
# Should have required attribute to be true
|
29
|
+
assert_equal true, sa1.required
|
30
|
+
|
31
|
+
# Should have unique answers
|
32
|
+
assert_equal true, sa1.unique
|
33
|
+
|
34
|
+
assert_equal "short_answer", sa1.field_type
|
35
|
+
|
36
|
+
|
37
|
+
sa1.hint = "It should be his/her personal email address, not his official address."
|
38
|
+
assert_equal "It should be his/her personal email address, not his official address.", sa1.hint
|
39
|
+
|
40
|
+
# Ensures object of this class
|
41
|
+
assert_instance_of FactoryForm::ShortAnswer, sa1
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_create_short_answer_field_with_all_other_optional_params
|
46
|
+
sa = FactoryForm::ShortAnswer.new(:id => "url_1", :label => "Personal web address",
|
47
|
+
:options => {
|
48
|
+
:hint => "It should be his/her personal web address",
|
49
|
+
:validation_format => "url",
|
50
|
+
:required => true,
|
51
|
+
:unique => false
|
52
|
+
}
|
53
|
+
)
|
54
|
+
assert_equal "url_1", sa.id
|
55
|
+
assert_equal "Personal web address", sa.label
|
56
|
+
assert_equal "It should be his/her personal web address", sa.hint
|
57
|
+
assert_equal "url", sa.validation_format
|
58
|
+
assert_equal true, sa.required
|
59
|
+
assert_equal false, sa.unique
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_should_not_create_short_answer_field_if_required_params_are_missing
|
64
|
+
# Raise expected argument exception as ID,label are required
|
65
|
+
assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::ShortAnswer.new() }
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_should_not_raise_exception_for_valid_params_passed
|
69
|
+
assert_nothing_raised(Exception){ FactoryForm::ShortAnswer.new(:id => "phone_number", :label => "Phone Number") }
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_should_raise_exception_if_duplicate_ID_exists
|
73
|
+
sa1 = FactoryForm::ShortAnswer.new(:id => "email_1", :label => "Official email address")
|
74
|
+
sa2 = FactoryForm::ShortAnswer.new(:id => "email_1", :label => "Personal email address")
|
75
|
+
form = FactoryForm::Form.new
|
76
|
+
form.add(sa1)
|
77
|
+
|
78
|
+
# Raise duplicate ID exception
|
79
|
+
assert_raise(FactoryForm::DuplicateIDException){ form.add(sa2) }
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
end
|