i18n-tools 0.0.4 → 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.
Files changed (64) hide show
  1. data/lib/core_ext/hash/iterate_nested.rb +35 -0
  2. data/lib/core_ext/hash/slice.rb +20 -0
  3. data/lib/core_ext/hash/sorted_yaml_style.rb +17 -0
  4. data/lib/core_ext/hash/symbolize_keys.rb +14 -0
  5. data/lib/core_ext/module/attribute_accessors.rb +48 -0
  6. data/lib/core_ext/object/deep_clone.rb +5 -0
  7. data/lib/core_ext/object/instance_variables.rb +9 -0
  8. data/lib/core_ext/object/meta_class.rb +5 -0
  9. data/lib/core_ext/object/tap.rb +6 -0
  10. data/lib/i18n/backend/simple_storage.rb +119 -0
  11. data/lib/i18n/commands/keys.rb +84 -0
  12. data/lib/i18n/exceptions/key_exists.rb +9 -0
  13. data/lib/i18n/index.rb +33 -0
  14. data/lib/i18n/index/base.rb +38 -0
  15. data/lib/i18n/index/file.rb +55 -0
  16. data/lib/i18n/index/format.rb +49 -0
  17. data/lib/i18n/index/key.rb +45 -0
  18. data/lib/i18n/index/occurence.rb +18 -0
  19. data/lib/i18n/index/simple.rb +69 -0
  20. data/lib/i18n/index/simple/data.rb +34 -0
  21. data/lib/i18n/index/simple/storage.rb +79 -0
  22. data/lib/i18n/ripper2ruby.rb +7 -0
  23. data/lib/i18n/ripper2ruby/translate_args_list.rb +89 -0
  24. data/lib/i18n/ripper2ruby/translate_call.rb +66 -0
  25. data/lib/i18n/translation_properties.rb +38 -0
  26. data/test/all.rb +1 -1
  27. data/test/core_ext/hash_iterate_nested.rb +31 -0
  28. data/test/fixtures/all.rb.src +106 -0
  29. data/test/fixtures/config.yml +3 -0
  30. data/test/fixtures/locale/de.yml +4 -0
  31. data/test/fixtures/locale/en.yml +4 -0
  32. data/test/fixtures/source_1.rb +2 -1
  33. data/test/fixtures/translate/double_key.rb +32 -0
  34. data/test/fixtures/translate/double_scope.rb +32 -0
  35. data/test/fixtures/translate/single_key.rb +10 -0
  36. data/test/fixtures/translate/single_scope.rb +32 -0
  37. data/test/i18n/backend/simple_storage_test.rb +81 -0
  38. data/test/i18n/backend/translation_properties_test.rb +33 -0
  39. data/test/i18n/index/all.rb +1 -0
  40. data/test/i18n/index/args_replace_test.rb +218 -0
  41. data/test/i18n/index/calls_replace_test.rb +67 -0
  42. data/test/i18n/index/commands_test.rb +75 -0
  43. data/test/i18n/index/key_test.rb +32 -0
  44. data/test/i18n/index/simple_test.rb +67 -0
  45. data/test/i18n/ripper2ruby/translate_call_test.rb +98 -0
  46. data/test/test_helper.rb +66 -9
  47. metadata +49 -32
  48. data/MIT-LICENSE +0 -20
  49. data/README.textile +0 -1
  50. data/bin/i18n-keys +0 -6
  51. data/lib/ansi.rb +0 -19
  52. data/lib/i18n/keys.rb +0 -51
  53. data/lib/i18n/keys/commands.rb +0 -53
  54. data/lib/i18n/keys/formatter.rb +0 -39
  55. data/lib/i18n/keys/index.rb +0 -209
  56. data/lib/i18n/keys/occurence.rb +0 -120
  57. data/lib/i18n/parser/erb_parser.rb +0 -54
  58. data/lib/i18n/parser/ruby_parser.rb +0 -93
  59. data/test/commands_test.rb +0 -1
  60. data/test/erb_parser_test.rb +0 -31
  61. data/test/index_test.rb +0 -135
  62. data/test/keys_test.rb +0 -75
  63. data/test/occurence_test.rb +0 -130
  64. data/test/ruby_parser_test.rb +0 -54
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'i18n'
3
+ require 'i18n/backend/simple_storage'
4
+
5
+ class I18nTranslationPropertiesTest < Test::Unit::TestCase
6
+ def setup
7
+ @backend = I18n.backend = I18n::Backend::SimpleStorage.new
8
+ @data = { :foo => 'Foo', :bar => 'Bar' }
9
+ @backend.send(:set_translation_properties, @data, :filename => 'path/to/file.yml')
10
+ end
11
+
12
+ define_method :"test: set_translation_properties includes TranslationProperties to the value's metaclass" do
13
+ assert @data[:foo].meta_class.include?(I18n::TranslationProperties)
14
+ assert @data[:bar].meta_class.include?(I18n::TranslationProperties)
15
+ end
16
+
17
+ define_method :"test: set_translation_properties defines the properties on TranslationProperties" do
18
+ assert_equal [:filename], I18n::TranslationProperties.property_names
19
+ end
20
+
21
+ define_method :"test: set_translation_properties sets the property values on the objects" do
22
+ assert_equal 'path/to/file.yml', @data[:foo].filename
23
+ assert_equal 'path/to/file.yml', @data[:bar].filename
24
+ end
25
+
26
+ define_method :"test: unset_translation_properties unsets the property instance_variables on the objects" do
27
+ @backend.send(:unset_translation_properties, @data)
28
+ assert_equal nil, @data[:foo].filename
29
+ assert_equal nil, @data[:bar].filename
30
+ assert_equal nil, @data[:foo].instance_variable_get(:@filename)
31
+ assert_equal nil, @data[:bar].instance_variable_get(:@filename)
32
+ end
33
+ end
@@ -0,0 +1 @@
1
+ Dir[File.dirname(__FILE__) + '/**/*_test.rb'].each { |file| require file }
@@ -0,0 +1,218 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ class I18nArgsReplaceTest < Test::Unit::TestCase
4
+ include Ruby
5
+
6
+ def init(src)
7
+ @code = Ripper::RubyBuilder.new(src).parse
8
+ @args = @code.select(Ruby::ArgsList).first.to_translate_args_list
9
+ end
10
+
11
+ # test type variations
12
+
13
+ define_method "test: replace a simple symbol at position 1 with a simple symbol" do
14
+ init("t(:bar)")
15
+
16
+ @args.replace_key(:bar, :oooooooo)
17
+
18
+ assert_equal [0, 1], @args.position.to_a
19
+ assert_equal 11, @args.length
20
+
21
+ assert_equal '(:oooooooo)', @args.to_ruby
22
+ assert_equal '(:oooooooo)', @args.src
23
+ assert_equal @args.parent.to_ruby, @args.root.src
24
+ end
25
+
26
+ define_method "test: replace a simple symbol at position 2 with a simple symbol" do
27
+ init("t(:'foo.bar')")
28
+ @args.replace_key(:bar, :oooooooo)
29
+ assert_equal '(:"foo.oooooooo")', @args.to_ruby
30
+ assert_equal @args.parent.to_ruby, @args.root.src
31
+ end
32
+
33
+ define_method "test: replace a simple symbol at position 1 with a quoted symbol" do
34
+ init("t(:bar)")
35
+ @args.replace_key(:bar, :'oooo.oooo')
36
+ assert_equal '(:"oooo.oooo")', @args.to_ruby
37
+ assert_equal @args.parent.to_ruby, @args.root.src
38
+ end
39
+
40
+ define_method "test: replace a simple symbol at position 2 with a quoted symbol" do
41
+ init("t(:'foo.bar')")
42
+ @args.replace_key(:bar, :'oooo.oooo')
43
+ assert_equal '(:"foo.oooo.oooo")', @args.to_ruby
44
+ assert_equal @args.parent.to_ruby, @args.root.src
45
+ end
46
+
47
+ define_method "test: replace a simple symbol at position 1 with a string (results in a symbol)" do
48
+ init("t(:bar)")
49
+ @args.replace_key(:bar, 'oooooooo')
50
+ assert_equal '(:oooooooo)', @args.to_ruby
51
+ assert_equal @args.parent.to_ruby, @args.root.src
52
+ end
53
+
54
+ define_method "test: replace a simple symbol at position 2 with a string (results in a symbol)" do
55
+ init("t(:'foo.bar')")
56
+ @args.replace_key(:bar, 'oooooooo')
57
+ assert_equal '(:"foo.oooooooo")', @args.to_ruby
58
+ assert_equal @args.parent.to_ruby, @args.root.src
59
+ end
60
+
61
+ define_method "test: replace a quoted symbol at position 1 with a simple symbol" do
62
+ init("t(:'foo.bar')")
63
+ @args.replace_key(:'foo.bar', :oooooooo)
64
+ assert_equal '(:oooooooo)', @args.to_ruby
65
+ assert_equal @args.parent.to_ruby, @args.root.src
66
+ end
67
+
68
+ define_method "test: replace a quoted symbol at position 2 with a simple symbol" do
69
+ init("t(:'bar.baz', :scope => :foo)")
70
+ @args.replace_key(:'bar.baz', :oooooooo)
71
+ assert_equal '(:"foo.oooooooo")', @args.to_ruby
72
+ assert_equal @args.parent.to_ruby, @args.root.src
73
+ end
74
+
75
+ define_method "test: replace a quoted symbol at position 1 with a quoted symbol" do
76
+ init("t(:'foo.bar')")
77
+ @args.replace_key(:'foo.bar', :'oooo.oooo')
78
+ assert_equal "(:\"oooo.oooo\")", @args.to_ruby
79
+ assert_equal @args.parent.to_ruby, @args.root.src
80
+ end
81
+
82
+ define_method "test: replace a quoted symbol at position 2 with a quoted symbol" do
83
+ init("t(:'bar.baz', :scope => :foo)")
84
+ @args.replace_key(:'bar.baz', :'oooo.oooo')
85
+ assert_equal "(:\"oooo.oooo\", :scope => :foo)", @args.to_ruby
86
+ assert_equal @args.parent.to_ruby, @args.root.src
87
+ end
88
+
89
+ define_method "test: replace a quoted symbol at position 1 with a string (results in a symbol)" do
90
+ init("t(:'foo.bar')")
91
+ @args.replace_key(:'foo.bar', 'oooooooo')
92
+ assert_equal "(:oooooooo)", @args.to_ruby
93
+ assert_equal @args.parent.to_ruby, @args.root.src
94
+ end
95
+
96
+ define_method "test: replace a quoted symbol at position 2 with a string (results in a symbol)" do
97
+ init("t(:'bar.baz', :scope => :foo)")
98
+ @args.replace_key(:'bar.baz', 'oooooooo')
99
+ assert_equal '(:"foo.oooooooo")', @args.to_ruby
100
+ assert_equal @args.parent.to_ruby, @args.root.src
101
+ end
102
+
103
+ define_method "test: replace a string at position 1 with a simple symbol" do
104
+ init("t('bar_1')")
105
+ @args.replace_key('bar_1', :oooooooo)
106
+ assert_equal "(:oooooooo)", @args.to_ruby
107
+ assert_equal @args.parent.to_ruby, @args.root.src
108
+ end
109
+
110
+ define_method "test: replace a string at position 1 with a quoted symbol" do
111
+ init("t('bar_1')")
112
+ @args.replace_key('bar_1', :'oooo.oooo')
113
+ assert_equal "(:\"oooo.oooo\")", @args.to_ruby
114
+ assert_equal @args.parent.to_ruby, @args.root.src
115
+ end
116
+
117
+ define_method "test: replace a string at position 1 with a string (results in a symbol)" do
118
+ init("t('bar_1')")
119
+ @args.replace_key('bar_1', 'oooooooo')
120
+ assert_equal "(:oooooooo)", @args.to_ruby
121
+ assert_equal @args.parent.to_ruby, @args.root.src
122
+ end
123
+
124
+
125
+ # test count variations
126
+
127
+ define_method :"test: replace_key :foo with :fuh in (:baz, :scope => [:foo, :bar])" do
128
+ init("t(:baz, :scope => [:'foo', :bar])")
129
+
130
+ @args.replace_key([:foo], [:fuh])
131
+ assert_equal "(:baz, :scope => [:fuh, :bar])", @args.to_ruby
132
+ assert_equal @args.parent.to_ruby, @args.root.src
133
+ end
134
+
135
+ define_method :"test: replace_key [:foo, :bar] with [:fuh, :bah] in (:baz, :scope => [:foo, :bar])" do
136
+ init("t(:baz, :scope => [:'foo', :bar])")
137
+
138
+ @args.replace_key([:foo, :bar], [:fuh, :bah])
139
+ assert_equal "(:baz, :scope => [:fuh, :bah])", @args.to_ruby
140
+ assert_equal @args.parent.to_ruby, @args.root.src
141
+ end
142
+
143
+ define_method :"test: replace_key [:foo, :bar] with [:fuh] in (:baz, :scope => [:foo, :bar])" do
144
+ init("t(:baz, :scope => [:'foo', :bar])")
145
+
146
+ @args.replace_key([:foo, :bar], [:fuh])
147
+ assert_equal "(:baz, :scope => :fuh)", @args.to_ruby
148
+ assert_equal @args.parent.to_ruby, @args.root.src
149
+ end
150
+
151
+ define_method :"test: replace_key [:foo] with [:foo, :fuh] in (:baz, :scope => [:foo, :bar])" do
152
+ init("t(:baz, :scope => [:'foo', :bar])")
153
+
154
+ @args.replace_key([:foo], [:foo, :fuh])
155
+ assert_equal "(:baz, :scope => [:foo, :fuh, :bar])", @args.to_ruby
156
+ assert_equal @args.parent.to_ruby, @args.root.src
157
+ end
158
+
159
+ define_method :"test: replace_key [:foo, :bar, :baz] with [:foo] in (:baz, :scope => [:foo, :bar])" do
160
+ init("t(:baz, :scope => [:'foo', :bar])")
161
+
162
+ @args.replace_key([:foo, :bar, :baz], [:foo])
163
+ assert_equal "(:foo)", @args.to_ruby
164
+ assert_equal @args.parent.to_ruby, @args.root.src
165
+ end
166
+
167
+ define_method :"test: replace_key [:foo, :bar, :baz] with [:foo, :bar] in (:baz, :scope => [:foo, :bar])" do
168
+ init("t(:baz, :scope => [:'foo', :bar])")
169
+
170
+ @args.replace_key([:foo, :bar, :baz], [:foo, :bar])
171
+ assert_equal "(:bar, :scope => :foo)", @args.to_ruby
172
+ assert_equal @args.parent.to_ruby, @args.root.src
173
+ end
174
+
175
+ define_method :"test: replace_key [:foo, :bar, :baz] with [:foo, :bar, :buz] in (:baz, :scope => [:foo, :bar])" do
176
+ init("t(:baz, :scope => [:'foo', :bar])")
177
+
178
+ @args.replace_key([:foo, :bar, :baz], [:foo, :bar, :buz])
179
+ assert_equal "(:buz, :scope => [:foo, :bar])", @args.to_ruby
180
+ assert_equal @args.parent.to_ruby, @args.root.src
181
+ end
182
+
183
+ define_method :"test: replace_key [:foo, :bar, :baz] with [:foo, :bar, :baz, :buz] in (:baz, :scope => [:foo, :bar])" do
184
+ init("t(:baz, :scope => [:'foo', :bar])")
185
+
186
+ @args.replace_key([:foo, :bar, :baz], [:foo, :bar, :baz, :buz])
187
+ assert_equal "(:buz, :scope => [:foo, :bar, :baz])", @args.to_ruby
188
+ assert_equal @args.parent.to_ruby, @args.root.src
189
+ end
190
+
191
+ define_method :"test: replace_key [:foo, :bar] with [:foo, :bar, :baz] in (:bar, :scope => [:foo])" do
192
+ init("t(:bar, :scope => [:foo])")
193
+
194
+ @args.replace_key([:foo, :bar], [:foo, :bar, :baz])
195
+ assert_equal "(:baz, :scope => [:foo, :bar])", @args.to_ruby
196
+ assert_equal @args.parent.to_ruby, @args.root.src
197
+ end
198
+
199
+ define_method :"test: replace a few keys on the same line and next line" do
200
+ src = "t(:a, :scope => [:a]); t(:b, :scope => [:b]);\n t(:c, :scope => [:ccc])\nt(:d, :scope => :d)\ne(:e)"
201
+ calls = Ripper::RubyBuilder.new(src).parse.statements
202
+
203
+ tokens = %w(a b c d e)
204
+ calls = ::Hash[*tokens.zip(calls).flatten]
205
+ src = ::Hash[*tokens.zip(calls.values.map { |c| c.src }).flatten]
206
+
207
+ calls['a'].to_translate_call.arguments.replace_key([:a], [:aaa])
208
+ calls['b'].to_translate_call.arguments.replace_key([:b], [:bbb])
209
+ calls['c'].to_translate_call.arguments.replace_key([:ccc], [:cc])
210
+
211
+ assert_equal 't(:a, :scope => :aaa)', calls['a'].src
212
+ assert_equal 't(:b, :scope => :bbb)', calls['b'].src
213
+ assert_equal 't(:c, :scope => :cc)', calls['c'].src
214
+ assert_equal 't(:d, :scope => :d)', calls['d'].src
215
+ assert_equal 'e(:e)', calls['e'].src
216
+ end
217
+ end
218
+
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'i18n/index'
3
+
4
+ class I18nCallReplaceTest < Test::Unit::TestCase
5
+ def setup
6
+ @root_dir = File.expand_path(File.dirname(__FILE__) + '/../../fixtures')
7
+ end
8
+
9
+ def teardown
10
+ @index.filenames.each { |file| FileUtils.mv("#{file}.backup", file) } if @index
11
+ @index.delete
12
+ end
13
+
14
+ def index(file)
15
+ @index ||= I18n::Index.load_or_create(:root_dir => @root_dir, :pattern => '/translate/' + file).tap do |index|
16
+ index.filenames.each { |file| FileUtils.cp(file, "#{file}.backup") }
17
+ end
18
+ end
19
+
20
+ def root(index)
21
+ index.files[index.occurences.first.filename].ruby.root
22
+ end
23
+
24
+ def assert_key_replacements(index, search, replace)
25
+ calls = []
26
+
27
+ while call = index.find_call(search)
28
+ calls << call
29
+ index.replace_key(call, search, replace)
30
+ putc '.'
31
+ end
32
+ src = root(index).src
33
+
34
+ search.to_s.gsub(/[^\w\.]/, '').split('.').each { |key| assert src.scan(key.to_s).empty? unless key.empty? }
35
+ replace.to_s.gsub(/[^\w\.]/, '').split('.').each { |key| assert src.scan(key.to_s).size == calls.size }
36
+
37
+ assert_equal src, calls.map { |c| c.to_ruby }.join("\n")
38
+ assert_equal src, calls.map { |c| c.src }.join("\n")
39
+ end
40
+
41
+ define_method :"test subsequent key replacements in: t(:foo)" do
42
+ 2.times do
43
+ index = index('single_key.rb')
44
+ assert_key_replacements(index, :foo, :'fuh.bah') and putc '.'
45
+ assert_key_replacements(index, '*.bah', :foo) and putc '.'
46
+ assert_key_replacements(index, 'fuh.*', '') and putc '.'
47
+ end
48
+ end
49
+
50
+ define_method :"test subsequent key replacements in: t(:bar, :scope => :foo)" do
51
+ 2.times do
52
+ index = index('single_scope.rb')
53
+ assert_key_replacements(index, :'*.bar', :'bah.bas') and putc '.'
54
+ assert_key_replacements(index, :'*.bah.bas', :'bar') and putc '.'
55
+ end
56
+ end
57
+
58
+ define_method :"test subsequent key replacements in: t(:'bar.baz', :scope => :foo)" do
59
+ 2.times do
60
+ index = index('double_key.rb')
61
+ assert_key_replacements(index, :'foo.bar.baz', :'fuh.bah.bas.bus') and putc '.'
62
+ assert_key_replacements(index, :'fuh.bah.bas.bus', :'foo.bar.baz') and putc '.'
63
+ end
64
+ end
65
+
66
+ # TODO add more tests for partial key matches
67
+ end
@@ -0,0 +1,75 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'i18n/backend/simple_storage'
3
+
4
+ class I18nCommandsKeysTest < Test::Unit::TestCase
5
+ @@fixtures_dir = File.expand_path(File.dirname(__FILE__) + '/../../fixtures/')
6
+ @@backup_dir = File.expand_path(File.dirname(__FILE__) + '/../../tmp/')
7
+
8
+ def setup
9
+ I18n.backend = I18n::Backend::SimpleStorage.new
10
+ @options = { :root_dir => @@fixtures_dir, :pattern => 'source_1.rb', :interactive => false }
11
+ FileUtils.cp_r(@@fixtures_dir, @@backup_dir)
12
+ @io = StringIO.new
13
+ @commands = I18n::Commands::Keys.new(nil, @io)
14
+ end
15
+
16
+ def teardown
17
+ FileUtils.rm_r(@@fixtures_dir)
18
+ FileUtils.mv(@@backup_dir, @@fixtures_dir)
19
+ I18n::Index.new.delete
20
+ end
21
+
22
+ def source(filename)
23
+ File.read(@@fixtures_dir + "/#{filename}")
24
+ end
25
+
26
+ define_method :"test find command" do
27
+ key = 'baaar'
28
+ filename = 'source_1.rb'
29
+ @commands.find(key, @options)
30
+
31
+ assert_match @io.string, %r(indexing files)
32
+ assert_match @io.string, %r(#{key}:.*#{filename})
33
+ end
34
+
35
+ define_method :"test replace command" do
36
+ filename = 'source_1.rb'
37
+ I18n.load_path += Dir[@@fixtures_dir + '/locale/*.yml']
38
+
39
+ assert_not_nil I18n.t(:foo)
40
+ assert_match %r(foo\.bar), source(filename)
41
+ assert_equal [:bar, :baaar, :"baz.fooo.baar", :"foo.bar", :bar_1], indexed_keys
42
+
43
+ @commands.replace('foo.*', 'baz', @options)
44
+
45
+ assert_equal({ :bar => 'Bar', :baz => 'Baz' }, I18n.t(:baz))
46
+ assert_raises(I18n::MissingTranslationData) { I18n.t(:foo, :raise => true) }
47
+ assert_match %r(baz\.bar), source(filename)
48
+ assert_no_match %r(foo\.bar), source(filename)
49
+ assert_equal [:bar, :baaar, :"baz.fooo.baar", :bar_1, :"baz.bar"].sort, indexed_keys.sort
50
+
51
+ @commands.replace('baz.bar', 'bazzz', @options)
52
+
53
+ assert_equal 'Bar', I18n.t(:bazzz)
54
+ assert_raises(I18n::MissingTranslationData) { I18n.t(:'baz.bar', :raise => true) }
55
+ assert_match %r(bazzz), source(filename)
56
+ assert_no_match %r(baz\.bar), source(filename)
57
+ assert_equal [:bar, :baaar, :"baz.fooo.baar", :bar_1, :"bazzz"].sort, indexed_keys.sort
58
+ end
59
+
60
+ protected
61
+
62
+ def indexed_keys
63
+ I18n::Index.load_or_create(@options).keys.map(&:key)
64
+ end
65
+
66
+ def stub_output(target)
67
+ (class << target; self; end).class_eval do
68
+ attr_accessor :out
69
+ define_method(:puts) { |str| out.puts(str) }
70
+ define_method(:string) { out.string }
71
+ end
72
+ target.out = StringIO.new
73
+ target
74
+ end
75
+ end
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ require 'i18n/index/simple'
4
+ require 'yaml'
5
+
6
+ class I18nIndexKeyTest < Test::Unit::TestCase
7
+ include I18n::Index
8
+
9
+ define_method :"test matches? given no keys returns true" do
10
+ assert Key.new(:foo).send(:matches?)
11
+ end
12
+
13
+ define_method :"test matches? given a literally matching key returns true" do
14
+ assert Key.new(:foo).send(:matches?, :foo, :bar)
15
+ end
16
+
17
+ define_method :"test pattern with no wildcards" do
18
+ assert_equal /^foo\.bar$/, Key.send(:pattern, :'foo.bar')
19
+ end
20
+
21
+ define_method :"test pattern with a dot separated wildcard at the beginning" do
22
+ assert_equal /\.foo\.bar$/, Key.send(:pattern, :'*.foo.bar')
23
+ end
24
+
25
+ define_method :"test pattern with a dot separated wildcard at the end" do
26
+ assert_equal /^foo\.bar\./, Key.send(:pattern, :'foo.bar.*')
27
+ end
28
+
29
+ define_method :"test pattern with a dot separated wildcard at the beginning and end" do
30
+ assert_equal /\.foo\.bar\./, Key.send(:pattern, :'*.foo.bar.*')
31
+ end
32
+ end
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ require 'i18n/index'
4
+ require 'yaml'
5
+
6
+ class I18nIndexSimpleTest < Test::Unit::TestCase
7
+ def setup
8
+ @root_dir = File.dirname(__FILE__) + '/../../fixtures'
9
+ @filename = @root_dir + '/source_1.rb'
10
+ @index = I18n::Index::Simple.new(:root_dir => @root_dir, :pattern => File.basename(@filename))
11
+ FileUtils.cp(@filename, "#{@filename}.backup")
12
+ end
13
+
14
+ def teardown
15
+ @index.delete
16
+ FileUtils.mv("#{@filename}.backup", @filename)
17
+ end
18
+
19
+
20
+ def assert_valid_index(index)
21
+ assert_equal 'source_1.rb', index.pattern
22
+ assert_equal 'fixtures', File.basename(index.root_dir)
23
+
24
+ keys = [:baaar, :bar, :bar_1, :"baz.fooo.baar", :"foo.bar"]
25
+ assert_equal keys.sort, index.data.keys.map(&:key).sort
26
+ assert_equal 2, index.data[:bar][:occurences].size
27
+
28
+ occurence = index.send(:data)[:bar][:occurences].first
29
+ assert_equal :bar, occurence.key.key # yuck
30
+ assert_equal 'source_1.rb', File.basename(occurence.filename)
31
+ assert_equal [2, 4], occurence.position.to_a
32
+ end
33
+
34
+ define_method :"test data is built lazily" do
35
+ assert_valid_index(@index)
36
+ end
37
+
38
+ define_method :"test update builds and saves index" do
39
+ @index.update
40
+ index = I18n::Index::Simple.send(:load, :root_dir => @root_dir)
41
+ assert_valid_index(index)
42
+ end
43
+
44
+ define_method :"test finds a call" do
45
+ assert_equal 't(:bar)', @index.find_call(:bar).to_ruby
46
+ end
47
+
48
+ define_method :"test subsequently replaces keys" do
49
+ @index.update # build and save
50
+
51
+ call = @index.find_call(:bar)
52
+ assert call
53
+
54
+ @index.replace_key(call, :bar, :bazooh)
55
+ assert File.read(@filename) =~ /:bazooh/
56
+
57
+ index = I18n::Index::Simple.send(:load, :root_dir => @root_dir) # reload the index
58
+ call = index.find_call(:bar)
59
+ assert call
60
+
61
+ index.replace_key(call, :bar, :bazuuh)
62
+ assert File.read(@filename) =~ /:bazuuh/
63
+
64
+ index = I18n::Index::Simple.send(:load, :root_dir => @root_dir) # reload the index
65
+ assert_nil index.find_call(:bar)
66
+ end
67
+ end