File: attribute_test.rb

package info (click to toggle)
ruby-enumerize 2.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 432 kB
  • sloc: ruby: 3,712; makefile: 6
file content (144 lines) | stat: -rw-r--r-- 4,318 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# frozen_string_literal: true

require 'test_helper'

class AttributeTest < Minitest::Spec
  def attr
    @attr ||= nil
  end

  def build_attr(*args, &block)
    @attr = Enumerize::Attribute.new(*args, &block)
  end

  it 'returns values' do
    build_attr nil, :foo, :in => [:a, :b]
    expect(attr.values).must_equal %w[a b]
  end

  it 'returns frozen values' do
    build_attr nil, :foo, :in => [:a, :b]
    expect(attr.values.map(&:frozen?)).must_equal [true, true]
  end

  it 'converts name to symbol' do
    build_attr nil, 'foo', :in => %w[a b]
    expect(attr.name).must_equal :foo
  end

  it 'uses custom value class' do
    value_class = Class.new(Enumerize::Value)
    build_attr nil, 'foo', :in => %w[a b], :value_class => value_class
    expect(attr.values.first).must_be_instance_of value_class
  end

  describe 'i18n scopes' do
    it 'returns scopes from options' do
      build_attr nil, 'foo', :in => %w[a b], :i18n_scope => %w[bar buzz]
      expect(attr.i18n_scopes).must_equal %w[bar buzz]
    end
  end

  describe 'arguments' do
    it 'returns arguments' do
      build_attr nil, :foo, :in => [:a, :b], :scope => true
      expect(attr.arguments).must_equal({:in => [:a, :b], :scope => true})
    end
  end

  describe 'options for select' do
    it 'returns all options for select' do
      store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
        build_attr nil, :foo, :in => %w[a b]
        expect(attr.options).must_equal [['a text', 'a'], ['b text', 'b']]
      end
    end

    it 'returns requested options for select via :only' do
      store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
        build_attr nil, :foo, :in => %w[a b]
        expect(attr.options(:only => :a)).must_equal [['a text', 'a']]
        expect(attr.options(:only => [:b])).must_equal [['b text', 'b']]
        expect(attr.options(:only => [])).must_equal []
      end
    end

    it 'returns requested options for select via :except' do
      store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
        build_attr nil, :foo, :in => %w[a b]
        expect(attr.options(:except => :a)).must_equal [['b text', 'b']]
        expect(attr.options(:except => :b)).must_equal [['a text', 'a']]
        expect(attr.options(:except => [])).must_equal [['a text', 'a'], ['b text', 'b']]
      end
    end

    it 'does not work with both :only and :except' do
      store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
        build_attr nil, :foo, :in => %w[a b]
        expect(proc { attr.options(:except => [], :only => []) }).must_raise ArgumentError
      end
    end
  end

  describe 'values hash' do
    before do
      build_attr nil, :foo, :in => {:a => 1, :b => 2}
    end

    it 'returns hash keys as values' do
      expect(attr.values).must_equal %w[a b]
    end

    it 'finds values by hash values' do
      expect(attr.find_value(1)).must_equal 'a'
      expect(attr.find_value(2)).must_equal 'b'
    end
  end

  it 'sets up shortcut methods for each value' do
    build_attr nil, :foo, :in => {:a => 1, :b => 2}

    expect(attr).must_respond_to :a
    expect(attr).must_respond_to :b

    expect(attr.a.value).must_equal 1
    expect(attr.b.value).must_equal 2
    expect(attr.a.text).must_equal 'A'
    expect(attr.b.text).must_equal 'B'
  end

  describe 'values hash with zero' do
    before do
      build_attr nil, :foo, :in => {:a => 1, :b => 2, :c => 0}
    end

    it 'returns hash keys as values' do
      expect(attr.values).must_equal %w[a b c]
    end

    it 'finds values by hash values' do
      expect(attr.find_value(1)).must_equal 'a'
      expect(attr.find_value(2)).must_equal 'b'
      expect(attr.find_value(0)).must_equal 'c'
    end

    it 'finds all values by hash values' do
      expect(attr.find_values(1, 2, 0)).must_equal ['a', 'b', 'c']
    end
  end

  describe 'boolean values hash' do
    before do
      build_attr nil, :foo, :in => {:a => true, :b => false}
    end

    it 'returns hash keys as values' do
      expect(attr.values).must_equal %w[a b]
    end

    it 'finds values by hash values' do
      expect(attr.find_value(true)).must_equal 'a'
      expect(attr.find_value(false)).must_equal 'b'
    end
  end
end