Skip to content

Commit a14b02e

Browse files
author
Nick Schwaderer
committed
Add plural finder methods to shoes-spec (buttons, paras, drawables, etc.)
- Add pluralize_dsl_name helper for proper English pluralization - Singular finders (button, para, etc.) still raise if not exactly one match - Plural finders (buttons, paras, etc.) return arrays of all matches - Add drawables() as plural of drawable() - Works in both niente and webview display services - Add tests for plural finders in lacci/test/
1 parent a235e7e commit a14b02e

File tree

3 files changed

+146
-8
lines changed

3 files changed

+146
-8
lines changed

lacci/lib/scarpe/niente/shoes_spec.rb

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,21 @@ def self.run_shoes_spec_test_code(code, class_name: nil, test_name: nil)
3636
end
3737

3838
class Niente::ShoesSpecTest < Minitest::Test
39+
# Helper to pluralize DSL names
40+
def self.pluralize_dsl_name(name)
41+
# Handle common English pluralization rules
42+
case name
43+
when /box$/
44+
name + "es"
45+
else
46+
name + "s"
47+
end
48+
end
49+
3950
Shoes::Drawable.drawable_classes.each do |drawable_class|
4051
finder_name = drawable_class.dsl_name
4152

53+
# Singular finder - raises if not exactly one match
4254
define_method(finder_name) do |*args|
4355
drawables = Shoes::App.find_drawables_by(drawable_class, *args)
4456

@@ -47,13 +59,27 @@ class Niente::ShoesSpecTest < Minitest::Test
4759

4860
Niente::ShoesSpecProxy.new(drawables[0])
4961
end
62+
63+
# Plural finder - returns array of all matches
64+
plural_name = pluralize_dsl_name(finder_name)
65+
define_method(plural_name) do |*args|
66+
drawables = Shoes::App.find_drawables_by(drawable_class, *args)
67+
drawables.map { |d| Niente::ShoesSpecProxy.new(d) }
68+
end
5069
end
5170

71+
# Singular drawable finder
5272
def drawable(*specs)
53-
drawables = Shoes::App.find_drawables_by(*specs)
54-
raise Shoes::Errors::MultipleDrawablesFoundError, "Found more than one #{finder_name} matching #{args.inspect}!" if drawables.size > 1
55-
raise Shoes::Errors::NoDrawablesFoundError, "Found no #{finder_name} matching #{args.inspect}!" if drawables.empty?
56-
Niente::ShoesSpecProxy.new(drawables[0])
73+
found = Shoes::App.find_drawables_by(*specs)
74+
raise Shoes::Errors::MultipleDrawablesFoundError, "Found more than one drawable matching #{specs.inspect}!" if found.size > 1
75+
raise Shoes::Errors::NoDrawablesFoundError, "Found no drawable matching #{specs.inspect}!" if found.empty?
76+
Niente::ShoesSpecProxy.new(found[0])
77+
end
78+
79+
# Plural drawables finder - returns array of all matches
80+
def drawables(*specs)
81+
found = Shoes::App.find_drawables_by(*specs)
82+
found.map { |d| Niente::ShoesSpecProxy.new(d) }
5783
end
5884
end
5985

lacci/test/test_plural_finders.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "test_helper"
4+
5+
class TestPluralFinders < NienteTest
6+
def test_buttons_returns_all_buttons
7+
run_test_niente_code(<<~SHOES_APP, app_test_code: <<~SHOES_SPEC)
8+
Shoes.app do
9+
@b1 = button "Button One"
10+
@b2 = button "Button Two"
11+
@b3 = button "Button Three"
12+
end
13+
SHOES_APP
14+
all_buttons = buttons
15+
assert_equal 3, all_buttons.size
16+
assert all_buttons.all? { |b| b.is_a?(Niente::ShoesSpecProxy) }
17+
SHOES_SPEC
18+
end
19+
20+
def test_paras_returns_all_paras
21+
run_test_niente_code(<<~SHOES_APP, app_test_code: <<~SHOES_SPEC)
22+
Shoes.app do
23+
@p1 = para "Para One"
24+
@p2 = para "Para Two"
25+
end
26+
SHOES_APP
27+
all_paras = paras
28+
assert_equal 2, all_paras.size
29+
SHOES_SPEC
30+
end
31+
32+
def test_plural_finder_with_variable_filter
33+
run_test_niente_code(<<~SHOES_APP, app_test_code: <<~SHOES_SPEC)
34+
Shoes.app do
35+
@b1 = button "Button One"
36+
@b2 = button "Other"
37+
end
38+
SHOES_APP
39+
# Filter by variable name - should return just that one
40+
filtered = buttons("@b1")
41+
assert_equal 1, filtered.size
42+
assert_equal "Button One", filtered[0].text
43+
SHOES_SPEC
44+
end
45+
46+
def test_plural_finder_empty_result_bad_variable
47+
run_test_niente_code(<<~SHOES_APP, app_test_code: <<~SHOES_SPEC)
48+
Shoes.app do
49+
@b1 = button "Button One"
50+
end
51+
SHOES_APP
52+
# Non-existent variable returns empty array
53+
no_match = buttons("@nonexistent")
54+
assert_equal 0, no_match.size
55+
assert_equal [], no_match
56+
SHOES_SPEC
57+
end
58+
59+
def test_drawables_plural
60+
run_test_niente_code(<<~SHOES_APP, app_test_code: <<~SHOES_SPEC)
61+
Shoes.app do
62+
@b1 = button "One"
63+
@b2 = button "Two"
64+
@p1 = para "Text"
65+
end
66+
SHOES_APP
67+
all_buttons = drawables(Shoes::Button)
68+
assert_equal 2, all_buttons.size
69+
SHOES_SPEC
70+
end
71+
72+
def test_singular_still_works
73+
run_test_niente_code(<<~SHOES_APP, app_test_code: <<~SHOES_SPEC)
74+
Shoes.app do
75+
@b1 = button "Only One"
76+
end
77+
SHOES_APP
78+
b = button("@b1")
79+
assert_equal "Only One", b.text
80+
SHOES_SPEC
81+
end
82+
end

lib/scarpe/shoes_spec.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,21 @@ def respond_to_missing?(method_name, include_private = false)
254254
module Scarpe::ShoesSpecTest
255255
include Scarpe::Test::HTMLAssertions
256256

257+
# Helper to pluralize DSL names
258+
def self.pluralize_dsl_name(name)
259+
# Handle common English pluralization rules
260+
case name
261+
when /box$/
262+
name + "es"
263+
else
264+
name + "s"
265+
end
266+
end
267+
257268
Shoes::Drawable.drawable_classes.each do |drawable_class|
258269
finder_name = drawable_class.dsl_name
259270

271+
# Singular finder - raises if not exactly one match
260272
define_method(finder_name) do |*args|
261273
# Scarpe-Webview only supports a single Shoes::App instance
262274
app = Shoes.APPS[0]
@@ -267,13 +279,31 @@ module Scarpe::ShoesSpecTest
267279

268280
Scarpe::ShoesSpecProxy.new(drawables[0])
269281
end
282+
283+
# Plural finder - returns array of all matches
284+
plural_name = pluralize_dsl_name(finder_name)
285+
define_method(plural_name) do |*args|
286+
app = Shoes.APPS[0]
287+
drawables = app.find_drawables_by(drawable_class, *args)
288+
drawables.map { |d| Scarpe::ShoesSpecProxy.new(d) }
289+
end
270290
end
291+
292+
# Singular drawable finder
271293
def drawable(*specs)
272-
drawables = app.find_drawables_by(*specs)
273-
raise Scarpe::MultipleDrawablesFoundError, "Found more than one #{finder_name} matching #{args.inspect}!" if drawables.size > 1
274-
raise Scarpe::NoDrawablesFoundError, "Found no #{finder_name} matching #{args.inspect}!" if drawables.empty?
294+
app = Shoes.APPS[0]
295+
found = app.find_drawables_by(*specs)
296+
raise Shoes::Errors::MultipleDrawablesFoundError, "Found more than one drawable matching #{specs.inspect}!" if found.size > 1
297+
raise Shoes::Errors::NoDrawablesFoundError, "Found no drawable matching #{specs.inspect}!" if found.empty?
298+
299+
Scarpe::ShoesSpecProxy.new(found[0])
300+
end
275301

276-
Scarpe::ShoesSpecProxy.new(drawables[0])
302+
# Plural drawables finder - returns array of all matches
303+
def drawables(*specs)
304+
app = Shoes.APPS[0]
305+
found = app.find_drawables_by(*specs)
306+
found.map { |d| Scarpe::ShoesSpecProxy.new(d) }
277307
end
278308

279309
def catscradle_dsl(&block)

0 commit comments

Comments
 (0)