xsltgem 0.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.
@@ -0,0 +1,112 @@
1
+ $test_root=File.expand_path File.dirname(__FILE__)
2
+ require $test_root + '/xslt_test_helper'
3
+
4
+ class BuiltinTemplatesTest < Test::Unit::TestCase
5
+ include XSLTTestHelper
6
+
7
+ BUILTIN_XSL = Pathname($test_root)+'..'+'assets'+'xslt_helpers.xsl'
8
+
9
+ def test_stylesheet_with_simple_sheetname
10
+ xsl = <<-XSL
11
+ <xsl:variable name="language">en</xsl:variable>
12
+ <xsl:template match="test">
13
+ <xsl:call-template name='stylesheet'>
14
+ <xsl:with-param name="sheet">sheetname</xsl:with-param>
15
+ </xsl:call-template>
16
+ </xsl:template>
17
+ XSL
18
+
19
+ result = html_from_xml_helper('<test><url-root></url-root></test>', xsl, BUILTIN_XSL)
20
+ assert_match %r{href="/stylesheets/sheetname.css"}, result
21
+ end
22
+
23
+ def test_stylesheet_with_url
24
+ url = 'http://foo/bar.css'
25
+ xsl = <<-XSL
26
+ <xsl:variable name="language">en</xsl:variable>
27
+ <xsl:template match="test">
28
+ <xsl:call-template name='stylesheet'>
29
+ <xsl:with-param name="sheet">#{url}</xsl:with-param>
30
+ </xsl:call-template>
31
+ </xsl:template>
32
+ XSL
33
+
34
+ result = html_from_xml_helper('<test><url-root></url-root></test>', xsl, BUILTIN_XSL)
35
+ assert_match %r{href="#{url}"}, result
36
+ end
37
+
38
+ def test_uri_delete_param_at_beginning
39
+ uri = "/controller/action?todelete=3&amp;keep=4"
40
+ xsl = <<-XSL
41
+ <xsl:template match="test">
42
+ <xsl:call-template name="uri-delete-param">
43
+ <xsl:with-param name="base-uri">#{uri}</xsl:with-param>
44
+ <xsl:with-param name="param">todelete</xsl:with-param>
45
+ </xsl:call-template>
46
+ </xsl:template>
47
+ XSL
48
+
49
+ result = html_from_xml_helper('<test />', xsl, BUILTIN_XSL)
50
+ assert_equal "/controller/action?keep=4", result
51
+ end
52
+
53
+ def test_uri_delete_param_in_middle
54
+ uri = "/controller/action?keep1=1&amp;todelete=2&amp;keep2=3"
55
+ xsl = <<-XSL
56
+ <xsl:template match="test">
57
+ <xsl:call-template name="uri-delete-param">
58
+ <xsl:with-param name="base-uri">#{uri}</xsl:with-param>
59
+ <xsl:with-param name="param">todelete</xsl:with-param>
60
+ </xsl:call-template>
61
+ </xsl:template>
62
+ XSL
63
+
64
+ result = html_from_xml_helper('<test />', xsl, BUILTIN_XSL)
65
+ assert_equal "/controller/action?keep1=1&amp;keep2=3", result
66
+ end
67
+
68
+ def test_uri_delete_param_at_end
69
+ uri = "/controller/action?keep1=1&amp;todelete=2"
70
+ xsl = <<-XSL
71
+ <xsl:template match="test">
72
+ <xsl:call-template name="uri-delete-param">
73
+ <xsl:with-param name="base-uri">#{uri}</xsl:with-param>
74
+ <xsl:with-param name="param">todelete</xsl:with-param>
75
+ </xsl:call-template>
76
+ </xsl:template>
77
+ XSL
78
+
79
+ result = html_from_xml_helper('<test />', xsl, BUILTIN_XSL)
80
+ assert_equal "/controller/action?keep1=1", result
81
+ end
82
+
83
+ def test_uri_delete_param_if_only_param
84
+ uri = "/controller/action?todelete=3"
85
+ xsl = <<-XSL
86
+ <xsl:template match="test">
87
+ <xsl:call-template name="uri-delete-param">
88
+ <xsl:with-param name="base-uri">#{uri}</xsl:with-param>
89
+ <xsl:with-param name="param">todelete</xsl:with-param>
90
+ </xsl:call-template>
91
+ </xsl:template>
92
+ XSL
93
+
94
+ result = html_from_xml_helper('<test />', xsl, BUILTIN_XSL)
95
+ assert_equal "/controller/action", result
96
+ end
97
+
98
+ def test_uri_delete_param_when_not_present
99
+ uri = "/v/location_search?language=de&amp;sort=minrate&amp;location=malaga"
100
+ xsl = <<-XSL
101
+ <xsl:template match="test">
102
+ <xsl:call-template name="uri-delete-param">
103
+ <xsl:with-param name="base-uri">#{uri}</xsl:with-param>
104
+ <xsl:with-param name="param">page</xsl:with-param>
105
+ </xsl:call-template>
106
+ </xsl:template>
107
+ XSL
108
+
109
+ result = html_from_xml_helper('<test />', xsl, BUILTIN_XSL)
110
+ assert_equal uri, result
111
+ end
112
+ end
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+
3
+ <terms lang="en">
4
+ <term name="language">English</term>
5
+ <term name="hello">Hello</term>
6
+ </terms>
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <terms>
3
+ <term name="HELLO">hello</term>
4
+ <term name="GOODBYE">goodbye</term>
5
+ </terms>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+
3
+ <terms lang="es">
4
+ <term name="HELLO">hola</term>
5
+ <term name="GOODBYE">adiós</term>
6
+ </terms>
7
+
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <terms lang="ja">
3
+ <term name="HELLO">hola</term>
4
+ <term name="GOODBYE">adiós</term>
5
+ </terms>
@@ -0,0 +1,21 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
3
+ <xsl:output method="html"/>
4
+
5
+ <!-- TODO customize transformation rules
6
+ syntax recommendation http://www.w3.org/TR/xslt
7
+ -->
8
+ <xsl:template match="/">
9
+ <html>
10
+ <head>
11
+ <title>erroneous_action.html.xsl</title>
12
+ </headx>
13
+ <body>
14
+ </body>
15
+ </html>
16
+ </xsl:template>
17
+ </xsl:stylesheet>
18
+ <!--
19
+ End of stylesheet missing on purpose - here to test that errors from xsltproc
20
+ are handled properly.
21
+ -->
@@ -0,0 +1,33 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+
3
+ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
4
+ <xsl:output method="xml"/>
5
+
6
+ <xsl:param name="title">default</xsl:param>
7
+ <xsl:template match="/">
8
+ <html>
9
+ <head>
10
+ <title><xsl:value-of select="$title" /></title>
11
+ </head>
12
+ <body>
13
+ <dt>
14
+ <xsl:apply-templates />
15
+ </dt>
16
+ </body>
17
+ </html>
18
+ </xsl:template>
19
+
20
+ <xsl:template match="*">
21
+ <dt><xsl:value-of select="name()" /></dt>
22
+ <dd><dl><xsl:apply-templates select="*|@*|text()" /></dl></dd>
23
+ </xsl:template>
24
+
25
+ <xsl:template match="@*">
26
+ <dt>@<xsl:value-of select="name()" /></dt>
27
+ <dd><xsl:value-of select="." /></dd>
28
+ </xsl:template>
29
+
30
+ <xsl:template match="text()">
31
+ <dt><xsl:value-of select="." /></dt>
32
+ </xsl:template>
33
+ </xsl:stylesheet>
@@ -0,0 +1,36 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+
3
+ <xsl:stylesheet version="1.0"
4
+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
5
+ xmlns:tr="http://www.hotelsearch.com/XMLSchema/2007/translation">
6
+ <xsl:output method="xml"/>
7
+
8
+ <xsl:param name="title">default</xsl:param>
9
+ <xsl:template match="/">
10
+ <html>
11
+ <head>
12
+ <title><xsl:value-of select="$title" /></title>
13
+ </head>
14
+ <body>
15
+ <p><tr:term>hello</tr:term> and <tr:term>language</tr:term></p>
16
+ <dt>
17
+ <xsl:apply-templates />
18
+ </dt>
19
+ </body>
20
+ </html>
21
+ </xsl:template>
22
+
23
+ <xsl:template match="*">
24
+ <dt><xsl:value-of select="name()" /></dt>
25
+ <dd><dl><xsl:apply-templates select="*|@*|text()" /></dl></dd>
26
+ </xsl:template>
27
+
28
+ <xsl:template match="@*">
29
+ <dt>@<xsl:value-of select="name()" /></dt>
30
+ <dd><xsl:value-of select="." /></dd>
31
+ </xsl:template>
32
+
33
+ <xsl:template match="text()">
34
+ <dt><xsl:value-of select="." /></dt>
35
+ </xsl:template>
36
+ </xsl:stylesheet>
@@ -0,0 +1,46 @@
1
+ $test_root=File.expand_path File.dirname(__FILE__)
2
+ require 'rubygems'
3
+ require 'mocha'
4
+
5
+ class TestTermLanguage < Test::Unit::TestCase
6
+ def use_test_translations(lang=:en)
7
+ TermLanguage.any_instance.
8
+ stubs(:translation_file).
9
+ returns($test_root + "/i18n/translation.#{lang}.xml")
10
+ end
11
+
12
+ def test_translation_file
13
+ TermLanguage.any_instance.stubs(:get_translations)
14
+
15
+ en = TermLanguage.new(:en)
16
+
17
+ file_path = en.translation_file
18
+ assert_equal(Pathname(RAILS_ROOT)+'public'+'xslts'+'i18n'+'normal.en.xml',
19
+ file_path)
20
+ end
21
+
22
+ def test_translate_in_english
23
+ use_test_translations(:en)
24
+ en = TermLanguage.new(:en)
25
+ assert_equal "hello", en.translate('HELLO')
26
+ end
27
+
28
+ def test_translate_in_spanish
29
+ use_test_translations(:es)
30
+ es = TermLanguage.new(:es)
31
+ assert_equal "hola", es.translate('HELLO')
32
+ end
33
+
34
+
35
+ def test_translate_with_missing_term
36
+ use_test_translations(:en)
37
+ en = TermLanguage.new(:en)
38
+ assert_equal "BINGO", en.translate('BINGO')
39
+ end
40
+
41
+ def test_square_brace_alias_works
42
+ use_test_translations(:en)
43
+ en = TermLanguage.new(:en)
44
+ assert_equal "goodbye", en['GOODBYE']
45
+ end
46
+ end
@@ -0,0 +1,78 @@
1
+ $test_root=File.expand_path File.dirname(__FILE__)
2
+ require $test_root + '/xslt_test_helper'
3
+
4
+ class TestTermLanguage < Test::Unit::TestCase
5
+ include XSLTRender
6
+
7
+ HELLO = <<-END
8
+ <?xml version="1.0" encoding="UTF-8"?>
9
+ <stuff xmlns:tr="http://www.hotelsearch.com/XMLSchema/2007/translation">
10
+ <tr:term>HELLO</tr:term>
11
+ </stuff>
12
+ END
13
+
14
+ HELLO_BEFORE_WORLD = <<-END
15
+ <?xml version="1.0" encoding="UTF-8"?>
16
+ <stuff xmlns:tr="http://www.hotelsearch.com/XMLSchema/2007/translation">
17
+ <tr:ordered before="HELLO">world</tr:ordered>
18
+ </stuff>
19
+ END
20
+
21
+ HELLO_WORLD_GOODBYE = <<-END
22
+ <?xml version="1.0" encoding="UTF-8"?>
23
+ <stuff xmlns:tr="http://www.hotelsearch.com/XMLSchema/2007/translation">
24
+ <tr:ordered before="HELLO" after="GOODBYE">world</tr:ordered>
25
+ </stuff>
26
+ END
27
+
28
+ NOT_PRESENT = <<-END
29
+ <?xml version="1.0" encoding="UTF-8"?>
30
+ <stuff xmlns:tr="http://www.hotelsearch.com/XMLSchema/2007/translation">
31
+ <tr:term>not present</tr:term>
32
+ </stuff>
33
+ END
34
+
35
+ PASS_THRU = <<-END
36
+ <?xml version="1.0" encoding="UTF-8"?>
37
+ <stuff xmlns:tr="http://www.hotelsearch.com/XMLSchema/2007/translation">
38
+ <tr:term pass-thru="true">pass thru</tr:term>
39
+ </stuff>
40
+ END
41
+
42
+ def setup
43
+ @i18n = $test_root + '/../lib/xslts/i18n.xsl'
44
+ @english = $test_root + '/i18n/translation.en.xml'
45
+ @spanish = $test_root + '/i18n/translation.es.xml'
46
+ @japanese = $test_root + '/i18n/translation.ja.xml'
47
+ end
48
+
49
+ def test_term_should_do_translation
50
+ xlated = html_from_xml(@i18n, HELLO, 'termfile' => @english)
51
+ assert_match /hello/, xlated
52
+ end
53
+
54
+ def test_term_should_not_pass_through_missing_terms_by_default
55
+ xlated = html_from_xml(@i18n, NOT_PRESENT, 'termfile' => @english)
56
+ assert_no_match /not present/, xlated
57
+ end
58
+
59
+ def test_term_should_pass_through_missing_terms
60
+ xlated = html_from_xml(@i18n, PASS_THRU, 'termfile' => @english)
61
+ assert_match /pass thru/, xlated
62
+ end
63
+
64
+ def test_order_defaults_to_left_to_right
65
+ xlated = html_from_xml(@i18n, HELLO_BEFORE_WORLD, 'termfile' => @english)
66
+ assert_match /hello world/, xlated
67
+ end
68
+
69
+ def test_before_and_after
70
+ xlated = html_from_xml(@i18n, HELLO_WORLD_GOODBYE, 'termfile' => @english)
71
+ assert_match /hello world goodbye/, xlated
72
+ end
73
+
74
+ def test_japanese_should_affect_order_and_spacing
75
+ xlated = html_from_xml(@i18n, HELLO_BEFORE_WORLD, 'termfile' => @japanese)
76
+ assert_match /worldhola/, xlated
77
+ end
78
+ end
@@ -0,0 +1,217 @@
1
+ $test_root = File.expand_path File.dirname(__FILE__)
2
+ require $test_root + '/xslt_test_helper'
3
+ require 'xslt_render'
4
+ require 'rubygems'
5
+ require 'mocha'
6
+ require 'tempfile'
7
+ require 'ostruct'
8
+
9
+ class MockLogger
10
+ def info(*args)
11
+ # don't do anything
12
+ end
13
+
14
+ alias warn info
15
+ alias debug info
16
+ end
17
+
18
+ class MockController
19
+ include XSLTRender
20
+
21
+ attr_accessor :action_name
22
+ attr_accessor :controller_name
23
+ attr_accessor :response
24
+
25
+ def self.controller_name
26
+ 'mock'
27
+ end
28
+
29
+ def params
30
+ {'spa' => nil};
31
+ end
32
+
33
+ def flash; end
34
+
35
+ def request
36
+ OpenStruct.new(:relative_url_root => '')
37
+ end
38
+
39
+ def initialize(values={})
40
+ values.each {|k,v| instance_variable_set("@#{k}", v)}
41
+
42
+ @response = OpenStruct.new
43
+ @xslt_root_dir = Pathname.new($test_root)
44
+ end
45
+
46
+ def render(options)
47
+ if options and options.kind_of? Hash and options[:text]
48
+ @response.body = options[:text]
49
+ end
50
+ end
51
+
52
+ def logger(*args)
53
+ MockLogger.new
54
+ end
55
+ end
56
+
57
+ class XSLTRenderTest < Test::Unit::TestCase
58
+ include XSLTRender
59
+
60
+ def use_test_translations(lang=:en)
61
+ TermLanguage.any_instance.
62
+ stubs(:translation_file).
63
+ returns($test_root + "/i18n/normal.#{lang}.xml")
64
+ end
65
+
66
+ def test_instance_hash
67
+ controller = MockController.new
68
+
69
+ controller.instance_eval do
70
+ @a = 'A'
71
+ @b = 'B'
72
+ end
73
+
74
+ assert_equal 'A', controller.instance_hash['a']
75
+ assert_equal 'B', controller.instance_hash['b']
76
+ end
77
+
78
+ def test_default_xslt_template
79
+ controller = MockController.new(:action_name => 'test_action')
80
+
81
+ assert_equal 'mock/test_action', controller.default_xslt_template
82
+ end
83
+
84
+ def test_page_xml
85
+ controller = MockController.new
86
+
87
+ controller.instance_eval do
88
+ @a = 'A'
89
+ @b = 42
90
+ end
91
+
92
+ dom = REXML::Document.new(xml = controller.page_xml(:root => 'page'))
93
+ assert top = dom.elements['/page'], "<page/> not in #{xml}"
94
+ assert a_node = dom.elements['/page/a'], "<a/> not in '#{xml}'"
95
+ assert_equal 'A', a_node.text, "'A' not in #{a_node.to_s}"
96
+ assert b_node = dom.elements['/page/b'], "<b/> not in '#{xml}'"
97
+ assert_equal '42', b_node.text, "'42' not in #{b_node.to_s}"
98
+ end
99
+
100
+ def test_page_xml_with_alternate_root
101
+ controller = MockController.new
102
+
103
+ controller.instance_eval do
104
+ @a = 'A'
105
+ @b = 'B'
106
+ end
107
+
108
+ dom = REXML::Document.new(xml = controller.page_xml(:root => "test"))
109
+ assert top = dom.elements['/test'], "<test/> not in #{xml}"
110
+ assert a_node = dom.elements['/test/a'], "<a/> not in '#{xml}'"
111
+ assert_equal 'A', a_node.text, "'A' not in #{a_node.to_s}"
112
+ assert b_node = dom.elements['/test/b'], "<b/> not in '#{xml}'"
113
+ assert_equal 'B', b_node.text, "'B' not in #{b_node.to_s}"
114
+ end
115
+
116
+
117
+ def test_xslt_render
118
+ controller = MockController.new(:action_name => 'test_action')
119
+
120
+ controller.instance_eval do
121
+ @some_interesting_value = 'interesting value'
122
+ end
123
+
124
+ controller.xslt_render
125
+
126
+ assert_xml_tag controller.response.body, :tag => 'dt', :content => 'some-interesting-value'
127
+ assert_xml_tag controller.response.body, :tag => 'dd/dl/dt', :content => 'interesting value'
128
+
129
+ controller = MockController.new(:action_name => 'test_action')
130
+ controller.instance_eval do
131
+ @some_other_value = 'other value'
132
+ end
133
+
134
+ controller.xslt_render
135
+
136
+ assert_xml_tag controller.response.body, :tag => 'dt', :content => 'some-other-value'
137
+ assert_xml_tag controller.response.body, :tag => 'dd/dl/dt', :content => 'other value'
138
+ end
139
+
140
+ def test_xslt_render_with_translation
141
+ controller = MockController.new(:action_name => 'test_translation')
142
+ use_test_translations
143
+
144
+ controller.xslt_render :language => 'en'
145
+
146
+ assert_match /Hello.*and.*English/, controller.response.body
147
+ end
148
+
149
+ def test_xslt_render_with_params
150
+ controller = MockController.new(:action_name => 'test_action')
151
+ use_test_translations
152
+
153
+ controller.xslt_render :language => 'en', :params => {:title => 'my page'}
154
+
155
+ assert_xml_tag controller.response.body, :tag => 'title', :content => 'my page'
156
+ end
157
+
158
+ def test_xslt_render_with_alternate_root
159
+ controller = MockController.new(:action_name => 'test_action')
160
+
161
+ controller.instance_eval do
162
+ @some_interesting_value = 'interesting value'
163
+ end
164
+
165
+ controller.xslt_render(:root => 'test')
166
+
167
+ assert_xml_tag controller.response.body, :tag => 'dt', :content => 'test'
168
+ end
169
+
170
+ def test_xslt_render_with_alternate_template
171
+ controller = MockController.new(:action_name => 'other_action')
172
+
173
+ controller.instance_eval do
174
+ @some_interesting_value = 'interesting value'
175
+ end
176
+
177
+ controller.xslt_render(:template => 'mock/test_action')
178
+
179
+ assert_xml_tag controller.response.body, :tag => 'dt', :content => 'page'
180
+ end
181
+
182
+ def test_render_throws_exception_for_missing_xslt
183
+ controller = MockController.new(:action_name => 'missing_action')
184
+
185
+ controller.instance_eval {@some_var = 'some val'}
186
+
187
+ assert_raises IOError do
188
+ controller.xslt_render
189
+ end
190
+ end
191
+
192
+ def test_render_raise_with_erroneous_action
193
+ controller = MockController.new(:action_name => 'erroneous_action')
194
+
195
+ controller.instance_eval {@some_var = 'some val'}
196
+
197
+ assert_raises XsltprocError do
198
+ controller.xslt_render
199
+ end
200
+ end
201
+
202
+ def test_html_from_xml_does_correct_transformation_and_uses_default_param
203
+ xml = '<some-interesting-value>interesting value</some-interesting-value>'
204
+ html = html_from_xml($test_root + '/mock/test_action.html.xsl', xml)
205
+ assert_xml_tag html, :tag => 'dt', :content => 'some-interesting-value'
206
+ assert_xml_tag html, :tag => 'dd/dl/dt', :content => 'interesting value'
207
+ assert_xml_tag html, :tag => 'title', :content => 'default'
208
+ end
209
+
210
+ def test_html_from_xml_sees_supplied_parameter
211
+ xml = '<some-interesting-value>interesting value</some-interesting-value>'
212
+ html = html_from_xml($test_root + '/mock/test_action.html.xsl', xml, 'title' => "a big deal")
213
+ assert_xml_tag html, :tag => 'dt', :content => 'some-interesting-value'
214
+ assert_xml_tag html, :tag => 'dd/dl/dt', :content => 'interesting value'
215
+ assert_xml_tag html, :tag => 'title', :content => 'a big deal'
216
+ end
217
+ end