Skip to content

Commit

Permalink
Merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
ericproulx committed Apr 1, 2024
2 parents 14c4610 + a48754c commit 8cdf1aa
Show file tree
Hide file tree
Showing 22 changed files with 482 additions and 773 deletions.
24 changes: 23 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ inherit_from: .rubocop_todo.yml
Layout/LineLength:
Max: 215

Lint/EmptyBlock:
Exclude:
- spec/**/*_spec.rb

Style/Documentation:
Enabled: false

Expand Down Expand Up @@ -60,4 +64,22 @@ RSpec/ExampleLength:
Max: 60

RSpec/NestedGroups:
Max: 4
Max: 6

RSpec/FilePath:
SpecSuffixOnly: true

RSpec/SpecFilePathFormat:
Enabled: false

RSpec/MultipleExpectations:
Enabled: false

RSpec/NamedSubject:
Enabled: false

RSpec/MultipleMemoizedHelpers:
Max: 11

RSpec/ContextWording:
Enabled: false
402 changes: 14 additions & 388 deletions .rubocop_todo.yml

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions spec/grape/api/defines_boolean_in_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@

describe Grape::API::Instance do
describe 'boolean constant' do
module DefinesBooleanInstanceSpec
class API < Grape::API
let(:app) do
Class.new(Grape::API) do
params do
requires :message, type: Boolean
requires :message, type: Grape::API::Boolean
end
post :echo do
{ class: params[:message].class.name, value: params[:message] }
end
end
end

def app
DefinesBooleanInstanceSpec::API
end

let(:expected_body) do
{ class: 'TrueClass', value: true }.to_s
end
Expand All @@ -28,7 +24,7 @@ def app
end

context 'Params endpoint type' do
subject { DefinesBooleanInstanceSpec::API.new.router.map['POST'].first.options[:params]['message'][:type] }
subject { app.new.router.map['POST'].first.options[:params]['message'][:type] }

it 'params type is a boolean' do
expect(subject).to eq 'Grape::API::Boolean'
Expand Down
30 changes: 14 additions & 16 deletions spec/grape/api/inherited_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
describe Grape::API::Helpers do
let(:user) { 'Miguel Caneo' }
let(:id) { '42' }

module InheritedHelpersSpec
class SuperClass < Grape::API
let(:api_super_class) do
Class.new(Grape::API) do
helpers do
params(:superclass_params) { requires :id, type: String }

Expand All @@ -14,8 +13,9 @@ def current_user
end
end
end

class OverriddenSubClass < SuperClass
end
let(:api_overridden_sub_class) do
Class.new(api_super_class) do
params { use :superclass_params }

helpers do
Expand All @@ -28,16 +28,18 @@ def current_user
"#{current_user}: #{params['id']}"
end
end

class SubClass < SuperClass
end
let(:api_sub_class) do
Class.new(api_super_class) do
params { use :superclass_params }

get 'resource' do
"#{current_user}: #{params['id']}"
end
end

class Example < SubClass
end
let(:api_example) do
Class.new(api_sub_class) do
params { use :superclass_params }

get 'resource' do
Expand All @@ -47,7 +49,7 @@ class Example < SubClass
end

context 'non overriding subclass' do
subject { InheritedHelpersSpec::SubClass }
subject { api_sub_class }

def app
subject
Expand All @@ -69,10 +71,8 @@ def app
end

context 'overriding subclass' do
subject { InheritedHelpersSpec::OverriddenSubClass }

def app
subject
api_overridden_sub_class
end

context 'given expected params' do
Expand All @@ -91,10 +91,8 @@ def app
end

context 'example subclass' do
subject { InheritedHelpersSpec::Example }

def app
subject
api_example
end

context 'given expected params' do
Expand Down
165 changes: 102 additions & 63 deletions spec/grape/api/mount_and_helpers_order_spec.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
# frozen_string_literal: true

describe Grape::API do
def app
subject
end

describe 'rescue_from' do
context 'when the API is mounted AFTER defining the class rescue_from handler' do
class APIRescueFrom < Grape::API
rescue_from :all do
error!({ type: 'all' }, 404)
end

get do
{ count: 1 / 0 }
let(:api_rescue_from) do
Class.new(Grape::API) do
rescue_from :all do
error!({ type: 'all' }, 404)
end

get do
{ count: 1 / 0 }
end
end
end

class MainRescueFromAfter < Grape::API
rescue_from ZeroDivisionError do
error!({ type: 'zero' }, 500)
end
let(:main_rescue_from_after) do
context = self

mount APIRescueFrom
Class.new(Grape::API) do
rescue_from ZeroDivisionError do
error!({ type: 'zero' }, 500)
end

mount context.api_rescue_from
end
end

subject { MainRescueFromAfter }
def app
main_rescue_from_after
end

it 'is rescued by the rescue_from ZeroDivisionError handler from Main class' do
get '/'
Expand All @@ -36,25 +40,32 @@ class MainRescueFromAfter < Grape::API
end

context 'when the API is mounted BEFORE defining the class rescue_from handler' do
class APIRescueFrom < Grape::API
rescue_from :all do
error!({ type: 'all' }, 404)
end

get do
{ count: 1 / 0 }
let(:api_rescue_from) do
Class.new(Grape::API) do
rescue_from :all do
error!({ type: 'all' }, 404)
end

get do
{ count: 1 / 0 }
end
end
end
let(:main_rescue_from_before) do
context = self

class MainRescueFromBefore < Grape::API
mount APIRescueFrom
Class.new(Grape::API) do
mount context.api_rescue_from

rescue_from ZeroDivisionError do
error!({ type: 'zero' }, 500)
rescue_from ZeroDivisionError do
error!({ type: 'zero' }, 500)
end
end
end

subject { MainRescueFromBefore }
def app
main_rescue_from_before
end

it 'is rescued by the rescue_from ZeroDivisionError handler from Main class' do
get '/'
Expand All @@ -67,21 +78,28 @@ class MainRescueFromBefore < Grape::API

describe 'before' do
context 'when the API is mounted AFTER defining the before helper' do
class APIBeforeHandler < Grape::API
get do
{ count: @count }.to_json
let(:api_before_handler) do
Class.new(Grape::API) do
get do
{ count: @count }.to_json
end
end
end
let(:main_before_handler_after) do
context = self

class MainBeforeHandlerAfter < Grape::API
before do
@count = 1
end
Class.new(Grape::API) do
before do
@count = 1
end

mount APIBeforeHandler
mount context.api_before_handler
end
end

subject { MainBeforeHandlerAfter }
def app
main_before_handler_after
end

it 'is able to access the variables defined in the before helper' do
get '/'
Expand All @@ -92,21 +110,28 @@ class MainBeforeHandlerAfter < Grape::API
end

context 'when the API is mounted BEFORE defining the before helper' do
class APIBeforeHandler < Grape::API
get do
{ count: @count }.to_json
let(:api_before_handler) do
Class.new(Grape::API) do
get do
{ count: @count }.to_json
end
end
end
let(:main_before_handler_before) do
context = self

class MainBeforeHandlerBefore < Grape::API
mount APIBeforeHandler
Class.new(Grape::API) do
mount context.api_before_handler

before do
@count = 1
before do
@count = 1
end
end
end

subject { MainBeforeHandlerBefore }
def app
main_before_handler_before
end

it 'is able to access the variables defined in the before helper' do
get '/'
Expand All @@ -119,21 +144,28 @@ class MainBeforeHandlerBefore < Grape::API

describe 'after' do
context 'when the API is mounted AFTER defining the after handler' do
class APIAfterHandler < Grape::API
get do
{ count: 1 }.to_json
let(:api_after_handler) do
Class.new(Grape::API) do
get do
{ count: 1 }.to_json
end
end
end
let(:main_after_handler_after) do
context = self

class MainAfterHandlerAfter < Grape::API
after do
error!({ type: 'after' }, 500)
end
Class.new(Grape::API) do
after do
error!({ type: 'after' }, 500)
end

mount APIAfterHandler
mount context.api_after_handler
end
end

subject { MainAfterHandlerAfter }
def app
main_after_handler_after
end

it 'is able to access the variables defined in the after helper' do
get '/'
Expand All @@ -144,21 +176,28 @@ class MainAfterHandlerAfter < Grape::API
end

context 'when the API is mounted BEFORE defining the after helper' do
class APIAfterHandler < Grape::API
get do
{ count: 1 }.to_json
let(:api_after_handler) do
Class.new(Grape::API) do
get do
{ count: 1 }.to_json
end
end
end
let(:main_after_handler_before) do
context = self

class MainAfterHandlerBefore < Grape::API
mount APIAfterHandler
Class.new(Grape::API) do
mount context.api_after_handler

after do
error!({ type: 'after' }, 500)
after do
error!({ type: 'after' }, 500)
end
end
end

subject { MainAfterHandlerBefore }
def app
main_after_handler_before
end

it 'is able to access the variables defined in the after helper' do
get '/'
Expand Down
Loading

0 comments on commit 8cdf1aa

Please sign in to comment.