require "rubygems" require "ruby-debug" require "spec" require "mocha" require "pp" require File.join(File.dirname(__FILE__), 'spec_helper') describe "MissingT" do before do @missing_t = MissingT.new @es_translations = {"es"=> {"zoo"=>{"elephant"=>"elefante", "bear"=>"oso", "lion"=>"leon", "bee" => "abeja"}, "lamp"=>"lampa", "book"=>"libro", "handkerchief"=>"panuelo", "pen" => "boli"}} @fr_translations = {"fr"=> {"zoo"=>{"elephant"=>"elephant", "bear"=>"ours", "lion"=>"lion", "wasp" => "guepe"}, "lamp"=>"lampe", "book"=>"livre", "handkerchief"=>"mouchoir", "mother" => "mere"}} @other_es_translations = { "es" => {"zoo" => {}}} @yet_other_es_translations = { "es" => {"zoo" => {"monkey" => "mono", "horse" => "caballo"}}} end describe "adding translations" do before do @missing_t.add_translations(@es_translations) end it "should pick up the new translations" do @missing_t.translations.should == @es_translations end it "should correctly merge different translations" do @missing_t.add_translations(@fr_translations) @missing_t["fr"]["zoo"].should have_key("wasp") @missing_t["fr"].should have_key("mother") @missing_t["es"]["zoo"].should have_key("bee") end it "should not overwrite translations keys" do @missing_t.add_translations(@other_es_translations) @missing_t["es"]["zoo"].should have_key("bear") @missing_t["es"]["zoo"].should have_key("bee") end it "should add the new translations even if they contain keys already in the translations hash" do @missing_t.add_translations(@yet_other_es_translations) @missing_t["es"]["zoo"].should have_key("monkey") @missing_t["es"]["zoo"].should have_key("bear") end end describe "hashification" do before do queries = ["zoo.bee", "zoo.departments.food", "zoo.departments.qa", "lamp", "mother", "mother.maiden_name"] @queries_hash = @missing_t.hashify(queries) @h = { "fr" => { "book" => "livre", "zoo" => {"elephant" => "elephant"} } } end it "should find a nested key and return it" do @h.should have_nested_key('fr.zoo.elephant') @h.should have_nested_key('fr.book') end it "should return false when it does not have a nested key" do @h.should_not have_nested_key('fr.zoo.seal') @h.should_not have_nested_key('xxx') end it "an empty hash should not have any nested keys" do {}.should_not have_nested_key(:puppy) end it "should turn strings to hash keys along their separators (dots)" do ["zoo", "lamp", "mother"].all? { |k| @queries_hash.key?(k) }.should == true ["bee", "departments"].all? { |k| @queries_hash["zoo"].key?(k) }.should == true @queries_hash["zoo"]["departments"].should have_key("food") @queries_hash["zoo"]["departments"].should have_key("qa") end end describe "extracting i18n queries" do before do content = <<-EOS