Skip to content

Commit 01673f0

Browse files
committed
Add tests for remaining exec_with_object call paths
Cover single-argument blocks in expose_nil condition and callable :as option. Fix misleading test context names that said "lambda" for do...end blocks which are procs.
1 parent 80cb2f9 commit 01673f0

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

spec/grape_entity/entity_spec.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
expect { subject.expose :name, as: :foo }.not_to raise_error
2828
end
2929

30+
it 'supports callable :as with single-argument lambda' do
31+
subject.expose :name, as: ->(obj) { obj[:name].upcase }
32+
expect(subject.represent({ name: 'test' }).serializable_hash).to eq('TEST' => 'test')
33+
end
34+
3035
it 'makes sure that :format_with as a proc cannot be used with a block' do
3136
# rubocop:disable Style/BlockDelimiters
3237
expect {
@@ -148,6 +153,12 @@ def initialize(a, b, c)
148153
subject.expose(:c)
149154
expect(subject.represent(model, option_a: 100).serializable_hash).to eq(a: 100, b: nil, c: 'value')
150155
end
156+
157+
it 'works with single-argument block' do
158+
subject.expose(:a, expose_nil: false) { |obj| obj.c }
159+
subject.expose(:b)
160+
expect(subject.represent(model).serializable_hash).to eq(a: 'value', b: nil)
161+
end
151162
end
152163
end
153164

@@ -477,7 +488,7 @@ def raises_argument_error
477488
end
478489
end
479490

480-
context 'with single-argument lambda' do
491+
context 'with single-argument block' do
481492
it 'passes only the object without raising ArgumentError' do
482493
subject.expose :that_method_without_args do |object|
483494
object.method_without_args
@@ -489,7 +500,7 @@ def raises_argument_error
489500
end
490501
end
491502

492-
context 'with two-argument lambda' do
503+
context 'with two-argument block' do
493504
it 'passes the object and options without raising ArgumentError' do
494505
subject.expose :that_method_without_args do |object, _options|
495506
object.method_without_args
@@ -501,7 +512,7 @@ def raises_argument_error
501512
end
502513
end
503514

504-
context 'with splat-argument lambda' do
515+
context 'with splat-argument block' do
505516
it 'passes the object and options' do
506517
subject.expose :args_count do |*args|
507518
args.size

0 commit comments

Comments
 (0)