From f46873388d57fcbde6ee994c813212374312e337 Mon Sep 17 00:00:00 2001 From: khintk Date: Tue, 6 Mar 2018 21:08:50 -0600 Subject: [PATCH 1/5] Made a few comments trying to understand the code --- test/retail_transaction_test.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/retail_transaction_test.rb b/test/retail_transaction_test.rb index 823cbfe..e5695dd 100644 --- a/test/retail_transaction_test.rb +++ b/test/retail_transaction_test.rb @@ -4,9 +4,9 @@ describe RetailTransaction do - let(:tx) { RetailTransaction.new } + let(:tx) { RetailTransaction.new } #declaring a new retail transaction - it "starts in the “ringing up” state" do + it "starts in the “ringing up” state" do #setting the only the ringing up state to be true. assert_equal true, tx.ringing_up? assert_equal false, tx.collecting_payment? assert_equal false, tx.processing_payment? @@ -14,16 +14,16 @@ assert_equal false, tx.payment_declined? end - it "starts out empty" do + it "starts out empty" do #? assert_equal true, tx.empty? end - it "cannot check out if no items" do + it "cannot check out if no items" do #What is the assert_invalid_transition assert_invalid_transition { tx.check_out! } end - describe "still ringing up, with items" do - before(:each) { tx.add_item("broccoli") } + describe "still ringing up, with items" do #describe group logically related tests. + before(:each) { tx.add_item("broccoli") } #says it is running the tx all over again. Before means before we do that we add a broccoli? it "can add more items" do tx.add_item("roller skates") @@ -48,7 +48,7 @@ tx.check_out! end - it "cannot add more items" do + it "cannot add more items" do #Don't really understand the job of assert_raises assert_raises do tx.add_item("roller skates") end @@ -69,12 +69,12 @@ describe "processing payment" do before(:each) do tx.add_item("bobcat") - tx.check_out! + tx.check_out! #is this setting the check_out to true? tx.payment_info = "15 cents and a nail" tx.process_payment! end - it "cannot add more items" do + it "cannot add more items" do #what is the job of assert_raises in here? assert_raises do tx.add_item("roller skates") end From f06fe2ac8dc20fad83b9c1f99d5d97e8295084ae Mon Sep 17 00:00:00 2001 From: khintk Date: Tue, 6 Mar 2018 21:09:30 -0600 Subject: [PATCH 2/5] Added the refund stage --- lib/retail_transaction.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/retail_transaction.rb b/lib/retail_transaction.rb index 1bfa0e3..a1f7226 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 :refund event :check_out do transitions from: :ringing_up, to: :collecting_payment, @@ -61,5 +62,10 @@ 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 From cb87e449d227016328ee04fced773d4186016526 Mon Sep 17 00:00:00 2001 From: khintk Date: Wed, 7 Mar 2018 21:31:54 -0600 Subject: [PATCH 3/5] Attempted the first part of the homework --- test/retail_transaction_test.rb | 53 ++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/test/retail_transaction_test.rb b/test/retail_transaction_test.rb index e5695dd..e90559d 100644 --- a/test/retail_transaction_test.rb +++ b/test/retail_transaction_test.rb @@ -4,9 +4,9 @@ describe RetailTransaction do - let(:tx) { RetailTransaction.new } #declaring a new retail transaction + let(:tx) { RetailTransaction.new } - it "starts in the “ringing up” state" do #setting the only the ringing up state to be true. + it "starts in the “ringing up” state" do assert_equal true, tx.ringing_up? assert_equal false, tx.collecting_payment? assert_equal false, tx.processing_payment? @@ -14,17 +14,16 @@ assert_equal false, tx.payment_declined? end - it "starts out empty" do #? + it "starts out empty" do assert_equal true, tx.empty? end - it "cannot check out if no items" do #What is the assert_invalid_transition + it "cannot check out if no items" do assert_invalid_transition { tx.check_out! } end - describe "still ringing up, with items" do #describe group logically related tests. - before(:each) { tx.add_item("broccoli") } #says it is running the tx all over again. Before means before we do that we add a broccoli? - + describe "still ringing up, with items" do + before(:each) { tx.add_item("broccoli") } it "can add more items" do tx.add_item("roller skates") end @@ -48,7 +47,7 @@ tx.check_out! end - it "cannot add more items" do #Don't really understand the job of assert_raises + it "cannot add more items" do assert_raises do tx.add_item("roller skates") end @@ -69,12 +68,12 @@ describe "processing payment" do before(:each) do tx.add_item("bobcat") - tx.check_out! #is this setting the check_out to true? + tx.check_out! tx.payment_info = "15 cents and a nail" tx.process_payment! end - it "cannot add more items" do #what is the job of assert_raises in here? + it "cannot add more items" do assert_raises do tx.add_item("roller skates") end @@ -86,6 +85,10 @@ end end + it "cannot be refunded while payment is processing" do + assert_invalid_transition { tx.refund! } + end + it "cannot re-process payment" do assert_invalid_transition { tx.process_payment! } end @@ -120,6 +123,10 @@ end end + it "cannot be refunded with declined payment" do + assert_invalid_transition { tx.refund! } + end + it "can reopen and add more items" do tx.reopen! assert_equal true, tx.ringing_up? @@ -152,5 +159,31 @@ it "cannot be reopened" do assert_invalid_transition { tx.reopen! } end + + it "can be refunded" do + tx.refund! + assert_equal true, tx.refunded? + end end + + describe "group for orders that are already 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.refunded! + + it "cannot be refunded again" do + assert_invalid_transition { tx.refund! } + end + + it "cannot be reopned again" do + assert_invalid_transition { tx.reopen! } + end + + end + + end From 4e1f976c2510e8b64a64f886847e5ed96ab3057a Mon Sep 17 00:00:00 2001 From: khintk Date: Wed, 7 Mar 2018 22:17:52 -0600 Subject: [PATCH 4/5] Attempted the second part of the homework --- lib/desugaring.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/desugaring.rb b/lib/desugaring.rb index 8d52908..7740dc4 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{|recipients| recipients.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{|recipients| recipients.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 @@ -110,7 +110,7 @@ def desugared_interpolation(recipients, event, message) # (Think: which names are local variables, and which are not?) # def desugared_implicit_self(recipients, event, message) - implement_me! + self.mail(message,{:to => recipients.map{|recipients| recipients.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 +131,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{|recipients| recipients.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 @@ -153,7 +153,7 @@ def desugared_implicit_parens(recipients, event, message) # get added before the things on the right. (a + b + c) means ((a + b) + c), NOT (a + (b + c)). # def desugared_operators(recipients, event, message) - implement_me! + self.mail(message,{:to => recipients.map{|recipients| recipients.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 72e4ec3376837ffc8d46f8fbfdca297c7ecb1f9a Mon Sep 17 00:00:00 2001 From: khintk Date: Wed, 7 Mar 2018 22:39:35 -0600 Subject: [PATCH 5/5] Fixed the bugs, and finished part 1 and part 2. Really enjoyed the hw! --- lib/desugaring.rb | 12 ++++++++---- lib/retail_transaction.rb | 2 +- test/desugaring_test.rb | 2 +- test/retail_transaction_test.rb | 8 +++----- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/desugaring.rb b/lib/desugaring.rb index 7740dc4..588c30e 100644 --- a/lib/desugaring.rb +++ b/lib/desugaring.rb @@ -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) - mail(message,{:to => recipients.map{|recipients| recipients.email},:subject => "You’re invited to" + event.title.to_s + "on" + event.date.to_s}) + mail(message,{:to => recipients.map{|recipients| recipients.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 @@ -110,7 +110,7 @@ def desugared_interpolation(recipients, event, message) # (Think: which names are local variables, and which are not?) # def desugared_implicit_self(recipients, event, message) - self.mail(message,{:to => recipients.map{|recipients| recipients.email},:subject => "You’re invited to" + event.title.to_s + "on" + event.date.to_s}) + self.mail(message,{:to => recipients.map{|recipients| recipients.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 +131,7 @@ def desugared_implicit_self(recipients, event, message) # but structurally quite similar! # def desugared_implicit_parens(recipients, event, message) - self.mail(message,{:to => recipients.map{|recipients| recipients.email},:subject => "You’re invited to" + event.title().to_s() + "on" + event.date().to_s()}) + self.mail(message,{:to => recipients.map{|recipients| recipients.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 @@ -153,7 +153,11 @@ def desugared_implicit_parens(recipients, event, message) # get added before the things on the right. (a + b + c) means ((a + b) + c), NOT (a + (b + c)). # def desugared_operators(recipients, event, message) - self.mail(message,{:to => recipients.map{|recipients| recipients.email},:subject => "You’re invited to". + (event.title().to_s()) + "on". + (event.date().to_s())}) + self.mail(message,{ + :to => recipients.map{|recipients| recipients.email}, + :subject => ((("You’re invited to ".+(event.title().to_s())).+(" on "))).+(event.date().to_s()) + }) + #self.mail(message,{:to => recipients.map{|recipients| recipients.email},:subject => "You’re invited to". + (event.title().to_s()) + "on". + (event.date().to_s())}) end # Compare that to the version at the top. diff --git a/lib/retail_transaction.rb b/lib/retail_transaction.rb index a1f7226..e25e322 100644 --- a/lib/retail_transaction.rb +++ b/lib/retail_transaction.rb @@ -39,7 +39,7 @@ def paid? state :processing_payment state :payment_declined state :settled - state :refund + state :refunded event :check_out do transitions from: :ringing_up, to: :collecting_payment, diff --git a/test/desugaring_test.rb b/test/desugaring_test.rb index 24e8a7b..e029f94 100644 --- a/test/desugaring_test.rb +++ b/test/desugaring_test.rb @@ -44,7 +44,7 @@ } ], exercise.mail_calls.first, - "Wrong arguments passed to mail method") + "Wrong arguments passed to mail method on variant #{variant}") end end diff --git a/test/retail_transaction_test.rb b/test/retail_transaction_test.rb index e90559d..ca78a7a 100644 --- a/test/retail_transaction_test.rb +++ b/test/retail_transaction_test.rb @@ -173,17 +173,15 @@ tx.payment_info = "15 cents and a nail" tx.process_payment! tx.payment_authorized! - tx.refunded! + tx.refund! + end it "cannot be refunded again" do assert_invalid_transition { tx.refund! } end - + it "cannot be reopned again" do assert_invalid_transition { tx.reopen! } end - end - - end