Sha256: 3260246652929e3f5d6b6e85763684913c2333ddd3f8f0b0ec2c75b48b271be4

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

require 'spec_helper'
require 'command_mapper/types/map'

describe CommandMapper::Types::Map do
  let(:map) { {1 => 'one', 2 => 'two'} }

  subject { described_class.new(map) }

  describe "#initialize" do
    it "must initialize #map" do
      expect(subject.map).to eq(map)
    end
  end

  describe ".[]" do
    subject { described_class[map] }

    it "must create a new Map with the given values" do
      expect(subject).to be_kind_of(described_class)
      expect(subject.map).to eq(map)
    end
  end

  describe "#validate" do
    context "when given a value that's a key in the map" do
      let(:value) { 2 }

      it "must return true" do
        expect(subject.validate(value)).to be(true)
      end
    end

    context "when given a value that's a value in the map" do
      let(:value) { "two" }

      it "must return true" do
        expect(subject.validate(value)).to be(true)
      end
    end

    context "when given a value that is not in the map" do
      let(:value) { 42 }

      it "must return [false, \"unknown value (...)\"]" do
        expect(subject.validate(value)).to eq(
          [false, "unknown value (#{value.inspect})"]
        )
      end
    end
  end

  describe "#format" do
    context "when given a value that's a key in the map" do
      let(:value) { 2 }

      it "must return the corresponding mapped value" do
        expect(subject.format(value)).to eq(map[value])
      end
    end

    context "when given a value that's a value in the map" do
      let(:value) { "two" }

      it "must return the value" do
        expect(subject.format(value)).to eq(value)
      end
    end

    context "when given a value that is not in the map" do
      let(:value) { 42 }

      it "must return the String version of the value" do
        expect {
          subject.format(value)
        }.to raise_error(KeyError,"value (#{value.inspect}) is not a key or value in the map: #{map.inspect}")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
command_mapper-0.2.0 spec/types/map_spec.rb
command_mapper-0.1.2 spec/types/map_spec.rb
command_mapper-0.1.1 spec/types/map_spec.rb