diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..05913ea --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 4.0.6 diff --git a/README.md b/README.md index fdd13d2..a70a459 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,18 @@ # Ruby Technical Test +## Dependencies + +* Ruby 4.0.6 +* Minitest 6.0.6 + +## Instructions + +Checkout class is in `checkout.rb` + +Run tests with `ruby -Ilib:test tests.rb` + +`main.rb` is a scratchpad + ## Initial Thoughts * Cart can be stored in an array of strings diff --git a/checkout.rb b/checkout.rb new file mode 100644 index 0000000..bae0ff4 --- /dev/null +++ b/checkout.rb @@ -0,0 +1,28 @@ +class Checkout + class InvalidItem < StandardError; end + + AVAILABLE_ITEMS = { + PI5: 60.00, + ZERO2W: 15.00, + PICO2: 4.00 + } + + def initialize + @items = [] + end + + def add(item) + raise InvalidItem unless valid_item(item) + @items.push item + end + + def items + @items + end + + private + + def valid_item(item) + return AVAILABLE_ITEMS.keys.include?(item) + end +end \ No newline at end of file diff --git a/main.rb b/main.rb new file mode 100644 index 0000000..93acc16 --- /dev/null +++ b/main.rb @@ -0,0 +1,9 @@ +require "./checkout.rb" + +checkout = Checkout.new() + +checkout.add(:PI5) +checkout.add(:ZERO2W) + +checkout.items # ["PI5", "ZERO2W"] +checkout.total # 75.00 diff --git a/tests.rb b/tests.rb new file mode 100644 index 0000000..e5c422e --- /dev/null +++ b/tests.rb @@ -0,0 +1,22 @@ +require "minitest/autorun" + +require "./checkout.rb" + +class TestCheckout < Minitest::Test + def setup + @checkout = Checkout.new + end + + def test_that_unknown_items_cannot_be_added + assert_raises Checkout::InvalidItem do + @checkout.add(:foo) + end + end + + def test_that_allowed_items_are_added + @checkout.add(:PI5) + @checkout.add(:ZERO2W) + + assert_equal [:PI5, :ZERO2W].sort, @checkout.items.sort + end +end \ No newline at end of file