Skip to content

Commit

Permalink
Don't define constants in blocks
Browse files Browse the repository at this point in the history
Blocks will leak out these constants to global scope
  • Loading branch information
jnbt committed Feb 24, 2023
1 parent 194372d commit a9f1596
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 97 deletions.
16 changes: 9 additions & 7 deletions spec/app_store/subscription_verification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,21 @@

private

DummyClient = Struct.new(:response) do
attr_reader :receipt_data, :secret
let(:dummy_client_class) do
Struct.new(:response) do
attr_reader :receipt_data, :secret

def verify(receipt_data, secret)
@receipt_data = receipt_data
@secret = secret
response
def verify(receipt_data, secret)
@receipt_data = receipt_data
@secret = secret
response
end
end
end

def with_mocked_response(response)
recorded = []
dummy = DummyClient.new(response)
dummy = dummy_client_class.new(response)
stub = proc do |*args|
recorded << args
dummy
Expand Down
14 changes: 13 additions & 1 deletion spec/app_store/verification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,21 @@

private

let(:dummy_client_class) do
Struct.new(:response) do
attr_reader :receipt_data, :secret

def verify(receipt_data, secret)
@receipt_data = receipt_data
@secret = secret
response
end
end
end

def with_mocked_response(response)
recorded = []
dummy = DummyClient.new(response)
dummy = dummy_client_class.new(response)
stub = proc do |*args|
recorded << args
dummy
Expand Down
28 changes: 11 additions & 17 deletions spec/app_store/verifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,21 @@

private

let(:dummy_verification_class) do
Struct.new(:endpoint, :data, :secret, :product_ids) do
attr_accessor :results

def call!
results.shift
end
end
end

def with_mocked_verifier(*results, &block)
@recorded ||= []
stub = proc do |*args|
@recorded << args
DummyAppStoreVerification.new(*args).tap { |v| v.results = results }
dummy_verification_class.new(*args).tap { |v| v.results = results }
end
CandyCheck::AppStore::Verification.stub :new, stub, &block
end
Expand All @@ -141,20 +151,4 @@ def assert_recorded(*calls)
def get_failure(code)
CandyCheck::AppStore::VerificationFailure.fetch(code)
end

class DummyAppStoreVerification
attr_reader :endpoint, :data, :secret, :product_ids
attr_accessor :results

def initialize(endpoint, data, secret, product_ids = nil)
@endpoint = endpoint
@data = data
@secret = secret
@product_ids = product_ids
end

def call!
results.shift
end
end
end
23 changes: 11 additions & 12 deletions spec/cli/commands/app_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@
environment: :sandbox,
}
end
let(:dummy_verifier_class) do
Struct.new(:config) do
attr_reader :arguments

def verify(*arguments)
@arguments = arguments
{ result: :stubbed }
end
end
end

before do
stub = proc do |*args|
@verifier = DummyAppStoreVerifier.new(*args)
@verifier = dummy_verifier_class.new(*args)
end
CandyCheck::AppStore::Verifier.stub :new, stub do
run_command!
Expand Down Expand Up @@ -42,15 +52,4 @@
_(out.lines).must_equal ["Hash:", { result: :stubbed }]
end
end

private

DummyAppStoreVerifier = Struct.new(:config) do
attr_reader :arguments

def verify(*arguments)
@arguments = arguments
{ result: :stubbed }
end
end
end
27 changes: 3 additions & 24 deletions spec/play_store/product_purchases/product_purchase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

describe "valid and non-consumed product" do
let(:fake_product_purchase) do
FakeProductPurchase.new(
OpenStruct.new(
consumption_state: 0,
developer_payload: "payload that gets stored and returned",
kind: "androidpublisher#productPurchase",
Expand Down Expand Up @@ -51,7 +51,7 @@

describe "valid and consumed product" do
let(:fake_product_purchase) do
FakeProductPurchase.new(
OpenStruct.new(
consumption_state: 1,
developer_payload: "payload that gets stored and returned",
kind: "androidpublisher#productPurchase",
Expand All @@ -72,7 +72,7 @@

describe "non-valid product" do
let(:fake_product_purchase) do
FakeProductPurchase.new(
OpenStruct.new(
consumption_state: 0,
developer_payload: "payload that gets stored and returned",
kind: "androidpublisher#productPurchase",
Expand All @@ -86,25 +86,4 @@
_(subject.valid?).must_be_false
end
end

private

class FakeProductPurchase
FIELDS = %i(
consumption_state
developer_payload
kind
order_id
purchase_state
purchase_time_millis
).freeze

attr_accessor(*FIELDS)

def initialize(hash)
FIELDS.each do |key|
public_send("#{key}=", hash[key])
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

describe "expired and canceled subscription" do
let(:fake_subscription_purchase) do
FakeSubscriptionPurchase.new(
OpenStruct.new(
kind: "androidpublisher#subscriptionPurchase",
start_time_millis: 1_459_540_113_244,
expiry_time_millis: 1_462_132_088_610,
Expand Down Expand Up @@ -66,8 +66,9 @@

describe "unexpired and renewing subscription" do
two_days_from_now = DateTime.now + 2

let(:fake_subscription_purchase) do
FakeSubscriptionPurchase.new(
OpenStruct.new(
kind: "androidpublisher#subscriptionPurchase",
start_time_millis: 1_459_540_113_244,
expiry_time_millis: (two_days_from_now.to_time.to_i * 1000),
Expand All @@ -89,7 +90,7 @@

describe "expired due to payment failure" do
let(:fake_subscription_purchase) do
FakeSubscriptionPurchase.new(
OpenStruct.new(
kind: "androidpublisher#subscriptionPurchase",
start_time_millis: 1_459_540_113_244,
expiry_time_millis: 1_462_132_088_610,
Expand All @@ -112,7 +113,7 @@
describe "subscription cancelation by user" do
describe "when subscription is not canceled" do
let(:fake_subscription_purchase) do
FakeSubscriptionPurchase.new(
OpenStruct.new(
kind: "androidpublisher#subscriptionPurchase",
start_time_millis: 1_459_540_113_244,
expiry_time_millis: 1_462_132_088_610,
Expand All @@ -137,7 +138,7 @@

describe "when subscription is canceled" do
let(:fake_subscription_purchase) do
FakeSubscriptionPurchase.new(
OpenStruct.new(
kind: "androidpublisher#subscriptionPurchase",
start_time_millis: 1_459_540_113_244,
expiry_time_millis: 1_462_132_088_610,
Expand Down Expand Up @@ -166,7 +167,7 @@

describe "expired with pending payment" do
let(:fake_subscription_purchase) do
FakeSubscriptionPurchase.new(
OpenStruct.new(
kind: "androidpublisher#subscriptionPurchase",
start_time_millis: 1_459_540_113_244,
expiry_time_millis: 1_462_132_088_610,
Expand All @@ -188,7 +189,7 @@

describe "trial" do
let(:fake_subscription_purchase) do
FakeSubscriptionPurchase.new(
OpenStruct.new(
kind: "androidpublisher#subscriptionPurchase",
start_time_millis: 1_459_540_113_244,
expiry_time_millis: 1_462_132_088_610,
Expand All @@ -209,29 +210,4 @@
_(subject.price_currency_code).must_equal "SOMECODE"
end
end

private

class FakeSubscriptionPurchase
FIELDS = %i(
kind
start_time_millis
expiry_time_millis
user_cancellation_time_millis
auto_renewing
developer_payload
cancel_reason
payment_state
price_amount_micros
price_currency_code
).freeze

attr_accessor(*FIELDS)

def initialize(hash)
FIELDS.each do |key|
public_send("#{key}=", hash[key])
end
end
end
end
10 changes: 6 additions & 4 deletions spec/play_store/verification_failure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
describe CandyCheck::PlayStore::VerificationFailure do
subject { CandyCheck::PlayStore::VerificationFailure.new(fake_error) }

let(:fake_error_class) do
Struct.new(:status_code, :message)
end

describe "denied" do
let(:fake_error) do
FakeError.new("401", "The current user has insufficient permissions")
fake_error_class.new("401", "The current user has insufficient permissions")
end

it "returns the code" do
Expand All @@ -19,7 +23,7 @@

describe "empty" do
let(:fake_error) do
FakeError.new(nil, nil)
fake_error_class.new(nil, nil)
end

it "returns an unknown code" do
Expand All @@ -30,6 +34,4 @@
_(subject.message).must_equal "Unknown error"
end
end

FakeError = Struct.new(:status_code, :message)
end

0 comments on commit a9f1596

Please sign in to comment.