Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_generate_localization_file.rb « test - github.com/mapsme/twine.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6d4bb494360b3c6b7e7860b6fe74e351c784c273 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'command_test'

class TestGenerateLocalizationFile < CommandTest
  def new_runner(language, file)
    options = {}
    options[:output_path] = File.join(@output_dir, file) if file
    options[:languages] = language if language

    twine_file = Twine::TwineFile.new
    twine_file.language_codes.concat KNOWN_LANGUAGES

    Twine::Runner.new(options, twine_file)
  end

  def prepare_mock_format_file_formatter(formatter_class)
    formatter = prepare_mock_formatter(formatter_class)
    formatter.expects(:format_file).returns(true)
  end

  def test_deducts_android_format_from_output_path
    prepare_mock_format_file_formatter Twine::Formatters::Android

    new_runner('fr', 'fr.xml').generate_localization_file
  end

  def test_deducts_apple_format_from_output_path
    prepare_mock_format_file_formatter Twine::Formatters::Apple

    new_runner('fr', 'fr.strings').generate_localization_file
  end

  def test_deducts_jquery_format_from_output_path
    prepare_mock_format_file_formatter Twine::Formatters::JQuery

    new_runner('fr', 'fr.json').generate_localization_file
  end

  def test_deducts_gettext_format_from_output_path
    prepare_mock_format_file_formatter Twine::Formatters::Gettext

    new_runner('fr', 'fr.po').generate_localization_file
  end

  def test_deducts_django_format_from_output_path
    prepare_mock_format_file_formatter Twine::Formatters::Django

    new_runner('fr', 'fr.po').generate_localization_file
  end

  def test_returns_error_for_ambiguous_output_path
    # both Gettext and Django use .po
    gettext_formatter = prepare_mock_formatter(Twine::Formatters::Gettext)
    gettext_formatter.stubs(:format_file).returns(true)
    django_formatter = prepare_mock_formatter(Twine::Formatters::Django, false)
    django_formatter.stubs(:format_file).returns(true)

    assert_raises Twine::Error do
      new_runner('fr', 'fr.po').generate_localization_file
    end
  end

  def test_deducts_language_from_output_path
    random_language = KNOWN_LANGUAGES.sample
    formatter = prepare_mock_formatter Twine::Formatters::Android
    formatter.expects(:format_file).with(random_language).returns(true)

    new_runner(nil, "#{random_language}.xml").generate_localization_file
  end

  def test_returns_error_if_nothing_written
    formatter = prepare_mock_formatter Twine::Formatters::Android
    formatter.expects(:format_file).returns(false)

    assert_raises Twine::Error do
      new_runner('fr', 'fr.xml').generate_localization_file
    end
  end

  class TestValidate < CommandTest
    def new_runner(validate)
      options = {}
      options[:output_path] = @output_path
      options[:languages] = ['en']
      options[:format] = 'android'
      options[:validate] = validate

      twine_file = build_twine_file 'en' do
        add_section 'Section' do
          add_definition key: 'value'
          add_definition key: 'value'
        end
      end

      Twine::Runner.new(options, twine_file)
    end

    def test_does_not_validate_twine_file
      prepare_mock_formatter Twine::Formatters::Android

      new_runner(false).generate_localization_file
    end

    def test_validates_twine_file_if_validate
      assert_raises Twine::Error do
        new_runner(true).generate_localization_file
      end
    end
  end
end