Sha256: 8d228c4e63c4420c3533eff4ff182a4e0191b4a6b74bd4cfbfdfc05eaaed92d5

Contents?: true

Size: 1.19 KB

Versions: 25

Compression:

Stored size: 1.19 KB

Contents

require 'test_helper'

class BitEnumTest < ActiveSupport::TestCase
  
  class MyEnum < ::Incline::BitEnum
    
    # raw bits.
    ALPHA = 1
    BRAVO = 2
    CHARLIE = 4
    DELTA = 8
    ECHO = 16
    
    # combined bits.
    FOXTROT = 5
    GOLF = 17
    
  end
  
  test 'should allow initialization' do
    item = MyEnum.new(6)
    assert item
    assert_equal 6, item.value
    assert_equal %w(BRAVO CHARLIE), item.name
    assert_equal 'BRAVO | CHARLIE', item.to_s
  end
  
  test 'should not allow invalid values' do
    assert_raises(ArgumentError){ MyEnum.new(0) }
    assert_raises(ArgumentError){ MyEnum.new(32) }
  end
  
  test 'should allow valid values' do
    assert MyEnum.new(1)
    assert MyEnum.new(7)
    assert MyEnum.new(15)
    assert MyEnum.new(31)
  end
  
  test 'should recognize names' do
    assert MyEnum.named?(1)
    assert MyEnum.named?(2)
    assert MyEnum.named?(3)
    assert MyEnum.named?(4)
    assert MyEnum.named?(5)
    assert_equal %w(ALPHA), MyEnum.name_for(1)
    assert_equal %w(BRAVO), MyEnum.name_for(2)
    assert_equal %w(ALPHA BRAVO), MyEnum.name_for(3)
    assert_equal %w(CHARLIE), MyEnum.name_for(4)
    assert_equal %w(FOXTROT), MyEnum.name_for(5)
  end
  
  
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
incline-0.2.26 test/lib/bit_enum_test.rb
incline-0.2.25 test/lib/bit_enum_test.rb
incline-0.2.24 test/lib/bit_enum_test.rb
incline-0.2.23 test/lib/bit_enum_test.rb
incline-0.2.22 test/lib/bit_enum_test.rb