File: value_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 (172 lines) | stat: -rw-r--r-- 5,302 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# frozen_string_literal: true

require 'test_helper'
require 'yaml'

class ValueTest < Minitest::Spec
  class Model
  end

  class Attr < Struct.new(:values, :name, :i18n_scopes, :klass)
    def value?(value)
      values.include?(value)
    end
  end

  let(:attr) { Attr.new([], "attribute_name", [], Model) }
  let(:val) { Enumerize::Value.new(attr, 'test_value', 1) }

  it 'is a string' do
    expect(val).must_be_kind_of String
  end

  describe 'equality' do
    it 'is compared to string' do
      expect(val).must_be :==, 'test_value'
      expect(val).wont_be :==, 'not_value'
    end

    it 'is compared to symbol' do
      expect(val).must_be :==, :test_value
      expect(val).wont_be :==, :not_value
    end

    it 'is compared to integer' do
      expect(val).must_be :==, 1
      expect(val).wont_be :==, 2
    end
  end

  describe 'translation' do
    it 'uses common translation' do
      store_translations(:en, :enumerize => {:attribute_name => {:test_value => "Common translation"}}) do
        expect(val.text).must_be :==, "Common translation"
      end
    end

    it 'uses default translation from the "default" section if its present' do
      store_translations(:en, :enumerize => {:defaults => {:attribute_name => {:test_value => "Common translation"}}}) do
        expect(val.text).must_be :==, "Common translation"
      end
    end

    it 'uses model specific translation' do
      attr.i18n_scopes = ["enumerize.model_name.attribute_name"]

      store_translations(:en, :enumerize => {:model_name => {:attribute_name => {:test_value => "Model Specific translation"}}}) do
        expect(val.text).must_be :==, "Model Specific translation"
      end
    end

    it 'uses model specific translation rather than common translation' do
      attr.i18n_scopes = ["enumerize.model_name.attribute_name"]

      store_translations(:en, :enumerize => {:attribute_name => {:test_value => "Common translation"}, :model_name => {:attribute_name => {:test_value => "Model Specific translation"}}}) do
        expect(val.text).must_be :==, "Model Specific translation"
      end
    end

    it 'uses simply humanized value when translation is undefined' do
      store_translations(:en, :enumerize => {}) do
        expect(val.text).must_be :==, "Test value"
      end
    end

    it 'uses specified in options translation scope' do
      attr.i18n_scopes = ["other.scope"]

      store_translations(:en, :other => {:scope => {:test_value => "Scope specific translation"}}) do
        expect(val.text).must_be :==, "Scope specific translation"
      end
    end

    it 'uses first found translation scope from options' do
      attr.i18n_scopes = ["nonexistent.scope", "other.scope"]

      store_translations(:en, :other => {:scope => {:test_value => "Scope specific translation"}}) do
        expect(val.text).must_be :==, "Scope specific translation"
      end
    end

    it 'allows to pass a proc as i18n_scopes param' do
      attr.i18n_scopes = [proc { |v| "other.scope.#{v}" }]

      store_translations(:en, :other => {:scope => {:"1" => {:test_value => "Scope specific translation"}}}) do
        expect(val.text).must_be :==, "Scope specific translation"
      end
    end
  end

  describe 'boolean methods comparison' do
    before do
      attr.values = [val, Enumerize::Value.new(attr, 'other_value')]
    end

    it 'returns true if value equals method' do
      expect(val.test_value?).must_equal true
    end

    it 'returns false if value does not equal method' do
      expect(val.other_value?).must_equal false
    end

    it 'raises NoMethodError if there are no values like boolean method' do
      expect(proc {
        val.some_method?
      }).must_raise NoMethodError
    end

    it 'raises ArgumentError if arguments are passed' do
      expect(proc {
        val.other_value?('<3')
      }).must_raise ArgumentError
    end

    it 'responds to methods for existing values' do
      expect(val).must_respond_to :test_value?
      expect(val).must_respond_to :other_value?
    end

    it 'returns a method object' do
      expect(val.method(:test_value?)).must_be_instance_of Method
    end

    it "doesn't respond to a method for not existing value" do
      expect(val).wont_respond_to :some_method?
    end

    it "doesn't respond to methods is value was modified" do
      modified_value = val.upcase

      expect(modified_value.upcase).wont_respond_to :some_method?
      expect(modified_value.upcase).wont_respond_to :test_value?
      expect(modified_value.upcase).wont_respond_to :other_value?
    end
  end

  describe 'serialization' do
    let(:val) { Enumerize::Value.new(attr, 'test_value') }

    it 'should be serialized to yaml as string value' do
      assert_equal YAML.dump('test_value'), YAML.dump(val)
    end

    it 'serializes with Marshal' do
      dump_value = Marshal.dump(val)
      expect(Marshal.load(dump_value)).must_equal 'test_value'
    end
  end

  describe 'initialize' do
    it 'no output if undefined boolean method' do
      assert_silent() { Enumerize::Value.new(attr, 'test_value') }
    end
  end

  describe '#as_json' do
    it 'returns String object, not Value object' do
      expect(val.as_json.class).must_equal String
      expect(val.as_json).must_equal 'test_value'
    end
  end
end