@@ -148,22 +148,83 @@ def test_templates_with_optional_locale_shares_common_object
148148
149149 def test_templates_sort_by_formats_json_first
150150 with_file "test/hello_world.html.erb" , "Hello HTML!"
151- with_file "test/hello_world.json.jbuilder " , "Hello JSON!"
151+ with_file "test/hello_world.json.builder " , "Hello JSON!"
152152
153- templates = resolver . find_all ( "hello_world" , "test" , false , locale : [ ] , formats : [ :json , :html ] , variants : :any , handlers : [ :erb , :jbuilder ] )
153+ templates = resolver . find_all ( "hello_world" , "test" , false , locale : [ ] , formats : [ :json , :html ] , variants : :any , handlers : [ :erb , :builder ] )
154154
155155 assert_equal 2 , templates . size
156156 assert_equal "Hello JSON!" , templates [ 0 ] . source
157+ assert_equal :json , templates [ 0 ] . format
158+ assert_equal "Hello HTML!" , templates [ 1 ] . source
159+ assert_equal :html , templates [ 1 ] . format
157160 end
158161
159162 def test_templates_sort_by_formats_html_first
160163 with_file "test/hello_world.html.erb" , "Hello HTML!"
161- with_file "test/hello_world.json.jbuilder " , "Hello JSON!"
164+ with_file "test/hello_world.json.builder " , "Hello JSON!"
162165
163- templates = resolver . find_all ( "hello_world" , "test" , false , locale : [ ] , formats : [ :html , :json ] , variants : :any , handlers : [ :erb , :jbuilder ] )
166+ templates = resolver . find_all ( "hello_world" , "test" , false , locale : [ ] , formats : [ :html , :json ] , variants : :any , handlers : [ :erb , :builder ] )
164167
165168 assert_equal 2 , templates . size
166169 assert_equal "Hello HTML!" , templates [ 0 ] . source
170+ assert_equal :html , templates [ 0 ] . format
171+ assert_equal "Hello JSON!" , templates [ 1 ] . source
172+ assert_equal :json , templates [ 1 ] . format
173+ end
174+
175+ def test_templates_with_variant
176+ with_file "test/hello_world.html+mobile.erb" , "Hello HTML!"
177+
178+ templates = resolver . find_all ( "hello_world" , "test" , false , locale : [ ] , formats : [ :html , :json ] , variants : :any , handlers : [ :erb , :builder ] )
179+
180+ assert_equal 1 , templates . size
181+ assert_equal "Hello HTML!" , templates [ 0 ] . source
182+ assert_kind_of ActionView ::Template ::Handlers ::ERB , templates [ 0 ] . handler
183+ assert_equal :html , templates [ 0 ] . format
184+ assert_equal "mobile" , templates [ 0 ] . variant
185+ end
186+
187+ def test_finds_variants_in_order
188+ with_file "test/hello_world.html+tricorder.erb" , "Hello Spock!"
189+ with_file "test/hello_world.html+lcars.erb" , "Hello Geordi!"
190+
191+ tricorder = context . find ( "hello_world" , "test" , false , [ ] , { variants : [ :tricorder ] } )
192+ lcars = context . find ( "hello_world" , "test" , false , [ ] , { variants : [ :lcars ] } )
193+
194+ assert_equal "Hello Spock!" , tricorder . source
195+ assert_equal "tricorder" , tricorder . variant
196+ assert_equal "Hello Geordi!" , lcars . source
197+ assert_equal "lcars" , lcars . variant
198+
199+ templates = context . find_all ( "hello_world" , "test" , false , [ ] , { variants : [ :tricorder , :lcars ] } )
200+ assert_equal [ tricorder , lcars ] , templates
201+
202+ templates = context . find_all ( "hello_world" , "test" , false , [ ] , { variants : [ :lcars , :tricorder ] } )
203+ assert_equal [ lcars , tricorder ] , templates
204+ end
205+
206+ def test_templates_no_format_with_variant
207+ with_file "test/hello_world+mobile.erb" , "Hello HTML!"
208+
209+ templates = resolver . find_all ( "hello_world" , "test" , false , locale : [ ] , formats : [ :html , :json ] , variants : :any , handlers : [ :erb , :builder ] )
210+
211+ assert_equal 1 , templates . size
212+ assert_equal "Hello HTML!" , templates [ 0 ] . source
213+ assert_kind_of ActionView ::Template ::Handlers ::ERB , templates [ 0 ] . handler
214+ assert_nil templates [ 0 ] . format
215+ assert_equal "mobile" , templates [ 0 ] . variant
216+ end
217+
218+ def test_templates_no_format_or_handler_with_variant
219+ with_file "test/hello_world+mobile" , "Hello HTML!"
220+
221+ templates = resolver . find_all ( "hello_world" , "test" , false , locale : [ ] , formats : [ :html , :json ] , variants : :any , handlers : [ :erb , :builder ] )
222+
223+ assert_equal 1 , templates . size
224+ assert_equal "Hello HTML!" , templates [ 0 ] . source
225+ assert_kind_of ActionView ::Template ::Handlers ::Raw , templates [ 0 ] . handler
226+ assert_nil templates [ 0 ] . format
227+ assert_equal "mobile" , templates [ 0 ] . variant
167228 end
168229
169230 def test_virtual_path_is_preserved_with_dot
0 commit comments