Follow the Brass

spec/caliber_batch_spec.rb

53 lines · ruby

Project files
# frozen_string_literal: true

require "caliber_batch"
require "cartridge_case"
require "foreign_object"

RSpec.describe CaliberBatch do
  subject(:batch) { described_class.new(caliber: "5.56") }

  it "accepts a case of its own caliber" do
    cartridge_case = CartridgeCase.new(caliber: "5.56")

    placement = batch.accept(cartridge_case)

    expect(placement).to be_placed
    expect(batch.contents).to eq([cartridge_case])
  end

  it "refuses a case of another caliber and does not hold it" do
    placement = batch.accept(CartridgeCase.new(caliber: "9mm"))

    expect(placement).to be_refused
    expect(placement.reason).to eq(:wrong_caliber)
    expect(batch.contents).to be_empty
  end

  it "refuses an object that is not a case at all" do
    placement = batch.accept(ForeignObject.new(description: "stripper clip"))

    expect(placement).to be_refused
    expect(placement.reason).to eq(:not_a_case)
  end

  it "refuses a case when every position is occupied" do
    tiny_batch = described_class.new(caliber: "5.56", capacity: 1)
    tiny_batch.accept(CartridgeCase.new(caliber: "5.56"))

    placement = tiny_batch.accept(CartridgeCase.new(caliber: "5.56"))

    expect(placement).to be_refused
    expect(placement.reason).to eq(:batch_full)
  end

  it "exposes frozen contents that do not leak internal state" do
    batch.accept(CartridgeCase.new(caliber: "5.56"))

    contents = batch.contents

    expect(contents).to be_frozen
    expect { contents << :intruder }.to raise_error(FrozenError)
    expect(batch.contents.length).to eq(1)
  end
end

Read along in the manual: The Reloading Lab · Download the project (zip)