From 55953bef94e3a8760dd8f60c7d18cbe7fbcb8a51 Mon Sep 17 00:00:00 2001 From: Cody Molho Date: Mon, 5 Mar 2018 17:45:47 -0600 Subject: [PATCH 1/3] finished part 1 refund transaction and tests --- lib/retail_transaction.rb | 5 ++++ test/retail_transaction_test.rb | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/lib/retail_transaction.rb b/lib/retail_transaction.rb index 1bfa0e3..545e1f0 100644 --- a/lib/retail_transaction.rb +++ b/lib/retail_transaction.rb @@ -39,6 +39,7 @@ def paid? state :processing_payment state :payment_declined state :settled + state :refunded event :check_out do transitions from: :ringing_up, to: :collecting_payment, @@ -61,5 +62,9 @@ def paid? event :payment_declined do transitions from: :processing_payment, to: :payment_declined end + + event :refund do + transitions from: :settled, to: :refunded + end end end diff --git a/test/retail_transaction_test.rb b/test/retail_transaction_test.rb index 823cbfe..ccb232e 100644 --- a/test/retail_transaction_test.rb +++ b/test/retail_transaction_test.rb @@ -64,6 +64,13 @@ assert_equal false, tx.collecting_payment? assert_equal true, tx.processing_payment? end + + # Cannot refund from collecting_payment state + it "cannot be refunded from collecting_payment" do + assert_raises do + tx.refund! + end + end end describe "processing payment" do @@ -138,6 +145,13 @@ assert_equal false, tx.payment_declined? assert_equal true, tx.processing_payment? end + + # Cannot refund from declined state + it "cannot be refunded from declined" do + assert_raises do + tx.refund! + end + end end describe "that is settled" do @@ -152,5 +166,39 @@ it "cannot be reopened" do assert_invalid_transition { tx.reopen! } end + + # Settled order can be refunded + it "can be refunded" do + tx.refund! + assert_equal true, tx.refunded? + end + end + + # Test orders that have been refunded + describe "that is refunded" do + before(:each) do + tx.add_item("bobcat") + tx.check_out! + tx.payment_info = "15 cents and a nail" + tx.process_payment! + tx.payment_authorized! + tx.refund! + end + + it "has been refunded" do + assert_equal true, tx.refunded? + end + + it "cannot be refunded again" do + assert_raises do + tx.refund! + end + end + + it "cannot be reopened" do + assert_raises do + tx.reopen! + end + end end end From 102185ba873dba735cf131f4283d0b696e3f1fd2 Mon Sep 17 00:00:00 2001 From: Cody Molho Date: Mon, 5 Mar 2018 18:39:32 -0600 Subject: [PATCH 2/3] finished part 2 desugaring except a couple I'm uncertain about --- lib/desugaring.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/desugaring.rb b/lib/desugaring.rb index 8d52908..3ffd51f 100644 --- a/lib/desugaring.rb +++ b/lib/desugaring.rb @@ -21,7 +21,7 @@ def all_the_sugar(recipients, event, message) # Copy the contents of the previous method here and remove this sugar. # def desugared_poetry(recipients, event, message) - implement_me! + mail(message, to: recipients.map(&:email), subject: "You’re invited to #{event.title} on #{event.date}") end # Ruby allows you to pass arguments identified by name instead of just by position. They are really just @@ -36,7 +36,7 @@ def desugared_poetry(recipients, event, message) # Copy the contents of the previous method here and remove this sugar. # def desugared_named_args(recipients, event, message) - implement_me! + mail(message, {to: recipients.map(&:email), subject: "You’re invited to #{event.title} on #{event.date}"}) end # Ruby’s general syntax for hashes is `{key => value, key => value, ...}`. Because it is so common to use @@ -51,7 +51,7 @@ def desugared_named_args(recipients, event, message) # Copy the contents of the previous method here and remove this sugar. # def desugared_symbol_keys(recipients, event, message) - implement_me! + mail(message, {:to => recipients.map(&:email), :subject => "You’re invited to #{event.title} on #{event.date}"}) end # You may be wondering how `map(&:email)` works. When you precede the last argument of a method call with @@ -70,7 +70,7 @@ def desugared_symbol_keys(recipients, event, message) # Copy the contents of the previous method here and remove this sugar. # def desugared_attr_proc(recipients, event, message) - implement_me! + mail(message, {:to => recipients.map { |x| x.email }, :subject => "You’re invited to #{event.title} on #{event.date}"}) end # You may recall from the Ruby koans that when you put `#{something}` in a `"`-delimited string, Ruby will @@ -88,7 +88,7 @@ def desugared_attr_proc(recipients, event, message) # Copy the contents of the previous method here and remove this sugar. # def desugared_interpolation(recipients, event, message) - implement_me! + mail(message, {:to => recipients.map { |x| x.email }, :subject => "You’re invited to " + event.title.to_s + " on " + event.date.to_s}) end # Ruby tracks local variables lexically at compile time. Wherever you say `x = y`, the compiler assumes that @@ -109,8 +109,9 @@ def desugared_interpolation(recipients, event, message) # Copy the contents of the previous method here and remove this sugar. # (Think: which names are local variables, and which are not?) # + # TODO Confused on this one def desugared_implicit_self(recipients, event, message) - implement_me! + self.mail(message, {:to => recipients.map { |x| x.email }, :subject => "You’re invited to " + event.title.to_s + " on " + event.date.to_s}) end # In Ruby, unlike Python, there are no properties distinct from method calls. When you say `x.y`, you are @@ -131,7 +132,7 @@ def desugared_implicit_self(recipients, event, message) # but structurally quite similar! # def desugared_implicit_parens(recipients, event, message) - implement_me! + self.mail(message, {:to => recipients.map() { |x| x.email }, :subject => "You’re invited to " + event.title().to_s + " on " + event.date().to_s}) end # In Ruby, every value is an object and every action is a method call. That includes operators. A binary @@ -152,8 +153,9 @@ def desugared_implicit_parens(recipients, event, message) # P.P.P.S. For full credit on this one, note that addition is left-associative: the things on the left # get added before the things on the right. (a + b + c) means ((a + b) + c), NOT (a + (b + c)). # + # TODO unsure whether this one is fully complete def desugared_operators(recipients, event, message) - implement_me! + self.mail(message,{:to=>recipients.map(){|x|x.email},:subject=>"You’re invited to ".+(event.title().to_s).+(" on ").+(event.date().to_s)}) end # Compare that to the version at the top. From eaf6d6ada18e27aee69040bb62afaffa5b6102db Mon Sep 17 00:00:00 2001 From: Cody Molho Date: Thu, 8 Mar 2018 19:15:10 -0600 Subject: [PATCH 3/3] final edits for part 2 --- lib/desugaring.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/desugaring.rb b/lib/desugaring.rb index 3ffd51f..14bcc4d 100644 --- a/lib/desugaring.rb +++ b/lib/desugaring.rb @@ -108,8 +108,6 @@ def desugared_interpolation(recipients, event, message) # # Copy the contents of the previous method here and remove this sugar. # (Think: which names are local variables, and which are not?) - # - # TODO Confused on this one def desugared_implicit_self(recipients, event, message) self.mail(message, {:to => recipients.map { |x| x.email }, :subject => "You’re invited to " + event.title.to_s + " on " + event.date.to_s}) end @@ -152,10 +150,10 @@ def desugared_implicit_parens(recipients, event, message) # P.P.S. Note that whitespace is syntactic sugar too. # P.P.P.S. For full credit on this one, note that addition is left-associative: the things on the left # get added before the things on the right. (a + b + c) means ((a + b) + c), NOT (a + (b + c)). - # - # TODO unsure whether this one is fully complete def desugared_operators(recipients, event, message) - self.mail(message,{:to=>recipients.map(){|x|x.email},:subject=>"You’re invited to ".+(event.title().to_s).+(" on ").+(event.date().to_s)}) + self.mail(message, + {:to=>recipients.map(){|x|x.email}, + :subject=>((("You’re invited to ".+(event.title().to_s)).+(" on ")).+(event.date().to_s))}) end # Compare that to the version at the top.