Sha256: 2233fb902abbbb7fb271cc87c31cf62a1baa6febb8c9c8237fb0c2af33dfe904

Contents?: true

Size: 961 Bytes

Versions: 2

Compression:

Stored size: 961 Bytes

Contents

require 'minitest_helper'

describe Rasti::Types::Enum do

  let(:enum) { Rasti::Types::Enum[:a,:b,:c] }

  it 'nil -> nil' do
    enum.cast(nil).must_be_nil
  end

  [:a, 'b', "c"].each do |value|
    it "#{value.inspect} -> #{value.to_s.inspect}" do
      enum.cast(value).must_equal value.to_s
    end
  end

  ['text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new].each do |value|
    it "#{value.inspect} -> CastError" do
      error = proc { enum.cast(value) }.must_raise Rasti::Types::CastError
      error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Enum[\"a\", \"b\", \"c\"]"
    end
  end

  it 'Values' do
    enum = Rasti::Types::Enum[:first_value, 'SecondValue', 'THIRD_VALUE']

    enum.first_value.must_equal 'first_value'
    enum.second_value.must_equal 'SecondValue'
    enum.third_value.must_equal 'THIRD_VALUE'
    enum.values.must_equal [enum.first_value, enum.second_value, enum.third_value]
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rasti-types-2.0.1 spec/enum_spec.rb
rasti-types-2.0.0 spec/enum_spec.rb