haml-i18n-extractor 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +110 -0
- data/Rakefile +54 -0
- data/TODO +5 -0
- data/bin/haml-i18n-extractor +31 -0
- data/haml-i18n-extractor.gemspec +29 -0
- data/lib/haml-i18n-extractor/core-ext/hash.rb +8 -0
- data/lib/haml-i18n-extractor/extractor.rb +84 -0
- data/lib/haml-i18n-extractor/haml_reader.rb +20 -0
- data/lib/haml-i18n-extractor/haml_writer.rb +21 -0
- data/lib/haml-i18n-extractor/text_finder.rb +94 -0
- data/lib/haml-i18n-extractor/text_replacer.rb +78 -0
- data/lib/haml-i18n-extractor/version.rb +7 -0
- data/lib/haml-i18n-extractor/yaml_tool.rb +74 -0
- data/lib/haml-i18n-extractor.rb +7 -0
- data/test/extractor_test.rb +78 -0
- data/test/haml_reader_test.rb +29 -0
- data/test/haml_writer_test.rb +34 -0
- data/test/support/bad.haml +12 -0
- data/test/support/ex1.haml +28 -0
- data/test/support/ex1.i18n-extractor.haml +28 -0
- data/test/support/ex1.output.haml +28 -0
- data/test/support/nothing_to_translate.haml +2 -0
- data/test/support/nothing_to_translate.i18n-extractor.haml +2 -0
- data/test/test_helper.rb +98 -0
- data/test/text_finder_test.rb +130 -0
- data/test/text_replacer_test.rb +67 -0
- data/test/yaml_tool_test.rb +68 -0
- metadata +168 -0
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Haml
|
4
|
+
class ExtractorTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@ex1 = Haml::I18n::Extractor.new(file_path("ex1.haml"))
|
8
|
+
end
|
9
|
+
|
10
|
+
test "it can process the haml and replace it with other text!" do
|
11
|
+
@ex1.run
|
12
|
+
end
|
13
|
+
|
14
|
+
test "can not initialize if the haml is not valid syntax" do
|
15
|
+
begin
|
16
|
+
Haml::I18n::Extractor.new(file_path("bad.haml"))
|
17
|
+
assert false, "should not get here"
|
18
|
+
rescue Haml::I18n::Extractor::InvalidSyntax
|
19
|
+
assert true, "it should fail with invalid syntax"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
test "can initialize if the haml is valid syntax" do
|
24
|
+
# setup method initializes
|
25
|
+
assert true, "extractor can initialize"
|
26
|
+
end
|
27
|
+
|
28
|
+
test "it can replace a string body and have expected output" do
|
29
|
+
expected_output = File.read(file_path("ex1.output.haml"))
|
30
|
+
assert_equal @ex1.new_body, expected_output
|
31
|
+
end
|
32
|
+
|
33
|
+
test "it writes the haml to an out file if valid haml output" do
|
34
|
+
FileUtils.rm_rf(@ex1.haml_writer.path)
|
35
|
+
assert_equal File.exists?(@ex1.haml_writer.path), false
|
36
|
+
@ex1.run
|
37
|
+
assert_equal File.exists?(@ex1.haml_writer.path), true
|
38
|
+
end
|
39
|
+
|
40
|
+
test "it writes the locale info to an out file when run" do
|
41
|
+
assert_equal File.exists?(@ex1.yaml_tool.locale_file), false
|
42
|
+
@ex1.run
|
43
|
+
assert_equal File.exists?(@ex1.yaml_tool.locale_file), true
|
44
|
+
assert_equal YAML.load(File.read(@ex1.yaml_tool.locale_file)), @ex1.yaml_tool.yaml_hash
|
45
|
+
end
|
46
|
+
|
47
|
+
test "it raises if there is nothing to translate" do
|
48
|
+
begin
|
49
|
+
@nothing_to_translate = Haml::I18n::Extractor.new(file_path("nothing_to_translate.haml"))
|
50
|
+
assert_equal @ex1.yaml_tool.locale_hash, nil
|
51
|
+
@nothing_to_translate.run
|
52
|
+
rescue Haml::I18n::Extractor::NothingToTranslate
|
53
|
+
assert true, "NothingToTranslate raised"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
test "sends a hash over of replacement info to its yaml tool when run" do
|
58
|
+
@ex1 = Haml::I18n::Extractor.new(file_path("ex1.haml"))
|
59
|
+
assert_equal @ex1.yaml_tool.locale_hash, nil
|
60
|
+
@ex1.run
|
61
|
+
assert @ex1.yaml_tool.locale_hash.is_a?(Hash), "its is hash of info about the files lines"
|
62
|
+
assert_equal @ex1.yaml_tool.locale_hash.size, @ex1.haml_reader.lines.size
|
63
|
+
end
|
64
|
+
|
65
|
+
test "it fails before it writes to an out file if it is not valid" do
|
66
|
+
begin
|
67
|
+
@ex1 = Haml::I18n::Extractor.new(file_path("ex1.haml"))
|
68
|
+
@ex1.stub(:assign_new_body, nil) do #nop
|
69
|
+
@ex1.haml_writer.body = File.read(file_path("bad.haml"))
|
70
|
+
@ex1.run
|
71
|
+
end
|
72
|
+
rescue Haml::I18n::Extractor::InvalidSyntax
|
73
|
+
assert true, "it should not allow invalid output to be written"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module Haml
|
5
|
+
class HamlReaderTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
TEMP_FILE_PATH = "/tmp/foo_haml_extractor_test"
|
8
|
+
|
9
|
+
def setup
|
10
|
+
FileUtils.rm_rf(TEMP_FILE_PATH)
|
11
|
+
10.times do |index|
|
12
|
+
File.open(TEMP_FILE_PATH, "a+") do |f|
|
13
|
+
f.puts "line #{index}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
@reader = Haml::I18n::Extractor::HamlReader.new(TEMP_FILE_PATH)
|
17
|
+
end
|
18
|
+
|
19
|
+
test "has a body and a path" do
|
20
|
+
assert_equal @reader.path, TEMP_FILE_PATH
|
21
|
+
assert_equal @reader.body, File.read(TEMP_FILE_PATH)
|
22
|
+
end
|
23
|
+
|
24
|
+
test "has an array of lines in that file" do
|
25
|
+
assert_equal @reader.lines.size, 10
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module Haml
|
5
|
+
class HamlWriterTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
ORIG_TEMP_FILE_PATH = "/tmp/foo_haml_extractor_test.haml"
|
8
|
+
|
9
|
+
def setup
|
10
|
+
File.open(ORIG_TEMP_FILE_PATH, "w") do |f|
|
11
|
+
10.times do |i|
|
12
|
+
f.puts "line #{i}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def teardown
|
18
|
+
FileUtils.rm_rf(ORIG_TEMP_FILE_PATH)
|
19
|
+
end
|
20
|
+
|
21
|
+
test "is initialized with an array of lines, an original path and constructs a body and path with it" do
|
22
|
+
@writer = Haml::I18n::Extractor::HamlWriter.new(ORIG_TEMP_FILE_PATH)
|
23
|
+
assert_equal @writer.path, "/tmp/foo_haml_extractor_test.i18n-extractor.haml"
|
24
|
+
end
|
25
|
+
|
26
|
+
test "it can write the file" do
|
27
|
+
@writer = Haml::I18n::Extractor::HamlWriter.new(ORIG_TEMP_FILE_PATH)
|
28
|
+
@writer.body = "This is what it is"
|
29
|
+
@writer.write_file
|
30
|
+
assert_equal File.read(@writer.path), "This is what it is\n"
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#header.page
|
2
|
+
%p#brand= link_to "Some place", '/'
|
3
|
+
#menu
|
4
|
+
%ul.header-links
|
5
|
+
- if true
|
6
|
+
- if false
|
7
|
+
%li
|
8
|
+
%a#cheese-rice{:href => url_for([:admin, :dashboard])}
|
9
|
+
%span.thing-ok Admin
|
10
|
+
#bla
|
11
|
+
%ul#admin-dropdown-box
|
12
|
+
%li
|
13
|
+
%a{:href => url_for([:admin, :dashboard])}
|
14
|
+
%span Admin Dashboard
|
15
|
+
%li
|
16
|
+
%a{:href => url_for([:admin, :stacks])}
|
17
|
+
%span Stacks
|
18
|
+
%li
|
19
|
+
%a{:href => url_for([:admin, :environments])}
|
20
|
+
%span t('.alerts')
|
21
|
+
%li
|
22
|
+
%a{:href => url_for([:admin, :environments])}
|
23
|
+
%span= t('.alerts')
|
24
|
+
%li
|
25
|
+
%a{:href => url_for([:admin, :accounts])}
|
26
|
+
%span Accounts
|
27
|
+
- if true
|
28
|
+
What is@ supposed to be, is supposed to be! ~
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#header.page
|
2
|
+
%p#brand= link_to t('.some_place'), '/'
|
3
|
+
#menu
|
4
|
+
%ul.header-links
|
5
|
+
- if true
|
6
|
+
- if false
|
7
|
+
%li
|
8
|
+
%a#cheese-rice{:href => url_for([:admin, :dashboard])}
|
9
|
+
%span.thing-ok= t('.admin')
|
10
|
+
#bla
|
11
|
+
%ul#admin-dropdown-box
|
12
|
+
%li
|
13
|
+
%a{:href => url_for([:admin, :dashboard])}
|
14
|
+
%span= t('.admin_dashboard')
|
15
|
+
%li
|
16
|
+
%a{:href => url_for([:admin, :stacks])}
|
17
|
+
%span= t('.stacks')
|
18
|
+
%li
|
19
|
+
%a{:href => url_for([:admin, :environments])}
|
20
|
+
%span t('.alerts')
|
21
|
+
%li
|
22
|
+
%a{:href => url_for([:admin, :environments])}
|
23
|
+
%span= t('.alerts')
|
24
|
+
%li
|
25
|
+
%a{:href => url_for([:admin, :accounts])}
|
26
|
+
%span= t('.accounts')
|
27
|
+
- if true
|
28
|
+
= t('.what_is_supposed_to_be_is_supp')
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#header.page
|
2
|
+
%p#brand= link_to t('.some_place'), '/'
|
3
|
+
#menu
|
4
|
+
%ul.header-links
|
5
|
+
- if true
|
6
|
+
- if false
|
7
|
+
%li
|
8
|
+
%a#cheese-rice{:href => url_for([:admin, :dashboard])}
|
9
|
+
%span.thing-ok= t('.admin')
|
10
|
+
#bla
|
11
|
+
%ul#admin-dropdown-box
|
12
|
+
%li
|
13
|
+
%a{:href => url_for([:admin, :dashboard])}
|
14
|
+
%span= t('.admin_dashboard')
|
15
|
+
%li
|
16
|
+
%a{:href => url_for([:admin, :stacks])}
|
17
|
+
%span= t('.stacks')
|
18
|
+
%li
|
19
|
+
%a{:href => url_for([:admin, :environments])}
|
20
|
+
%span t('.alerts')
|
21
|
+
%li
|
22
|
+
%a{:href => url_for([:admin, :environments])}
|
23
|
+
%span= t('.alerts')
|
24
|
+
%li
|
25
|
+
%a{:href => url_for([:admin, :accounts])}
|
26
|
+
%span= t('.accounts')
|
27
|
+
- if true
|
28
|
+
= t('.what_is_supposed_to_be_is_supp')
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
#######################################################################
|
2
|
+
## copied over from haml for this to just work. TODO Cleanup. ##
|
3
|
+
## mini test stuff ##
|
4
|
+
#######################################################################
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
gem "minitest"
|
8
|
+
require 'bundler/setup'
|
9
|
+
require 'minitest/autorun'
|
10
|
+
require 'action_pack'
|
11
|
+
require 'action_controller'
|
12
|
+
require 'action_view'
|
13
|
+
require 'nokogiri'
|
14
|
+
require 'rails'
|
15
|
+
require 'fileutils'
|
16
|
+
|
17
|
+
require 'pry'
|
18
|
+
module Declarative
|
19
|
+
def test(name, &block)
|
20
|
+
define_method("test #{name}", &block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class MiniTest::Unit::TestCase
|
25
|
+
|
26
|
+
extend Declarative
|
27
|
+
|
28
|
+
def render(text, options = {}, base = nil, &block)
|
29
|
+
scope = options.delete(:scope) || Object.new
|
30
|
+
locals = options.delete(:locals) || {}
|
31
|
+
engine = Haml::Engine.new(text, options)
|
32
|
+
return engine.to_html(base) if base
|
33
|
+
engine.to_html(scope, locals, &block)
|
34
|
+
end
|
35
|
+
|
36
|
+
def assert_warning(message)
|
37
|
+
the_real_stderr, $stderr = $stderr, StringIO.new
|
38
|
+
yield
|
39
|
+
|
40
|
+
if message.is_a?(Regexp)
|
41
|
+
assert_match message, $stderr.string.strip
|
42
|
+
else
|
43
|
+
assert_equal message.strip, $stderr.string.strip
|
44
|
+
end
|
45
|
+
ensure
|
46
|
+
$stderr = the_real_stderr
|
47
|
+
end
|
48
|
+
|
49
|
+
def silence_warnings(&block)
|
50
|
+
Haml::Util.silence_warnings(&block)
|
51
|
+
end
|
52
|
+
|
53
|
+
def rails_form_opener
|
54
|
+
'<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>'
|
55
|
+
end
|
56
|
+
|
57
|
+
def assert_raises_message(klass, message)
|
58
|
+
yield
|
59
|
+
rescue Exception => e
|
60
|
+
assert_instance_of(klass, e)
|
61
|
+
assert_equal(message, e.message)
|
62
|
+
else
|
63
|
+
flunk "Expected exception #{klass}, none raised"
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.error(*args)
|
67
|
+
Haml::Error.message(*args)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
##############################
|
73
|
+
## project specific helpers ##
|
74
|
+
##############################
|
75
|
+
|
76
|
+
require File.dirname(__FILE__) + "/../lib/" + 'haml-i18n-extractor'
|
77
|
+
|
78
|
+
def file_path(name)
|
79
|
+
File.dirname(__FILE__) + "/support/#{name}"
|
80
|
+
end
|
81
|
+
|
82
|
+
def without_rails_mode
|
83
|
+
Object.send(:remove_const, :Rails) if defined?(Rails)
|
84
|
+
yield
|
85
|
+
end
|
86
|
+
|
87
|
+
def with_rails_mode
|
88
|
+
create_klass=<<EOR
|
89
|
+
module Rails
|
90
|
+
def self.root
|
91
|
+
"/data/current/name"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
EOR
|
95
|
+
eval create_klass
|
96
|
+
yield
|
97
|
+
end
|
98
|
+
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Haml
|
4
|
+
class StringFinderTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
# empty line
|
7
|
+
test "empty line does not explode" do
|
8
|
+
assert_equal find_text(""), ""
|
9
|
+
assert_equal find_type(""), :text
|
10
|
+
end
|
11
|
+
|
12
|
+
# regular text mode
|
13
|
+
test "regular text without whitespaces" do
|
14
|
+
assert_equal find_text("iphone"), "iphone"
|
15
|
+
assert_equal find_type("iphone"), :text
|
16
|
+
end
|
17
|
+
|
18
|
+
test "regular text with whitespaces" do
|
19
|
+
assert_equal find_text(" iphone4"), "iphone4"
|
20
|
+
assert_equal find_type(" iphone4"), :text
|
21
|
+
end
|
22
|
+
|
23
|
+
# silent script mode
|
24
|
+
test "silent scripts with strings" do
|
25
|
+
assert_equal find_text("- 'jessica'"), "jessica"
|
26
|
+
assert_equal find_type("- 'jessica'"), :silent
|
27
|
+
end
|
28
|
+
|
29
|
+
test "silent scripts with no strings" do
|
30
|
+
assert_equal find_text("- if true"), ""
|
31
|
+
assert_equal find_type("- if true"), :silent
|
32
|
+
end
|
33
|
+
|
34
|
+
# html element mode
|
35
|
+
test "%tag Text is an element" do
|
36
|
+
assert_equal find_text("%span Text to find"), "Text to find"
|
37
|
+
assert_equal find_type("%span Text to find"), :element
|
38
|
+
end
|
39
|
+
|
40
|
+
test "%tag Text with class is an element" do
|
41
|
+
assert_equal find_text("%span.whatever-with-thing Text to find"), "Text to find"
|
42
|
+
assert_equal find_type("%span.whatever-with-thing Text to find"), :element
|
43
|
+
end
|
44
|
+
|
45
|
+
test "%tag Text with ruby eval code is a loud script" do
|
46
|
+
assert_equal find_text("%span= ruby_eval_code"), ""
|
47
|
+
assert_equal find_type("%span= ruby_eval_code"), :loud
|
48
|
+
end
|
49
|
+
|
50
|
+
test "%tag Text with ruby eval code and class is a loud script" do
|
51
|
+
assert_equal find_text( "%span#whatever-with-thing= ruby_eval_code"), ""
|
52
|
+
assert_equal find_type( "%span#whatever-with-thing= ruby_eval_code"), :loud
|
53
|
+
end
|
54
|
+
|
55
|
+
test "Ruby style tags %a{'href' => 'http://whatever'} whatever" do
|
56
|
+
assert_equal find_text("%a{'href' => 'http://whatever'} whatever"), "whatever"
|
57
|
+
assert_equal find_type("%a{'href' => 'http://whatever'} whatever"), :element
|
58
|
+
end
|
59
|
+
|
60
|
+
# loud scripts / ruby eval mode
|
61
|
+
test "loud scripts with strings" do
|
62
|
+
assert_equal find_text('= "bob"'), "bob"
|
63
|
+
assert_equal find_text("= 'bob'"), "bob"
|
64
|
+
assert_equal find_type("= 'bob'"), :loud
|
65
|
+
end
|
66
|
+
|
67
|
+
test "loud scripts does not interpolate ruby vars in strings" do
|
68
|
+
assert_equal find_text('= "ruby can #{var}"'), "ruby can \#{var}"
|
69
|
+
assert_equal find_type('= "ruby can #{var}"'), :loud
|
70
|
+
end
|
71
|
+
|
72
|
+
# special loud scripts exceptions
|
73
|
+
test "it finds link_to texts as an exception to the rule" do
|
74
|
+
assert_equal find_text('= link_to "This should be found", "/"'), "This should be found"
|
75
|
+
assert_equal find_type('= link_to "This should be found", "/"'), :loud
|
76
|
+
end
|
77
|
+
|
78
|
+
test "it finds link_to texts as an exception to the rule and does not interpolate" do
|
79
|
+
assert_equal find_text('= "Statistics for #{@name}"'), "Statistics for \#{@name}"
|
80
|
+
assert_equal find_type('= "Statistics for #{@name}"'), :loud
|
81
|
+
end
|
82
|
+
|
83
|
+
# html element mode with ruby evaling
|
84
|
+
test "html element with ruby eval with strings" do
|
85
|
+
assert_equal find_text('%p= "bob"'), "bob"
|
86
|
+
assert_equal find_text("%p.what= 'bob'"), "bob"
|
87
|
+
assert_equal find_text("%p.what{:attr => :val}= 'bob'"), "bob"
|
88
|
+
assert_equal find_type("%p.what{:attr => :val}= 'bob'"), :loud
|
89
|
+
end
|
90
|
+
|
91
|
+
test "html element loud scripts does not interpolate ruby vars in strings" do
|
92
|
+
assert_equal find_text('%p= "ruby can #{var}"'), "ruby can \#{var}"
|
93
|
+
assert_equal find_text('%p.what= "ruby can #{var}"'), "ruby can \#{var}"
|
94
|
+
assert_equal find_text('%p.what{:attr => :val}= "ruby can #{var}"'), "ruby can \#{var}"
|
95
|
+
assert_equal find_type('%p.what{:attr => :val}= "ruby can #{var}"'), :loud
|
96
|
+
end
|
97
|
+
|
98
|
+
test "html element it finds link_to texts as an exception to the rule" do
|
99
|
+
assert_equal find_text('%p= link_to "This should be found", "/"'), "This should be found"
|
100
|
+
assert_equal find_text('%p.what= link_to "This should be found", "/"'), "This should be found"
|
101
|
+
assert_equal find_text('%p.what{:attr => :val}= link_to "This should be found", "/"'), "This should be found"
|
102
|
+
assert_equal find_type('%p.what{:attr => :val}= link_to "This should be found", "/"'), :loud
|
103
|
+
end
|
104
|
+
|
105
|
+
test "html element it finds link_to texts as an exception to the rule and does not interpolate" do
|
106
|
+
assert_equal find_text('%p= "Statistics for #{@name}"'), "Statistics for \#{@name}"
|
107
|
+
assert_equal find_text('%p.what= "Statistics for #{@name}"'), "Statistics for \#{@name}"
|
108
|
+
assert_equal find_text('%p.what{:attr => :val}= "Statistics for #{@name}"'), "Statistics for \#{@name}"
|
109
|
+
assert_equal find_type('%p.what{:attr => :val}= "Statistics for #{@name}"'), :loud
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def process_haml(haml)
|
115
|
+
text_finder = Haml::I18n::Extractor::TextFinder.new(haml)
|
116
|
+
text_finder.process_by_regex
|
117
|
+
end
|
118
|
+
|
119
|
+
def find_type(haml)
|
120
|
+
type, text = process_haml(haml)
|
121
|
+
type
|
122
|
+
end
|
123
|
+
|
124
|
+
def find_text(haml)
|
125
|
+
type, text = process_haml(haml)
|
126
|
+
text
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Haml
|
4
|
+
class TextReplacerTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
test "it initializes with the line it is going to replace and the match to replace" do
|
7
|
+
Haml::I18n::Extractor::TextReplacer.new("this is whatever", "this is whatever", :text)
|
8
|
+
end
|
9
|
+
|
10
|
+
test "but it raises if passed a wrong line type" do
|
11
|
+
begin
|
12
|
+
replacer = Haml::I18n::Extractor::TextReplacer.new("regular text", "regular text", :this_is_not_defined)
|
13
|
+
assert false, 'should raise'
|
14
|
+
rescue Haml::I18n::Extractor::NotDefinedLineType
|
15
|
+
assert true, 'raised NotDefinedLineType'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# some text replacement examples
|
20
|
+
test "it can replace the body of haml with t() characters" do
|
21
|
+
replacer = Haml::I18n::Extractor::TextReplacer.new("this is whatever", "this is whatever", :text)
|
22
|
+
assert_equal replacer.replace_hash, { :modified_line => "= t('.this_is_whatever')",
|
23
|
+
:keyname => "t('.this_is_whatever')", :replaced_text => "this is whatever" }
|
24
|
+
end
|
25
|
+
|
26
|
+
test "it can replace the body of haml with t() characters example" do
|
27
|
+
replacer = Haml::I18n::Extractor::TextReplacer.new("%span Admin Dashboard", "Admin Dashboard", :element)
|
28
|
+
assert_equal replacer.replace_hash, { :modified_line => "%span= t('.admin_dashboard')",
|
29
|
+
:keyname => "t('.admin_dashboard')", :replaced_text => "Admin Dashboard" }
|
30
|
+
end
|
31
|
+
|
32
|
+
test "it won't replace already replaced t() characters if they are not ruby evaled" do
|
33
|
+
replacer = Haml::I18n::Extractor::TextReplacer.new("%span t('.admin_dashboard')", "t('.admin_dashboard')", :element)
|
34
|
+
assert_equal replacer.replace_hash, { :modified_line => "%span t('.admin_dashboard')",
|
35
|
+
:keyname => "t('.admin_dashboard')", :replaced_text => "t('.admin_dashboard')" }
|
36
|
+
end
|
37
|
+
|
38
|
+
test "it won't replace already replaced t() characters that are ruby evaled" do
|
39
|
+
replacer = Haml::I18n::Extractor::TextReplacer.new("%span= t('.admin_dashboard')", "t('.admin_dashboard')", :loud)
|
40
|
+
assert_equal replacer.replace_hash, { :modified_line => "%span= t('.admin_dashboard')",
|
41
|
+
:keyname => "t('.admin_dashboard')", :replaced_text => "t('.admin_dashboard')" }
|
42
|
+
end
|
43
|
+
|
44
|
+
test "it can replace the body of haml with t() characters example for link_to and removes surrounding quotes as well" do
|
45
|
+
replacer = Haml::I18n::Extractor::TextReplacer.new(%{%p#brand= link_to 'Some Place', '/'}, "Some Place", :loud)
|
46
|
+
assert_equal replacer.replace_hash, { :modified_line => %{%p#brand= link_to t('.some_place'), '/'} ,
|
47
|
+
:keyname => "t('.some_place')", :replaced_text => "Some Place" }
|
48
|
+
|
49
|
+
replacer = Haml::I18n::Extractor::TextReplacer.new(%{%p#brand= link_to "Some Place", "/"}, "Some Place", :loud)
|
50
|
+
assert_equal replacer.replace_hash, { :modified_line => %{%p#brand= link_to t('.some_place'), "/"} ,
|
51
|
+
:keyname => "t('.some_place')", :replaced_text => "Some Place" }
|
52
|
+
end
|
53
|
+
|
54
|
+
# keyname restrictions
|
55
|
+
test "it limits the characters of the t namespace it provides to LIMIT_KEY_NAME" do
|
56
|
+
replacer = Haml::I18n::Extractor::TextReplacer.new("this is whatever" * 80, "this is whatever" * 80, :text)
|
57
|
+
assert_equal replacer.replace_hash[:modified_line].size, Haml::I18n::Extractor::TextReplacer::LIMIT_KEY_NAME + 8 # = t('.')
|
58
|
+
end
|
59
|
+
|
60
|
+
test "it does not allow weird characters in the keyname" do
|
61
|
+
replacer = Haml::I18n::Extractor::TextReplacer.new("this (is `ch@racter ~ madness!", "this (is `ch@racter ~ madness!", :text)
|
62
|
+
assert_equal replacer.replace_hash, { :modified_line => "= t('.this_is_chracter_madness')",
|
63
|
+
:keyname => "t('.this_is_chracter_madness')", :replaced_text => "this (is `ch@racter ~ madness!" }
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Haml
|
4
|
+
class YamlToolTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@temp_locale_dir = File.expand_path(__FILE__) + "/tmp/"
|
8
|
+
end
|
9
|
+
|
10
|
+
def setup_locale_hash
|
11
|
+
@ex1 = Haml::I18n::Extractor.new(file_path("ex1.haml"))
|
12
|
+
@ex1.run
|
13
|
+
end
|
14
|
+
|
15
|
+
def ex1_yaml_hash
|
16
|
+
{"en"=>
|
17
|
+
{"support"=>
|
18
|
+
{"some_place"=>"Some place",
|
19
|
+
"admin"=>"Admin",
|
20
|
+
"admin_dashboard"=>"Admin Dashboard",
|
21
|
+
"stacks"=>"Stacks",
|
22
|
+
"alerts"=>"t('.alerts')",
|
23
|
+
"accounts"=>"Accounts",
|
24
|
+
"what_is_supposed_to_be_is_supp"=>
|
25
|
+
"What is@ supposed to be, is supposed to be! ~"
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
# test "locale dir defaults for rails if you do not pass anything" do
|
32
|
+
# with_rails_mode do
|
33
|
+
# yaml_tool = Haml::I18n::Extractor::YamlTool.new
|
34
|
+
# assert_equal yaml_tool.locales_dir, "/data/current/name/config/locales"
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
|
38
|
+
test "locale dir defaults to cwd if no rails" do
|
39
|
+
without_rails_mode do
|
40
|
+
yaml_tool = Haml::I18n::Extractor::YamlTool.new
|
41
|
+
assert_equal yaml_tool.locales_dir, File.expand_path(".")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
test "you can set the locale_dir" do
|
46
|
+
yaml_tool = Haml::I18n::Extractor::YamlTool.new(@temp_locale_dir)
|
47
|
+
assert_equal yaml_tool.locales_dir, @temp_locale_dir
|
48
|
+
end
|
49
|
+
|
50
|
+
test "it relies on the locale_hash having a certain format" do
|
51
|
+
setup_locale_hash
|
52
|
+
@ex1.yaml_tool.locale_hash.each do |line_no, info_for_line|
|
53
|
+
assert info_for_line.has_key?(:modified_line), "hash info has :modified_line key"
|
54
|
+
assert info_for_line.has_key?(:keyname), "hash info has :keyname key"
|
55
|
+
assert info_for_line.has_key?(:replaced_text), "hash info has :replaced_text key"
|
56
|
+
# FIXME: since the scope right now is we're running this per file this will be the same, but keeping this right now.
|
57
|
+
assert info_for_line.has_key?(:path), "hash info has :path key"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
test "constructs a yaml hash according to the view in rails mode" do
|
62
|
+
setup_locale_hash
|
63
|
+
yaml_tool = @ex1.yaml_tool
|
64
|
+
assert_equal yaml_tool.yaml_hash, ex1_yaml_hash
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|