<commit_before># -*- coding: UTF-8 -*-

require 'spec_helper'
require 'tmpdir'
require 'gettext/tools'

# Stolen from ActiveSupport. We have to cut and paste it here so it
# does not turn the encoding back to US-ASCII. Strange issue.
class String
  def strip_heredoc
    indent = scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
    gsub(/^[ \t]{#{indent}}/, '')
  end
end

module Haml
  describe MagicTranslations do
    describe '.enable' do
      after { Haml::MagicTranslations.disable }
      context 'when using :i18n as backend' do
        before { Haml::MagicTranslations.enable :i18n }
        it { Haml::MagicTranslations.should be_enabled }
        it { Haml::MagicTranslations::EngineMethods.
                 magic_translations_helpers.should == I18n::Gettext::Helpers }
      end
      context 'when using :gettext as backend' do
        before { Haml::MagicTranslations.enable :gettext }
        it { Haml::MagicTranslations.should be_enabled }
        it { Haml::MagicTranslations::EngineMethods.
               magic_translations_helpers.should == GetText }
      end
      context 'when using :fast_gettext as backend' do
        before { Haml::MagicTranslations.enable :fast_gettext }
        it { Haml::MagicTranslations.should be_enabled }
        it { Haml::MagicTranslations::EngineMethods.
               magic_translations_helpers.should == FastGettext::Translation }
      end
      context 'when giving another backend' do
        it 'should raise an error' do
          expect {
            Haml::MagicTranslations.enable :whatever
          }.to raise_error(ArgumentError)
        end
        it { Haml::MagicTranslations.should_not be_enabled }
      end
    end

    describe '.disable' do
      it 'should set Haml::MagicTranslations.enabled to false' do
        Haml::MagicTranslations.disable
        Haml::MagicTranslations.should_not be_enabled
      end
    end

    shared_examples 'Haml magic translations' do
      it 'should translate text using existing locales' do
        render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
          %p Magic translations works!
          %p Here with interpolation, and everything thanks to #{I18n.name} and #{GetText.name}
        HAML
          <p>Magiczne tłumaczenie działa!</p>
          <p>A tutaj razem z interpolacją, a to wszystko dzięki połączeniu I18n i GetText</p>
        HTML
      end

      it 'should leave text without changes when translation is not available' do
        render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
          %p Untranslated thanks to #{I18n.name} and #{GetText.name}
        HAML
          <p>Untranslated thanks to I18n and GetText</p>
        HTML
      end

      it 'should translate text with multiline plain text' do
       render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
          %p Magic translations works!
          %p
            Now we will check multiline strings,
            which should be also translated,
            with interpolation #{'Interpolation'.upcase}
        HAML
          <p>Magiczne tłumaczenie działa!</p>
          <p>
            Kolejny wieloliniowy tekst,
            który powinien zostać przetłumaczony,
            interpolacja INTERPOLATION też działa!
          </p>
        HTML
      end

      it 'should translate escaped tags' do
        render(<<-HAML.strip_heredoc).should == <<-HTML.strip_heredoc
          %p& Magic translations works!
        HAML
          <p>Magiczne tłumaczenie działa!</p>
        HTML
      end

      it 'should translate unescaped tags' do
        render(<<-HAML.strip_heredoc).should == <<-HTML.strip_heredoc
          %p! Magic translations works!
        HAML
          <p>Magiczne tłumaczenie działa!</p>
        HTML
      end

      it 'should not translate evaluated tags' do
        render(<<-HAML.strip_heredoc).should == <<-HTML.strip_heredoc
          %p= 'Magic translations works!'
        HAML
          <p>Magic translations works!</p>
        HTML
      end

      it 'should not translate escaped evaluated tags' do
        render(<<-HAML.strip_heredoc).should == <<-HTML.strip_heredoc
          %p&= 'Magic translations works!'
        HAML
          <p>Magic translations works!</p>
        HTML
      end

      it 'should not translate unescaped evaluated tags' do
        render(<<-HAML.strip_heredoc).should == <<-HTML.strip_heredoc
          %p!= 'Magic translations works!'
        HAML
          <p>Magic translations works!</p>
        HTML
      end

      context 'when translating strings in Javascript' do
        it "should translate strings inside _('')" do
          render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
            :javascript
              var text = _('Magic translations works!');
          HAML
            <script type='text/javascript'>
              //<![CDATA[
                var text = "Magiczne tłumaczenie działa!";
              //]]>
            </script>
          HTML
        end
        it 'should translate strings inside _("")' do
          render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
            :javascript
              var text = _("Magic translations works!");
          HAML
            <script type='text/javascript'>
              //<![CDATA[
                var text = "Magiczne tłumaczenie działa!";
              //]]>
            </script>
          HTML
        end
        it 'should not choke on single-quote' do
          render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
            :javascript
              var text = _("Don't you think?");
          HAML
            <script type='text/javascript'>
              //<![CDATA[
                var text = "Don't you think?";
              //]]>
            </script>
          HTML
        end<commit_msg>Adjust translated JavaScript strings in specs<commit_after># -*- coding: UTF-8 -*-

require 'spec_helper'
require 'tmpdir'
require 'gettext/tools'

# Stolen from ActiveSupport. We have to cut and paste it here so it
# does not turn the encoding back to US-ASCII. Strange issue.
class String
  def strip_heredoc
    indent = scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
    gsub(/^[ \t]{#{indent}}/, '')
  end
end

module Haml
  describe MagicTranslations do
    describe '.enable' do
      after { Haml::MagicTranslations.disable }
      context 'when using :i18n as backend' do
        before { Haml::MagicTranslations.enable :i18n }
        it { Haml::MagicTranslations.should be_enabled }
        it { Haml::MagicTranslations::EngineMethods.
                 magic_translations_helpers.should == I18n::Gettext::Helpers }
      end
      context 'when using :gettext as backend' do
        before { Haml::MagicTranslations.enable :gettext }
        it { Haml::MagicTranslations.should be_enabled }
        it { Haml::MagicTranslations::EngineMethods.
               magic_translations_helpers.should == GetText }
      end
      context 'when using :fast_gettext as backend' do
        before { Haml::MagicTranslations.enable :fast_gettext }
        it { Haml::MagicTranslations.should be_enabled }
        it { Haml::MagicTranslations::EngineMethods.
               magic_translations_helpers.should == FastGettext::Translation }
      end
      context 'when giving another backend' do
        it 'should raise an error' do
          expect {
            Haml::MagicTranslations.enable :whatever
          }.to raise_error(ArgumentError)
        end
        it { Haml::MagicTranslations.should_not be_enabled }
      end
    end

    describe '.disable' do
      it 'should set Haml::MagicTranslations.enabled to false' do
        Haml::MagicTranslations.disable
        Haml::MagicTranslations.should_not be_enabled
      end
    end

    shared_examples 'Haml magic translations' do
      it 'should translate text using existing locales' do
        render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
          %p Magic translations works!
          %p Here with interpolation, and everything thanks to #{I18n.name} and #{GetText.name}
        HAML
          <p>Magiczne tłumaczenie działa!</p>
          <p>A tutaj razem z interpolacją, a to wszystko dzięki połączeniu I18n i GetText</p>
        HTML
      end

      it 'should leave text without changes when translation is not available' do
        render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
          %p Untranslated thanks to #{I18n.name} and #{GetText.name}
        HAML
          <p>Untranslated thanks to I18n and GetText</p>
        HTML
      end

      it 'should translate text with multiline plain text' do
       render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
          %p Magic translations works!
          %p
            Now we will check multiline strings,
            which should be also translated,
            with interpolation #{'Interpolation'.upcase}
        HAML
          <p>Magiczne tłumaczenie działa!</p>
          <p>
            Kolejny wieloliniowy tekst,
            który powinien zostać przetłumaczony,
            interpolacja INTERPOLATION też działa!
          </p>
        HTML
      end

      it 'should translate escaped tags' do
        render(<<-HAML.strip_heredoc).should == <<-HTML.strip_heredoc
          %p& Magic translations works!
        HAML
          <p>Magiczne tłumaczenie działa!</p>
        HTML
      end

      it 'should translate unescaped tags' do
        render(<<-HAML.strip_heredoc).should == <<-HTML.strip_heredoc
          %p! Magic translations works!
        HAML
          <p>Magiczne tłumaczenie działa!</p>
        HTML
      end

      it 'should not translate evaluated tags' do
        render(<<-HAML.strip_heredoc).should == <<-HTML.strip_heredoc
          %p= 'Magic translations works!'
        HAML
          <p>Magic translations works!</p>
        HTML
      end

      it 'should not translate escaped evaluated tags' do
        render(<<-HAML.strip_heredoc).should == <<-HTML.strip_heredoc
          %p&= 'Magic translations works!'
        HAML
          <p>Magic translations works!</p>
        HTML
      end

      it 'should not translate unescaped evaluated tags' do
        render(<<-HAML.strip_heredoc).should == <<-HTML.strip_heredoc
          %p!= 'Magic translations works!'
        HAML
          <p>Magic translations works!</p>
        HTML
      end

      context 'when translating strings in Javascript' do
        it "should translate strings inside _('')" do
          render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
            :javascript
              var text = _('Magic translations works!');
          HAML
            <script type='text/javascript'>
              //<![CDATA[
                var text = "Magiczne t\u0142umaczenie dzia\u0142a!";
              //]]>
            </script>
          HTML
        end
        it 'should translate strings inside _("")' do
          render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
            :javascript
              var text = _("Magic translations works!");
          HAML
            <script type='text/javascript'>
              //<![CDATA[
                var text = "Magiczne t\u0142umaczenie dzia\u0142a!";
              //]]>
            </script>
          HTML
        end
        it 'should not choke on single-quote' do
          render(<<-'HAML'.strip_heredoc).should == <<-'HTML'.strip_heredoc
            :javascript
              var text = _("Don't you think?");
          HAML
            <script type='text/javascript'>
              //<![CDATA[
                var text = "Don't you think?";
              //]]>
            </script>
          HTML
        end
{'fraction_non_alphanumeric': 0.10273037542662115, 'fraction_numerical': 0.004095563139931741, 'mean_word_length': 2.017765190525232, 'pattern_counts': {'":': 0, '<': 149, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1769726', 'n_tokens_mistral': 3585, 'n_tokens_neox': 3325, 'n_words': 914}