# encoding: utf-8 # # This file is part of the lazier gem. Copyright (C) 2013 and above Shogun . # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php. # require "spec_helper" describe Lazier::I18n do subject { c = ::Object.new c.class_eval { include Lazier::I18n } c } let(:root_path) { ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") } before(:each) do ENV["LANG"] = "en" allow(::R18n::I18n).to receive(:system_locale).and_return("en") end describe "#i18n_setup" do it "should set the root and the path" do subject.i18n_setup("ROOT", root_path) expect(subject.instance_variable_get(:@i18n_root)).to eq(:ROOT) expect(subject.instance_variable_get(:@i18n_locales_path)).to eq(root_path) end end describe "#i18n" do it "should call the private method if nothing is set" do subject.instance_variable_set(:@i18n, nil) expect(subject).to receive(:i18n_load_locale) subject.i18n end end describe "#i18n=" do it "should call the private method if nothing is set" do expect(subject).to receive(:i18n_load_locale).and_return("LOCALE") subject.i18n = :en expect(subject.instance_variable_get(:@i18n)).to eq("LOCALE") end end describe "#i18n_load_locale" do it "should set using system locale if called without arguments" do subject.i18n_setup("lazier", root_path) expect(::R18n::I18n).to receive(:new).with([ENV["LANG"]].compact.uniq, root_path).and_call_original subject.i18n = nil end it "should set the requested locale" do subject.i18n_setup("lazier", root_path) expect(::R18n::I18n).to receive(:new).with(["it", ENV["LANG"]].compact.uniq, root_path).and_call_original subject.i18n = :it end it "should call the root" do Lazier.load! t = ::Object.new expect_any_instance_of(::R18n::I18n).to receive(:t).and_return(t) expect(t).to receive("ROOT") subject.i18n_setup("ROOT", root_path) subject.i18n = :it end it "should only pass valid translations" do subject.i18n_setup("lazier", root_path) expect(::R18n::I18n).to receive(:new).with([ENV["LANG"]].compact.uniq, root_path).and_call_original subject.i18n = "INVALID" end it "should raise an exception if no valid translation are found" do ENV["LANG"] = "INVALID" allow(::R18n::I18n).to receive(:system_locale).and_return("INVALID") subject.i18n_setup("ROOT", "/dev/") expect { subject.i18n = "INVALID" }.to raise_error(::Lazier::Exceptions::MissingTranslation) end end end