Sha256: 1a2a4a09df9826206ccd327c8faa7484a0f1c6fc6af747d9ec1fc611898f0397

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

require 'helper'

class Model
  include HasConstant

  attr_accessor :salutation
end

class TestHasConstant < Test::Unit::TestCase
  should 'default accessor to singular of the constant name' do
    Model.has_constant :titles, ['Mr', 'Mrs']
    assert Model.new.respond_to?(:title)
    assert Model.new.respond_to?(:title=)
  end

  should 'raise an exception when a value is provided which is not in the list' do
    Model.has_constant :titles, ['Mr', 'Mrs']
    m = Model.new
    assert_raise ArgumentError do
      m.title = 'Ms'
    end
  end

  should 'be able to override accessor' do
    Model.has_constant :titles, ['Mr', 'Mrs'], :accessor => :salutation
    m = Model.new
    m.salutation = 'Mr'
    assert_equal 'Mr', m.salutation
  end

  should 'be able to use an array' do
    Model.has_constant :titles, ['Mr', 'Mrs']
    assert_equal ['Mr', 'Mrs'], Model.titles
  end

  should 'be able to use a proc' do
    Model.has_constant :titles, Proc.new { ['Mr', 'Mrs'] }
    assert_equal ['Mr', 'Mrs'], Model.titles
  end

  should 'be able to use lambda' do
    Model.has_constant :titles, lambda { ['Mr', 'Mrs'] }
    assert_equal ['Mr', 'Mrs'], Model.titles
  end

  should 'provide singular_is? comparison method' do
    Model.has_constant :titles, ['Mr', 'Mrs']
    m = Model.new
    m.title = 'Mr'
    assert m.title_is?('Mr')
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
has_constant-0.2.9 test/has_constant_test.rb
has_constant-0.2.8 test/has_constant_test.rb
has_constant-0.2.7 test/has_constant_test.rb
has_constant-0.2.6 test/has_constant_test.rb
has_constant-0.2.5 test/has_constant_test.rb
has_constant-0.2.4 test/has_constant_test.rb