spec/ruby/i18n/js/segment_spec.rb in i18n-js-3.8.2 vs spec/ruby/i18n/js/segment_spec.rb in i18n-js-3.8.3

- old
+ new

@@ -129,12 +129,12 @@ it "should write the file" do file_should_exist "segment.js" expect(File.open(File.join(temp_path, "segment.js")){|f| f.read}).to eql <<-EOF MyNamespace.translations || (MyNamespace.translations = {}); -MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), {"test":"Test"}); -MyNamespace.translations["fr"] = I18n.extend((MyNamespace.translations["fr"] || {}), {"test":"Test2"}); +MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"test":"Test"}')); +MyNamespace.translations["fr"] = I18n.extend((MyNamespace.translations["fr"] || {}), JSON.parse('{"test":"Test2"}')); EOF end end context "when file includes %{locale}" do @@ -144,31 +144,73 @@ file_should_exist "en.js" file_should_exist "fr.js" expect(File.open(File.join(temp_path, "en.js")){|f| f.read}).to eql <<-EOF MyNamespace.translations || (MyNamespace.translations = {}); -MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), {"test":"Test"}); +MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"test":"Test"}')); EOF expect(File.open(File.join(temp_path, "fr.js")){|f| f.read}).to eql <<-EOF MyNamespace.translations || (MyNamespace.translations = {}); -MyNamespace.translations["fr"] = I18n.extend((MyNamespace.translations["fr"] || {}), {"test":"Test2"}); +MyNamespace.translations["fr"] = I18n.extend((MyNamespace.translations["fr"] || {}), JSON.parse('{"test":"Test2"}')); EOF end end + context "when file includes single quote" do + let(:file){ "tmp/i18n-js/%{locale}.js" } + let(:translations){ { en: { "a" => "Test's" } } } + + it "should write files" do + file_should_exist "en.js" + + expect(File.open(File.join(temp_path, "en.js")){|f| f.read}).to eql <<-EOF +MyNamespace.translations || (MyNamespace.translations = {}); +MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"a":"Test\\'s"}')); + EOF + end + end + + context "when file includes escaped double quote" do + let(:file){ "tmp/i18n-js/%{locale}.js" } + let(:translations){ { en: { "a" => 'say "hello"' } } } + + it "should escape double quote" do + file_should_exist "en.js" + + expect(File.open(File.join(temp_path, "en.js")){|f| f.read}).to eql <<-EOF +MyNamespace.translations || (MyNamespace.translations = {}); +MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"a":"say \\\\"hello\\\\""}')); + EOF + end + end + + context "when file includes backslash in double quote" do + let(:file){ "tmp/i18n-js/%{locale}.js" } + let(:translations){ { en: { "double-backslash-in-double-quote" => '"\\\\"' } } } + + it "should escape backslash" do + file_should_exist "en.js" + + expect(File.open(File.join(temp_path, "en.js")){|f| f.read}).to eql <<-EOF +MyNamespace.translations || (MyNamespace.translations = {}); +MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"double-backslash-in-double-quote":"\\\\"\\\\\\\\\\\\\\\\\\\\""}')); + EOF + end + end + context "when js_extend is true" do let(:js_extend){ true } let(:translations){ { en: { "b" => "Test", "a" => "Test" } } } it 'should output the keys as sorted' do file_should_exist "segment.js" expect(File.open(File.join(temp_path, "segment.js")){|f| f.read}).to eql <<-EOF MyNamespace.translations || (MyNamespace.translations = {}); -MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), {"a":"Test","b":"Test"}); +MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"a":"Test","b":"Test"}')); EOF end end context "when js_extend is false" do @@ -179,11 +221,11 @@ it 'should output the keys as sorted' do file_should_exist "segment.js" expect(File.open(File.join(temp_path, "segment.js")){|f| f.read}).to eql <<-EOF MyNamespace.translations || (MyNamespace.translations = {}); -MyNamespace.translations["en"] = {"a":"Test","b":"Test"}; +MyNamespace.translations["en"] = JSON.parse('{"a":"Test","b":"Test"}'); EOF end end context "when sort_translation_keys is true" do @@ -194,11 +236,11 @@ it 'should output the keys as sorted' do file_should_exist "segment.js" expect(File.open(File.join(temp_path, "segment.js")){|f| f.read}).to eql <<-EOF MyNamespace.translations || (MyNamespace.translations = {}); -MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), {"a":"Test","b":"Test"}); +MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"a":"Test","b":"Test"}')); EOF end end context "when sort_translation_keys is false" do @@ -209,10 +251,10 @@ it 'should output the keys as sorted' do file_should_exist "segment.js" expect(File.open(File.join(temp_path, "segment.js")){|f| f.read}).to eql <<-EOF MyNamespace.translations || (MyNamespace.translations = {}); -MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), {"b":"Test","a":"Test"}); +MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"b":"Test","a":"Test"}')); EOF end end end end