lapillus 0.0.2
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/examples/guest_book.rb +50 -0
- data/examples/hello_world.rb +12 -0
- data/examples/persons.rb +30 -0
- data/examples/tc_guest_book.rb +34 -0
- data/examples/tc_hello_world.rb +24 -0
- data/examples/tc_persons.rb +13 -0
- data/html/GuestBook.html +24 -0
- data/html/HelloWorld.html +5 -0
- data/html/LapillusTesterTestPage.html +12 -0
- data/html/Persons.html +7 -0
- data/html/TestPage.html +8 -0
- data/html/TestRemotePanel.html +8 -0
- data/lib/changelog.txt +6 -0
- data/lib/lapillus/base.rb +276 -0
- data/lib/lapillus/behaviours.rb +208 -0
- data/lib/lapillus/components.rb +116 -0
- data/lib/lapillus/containers.rb +234 -0
- data/lib/lapillus/dispatcher.rb +216 -0
- data/lib/lapillus/form_components.rb +88 -0
- data/lib/lapillus/lapillus_server.rb +21 -0
- data/lib/lapillus/lapillus_testers.rb +167 -0
- data/lib/lapillus/mongrel_server.rb +69 -0
- data/lib/lapillus/multiview.rb +45 -0
- data/lib/lapillus/pager.rb +104 -0
- data/lib/lapillus/process_upload.rb +11 -0
- data/lib/lapillus/web_application.rb +12 -0
- data/lib/lapillus/webrick_server.rb +90 -0
- data/lib/lapillus.rb +33 -0
- data/lib/license.txt +24 -0
- data/test/tc_base.rb +63 -0
- data/test/tc_behaviours.rb +106 -0
- data/test/tc_bookmarkablepagelink.rb +58 -0
- data/test/tc_components.rb +285 -0
- data/test/tc_containers.rb +173 -0
- data/test/tc_dispatcher.rb +106 -0
- data/test/tc_examples.rb +193 -0
- data/test/tc_form.rb +231 -0
- data/test/tc_fragments.rb +114 -0
- data/test/tc_hierarchy.rb +34 -0
- data/test/tc_lapillus.rb +30 -0
- data/test/tc_lapillus_testers.rb +108 -0
- data/test/tc_multiview.rb +91 -0
- data/test/tc_pager.rb +94 -0
- data/test/tc_rendering.rb +74 -0
- data/test/ts_all_test.rb +20 -0
- metadata +94 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
require 'lapillus'
|
|
2
|
+
require 'rexml/document'
|
|
3
|
+
require 'test/unit'
|
|
4
|
+
include Lapillus #hmm shouldn't this be moved!? to lapillus/lapillus
|
|
5
|
+
|
|
6
|
+
module Lapillus
|
|
7
|
+
class Component
|
|
8
|
+
public :hierarchy
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class TC_containers < Test::Unit::TestCase
|
|
13
|
+
attr_reader :webpage
|
|
14
|
+
|
|
15
|
+
def setup
|
|
16
|
+
@webpage = Webpage.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def html_template(input)
|
|
20
|
+
"<html>#{input}</html>"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def expected_html(output)
|
|
24
|
+
"<html>#{output}</html>"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_container
|
|
28
|
+
container = Container.new("container")
|
|
29
|
+
component = Component.new("component")
|
|
30
|
+
container.add(component)
|
|
31
|
+
assert_equal([component], container.components)
|
|
32
|
+
assert(component.has_parent?)
|
|
33
|
+
assert_equal(container, component.parent)
|
|
34
|
+
assert(!container.has_parent?)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_hierarchy
|
|
38
|
+
first = Container.new("first")
|
|
39
|
+
second = Container.new("second")
|
|
40
|
+
three = Component.new("three")
|
|
41
|
+
first.add(second.add(three))
|
|
42
|
+
assert_equal([first, second, three], three.hierarchy)
|
|
43
|
+
assert_equal(first, three.webpage)
|
|
44
|
+
assert_equal(first, second.webpage)
|
|
45
|
+
assert_equal(first, first.webpage)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def test_path
|
|
49
|
+
first = Container.new("first")
|
|
50
|
+
second = Container.new("second")
|
|
51
|
+
three = Component.new("three")
|
|
52
|
+
first.add(second.add(three))
|
|
53
|
+
assert_equal("second.three", three.path)
|
|
54
|
+
assert_equal("second", second.path)
|
|
55
|
+
assert_equal("", first.path)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_get
|
|
59
|
+
first = Container.new("first")
|
|
60
|
+
second = Container.new("second")
|
|
61
|
+
three = Component.new("three")
|
|
62
|
+
four = Component.new("four")
|
|
63
|
+
second.add(four)
|
|
64
|
+
first.add(second.add(three))
|
|
65
|
+
assert_equal(three, first["second.three"])
|
|
66
|
+
assert_equal(second, first["second"])
|
|
67
|
+
assert_equal(four, second["four"])
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def test_webpage
|
|
71
|
+
html = "<html><body>it is a brave new world!</body></html>"
|
|
72
|
+
result = webpage.render(html)
|
|
73
|
+
assert_equal(html, result)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
#TODO: add rendering test for Webpage with a component
|
|
77
|
+
|
|
78
|
+
class TestWebpage < Webpage
|
|
79
|
+
attr_accessor :values
|
|
80
|
+
listview "listview", :model => :values do |text|
|
|
81
|
+
label "text", :model => text
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def test_listview_components
|
|
86
|
+
test = TestWebpage.new
|
|
87
|
+
test.values = ["first","second"]
|
|
88
|
+
listview = test.listview
|
|
89
|
+
label1 = listview[0].text
|
|
90
|
+
label2 = listview[1].text
|
|
91
|
+
assert_equal("first", label1.value)
|
|
92
|
+
assert_equal("second", label2.value)
|
|
93
|
+
assert_equal("listview.0.text", label1.path)
|
|
94
|
+
assert_equal("listview.1.text", label2.path)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def test_listview_rendering
|
|
98
|
+
html = html_template('<span lapillus:id="listview">'+
|
|
99
|
+
'<p><span lapillus:id="text">Comment text goes here.</span>'+
|
|
100
|
+
'</p></span>')
|
|
101
|
+
expected = expected_html('<span lapillus:id="listview">'+
|
|
102
|
+
'<p><span lapillus:id="text">first</span>'+
|
|
103
|
+
'</p><p><span lapillus:id="text">second</span>'+
|
|
104
|
+
'</p></span>')
|
|
105
|
+
testpanel = TestWebpage.new
|
|
106
|
+
testpanel.values = ["first","second"]
|
|
107
|
+
listview = testpanel.listview
|
|
108
|
+
result = testpanel.render(html)
|
|
109
|
+
assert_equal(expected, result)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def test_listview_set_value
|
|
113
|
+
test = TestWebpage.new
|
|
114
|
+
test.values = ["first","second"]
|
|
115
|
+
listview = test.listview
|
|
116
|
+
label1 = listview[0].text
|
|
117
|
+
label2 = listview[1].text
|
|
118
|
+
assert_equal("first", label1.value)
|
|
119
|
+
assert_equal("second", label2.value)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# TODO: reenable show tree
|
|
123
|
+
# def test_webpage_to_s
|
|
124
|
+
# listview = ListView.new("list", ["jan", "piet", "klaas"]) do |name|
|
|
125
|
+
# add(Label.new("label", name))
|
|
126
|
+
# end
|
|
127
|
+
# label = Label.new("label", "Hello World")
|
|
128
|
+
# webpage.add(label)
|
|
129
|
+
# webpage.add(listview)
|
|
130
|
+
# puts webpage.show_tree
|
|
131
|
+
# end
|
|
132
|
+
|
|
133
|
+
#NOTE: a remote panel is a panel with its own html file
|
|
134
|
+
#implementation at the moment is naieve
|
|
135
|
+
#just copy the body
|
|
136
|
+
def test_remote_panel_render_from_within_webpage
|
|
137
|
+
webpage = TestPage.new #NOTE: uses html/TestPage.html
|
|
138
|
+
remote_panel = TestRemotePanel.new("remote_panel") # html/TestRemotePanel.html
|
|
139
|
+
webpage.add(remote_panel)
|
|
140
|
+
expected = <<EOF
|
|
141
|
+
<html>
|
|
142
|
+
<body>
|
|
143
|
+
This should be visible
|
|
144
|
+
<span lapillus:id="remote_panel">
|
|
145
|
+
contents of remote panel
|
|
146
|
+
</span>
|
|
147
|
+
</body>
|
|
148
|
+
</html>
|
|
149
|
+
EOF
|
|
150
|
+
assert_equal(expected.strip, webpage.render)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def test_remote_panel_render_indepent_from_webpage
|
|
154
|
+
remote_panel = TestRemotePanel.new("remote_panel") # html/TestRemotePanel.html end
|
|
155
|
+
expected = <<EOF
|
|
156
|
+
<html>
|
|
157
|
+
<title>
|
|
158
|
+
not visible
|
|
159
|
+
</title>
|
|
160
|
+
<body>
|
|
161
|
+
contents of remote panel
|
|
162
|
+
</body>
|
|
163
|
+
</html>
|
|
164
|
+
EOF
|
|
165
|
+
assert_equal(expected.strip, remote_panel.render)
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
class TestPage < Webpage
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
class TestRemotePanel < RemotePanel
|
|
173
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'lapillus/containers'
|
|
3
|
+
require 'lapillus/dispatcher'
|
|
4
|
+
require 'lapillus/web_application'
|
|
5
|
+
|
|
6
|
+
include Lapillus
|
|
7
|
+
|
|
8
|
+
module Fake_output
|
|
9
|
+
def render
|
|
10
|
+
"<html>expected output</html>"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class MockWebpage < Webpage
|
|
15
|
+
include Fake_output
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class MockBookmarkableWebpage < BookmarkableWebpage
|
|
19
|
+
|
|
20
|
+
def initialize(page_parameters)
|
|
21
|
+
@page_parameters=page_parameters
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def render
|
|
25
|
+
parameters=@page_parameters.collect{|k,v| "#{k}=#{v}"}.join("\n")
|
|
26
|
+
"<html>#{parameters}</html>"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class TC_Dispatcher < Test::Unit::TestCase
|
|
31
|
+
def test_get_homepage
|
|
32
|
+
mock_cgi = MockCgi.new('')
|
|
33
|
+
mock_application = WebApplication.new
|
|
34
|
+
mock_application.homepage = MockWebpage
|
|
35
|
+
Dispatcher.get(mock_cgi, mock_application)
|
|
36
|
+
assert_equal("<html>expected output</html>", mock_cgi.output)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_get_homepage_with_trailing_slash
|
|
40
|
+
mock_cgi = MockCgi.new('/')
|
|
41
|
+
mock_application = WebApplication.new
|
|
42
|
+
mock_application.homepage = MockWebpage
|
|
43
|
+
Dispatcher.get(mock_cgi, mock_application)
|
|
44
|
+
assert_equal("<html>expected output</html>", mock_cgi.output)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_get_bookmarkable_page
|
|
48
|
+
mock_cgi = MockCgi.new('/page')
|
|
49
|
+
mock_application = WebApplication.new
|
|
50
|
+
mock_application.mount_bookmarkable_page("page", MockBookmarkableWebpage)
|
|
51
|
+
Dispatcher.get(mock_cgi, mock_application)
|
|
52
|
+
assert_equal("<html></html>", mock_cgi.output)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_get_bookmarkable_page_with_trailing_slash
|
|
56
|
+
mock_cgi = MockCgi.new('/page/')
|
|
57
|
+
mock_application = WebApplication.new
|
|
58
|
+
mock_application.mount_bookmarkable_page("page", MockBookmarkableWebpage)
|
|
59
|
+
Dispatcher.get(mock_cgi, mock_application)
|
|
60
|
+
assert_equal("<html></html>", mock_cgi.output)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_get_bookmarkable_page_with_parameters
|
|
64
|
+
mock_cgi = MockCgi.new('/page/param1/value/param2/value2')
|
|
65
|
+
mock_application = WebApplication.new
|
|
66
|
+
mock_application.mount_bookmarkable_page("page", MockBookmarkableWebpage)
|
|
67
|
+
Dispatcher.get(mock_cgi, mock_application)
|
|
68
|
+
assert_equal("<html>param1=value\nparam2=value2</html>", mock_cgi.output)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def test_session
|
|
72
|
+
mock_cgi = MockCgi.new('')
|
|
73
|
+
mock_application = WebApplication.new
|
|
74
|
+
mock_application.homepage = MockWebpage
|
|
75
|
+
Dispatcher.get(mock_cgi, mock_application)
|
|
76
|
+
assert_equal("<html>expected output</html>", mock_cgi.output)
|
|
77
|
+
session = Dispatcher.get_session(mock_cgi)
|
|
78
|
+
assert(!session.new_session)
|
|
79
|
+
page = session['']
|
|
80
|
+
assert_not_nil(page)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
class MockCgi
|
|
85
|
+
attr_reader :env_table
|
|
86
|
+
attr_reader :params
|
|
87
|
+
attr_reader :cookies
|
|
88
|
+
attr_reader :output
|
|
89
|
+
def initialize(path_info)
|
|
90
|
+
@env_table = Hash.new
|
|
91
|
+
@params = Hash.new
|
|
92
|
+
@cookies = Hash.new
|
|
93
|
+
env_table['PATH_INFO']=path_info
|
|
94
|
+
end
|
|
95
|
+
def key?(key)
|
|
96
|
+
return params.has_key?(key)
|
|
97
|
+
end
|
|
98
|
+
alias has_key? key?
|
|
99
|
+
def out(http_headers)
|
|
100
|
+
params["_session_id"] = @output_cookies.to_s.split(";")[0].split("_session_id=")[1]
|
|
101
|
+
@output = yield
|
|
102
|
+
end
|
|
103
|
+
def [](key)
|
|
104
|
+
return params[key]
|
|
105
|
+
end
|
|
106
|
+
end
|
data/test/tc_examples.rb
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
require 'lapillus'
|
|
2
|
+
include Lapillus
|
|
3
|
+
|
|
4
|
+
class TC_examples < Test::Unit::TestCase
|
|
5
|
+
class MyFirstWebpage < Webpage
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def test_new_webpage
|
|
9
|
+
page = MyFirstWebpage.new
|
|
10
|
+
assert_equal(0, page.components.size)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class MyLabelWebpage < Webpage
|
|
14
|
+
label "my_label"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_new_label
|
|
18
|
+
page = MyLabelWebpage.new
|
|
19
|
+
assert_equal(1, page.components.size)
|
|
20
|
+
assert(!page.my_label.has_model?)
|
|
21
|
+
assert_equal("", page.my_label.value)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
#TODO: example with two labels, with different identifiers
|
|
25
|
+
|
|
26
|
+
class MyLabelWebpage2 < Webpage
|
|
27
|
+
label "label", :model => "Hello world!"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_label
|
|
31
|
+
page = MyLabelWebpage2.new
|
|
32
|
+
assert_equal(1, page.components.size)
|
|
33
|
+
assert(page.label.has_model?)
|
|
34
|
+
assert_equal("Hello world!", page.label.model)
|
|
35
|
+
assert_equal("Hello world!", page.label.value)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
class Person
|
|
39
|
+
attr_reader :name
|
|
40
|
+
|
|
41
|
+
def initialize(name)
|
|
42
|
+
@name = name
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def to_s
|
|
46
|
+
"person: #{name}"
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
class MyLabelWebpage3 < Webpage
|
|
51
|
+
attr_reader :person
|
|
52
|
+
label "person_label", :model => :person
|
|
53
|
+
|
|
54
|
+
def initialize(person)
|
|
55
|
+
super()
|
|
56
|
+
@person = person
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def test_label_with_attribute_as_model
|
|
61
|
+
person = Person.new("Klaasje")
|
|
62
|
+
page = MyLabelWebpage3.new(person)
|
|
63
|
+
assert_equal(1, page.components.size)
|
|
64
|
+
assert_equal(person, page.person_label.model)
|
|
65
|
+
assert_equal("person: Klaasje", page.person_label.value)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
#TODO: add example with two labels, with different identifiers
|
|
69
|
+
|
|
70
|
+
class MyLabelWebpage4 < Webpage
|
|
71
|
+
attr_reader :person
|
|
72
|
+
label "person_name", :model => :person, :property => :name
|
|
73
|
+
|
|
74
|
+
def initialize(person)
|
|
75
|
+
super()
|
|
76
|
+
@person = person
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_label_with_attribute_as_model_and_property
|
|
81
|
+
person = Person.new("Klaasje")
|
|
82
|
+
page = MyLabelWebpage4.new(person)
|
|
83
|
+
assert_equal(1, page.components.size)
|
|
84
|
+
assert_equal(person, page.person_name.model)
|
|
85
|
+
assert_equal("Klaasje", page.person_name.value)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
#TODO: add example with a listview with a model and a property
|
|
89
|
+
|
|
90
|
+
class MyListViewWebpage < Webpage
|
|
91
|
+
listview "listview", :model => ["value1", "value2"] do |item|
|
|
92
|
+
label "item", :model => item
|
|
93
|
+
label "another_thing", :model => "message"
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def test_listview
|
|
98
|
+
page = MyListViewWebpage.new
|
|
99
|
+
assert_equal(1, page.components.size)
|
|
100
|
+
assert_equal(2, page.listview.size)
|
|
101
|
+
assert_equal("value1", page.listview[0].item.value)
|
|
102
|
+
assert_equal("value2", page.listview[1].item.value)
|
|
103
|
+
assert_equal("message", page.listview[0].another_thing.value)
|
|
104
|
+
assert_equal("message", page.listview[1].another_thing.value)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
class MyListViewWebpage2 < Webpage
|
|
108
|
+
attr_reader :persons
|
|
109
|
+
listview "personlist", :model => :persons do |person|
|
|
110
|
+
label "name", :model => person, :property => :name
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def initialize
|
|
114
|
+
super()
|
|
115
|
+
person1 = Person.new("jantje")
|
|
116
|
+
person2 = Person.new("pietje")
|
|
117
|
+
@persons = [person1, person2]
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def test_listview_with_attribute_as_model
|
|
122
|
+
page = MyListViewWebpage2.new
|
|
123
|
+
assert_equal(1, page.components.size)
|
|
124
|
+
assert_equal(2, page.personlist.size)
|
|
125
|
+
assert_equal("jantje", page.personlist[0].name.value)
|
|
126
|
+
assert_equal("pietje", page.personlist[1].name.value)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
class MyForm < Form
|
|
130
|
+
textfield "textfield"
|
|
131
|
+
password_textfield "password"
|
|
132
|
+
textarea "textarea"
|
|
133
|
+
fileuploadfield "fileuploadfield"
|
|
134
|
+
|
|
135
|
+
def on_submit(button)
|
|
136
|
+
puts "the user entered #{textfield.value}"
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def test_form
|
|
141
|
+
form = MyForm.new("my_form")
|
|
142
|
+
assert_equal(4, form.components.size)
|
|
143
|
+
assert_equal("", form.textfield.value)
|
|
144
|
+
assert_equal("", form.password.value)
|
|
145
|
+
assert_equal("", form.textarea.value)
|
|
146
|
+
values = { "textfield" => "value1",
|
|
147
|
+
"password" => "value2",
|
|
148
|
+
"textarea" => "value3"
|
|
149
|
+
}
|
|
150
|
+
form.post(values)
|
|
151
|
+
assert_equal("value1", form.textfield.value)
|
|
152
|
+
assert_equal("value2", form.password.value)
|
|
153
|
+
assert_equal("value3", form.textarea.value)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
class MyModel
|
|
157
|
+
attr_accessor :property1
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
class MyForm2 < Form
|
|
161
|
+
textfield "textfield", :model => :model, :property => :property1
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def test_form_with_model
|
|
165
|
+
model = MyModel.new
|
|
166
|
+
model.property1 = "value1"
|
|
167
|
+
form = MyForm2.new("my_form", model)
|
|
168
|
+
assert_equal(1, form.components.size)
|
|
169
|
+
assert_equal("value1", form.textfield.value)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
class MyPanel < Panel
|
|
173
|
+
alias person model
|
|
174
|
+
label "name", :model => :person, :property => :name
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
class MyPanelWebpage < Webpage
|
|
178
|
+
attr_reader :person
|
|
179
|
+
panel "panel", MyPanel, :person
|
|
180
|
+
|
|
181
|
+
def initialize(person)
|
|
182
|
+
super()
|
|
183
|
+
@person = person
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def test_panel
|
|
188
|
+
person = Person.new("jantje")
|
|
189
|
+
page = MyPanelWebpage.new(person)
|
|
190
|
+
assert_equal(person.name, page.panel.name.value)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
end
|
data/test/tc_form.rb
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'lapillus'
|
|
3
|
+
|
|
4
|
+
include Lapillus
|
|
5
|
+
|
|
6
|
+
class TC_Form < Test::Unit::TestCase
|
|
7
|
+
class TestPage < Webpage
|
|
8
|
+
attr_writer :called
|
|
9
|
+
def called?
|
|
10
|
+
@called
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def html_template(input)
|
|
15
|
+
return "<html><form lapillus:id=\"test_form\">#{input}</form></html>\n"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def expected_html(output)
|
|
19
|
+
return "<html><form enctype=\"multipart/form-data\" method=\"post\" lapillus:id=\"test_form\">#{output}<input name=\"page\" type=\"hidden\" value=\"TC_Form::TestPage\"/></form></html>"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def expected_html2(output)
|
|
23
|
+
return "<html><form enctype=\"multipart/form-data\" method=\"post\" lapillus:id=\"test_form\">#{output}<input name=\"page\" type=\"hidden\" value=\"Lapillus::Webpage\"/></form></html>"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
class FormValue
|
|
27
|
+
attr_accessor :called
|
|
28
|
+
def called?
|
|
29
|
+
return @called
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class MyForm < Form
|
|
34
|
+
attr_reader :value
|
|
35
|
+
def initialize(id)
|
|
36
|
+
super(id)
|
|
37
|
+
@value = FormValue.new
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def on_submit(button)
|
|
41
|
+
value.called=true
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_form
|
|
46
|
+
html = html_template("more stuff here")
|
|
47
|
+
page = TestPage.new
|
|
48
|
+
form = MyForm.new("test_form")
|
|
49
|
+
page.add(form)
|
|
50
|
+
result = page.render(html)
|
|
51
|
+
assert_equal(expected_html("more stuff here"), result)
|
|
52
|
+
hash = {"test_form.fake"=>"fake"}
|
|
53
|
+
page.post(hash)
|
|
54
|
+
assert(form.value.called?)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class Comment
|
|
58
|
+
attr_accessor :area
|
|
59
|
+
attr_accessor :field
|
|
60
|
+
def initialize text
|
|
61
|
+
@area = text
|
|
62
|
+
@field = text
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class MyTextfieldForm < Form
|
|
68
|
+
attr_reader :comment
|
|
69
|
+
textfield "field", :model => :comment, :property => :field
|
|
70
|
+
def initialize(id, comment)
|
|
71
|
+
super(id)
|
|
72
|
+
@comment = comment
|
|
73
|
+
end
|
|
74
|
+
def on_submit(button)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def test_form_textfield
|
|
79
|
+
html = html_template('<input type="text" lapillus:id="field"/>')
|
|
80
|
+
comment = Comment.new("supernice")
|
|
81
|
+
page = Webpage.new
|
|
82
|
+
form = MyTextfieldForm.new("test_form", comment)
|
|
83
|
+
page.add(form)
|
|
84
|
+
result = page.render(html)
|
|
85
|
+
assert_equal(expected_html2('<input name="test_form.field" type="text" lapillus:id="field" value="supernice"/>'), result)
|
|
86
|
+
post_values = Hash['test_form.field' => 'value']
|
|
87
|
+
page.post(post_values)
|
|
88
|
+
assert_equal("value", comment.field)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
class MyAreaForm < Form
|
|
92
|
+
attr_reader :comment
|
|
93
|
+
textarea "area", :model => :comment, :property => :area
|
|
94
|
+
def initialize(id, comment)
|
|
95
|
+
super(id)
|
|
96
|
+
@comment = comment
|
|
97
|
+
end
|
|
98
|
+
def on_submit(button)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def test_form_textarea
|
|
103
|
+
comment = Comment.new("test test test")
|
|
104
|
+
html = html_template('<textarea lapillus:id="area">some text</textarea>')
|
|
105
|
+
page = Webpage.new
|
|
106
|
+
form = MyAreaForm.new("test_form", comment)
|
|
107
|
+
page.add(form)
|
|
108
|
+
result = page.render(html)
|
|
109
|
+
assert_equal(expected_html2('<textarea name="test_form.area" lapillus:id="area">test test test</textarea>'), result)
|
|
110
|
+
post_values = Hash['test_form.area' => 'value']
|
|
111
|
+
page.post(post_values)
|
|
112
|
+
assert_equal("value", comment.area)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
class UploadFile
|
|
116
|
+
#TODO: Develop into proper model for FileUpload
|
|
117
|
+
attr_accessor :upload_file
|
|
118
|
+
def initialize upload_file
|
|
119
|
+
@upload_file = upload_file
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
class MyUploadfieldForm < Form
|
|
124
|
+
attr_reader :upload_file
|
|
125
|
+
fileuploadfield "upload_field", :model => :upload_file, :property => :upload_file
|
|
126
|
+
def initialize(id, upload_file)
|
|
127
|
+
super(id)
|
|
128
|
+
@upload_file = upload_file
|
|
129
|
+
end
|
|
130
|
+
def on_submit(button)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def test_form_upload_field
|
|
135
|
+
upload_file = UploadFile.new( "some file huh?" )
|
|
136
|
+
html = html_template('<input lapillus:id="upload_field" type="file"/>')
|
|
137
|
+
page = Webpage.new
|
|
138
|
+
form = MyUploadfieldForm.new("test_form", upload_file)
|
|
139
|
+
page.add(form)
|
|
140
|
+
result = page.render(html)
|
|
141
|
+
assert_equal(expected_html2('<input name="test_form.upload_field" type="file" lapillus:id="upload_field"/>'), result)
|
|
142
|
+
#TODO: Acceptance test with a real file?
|
|
143
|
+
post_values = Hash['test_form.upload_field' => 'value']
|
|
144
|
+
page.post(post_values)
|
|
145
|
+
assert_equal("value", upload_file.upload_file)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
class TestForm < Form
|
|
149
|
+
alias comment model
|
|
150
|
+
textarea "area", :model => :comment, :property => :area
|
|
151
|
+
def on_submit(button)
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def test_form_model
|
|
156
|
+
html = html_template('<textarea lapillus:id="area">some text</textarea>' )
|
|
157
|
+
page = TestPage.new
|
|
158
|
+
page.add(TestForm.new("test_form", Comment.new("nice")))
|
|
159
|
+
result = page.render(html)
|
|
160
|
+
assert_equal(expected_html('<textarea name="test_form.area" lapillus:id="area">nice</textarea>'), result)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class ComplexForm < Form
|
|
165
|
+
fileuploadfield "uploadveld"
|
|
166
|
+
def on_submit(button)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
def test_complex_form
|
|
170
|
+
html = <<EOF
|
|
171
|
+
<html><form lapillus:id="add_form">
|
|
172
|
+
<input type="file" name="uploadveld" id="uploadveld" lapillus:id="uploadveld"/>
|
|
173
|
+
<br/>
|
|
174
|
+
<input type="submit" name="submit" id="submit" value="Voeg tekst toe"/>
|
|
175
|
+
<input type="submit" name="submit" id="submit" value="Voeg tekstkader toe"/>
|
|
176
|
+
</form></html>
|
|
177
|
+
EOF
|
|
178
|
+
expected = <<EOF
|
|
179
|
+
<html><form enctype="multipart/form-data" method="post" lapillus:id="add_form">
|
|
180
|
+
<input name="add_form.uploadveld" type="file" id="uploadveld" lapillus:id="uploadveld"/>
|
|
181
|
+
<br/>
|
|
182
|
+
<input name="submit" type="submit" id="submit" value="Voeg tekst toe"/>
|
|
183
|
+
<input name="submit" type="submit" id="submit" value="Voeg tekstkader toe"/>
|
|
184
|
+
<input name="page" type="hidden" value="Lapillus::Webpage"/></form></html>
|
|
185
|
+
EOF
|
|
186
|
+
webpage = Lapillus::Webpage.new
|
|
187
|
+
webpage.add ComplexForm.new("add_form")
|
|
188
|
+
output = webpage.render html
|
|
189
|
+
assert_equal(expected.strip, output)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
include Lapillus
|
|
193
|
+
|
|
194
|
+
class Form1 < Form
|
|
195
|
+
attr_reader :do_not_submit
|
|
196
|
+
def on_submit(button)
|
|
197
|
+
@do_not_submit = true
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
class Form2 < Form
|
|
202
|
+
attr_reader :submit_this_form
|
|
203
|
+
def on_submit(button)
|
|
204
|
+
@submit_this_form = true
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def test_post_1_webpage_with_multiple_forms
|
|
209
|
+
webpage = Webpage.new
|
|
210
|
+
form1 = Form1.new("form1")
|
|
211
|
+
form2 = Form2.new("form2")
|
|
212
|
+
webpage.add(form1)
|
|
213
|
+
webpage.add(form2)
|
|
214
|
+
hash = {"form2.field" => "value"}
|
|
215
|
+
webpage.post(hash)
|
|
216
|
+
assert(!form1.do_not_submit)
|
|
217
|
+
assert(form2.submit_this_form)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def test_form_within_panel
|
|
221
|
+
webpage = Webpage.new
|
|
222
|
+
panel = Panel.new("panel")
|
|
223
|
+
form2 = Form2.new("form2")
|
|
224
|
+
webpage.add(panel)
|
|
225
|
+
panel.add(form2)
|
|
226
|
+
hash = {"form2.field" => "value"}
|
|
227
|
+
webpage.post(hash)
|
|
228
|
+
assert(form2.submit_this_form)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
end
|