require 'spec_helper'

describe TranslationIO::Client::BaseOperation::SaveNewYamlFilesStep do

  it 'saves new YAML files' do
    target_locales    = ['fr', 'nl']
    yaml_locales_path = 'tmp/config/locales'

    yaml_po_data_fr = <<-EOS
msgctxt "main.bye"
msgid "Bye world"
msgstr "Au revoir le monde"

msgctxt "other.stuff"
msgid "This is stuff"
msgstr "Ce sont des choses"
EOS

    yaml_po_data_nl = <<-EOS
msgctxt "main.bye"
msgid "Bye world"
msgstr "Afscheid van de wereld"

msgctxt "other.stuff"
msgid "This is stuff"
msgstr ""
EOS

    parsed_response = {
      'yaml_po_data_fr' => yaml_po_data_fr,
      'yaml_po_data_nl' => yaml_po_data_nl,
      'project_url'     => 'https://translation.io/alfred/dummy'
    }

    operation_step = TranslationIO::Client::BaseOperation::SaveNewYamlFilesStep.new(target_locales, yaml_locales_path, parsed_response)
    operation_step.run

    expected_yaml_content_fr = <<-EOS
# WARNING. THIS FILE WAS AUTO-GENERATED BY THE TRANSLATION GEM.
# IF YOU UPDATE IT, YOUR CHANGES WILL BE LOST AT THE NEXT SYNC.
#
# To update this file, use this translation interface:
# https://translation.io/alfred/dummy/fr
#
---
fr:
  main:
    bye: Au revoir le monde
  other:
    stuff: Ce sont des choses
EOS

    expected_yaml_content_nl = <<-EOS
# WARNING. THIS FILE WAS AUTO-GENERATED BY THE TRANSLATION GEM.
# IF YOU UPDATE IT, YOUR CHANGES WILL BE LOST AT THE NEXT SYNC.
#
# To update this file, use this translation interface:
# https://translation.io/alfred/dummy/nl
#
---
nl:
  main:
    bye: Afscheid van de wereld
  other:
    stuff:
EOS

    File.read('tmp/config/locales/translation.fr.yml').strip.should == expected_yaml_content_fr.strip
    File.read('tmp/config/locales/translation.nl.yml').strip.should == expected_yaml_content_nl.strip
  end

  it 'saves new YAML files with a custom yaml_line_width and no extra trailing space' do
    TranslationIO.config.yaml_line_width = 30

    target_locales    = ['fr']
    yaml_locales_path = 'tmp/config/locales'

    # greetings with empty translation is here to test that extra space is absent in resulting YAML
    # https://github.com/translation/rails/issues/13
    yaml_po_data_fr = <<-EOS
msgctxt "main.greetings"
msgid "Hello"
msgstr ""

msgctxt "main.bye"
msgid "Bye world"
msgstr "Au revoir le monde avec une phrase particulièrement longue et poétique."
EOS

    parsed_response = {
      'yaml_po_data_fr' => yaml_po_data_fr,
      'project_url'     => 'https://translation.io/alfred/dummy'
    }

    operation_step = TranslationIO::Client::BaseOperation::SaveNewYamlFilesStep.new(target_locales, yaml_locales_path, parsed_response)
    operation_step.run


    # ruby <= 2.2
    rails_2_2_expected_yaml_content_fr = <<-EOS
# WARNING. THIS FILE WAS AUTO-GENERATED BY THE TRANSLATION GEM.
# IF YOU UPDATE IT, YOUR CHANGES WILL BE LOST AT THE NEXT SYNC.
#
# To update this file, use this translation interface:
# https://translation.io/alfred/dummy/fr
#
---
fr:
  main:
    greetings:
    bye: >-
      Au revoir le monde avec une
      phrase particulièrement longue
      et poétique.
EOS

    # ruby >= 2.3
    rails_2_3_expected_yaml_content_fr = <<-EOS
# WARNING. THIS FILE WAS AUTO-GENERATED BY THE TRANSLATION GEM.
# IF YOU UPDATE IT, YOUR CHANGES WILL BE LOST AT THE NEXT SYNC.
#
# To update this file, use this translation interface:
# https://translation.io/alfred/dummy/fr
#
---
fr:
  main:
    greetings:
    bye: Au revoir le monde avec
      une phrase particulièrement
      longue et poétique.
EOS

    File.read('tmp/config/locales/translation.fr.yml').strip.should be_in [
      rails_2_2_expected_yaml_content_fr.strip,
      rails_2_3_expected_yaml_content_fr.strip
    ]
  end
end
