Sha256: 24da794703d6906cb4744bb1c1b6c9cb0b74f8cd13f1fd65c8ef72f70f27744d

Contents?: true

Size: 1.66 KB

Versions: 26

Compression:

Stored size: 1.66 KB

Contents

require 'rails_helper'
require 'sorbet-rails/model_rbi_formatter'

RSpec.describe 'TypedEnum' do
  let!(:harry) do
    Wizard.create!(
      name: 'Harry Potter',
      house: :Gryffindor,
    )
  end

  it 'generates typed enum classes' do
    expect(Wizard.const_defined?(:House)).to be(true)
    expect(Wizard::House.constants).to match_array([
      :Gryffindor,
      :Hufflepuff,
      :Ravenclaw,
      :Slytherin,
    ])
    expect(Wizard::House::Gryffindor).to be_a(Wizard::House)
    expect(Wizard::House::Hufflepuff).to be_a(Wizard::House)
    expect(Wizard::House::Ravenclaw).to be_a(Wizard::House)
    expect(Wizard::House::Slytherin).to be_a(Wizard::House)
  end

  it 'can retrieve enum value correctly' do
    expect(harry.typed_house).to be(Wizard::House::Gryffindor)
    expect(harry.house).to eql('Gryffindor')
    expect(harry.Gryffindor?).to be(true)
    expect(harry.Slytherin?).to be(false)
  end

  it 'works when setting enum to nil' do
    harry.typed_house = nil
    expect(harry.typed_house).to be(nil)
    expect(harry.house).to be(nil)
    expect(harry.Gryffindor?).to be(false)
  end

  it 'works when setting enum value by typed_enum' do
    harry.typed_house = Wizard::House::Slytherin
    expect(harry.typed_house).to be(Wizard::House::Slytherin)
    expect(harry.house).to eql('Slytherin')
    expect(harry.Gryffindor?).to be(false)
    expect(harry.Slytherin?).to be(true)
  end

  it 'works when setting enum value by Rails setter' do
    harry.house = :Slytherin
    expect(harry.typed_house).to be(Wizard::House::Slytherin)
    expect(harry.house).to eql('Slytherin')
    expect(harry.Gryffindor?).to be(false)
    expect(harry.Slytherin?).to be(true)
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
sorbet-rails-0.7.34 spec/typed_enum_spec.rb
sorbet-rails-0.7.33 spec/typed_enum_spec.rb
sorbet-rails-0.7.32 spec/typed_enum_spec.rb
sorbet-rails-0.7.31 spec/typed_enum_spec.rb
sorbet-rails-0.7.27 spec/typed_enum_spec.rb
sorbet-rails-0.7.26 spec/typed_enum_spec.rb
sorbet-rails-0.7.25 spec/typed_enum_spec.rb
sorbet-rails-0.7.24 spec/typed_enum_spec.rb
sorbet-rails-0.7.23 spec/typed_enum_spec.rb
sorbet-rails-0.7.22 spec/typed_enum_spec.rb
sorbet-rails-0.7.20 spec/typed_enum_spec.rb
sorbet-rails-0.7.19 spec/typed_enum_spec.rb
sorbet-rails-0.7.18 spec/typed_enum_spec.rb
sorbet-rails-0.7.5 spec/typed_enum_spec.rb
sorbet-rails-0.7.4 spec/typed_enum_spec.rb
sorbet-rails-0.7.3 spec/typed_enum_spec.rb
sorbet-rails-0.7.2 spec/typed_enum_spec.rb
sorbet-rails-0.7.1 spec/typed_enum_spec.rb
sorbet-rails-0.7.0 spec/typed_enum_spec.rb
sorbet-rails-0.6.5.1 spec/typed_enum_spec.rb