<commit_before>require 'spec_helper'
require 'json'
require 'ostruct'

describe Checkout do

  before(:each) do
    file = File.read('spec/items.json')
    items = JSON.parse(file)
    items["items"].each do |item|
      item_obj = OpenStruct.new(item)
      instance_variable_set(:"@#{item_obj.code}", Item.new(item_obj.code, item_obj.name, item_obj.price))
    end
    @checkout = Checkout.new(PricingRules.new)
  end

  it 'returns the total price test 1' do
    [@GR1,@SR1,@GR1,@GR1,@CF1].each { |item| @checkout.scan(item) }
    expect(@checkout.total_price).to eq(22.45)
  end

  it 'returns the total price test 2' do
    [@GR1,@GR1].each { |item| @checkout.scan(item) }
    expect(@checkout.total_price).to eq(3.11)
  end

  it 'returns the total price test 3' do
    [@SR1,@SR1,@GR1,@SR1].each { |item| @checkout.scan(item) }
    expect(@checkout.total_price).to eq(16.61)
  end


end<commit_msg>Add more specs to verify checkout<commit_after>require 'spec_helper'
require 'json'
require 'ostruct'

describe Checkout do
  describe '#total_price' do
    before :all do
      file = File.read('spec/items.json')
      items = JSON.parse(file)
      items["items"].each do |item|
        item_obj = OpenStruct.new(item)
        instance_variable_set(:"@#{item_obj.code}", Item.new(item_obj.code, item_obj.name, item_obj.price))
      end
    end

    context 'scenarious from specification' do
      before(:each) do
        @checkout = Checkout.new(PricingRules.new)
      end

      it 'returns the total price test 1' do
        [@GR1,@SR1,@GR1,@GR1,@CF1].each { |item| @checkout.scan(item) }
        expect(@checkout.total_price).to eq(22.45)
      end

      it 'returns the total price test 2' do
        [@GR1,@GR1].each { |item| @checkout.scan(item) }
        expect(@checkout.total_price).to eq(3.11)
      end

      it 'returns the total price test 3' do
        [@SR1,@SR1,@GR1,@SR1].each { |item| @checkout.scan(item) }
        expect(@checkout.total_price).to eq(16.61)
      end
    end

    context 'additional scenarious for verification' do
      context 'without pricing rules' do
        before { @checkout = Checkout.new(PricingRules.new([])) }

        it 'should return sum of all prices' do
          @checkout.scan(@GR1)
          @checkout.scan(@CF1)
          @checkout.scan(@SR1)

          expect(@checkout.total_price).to eq(19.34)
        end
      end

      context "buy one 'Green Tea' get one for free" do
        before { @checkout = Checkout.new(PricingRules.new) }

        it 'should give one for free' do
          2.times { @checkout.scan(@GR1) }

          expect(@checkout.total_price).to eq(3.11)
        end

        it 'should give one for free, but third should cost as usual' do
          3.times { @checkout.scan(@GR1) }

          expect(@checkout.total_price.round(2)).to eq(3.11 * 2)
        end

        it 'should return price of 3 when buying 5' do
          5.times { @checkout.scan(@GR1) }

          expect(@checkout.total_price.round(2)).to eq(3.11 * 3)
        end

        it 'should return regular total' do
          @checkout.scan(@CF1)

          expect(@checkout.total_price).to eq(11.23)
        end
      end

      context "when using 'buy more pay less' discount rule" do
        before { @checkout = Checkout.new(PricingRules.new) }

        it 'should cost less per item' do
          3.times { @checkout.scan(@SR1) }

          expect(@checkout.total_price).to eq(4.50 * 3)
        end

        it 'should not change anything' do
          2.times { @checkout.scan(@SR1) }

          expect(@checkout.total_price).to eq(10.00)
        end

        it 'should do not discount other items' do
          3.times { @checkout.scan(@SR1) }
          3.times { @checkout.scan(@CF1) }

          expect(@checkout.total_price).to eq(4.50 * 3 + 11.23 * 3)
        end
      end

      context 'when combining two discount rules' do
        before { @checkout = Checkout.new(PricingRules.new) }

        it 'should discount green tea and strawberries' do
          3.times do
            @checkout.scan(@SR1)
            @checkout.scan(@GR1)
          end

          expect(@checkout.total_price).to eq(19.72)
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12591889969172398, 'fraction_numerical': 0.027270571496324402, 'mean_word_length': 2.3556085918854417, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2059518', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1399, 'n_words': 344}