Skip to content

Commit 0ed9737

Browse files
committed
Fix some tests and add more tests for the snippet interpolation logic
1 parent d62bdd8 commit 0ed9737

File tree

5 files changed

+61
-25
lines changed

5 files changed

+61
-25
lines changed

app/models/ingredient.rb

-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ def dependencies_satisfied?(recipe)
6363
requires.all? { |dep| recipe.ingredients.any? { |i| i.name == dep } }
6464
end
6565

66-
def template_with_interpolated_snippets
67-
interpolate_snippets
68-
template_content
69-
end
70-
7166
def configuration_for(configuration)
7267
# Validate configuration against configures_with schema
7368
configures_with.each do |key, validator|

app/views/ingredients/_form.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%= form_with(model: ingredient, class: "space-y-8 divide-y divide-gray-200" do |f| %>
1+
<%= form_with(model: ingredient, class: "space-y-8 divide-y divide-gray-200") do |f| %>
22
<% if ingredient.errors.any? %>
33
<div class="rounded-md bg-red-50 p-4">
44
<div class="flex">

test/models/ingredient_test.rb

+49-16
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class IngredientTest < ActiveSupport::TestCase
267267
assert_equal [ "existing snippet" ], ingredient.snippets
268268
end
269269

270-
test "interpolates snippets into template content" do
270+
test "Does not interpolate snippets into template content" do
271271
ingredient = Ingredient.new(
272272
name: "Test Ingredient",
273273
category: "Testing",
@@ -277,35 +277,68 @@ class IngredientTest < ActiveSupport::TestCase
277277
ingredient.new_snippets = [ '"foo"', '"bar"' ]
278278
ingredient.save!
279279

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}}'
281281
assert_equal expected_content, ingredient.template_content
282282
end
283283

284-
test "handles missing snippets gracefully" do
284+
test "handles template without placeholders" do
285285
ingredient = Ingredient.new(
286286
name: "Test Ingredient",
287287
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"',
289289
created_by: users(:john)
290290
)
291-
ingredient.new_snippets = [ '"foo"' ] # Only one snippet provided
291+
ingredient.new_snippets = [ '"foo"', '"bar"' ]
292292
ingredient.save!
293293

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
297295
end
298296

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
300303
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" ]
305306
)
306-
ingredient.new_snippets = [ '"foo"', '"bar"' ]
307-
ingredient.save!
307+
assert_equal "First \"hello\" and then \"world\"", ingredient.template_with_interpolated_snippets
308+
end
308309

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}}\nMore code:\n{{2}}",
313+
snippets: [ "def foo\n puts 'bar'\nend", "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\nSELECT * FROM users\nSQL" ]
333+
)
334+
assert_equal "Code: <<~SQL\nSELECT * FROM users\nSQL", 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
310343
end
311344
end

test/services/data_repository_service_test.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ def setup
8888
end
8989

9090
test "writes ingredient to repository" do
91-
ingredient = Data.define(:name, :template_content, :created_by, :id).new(
91+
ingredient = Data.define(:name, :template_content, :created_by, :id) do
92+
def template_with_interpolated_snippets
93+
template_content
94+
end
95+
end.new(
9296
name: "test_ingredient",
9397
template_content: "# Test template",
9498
created_by: @user,
@@ -274,7 +278,11 @@ def update(head_commit_sha: nil)
274278
end
275279

276280
test "raises error when writing ingredient template to local filesystem fails" do
277-
ingredient = Data.define(:name, :template_content, :created_by, :id).new(
281+
ingredient = Data.define(:name, :template_content, :created_by, :id) do
282+
def template_with_interpolated_snippets
283+
template_content
284+
end
285+
end.new(
278286
name: "test_ingredient",
279287
template_content: "# Test template",
280288
created_by: @user,

test/system/ingredients_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class IngredientsTest < ApplicationSystemTestCase
5757
fill_in "Description", with: "A test ingredient description"
5858

5959
page.execute_script(<<~JS)
60-
document.querySelector('.CodeMirror').CodeMirror.setValue("gem 'test_gem'")
60+
document.querySelector('textarea[name="ingredient[template_content]"] + div[class*="CodeMirror"]').CodeMirror.setValue("gem 'test_gem'")
6161
JS
6262

6363
fill_in "Category", with: "Authentication"

0 commit comments

Comments
 (0)