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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
require 'spec_helper'
class ApplicationController < ActionController::Base; end
class InflectedTranslateController < ApplicationController; end
class InflectedStrictController < ApplicationController; end
describe I18n.inflector.options.class do
context "instance I18n.inflector.options" do
it "should contain verify_methods switch" do
expect(I18n.inflector.options).to respond_to :verify_methods
end
it "should have default value set to false" do
expect(I18n.inflector.options.verify_methods).to eq(false)
end
end
end
describe ApplicationController do
before do
I18n.config.available_locales = :xx, :ns
I18n.locale = :xx
I18n.backend.store_translations(:xx, :i18n => { :inflections => {
:gender => {
:m => 'male',
:f => 'female',
:n => 'neuter',
:s => 'strange',
:masculine => '@m',
:feminine => '@f',
:neuter => '@n',
:neutral => '@neuter',
:default => 'neutral' },
:time => {
:present => 'present',
:past => 'past',
:future => 'future'},
:@gender => {
:m => 'male',
:f => 'female',
:d => 'dude',
:x => 'tester',
:n => 'neuter',
:default => 'n'},
:person => {
:i => 'I',
:you => 'You',
:it => 'It'},
:@person => {
:i => 'I',
:you => 'You',
:it => 'It'},
} })
I18n.backend.store_translations(:ns, 'welcome' => 'Dear @{f:Lady|m:Sir|n:You|All}!')
I18n.backend.store_translations(:xx, 'welcome' => 'Dear @{f:Lady|m:Sir|n:You|All}!')
I18n.backend.store_translations(:xx, 'welcome_strict' => 'Dear @gender{f:Lady|m:Sir|d:Dude|n:You|All}!')
I18n.backend.store_translations(:xx, 'to_be' => 'Oh @{i:I am|you:You are|it:It is}')
I18n.backend.store_translations(:xx, 'to_be_strict' => 'Oh @person{i:me|you:You are|it:It is}')
I18n.backend.store_translations(:xx, 'hitime' => '@{present,past,future:~}!')
end
describe ".inflection_method" do
before do
class AnotherController < InflectedTranslateController; end
end
it "should be albe to assign a mehtod to the inflection kind" do
expect{AnotherController.inflection_method(:users_gender => :gender)}.not_to raise_error
end
it "should be albe to assign a mehtod to the strict inflection kind" do
expect{AnotherController.inflection_method(:users_gender => :@gender)}.not_to raise_error
end
it "should be albe to accept single Symbol argument" do
expect{AnotherController.inflection_method(:time)}.not_to raise_error
expect{AnotherController.inflection_method(:@time)}.not_to raise_error
end
it "should be albe to accept single String argument" do
expect{AnotherController.inflection_method('time')}.not_to raise_error
expect{AnotherController.inflection_method('@time')}.not_to raise_error
end
it "should be albe to accept Array<Symbol> argument" do
expect{AnotherController.inflection_method([:time])}.not_to raise_error
expect{AnotherController.inflection_method([:@time])}.not_to raise_error
end
it "should raise an error when method name is wrong" do
expect{AnotherController.inflection_method}.to raise_error
expect{AnotherController.inflection_method(nil => :blabla)}.to raise_error
expect{AnotherController.inflection_method(:blabla => nil)}.to raise_error
expect{AnotherController.inflection_method(nil => :@blabla)}.to raise_error
expect{AnotherController.inflection_method(:@blabla => nil)}.to raise_error
expect{AnotherController.inflection_method(:"@")}.to raise_error
expect{AnotherController.inflection_method({''=>''})}.to raise_error
expect{AnotherController.inflection_method(nil => nil)}.to raise_error
expect{AnotherController.inflection_method(nil)}.to raise_error
expect{AnotherController.inflection_method([nil])}.to raise_error
expect{AnotherController.inflection_method([''])}.to raise_error
expect{AnotherController.inflection_method([])}.to raise_error
expect{AnotherController.inflection_method({})}.to raise_error
end
end
describe ".no_inflection_method" do
before do
class AnotherController < InflectedTranslateController; end
end
it "should be albe to split a mehtod of the inflection kind" do
expect{AnotherController.no_inflection_method(:users_gender)}.not_to raise_error
end
it "should be albe to accept single Symbol argument" do
expect{AnotherController.no_inflection_method(:time)}.not_to raise_error
expect{AnotherController.no_inflection_method(:@time)}.not_to raise_error
end
it "should be albe to accept single String argument" do
expect{AnotherController.no_inflection_method('time')}.not_to raise_error
expect{AnotherController.no_inflection_method('@time')}.not_to raise_error
end
it "should be albe to accept Array<Symbol> argument" do
expect{AnotherController.no_inflection_method([:time])}.not_to raise_error
expect{AnotherController.no_inflection_method([:@time])}.not_to raise_error
end
it "should raise an error when method name is wrong" do
expect{AnotherController.no_inflection_method}.to raise_error
expect{AnotherController.no_inflection_method(nil)}.to raise_error
expect{AnotherController.no_inflection_method([nil])}.to raise_error
expect{AnotherController.no_inflection_method(:"@")}.to raise_error
expect{AnotherController.no_inflection_method([''])}.to raise_error
expect{AnotherController.no_inflection_method([])}.to raise_error
expect{AnotherController.no_inflection_method({})}.to raise_error
end
end
describe ".no_inflection_kind" do
before do
class AnotherController < InflectedTranslateController; end
end
it "should be albe to spit a mehtod of the inflection kind" do
expect{AnotherController.no_inflection_kind(:gender)}.not_to raise_error
expect{AnotherController.no_inflection_kind(:@gender)}.not_to raise_error
end
it "should be albe to accept single Symbol argument" do
expect{AnotherController.no_inflection_kind(:time)}.not_to raise_error
expect{AnotherController.no_inflection_kind(:@time)}.not_to raise_error
end
it "should be albe to accept single String argument" do
expect{AnotherController.no_inflection_kind('time')}.not_to raise_error
expect{AnotherController.no_inflection_kind('@time')}.not_to raise_error
end
it "should be albe to accept Array<Symbol> argument" do
expect{AnotherController.no_inflection_kind([:time])}.not_to raise_error
expect{AnotherController.no_inflection_kind([:@time])}.not_to raise_error
end
it "should raise an error when method name is wrong" do
expect{AnotherController.no_inflection_kind}.to raise_error
expect{AnotherController.no_inflection_kind(nil)}.to raise_error
expect{AnotherController.no_inflection_kind(:"@")}.to raise_error
expect{AnotherController.no_inflection_kind([nil])}.to raise_error
expect{AnotherController.no_inflection_kind([''])}.to raise_error
expect{AnotherController.no_inflection_kind([])}.to raise_error
expect{AnotherController.no_inflection_kind({})}.to raise_error
end
end
describe ".i18n_inflector_kinds" do
before do
InflectedTranslateController.inflection_method(:users_gender => :gender)
InflectedTranslateController.inflection_method(:time)
@expected_hash = { :gender => :users_gender, :time => :time }
end
it "should be callable" do
expect{InflectedTranslateController.i18n_inflector_kinds}.not_to raise_error
end
it "should be able to read methods assigned to inflection kinds" do
expect(InflectedTranslateController.i18n_inflector_kinds).to eq(@expected_hash)
end
end
describe "controller instance methods" do
before do
class InflectedTranslateController
def trn(*args, **opts); t(*args, **opts) end
def t_male; t('welcome') end
def users_gender; :m end
def time
kind, locale = yield
kind == :time ? :past : nil
end
end
class InflectedStrictController
inflection_method :@gender
def gender; :m end
def trn(*args); translate(*args) end
end
class InflectedStrictOverrideController < InflectedTranslateController
inflection_method :users_dude => :@gender
inflection_method :users_female => :gender
inflection_method :person_i => :person
no_inflection_method_for :@person
def users_female; :f end
def users_dude; :d end
def person_i; :i end
end
class NomethodController < InflectedTranslateController
inflection_method :nonexistent => :gender
end
class MethodDisabledController < InflectedTranslateController
no_inflection_method :users_gender
end
@controller = InflectedTranslateController.new
@strict_controller = InflectedStrictController.new
@strict_over_controller = InflectedStrictOverrideController.new
@disabled_controller = MethodDisabledController.new
@nomethod_controller = NomethodController.new
end
describe "#i18n_inflector_kinds" do
before do
@expected_hash = {:gender => :users_gender, :time => :time }
end
it "should be able to read methods assigned to inflection kinds" do
expect(@controller.i18n_inflector_kinds).to eq(@expected_hash)
end
end
describe "#translate" do
it "should translate using inflection patterns and pick up the right value" do
expect(@controller.trn('welcome')).to eq('Dear Sir!')
expect(@controller.trn('welcome_strict')).to eq('Dear Sir!')
expect(@strict_controller.trn('welcome_strict')).to eq('Dear Sir!')
end
it "should make use of a block passed to inflection method" do
expect(@controller.trn('hitime')).to eq('past!')
end
it "should make use of inherited inflection method assignments" do
expect(@strict_over_controller.trn('hitime')).to eq('past!')
end
it "should make use of overriden inflection method assignments" do
expect(@strict_over_controller.trn('welcome')).to eq('Dear Lady!')
end
it "should prioritize strict kinds when both inflection options are passed" do
expect(@strict_over_controller.trn('welcome_strict')).to eq('Dear Dude!')
expect(@strict_over_controller.trn('welcome')).to eq('Dear Lady!')
end
it "should use regular kind option when strict kind option is missing" do
expect(@strict_over_controller.trn('to_be')).to eq('Oh I am')
expect(@strict_over_controller.trn('to_be_strict')).to eq('Oh me')
end
it "should make use of disabled inflection method assignments" do
expect(@disabled_controller.trn('welcome')).to eq('Dear You!')
end
it "should raise exception when method does not exists" do
expect{@nomethod_controller.translated_male}.to raise_error(NameError)
end
it "should not raise when method does not exists and verify_methods is enabled" do
expect{@nomethod_controller.trn('welcome', :inflector_verify_methods => true)}.not_to raise_error
I18n.inflector.options.verify_methods = true
expect{@nomethod_controller.trn('welcome')}.not_to raise_error
end
it "should translate with the :inflector_lazy_methods switch turned off" do
expect(@strict_over_controller.trn('welcome', :inflector_lazy_methods => false)).to eq('Dear Lady!')
end
it "should omit pattern interpolation when locale is not inflected" do
expect(@strict_over_controller.trn('welcome', :locale => :ns)).to eq('Dear !')
end
end
describe "#t" do
it "should call translate" do
expect(@controller.t_male).to eq('Dear Sir!')
end
end
end
end
|