@@ -267,7 +267,7 @@ class IngredientTest < ActiveSupport::TestCase
267
267
assert_equal [ "existing snippet" ] , ingredient . snippets
268
268
end
269
269
270
- test "interpolates snippets into template content" do
270
+ test "Does not interpolate snippets into template content" do
271
271
ingredient = Ingredient . new (
272
272
name : "Test Ingredient" ,
273
273
category : "Testing" ,
@@ -277,35 +277,68 @@ class IngredientTest < ActiveSupport::TestCase
277
277
ingredient . new_snippets = [ '"foo"' , '"bar"' ]
278
278
ingredient . save!
279
279
280
- expected_content = 'say "This is my template"\ncreate_file "myfile.rb", "foo" \ncreate_file "my_other_file.rb", "bar" '
280
+ expected_content = 'say "This is my template"\ncreate_file "myfile.rb", {{1}} \ncreate_file "my_other_file.rb", {{2}} '
281
281
assert_equal expected_content , ingredient . template_content
282
282
end
283
283
284
- test "handles missing snippets gracefully " do
284
+ test "handles template without placeholders " do
285
285
ingredient = Ingredient . new (
286
286
name : "Test Ingredient" ,
287
287
category : "Testing" ,
288
- template_content : 'say "This is my template"\ncreate_file "myfile.rb", {{1}}\ncreate_file "my_other_file.rb", {{2}} ' ,
288
+ template_content : 'say "This is my template"' ,
289
289
created_by : users ( :john )
290
290
)
291
- ingredient . new_snippets = [ '"foo"' ] # Only one snippet provided
291
+ ingredient . new_snippets = [ '"foo"' , '"bar"' ]
292
292
ingredient . save!
293
293
294
- # The second placeholder should remain unchanged
295
- expected_content = 'say "This is my template"\ncreate_file "myfile.rb", "foo"\ncreate_file "my_other_file.rb", {{2}}'
296
- assert_equal expected_content , ingredient . template_content
294
+ assert_equal 'say "This is my template"' , ingredient . template_content
297
295
end
298
296
299
- test "handles template without placeholders" do
297
+ test "template_with_interpolated_snippets with no snippets returns original template" do
298
+ ingredient = Ingredient . new ( template_content : "some template" , snippets : [ ] )
299
+ assert_equal "some template" , ingredient . template_with_interpolated_snippets
300
+ end
301
+
302
+ test "template_with_interpolated_snippets interpolates single-line snippets" do
300
303
ingredient = Ingredient . new (
301
- name : "Test Ingredient" ,
302
- category : "Testing" ,
303
- template_content : 'say "This is my template"' ,
304
- created_by : users ( :john )
304
+ template_content : "First {{1}} and then {{2}}" ,
305
+ snippets : [ "hello" , "world" ]
305
306
)
306
- ingredient . new_snippets = [ '"foo"' , '"bar"' ]
307
- ingredient . save!
307
+ assert_equal "First \" hello \" and then \" world \" " , ingredient . template_with_interpolated_snippets
308
+ end
308
309
309
- assert_equal 'say "This is my template"' , ingredient . template_content
310
+ test "template_with_interpolated_snippets interpolates multi-line snippets" do
311
+ ingredient = Ingredient . new (
312
+ template_content : "Code:\n {{1}}\n More code:\n {{2}}" ,
313
+ snippets : [ "def foo\n puts 'bar'\n end" , "x = 1" ]
314
+ )
315
+ expected = <<~EXPECTED
316
+ Code:
317
+ <<~SNIPPET_1
318
+ def foo
319
+ puts 'bar'
320
+ end
321
+ SNIPPET_1
322
+
323
+ More code:
324
+ "x = 1"
325
+ EXPECTED
326
+ assert_equal expected . chomp , ingredient . template_with_interpolated_snippets
327
+ end
328
+
329
+ test "template_with_interpolated_snippets preserves heredoc snippets" do
330
+ ingredient = Ingredient . new (
331
+ template_content : "Code: {{1}}" ,
332
+ snippets : [ "<<~SQL\n SELECT * FROM users\n SQL" ]
333
+ )
334
+ assert_equal "Code: <<~SQL\n SELECT * FROM users\n SQL" , ingredient . template_with_interpolated_snippets
335
+ end
336
+
337
+ test "template_with_interpolated_snippets preserves quoted snippets" do
338
+ ingredient = Ingredient . new (
339
+ template_content : "Text: {{1}}" ,
340
+ snippets : [ "'already quoted'" ]
341
+ )
342
+ assert_equal "Text: 'already quoted'" , ingredient . template_with_interpolated_snippets
310
343
end
311
344
end
0 commit comments