tr8n_core 4.0.7 → 4.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/lib/tr8n/api_client.rb +28 -18
  4. data/lib/tr8n/application.rb +37 -64
  5. data/lib/tr8n/cache.rb +8 -41
  6. data/lib/tr8n/cache_adapters/file.rb +1 -2
  7. data/lib/tr8n/cache_adapters/memcache.rb +2 -2
  8. data/lib/tr8n/cache_adapters/redis.rb +2 -2
  9. data/lib/tr8n/config.rb +48 -13
  10. data/lib/tr8n/decorators/html.rb +3 -10
  11. data/lib/tr8n/language.rb +25 -112
  12. data/lib/tr8n/language_case.rb +1 -1
  13. data/lib/tr8n/session.rb +2 -9
  14. data/lib/tr8n/source.rb +12 -44
  15. data/lib/tr8n/{tokens/data_tokenizer.rb → tokenizers/data.rb} +4 -3
  16. data/lib/tr8n/{tokens/decoration_tokenizer.rb → tokenizers/decoration.rb} +6 -5
  17. data/lib/tr8n/tokenizers/dom.rb +346 -0
  18. data/lib/tr8n/tokens/data.rb +15 -22
  19. data/lib/tr8n/tokens/transform.rb +24 -12
  20. data/lib/tr8n/translation_key.rb +6 -29
  21. data/lib/tr8n/utils.rb +5 -0
  22. data/lib/tr8n_core/version.rb +1 -1
  23. data/lib/tr8n_core.rb +7 -26
  24. metadata +30 -100
  25. data/lib/tr8n/cache_adapters/cdb.rb +0 -87
  26. data/lib/tr8n/tokens/hidden.rb +0 -57
  27. data/spec/application_spec.rb +0 -52
  28. data/spec/base_spec.rb +0 -19
  29. data/spec/cache/adapters/file_spec.rb +0 -32
  30. data/spec/cache/adapters/memcache_spec.rb +0 -15
  31. data/spec/cache/generators/file_generator_spec.rb +0 -30
  32. data/spec/config_spec.rb +0 -32
  33. data/spec/decorator_spec.rb +0 -12
  34. data/spec/decorators/base_spec.rb +0 -14
  35. data/spec/decorators/default_spec.rb +0 -12
  36. data/spec/decorators/html_spec.rb +0 -50
  37. data/spec/ext/array_spec.rb +0 -12
  38. data/spec/ext/hash_spec.rb +0 -15
  39. data/spec/ext/string_spec.rb +0 -10
  40. data/spec/fixtures/application.json +0 -112
  41. data/spec/fixtures/languages/en-US.json +0 -1424
  42. data/spec/fixtures/languages/es.json +0 -291
  43. data/spec/fixtures/languages/ru.json +0 -582
  44. data/spec/fixtures/translations/ru/basic.json +0 -56
  45. data/spec/fixtures/translations/ru/counters.json +0 -43
  46. data/spec/fixtures/translations/ru/genders.json +0 -171
  47. data/spec/fixtures/translations/ru/last_names.txt +0 -200
  48. data/spec/fixtures/translations/ru/names.json +0 -1
  49. data/spec/fixtures/translations/ru/names.txt +0 -458
  50. data/spec/language_case_rule_spec.rb +0 -57
  51. data/spec/language_case_spec.rb +0 -58
  52. data/spec/language_context_rule_spec.rb +0 -75
  53. data/spec/language_context_spec.rb +0 -333
  54. data/spec/language_spec.rb +0 -615
  55. data/spec/logger_spec.rb +0 -15
  56. data/spec/rules_engine/evaluator_spec.rb +0 -150
  57. data/spec/rules_engine/parser_spec.rb +0 -31
  58. data/spec/source_spec.rb +0 -13
  59. data/spec/spec_helper.rb +0 -101
  60. data/spec/tokens/data_spec.rb +0 -114
  61. data/spec/tokens/data_tokenizer_spec.rb +0 -29
  62. data/spec/tokens/decoration_tokenizer_spec.rb +0 -81
  63. data/spec/tokens/hidden_spec.rb +0 -24
  64. data/spec/tokens/method_spec.rb +0 -84
  65. data/spec/tokens/transform_spec.rb +0 -50
  66. data/spec/translation_key_spec.rb +0 -96
  67. data/spec/translation_spec.rb +0 -24
  68. data/spec/utils_spec.rb +0 -62
@@ -1,150 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Tr8n::RulesEngine::Evaluator do
6
- describe "#evaluator" do
7
- it "evaluates standard expressions" do
8
- e = Tr8n::RulesEngine::Evaluator.new
9
- e.evaluate(["label", "greeting", "hello world"])
10
- expect(e.vars).to eq({"greeting"=>"hello world"})
11
-
12
- expect(e.evaluate(["quote", [1,2,3]])).to eq([1,2,3])
13
- expect(e.evaluate(["quote", ["a","b","c"]])).to eq(["a","b","c"])
14
-
15
- expect(e.evaluate(["car", ["+", 1, 2]])).to eq(1)
16
- expect(e.evaluate(["cdr", ["+", 1, 2]])).to eq([1, 2])
17
- expect(e.evaluate(["cons", 1, ["quote", [2, 3]]])).to eq([1, 2, 3])
18
-
19
- expect(e.evaluate(["eq", 1, 1])).to be_truthy
20
-
21
- expect(e.evaluate(["atom", :a])).to be_truthy
22
- expect(e.evaluate(["atom", "hello"])).to be_truthy
23
- expect(e.evaluate(["atom", 1])).to be_truthy
24
- expect(e.evaluate(["atom", 1.4])).to be_truthy
25
- expect(e.evaluate(["atom", ["eq", 1, 1]])).to be_falsey
26
-
27
- expect(e.evaluate(["cond", ["eq", 1, 1], 1, 0])).to eq(1)
28
- expect(e.evaluate(["cond", ["eq", 1, 2], 1, 0])).to eq(0)
29
- end
30
-
31
- it "evaluates rules" do
32
- e = Tr8n::RulesEngine::Evaluator.new
33
- expect(e.evaluate(["=", "1", "1"])).to be_truthy
34
- expect(e.evaluate(["=", "1", "2"])).to be_falsey
35
-
36
- expect(e.evaluate(["!=", "2", "1"])).to be_truthy
37
- expect(e.evaluate(["!=", "2", "2"])).to be_falsey
38
-
39
- expect(e.evaluate([">", "2", "1"])).to be_truthy
40
- expect(e.evaluate([">", "1", "2"])).to be_falsey
41
-
42
- expect(e.evaluate(["<", "2", "3"])).to be_truthy
43
- expect(e.evaluate(["<", "3", "2"])).to be_falsey
44
-
45
- expect(e.evaluate(["+", 1, 2])).to eq(3)
46
- expect(e.evaluate(["+", -1, 2])).to eq(1)
47
-
48
- expect(e.evaluate(["-", 2, 1])).to eq(1)
49
- expect(e.evaluate(["*", 2, 10])).to eq(20)
50
- expect(e.evaluate(["/", 20, 10])).to eq(2)
51
-
52
- expect(e.evaluate(["!", ["=", "1", "2"]])).to be_truthy
53
-
54
- expect(e.evaluate(["&&", ["=", "1", "1"], ["=", 10, ["/", 20, 2]]])).to be_truthy
55
- expect(e.evaluate(["||", ["=", "2", "1"], ["=", 10, ["/", 20, 2]]])).to be_truthy
56
-
57
- expect(e.evaluate(["if", ["=", 1, 2], 1, 0])).to eq(0)
58
-
59
- e.evaluate(["let", "@n", 1])
60
- expect(e.vars).to eq({"@n"=>1})
61
- expect(e.evaluate(["=", 1, "@n"])).to be_truthy
62
-
63
- e.evaluate(["let", "@n", 11])
64
- expect(e.vars).to eq({"@n"=>11})
65
- expect(e.evaluate(["=", 11, "@n"])).to be_truthy
66
-
67
- expect(e.evaluate(["and", ["=", "1", "1"], ["=", 10, ["/", 20, 2]]])).to be_truthy
68
- expect(e.evaluate(["or", ["=", "2", "1"], ["=", 10, ["/", 20, 2]]])).to be_truthy
69
-
70
- expect(e.evaluate(["mod", 23, 10])).to eq(3)
71
- expect(e.evaluate(["mod", 2.3, 10])).to eq(2.3)
72
-
73
- expect(e.evaluate(["match", "/hello/", "hello world"])).to be_truthy
74
- expect(e.evaluate(["match", "hello", "hello world"])).to be_truthy
75
- expect(e.evaluate(["match", "/^h/", "hello world"])).to be_truthy
76
- expect(e.evaluate(["match", "/^e/", "hello world"])).to be_falsey
77
- expect(e.evaluate(["match", "/^h.*d$/", "hello world"])).to be_truthy
78
-
79
- expect(e.evaluate(["in", "1,2", "1"])).to be_truthy
80
- expect(e.evaluate(["in", "1,2", 1])).to be_truthy
81
- expect(e.evaluate(["in", "a,b,c", "a"])).to be_truthy
82
- expect(e.evaluate(["in", "a..c, d..z", "h"])).to be_truthy
83
-
84
- expect(e.evaluate(["in", "1..5, 10..22", 21])).to be_truthy
85
- expect(e.evaluate(["in", "1..5, 10..22", 22])).to be_truthy
86
- expect(e.evaluate(["in", "1..5, 10..22", 1])).to be_truthy
87
- expect(e.evaluate(["in", "1..5, 10..22", 23])).to be_falsey
88
- expect(e.evaluate(["in", "1..5, 10..22", "9"])).to be_falsey
89
-
90
- expect(e.evaluate(["within", "1..3", 2.3])).to be_truthy
91
- expect(e.evaluate(["within", "1..3", 3.3])).to be_falsey
92
-
93
- expect(e.evaluate(["replace", "/^hello/", "hi", "hello world"])).to eq("hi world")
94
- expect(e.evaluate(["replace", "world", "hi", "hello world"])).to eq("hello hi")
95
- expect(e.evaluate(["replace", "o", "a", "hello world"])).to eq("hella warld")
96
- expect(e.evaluate(["replace", "/world$/", "moon", "hello moon"])).to eq("hello moon")
97
-
98
- expect(e.evaluate(["append", "world", "hello "])).to eq("hello world")
99
- expect(e.evaluate(["prepend", "hello ", "world"])).to eq("hello world")
100
-
101
- expect(e.evaluate(["true"])).to be_truthy
102
- expect(e.evaluate(["false"])).to be_falsey
103
-
104
- expect(e.evaluate(["date", "2011-01-01"])).to eq(Date.new(2011, 1, 1))
105
- expect(e.evaluate(["today"])).to eq(Date.today)
106
-
107
- expect(e.evaluate(["time", "2011-01-01 10:9:8"])).to eq(Time.new(2011, 1, 1, 10, 9, 8))
108
-
109
- expect(e.evaluate([">", ["date", "3014-01-01"], ["today"]])).to be_truthy
110
- end
111
-
112
- it "evaluates expressions" do
113
- e = Tr8n::RulesEngine::Evaluator.new
114
-
115
- e.evaluate(["let", "@n", 1])
116
- expect(e.evaluate(["&&", ["=", 1, ["mod", "@n", 10]], ["!=", 11, ["mod", "@n", 100]]])).to be_truthy
117
- expect(e.evaluate(["&&", ["in", "2..4", ["mod", "@n", 10]], ["not", ["in", "12..14", ["mod", "@n", 100]]]])).to be_falsey
118
- expect(e.evaluate(["||", ["=", 0, ["mod", "@n", 10]], ["in", "5..9", ["mod", "@n", 10]], ["in", "11..14", ["mod", "@n", 100]]])).to be_falsey
119
-
120
- e.evaluate(["let", "@n", 21])
121
- expect(e.evaluate(["&&", ["=", 1, ["mod", "@n", 10]], ["!=", 11, ["mod", "@n", 100]]])).to be_truthy
122
- expect(e.evaluate(["&&", ["in", "2..4", ["mod", "@n", 10]], ["not", ["in", "12..14", ["mod", "@n", 100]]]])).to be_falsey
123
- expect(e.evaluate(["||", ["=", 0, ["mod", "@n", 10]], ["in", "5..9", ["mod", "@n", 10]], ["in", "11..14", ["mod", "@n", 100]]])).to be_falsey
124
-
125
- e.evaluate(["let", "@n", 11])
126
- expect(e.evaluate(["&&", ["=", 1, ["mod", "@n", 10]], ["!=", 11, ["mod", "@n", 100]]])).to be_falsey
127
- expect(e.evaluate(["&&", ["in", "2..4", ["mod", "@n", 10]], ["not", ["in", "12..14", ["mod", "@n", 100]]]])).to be_falsey
128
- expect(e.evaluate(["||", ["=", 0, ["mod", "@n", 10]], ["in", "5..9", ["mod", "@n", 10]], ["in", "11..14", ["mod", "@n", 100]]])).to be_truthy
129
-
130
- rules = {
131
- "one" => ["&&", ["=", 1, ["mod", "@n", 10]], ["!=", 11, ["mod", "@n", 100]]],
132
- "few" => ["&&", ["in", "2..4", ["mod", "@n", 10]], ["not", ["in", "12..14", ["mod", "@n", 100]]]],
133
- "many" => ["||", ["=", 0, ["mod", "@n", 10]], ["in", "5..9", ["mod", "@n", 10]], ["in", "11..14", ["mod", "@n", 100]]]
134
- }
135
-
136
- {
137
- "one" => [1, 21, 31, 41, 51, 61],
138
- "few" => [2,3,4, 22,23,24, 32,33,34],
139
- "many" => [0, 5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 25,26,27,28,29,30, 35,36,37,38,39,40]
140
- }.each do |key, values|
141
- values.each do |value|
142
- e.evaluate(["let", "@n", value])
143
- expect(e.evaluate(rules[key])).to be_truthy
144
- end
145
- end
146
-
147
- end
148
-
149
- end
150
- end
@@ -1,31 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Tr8n::RulesEngine::Parser do
6
- describe "#parser" do
7
- it "parses expressions" do
8
- expect(Tr8n::RulesEngine::Parser.new("(= 1 (mod n 10))").tokens).to eq(
9
- ["(", "=", "1", "(", "mod", "n", "10", ")", ")"]
10
- )
11
-
12
- expect(Tr8n::RulesEngine::Parser.new("(&& (= 1 (mod @n 10)) (!= 11 (mod @n 100)))").tokens).to eq(
13
- ["(", "&&", "(", "=", "1", "(", "mod", "@n", "10", ")", ")", "(", "!=", "11", "(", "mod", "@n", "100", ")", ")", ")"]
14
- )
15
-
16
- {
17
- "(= 1 1)" => ["=", 1, 1],
18
- "(+ 1 1)" => ["+", 1, 1],
19
- "(= 1 (mod n 10))" => ["=", 1, ["mod", "n", 10]],
20
- "(&& 1 1)" => ["&&", 1, 1],
21
- "(mod @n 10)" => ["mod", "@n", 10],
22
- "(&& (= 1 (mod @n 10)) (!= 11 (mod @n 100)))" => ["&&", ["=", 1, ["mod", "@n", 10]], ["!=", 11, ["mod", "@n", 100]]],
23
- "(&& (in '2..4' (mod @n 10)) (not (in '12..14' (mod @n 100))))" => ["&&", ["in", "2..4", ["mod", "@n", 10]], ["not", ["in", "12..14", ["mod", "@n", 100]]]],
24
- "(|| (= 0 (mod @n 10)) (in '5..9' (mod @n 10)) (in '11..14' (mod @n 100)))" => ["||", ["=", 0, ["mod", "@n", 10]], ["in", "5..9", ["mod", "@n", 10]], ["in", "11..14", ["mod", "@n", 100]]]
25
- }.each do |source, target|
26
- expect(Tr8n::RulesEngine::Parser.new(source).parse).to eq(target)
27
- end
28
-
29
- end
30
- end
31
- end
data/spec/source_spec.rb DELETED
@@ -1,13 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Tr8n::Source do
6
- describe "#initialize" do
7
- it "helper methods" do
8
- expect(Tr8n::Source.normalize("https://travis-ci.org/tr8n/tr8n_ruby_core")).to eq("/tr8n/tr8n_ruby_core")
9
- expect(Tr8n::Source.normalize("https://www.google.com/search?q=test&source=lnms")).to eq("/search")
10
- expect(Tr8n::Source.cache_prefix).to eq('s@')
11
- end
12
- end
13
- end
data/spec/spec_helper.rb DELETED
@@ -1,101 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'rspec'
4
- require 'json'
5
-
6
- require 'simplecov'
7
- require 'coveralls'
8
-
9
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
10
- SimpleCov::Formatter::HTMLFormatter,
11
- Coveralls::SimpleCov::Formatter
12
- ]
13
- SimpleCov.start
14
-
15
- require 'tr8n_core'
16
-
17
- RSpec.configure do |config|
18
- config.raise_errors_for_deprecations!
19
- config.mock_with :rspec do |c|
20
- c.syntax = [:should, :expect]
21
- end
22
- end
23
-
24
- Tr8n.configure do |config|
25
- config.format = :text
26
- end
27
-
28
- def fixtures_root
29
- File.join(File.dirname(__FILE__), 'fixtures')
30
- end
31
-
32
- def load_json(file_path)
33
- JSON.parse(File.read("#{fixtures_root}/#{file_path}"))
34
- end
35
-
36
- def load_translation_key_from_hash(app, hash)
37
- app.cache_translation_key(Tr8n::TranslationKey.new(hash.merge(:application => app)))
38
- end
39
-
40
- def load_translation_key_from_array(app, arr)
41
- arr.each do |tkey|
42
- load_translation_key_from_hash(app, tkey)
43
- end
44
- end
45
-
46
- def load_translation_keys_from_file(app, path)
47
- load_translation_key_from_array(app, load_json(path))
48
- end
49
-
50
- def stub_object(attrs)
51
- user = double()
52
- attrs.each do |key, value|
53
- user.stub(key) { value }
54
- end
55
- user
56
- end
57
-
58
- def init_application(locales = [], path = 'application.json')
59
- locales = ['en-US', 'ru', 'es'] if locales.size == 0
60
- app = Tr8n::Application.new(load_json(path))
61
- locales.each do |locale|
62
- app.add_language(Tr8n::Language.new(load_json("languages/#{locale}.json")))
63
- end
64
- Tr8n.session.application = app
65
- Tr8n.session.current_language = app.language('en-US')
66
- app
67
- end
68
-
69
- RSpec.configure do |config|
70
- config.before do
71
- ARGV.replace []
72
- end
73
-
74
- config.expect_with :rspec do |c|
75
- c.syntax = :expect
76
- end
77
-
78
- def capture(stream)
79
- begin
80
- stream = stream.to_s
81
- eval "$#{stream} = StringIO.new"
82
- yield
83
- result = eval("$#{stream}").string
84
- ensure
85
- eval("$#{stream} = #{stream.upcase}")
86
- end
87
-
88
- result
89
- end
90
-
91
- def source_root
92
- fixtures_root
93
- end
94
-
95
- def destination_root
96
- File.join(File.dirname(__FILE__), 'sandbox')
97
- end
98
-
99
- alias :silence :capture
100
- end
101
-
@@ -1,114 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Tr8n::Tokens::Data do
6
- before do
7
- @app = init_application
8
- @english = @app.language('en-US')
9
- @tkey = Tr8n::TranslationKey.new({
10
- :label => "Hello {user}",
11
- :application => @app,
12
- :locale => 'en-US'
13
- })
14
- @token = @tkey.data_tokens.first
15
- end
16
-
17
- describe "initialize" do
18
- it "should parse data token" do
19
- expect(@token.class.name).to eq("Tr8n::Tokens::Data")
20
- expect(@token.context_keys).to eq([])
21
- end
22
- end
23
-
24
- describe "substitute" do
25
- it "should substitute values" do
26
- user = stub_object({:first_name => "Tom", :last_name => "Anderson", :gender => "Male", :to_s => "Tom Anderson"})
27
- user.stub(:last_name_with_prefix) {|prefix| "#{prefix} #{user.last_name}"}
28
-
29
- # tr("Hello {user}", "", {:user => current_user}}
30
- expect(@token.token_value(user, @english)).to eq(user.to_s)
31
-
32
- # tr("Hello {user}", "", {:user => [current_user, current_user.name]}}
33
- expect(@token.token_value([user, user.first_name], @english)).to eq(user.first_name)
34
-
35
- # tr("Hello {user}", "", {:user => [current_user, :name]}}
36
- expect(@token.token_value([user, :first_name], @english)).to eq(user.first_name)
37
-
38
- # tr("Hello {user}", "", {:user => {:object => current_user, :value => current_user.name}]}}
39
- expect(@token.token_value({:object => user, :value => user.to_s}, @english)).to eq(user.to_s)
40
-
41
- # tr("Hello {user}", "", {:user => {:object => current_user, :attribute => :first_name}]}}
42
- expect(@token.token_value({:object => user, :attribute => :first_name}, @english)).to eq(user.first_name)
43
- expect(@token.token_value({:object => {:first_name => "Michael"}, :attribute => :first_name}, @english)).to eq("Michael")
44
- end
45
-
46
- it "should perform complete substitution" do
47
- user = stub_object({:first_name => "Tom", :last_name => "Anderson", :gender => "Male", :to_s => "Tom Anderson"})
48
- user.stub(:last_name_with_prefix) {|prefix| "#{prefix} #{user.last_name}"}
49
-
50
- [
51
- {:user => user}, "Hello Tom Anderson",
52
- {:user => [user, user.first_name]}, "Hello Tom",
53
- {:user => [user, :first_name]}, "Hello Tom",
54
- {:user => {:object => user, :value => user.to_s}}, "Hello Tom Anderson",
55
- {:user => {:object => user, :attribute => :first_name}}, "Hello Tom",
56
- {:user => {:object => {:first_name => "Michael"}, :attribute => :first_name}}, "Hello Michael"
57
- ].each_slice(2).to_a.each do |pair|
58
- expect(@token.substitute(@tkey.label, pair.first, @english)).to eq(pair.last)
59
- end
60
-
61
- end
62
-
63
- it "should substitute token with array values" do
64
- tkey = Tr8n::TranslationKey.new({
65
- :label => "Hello {users}",
66
- :application => @app,
67
- :locale => 'en-US'
68
- })
69
- token = tkey.data_tokens.first
70
-
71
- users = []
72
- 1.upto(6) do |i|
73
- users << stub_object({:first_name => "First name #{i}", :last_name => "Last name #{i}", :gender => "Male"})
74
- end
75
-
76
- Tr8n.session.with_block_options(:dry => true) do
77
- expect(token.token_value([users, :first_name], @english)).to match("2 others")
78
-
79
- expect(token.token_value([users, :first_name, {
80
- :description => "List joiner",
81
- :limit => 4,
82
- :separator => ", ",
83
- :joiner => 'and',
84
- :less => '{laquo} less',
85
- :expandable => false,
86
- :collapsable => true
87
- }], @english)).to eq("First name 1, First name 2, First name 3, First name 4 and 2 others")
88
-
89
- expect(token.token_value([users, :first_name, {
90
- :description => "List joiner",
91
- :limit => 10,
92
- :separator => ", ",
93
- :joiner => 'and',
94
- :less => '{laquo} less',
95
- :expandable => false,
96
- :collapsable => true
97
- }], @english)).to eq("First name 1, First name 2, First name 3, First name 4, First name 5 and First name 6")
98
-
99
- expect(token.token_value([users, :first_name, {
100
- :joiner => 'or',
101
- :expandable => false,
102
- }], @english)).to eq("First name 1, First name 2, First name 3, First name 4 or 2 others")
103
-
104
- expect(token.token_value([users, :first_name, {
105
- :limit => 2,
106
- :joiner => 'or',
107
- :expandable => false,
108
- }], @english)).to eq("First name 1, First name 2 or 4 others")
109
-
110
- end
111
- end
112
- end
113
-
114
- end
@@ -1,29 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Tr8n::Tokens::DataTokenizer do
6
- before do
7
-
8
- end
9
-
10
- describe "initialize" do
11
- it "should parse the text correctly" do
12
- dt = Tr8n::Tokens::DataTokenizer.new("Hello World")
13
- expect(dt.tokens).to be_empty
14
-
15
- dt = Tr8n::Tokens::DataTokenizer.new("Hello {world}")
16
- expect(dt.tokens.count).to equal(1)
17
- expect(dt.tokens.first.name).to eq("world")
18
- expect(dt.tokens.first.name(:parens => true)).to eq("{world}")
19
-
20
- dt = Tr8n::Tokens::DataTokenizer.new("Dear {user:gender}")
21
- expect(dt.tokens.count).to equal(1)
22
- expect(dt.tokens.first.name).to eq("user")
23
- expect(dt.tokens.first.name(:parens => true)).to eq("{user}")
24
- expect(dt.tokens.first.context_keys).to eq(['gender'])
25
- expect(dt.tokens.first.name(:parens => true, :context_keys => true)).to eq("{user:gender}")
26
- end
27
- end
28
- end
29
-
@@ -1,81 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Tr8n::Tokens::DecorationTokenizer do
6
-
7
- describe "parse" do
8
- it "should correctly parse tokens" do
9
- dt = Tr8n::Tokens::DecorationTokenizer.new("Hello World")
10
- expect(dt.fragments).to eq(["[tr8n]", "Hello World", "[/tr8n]"])
11
- expect(dt.parse).to eq(["tr8n", "Hello World"])
12
-
13
- dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello World]")
14
- expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello World", "]", "[/tr8n]"])
15
- expect(dt.parse).to eq(["tr8n", ["bold", "Hello World"]])
16
-
17
- dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello World")
18
- expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello World", "[/tr8n]"])
19
- expect(dt.parse).to eq(["tr8n", ["bold", "Hello World"]])
20
-
21
- dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello [strong: World]]")
22
- expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello ", "[strong:", " World", "]", "]", "[/tr8n]"])
23
- expect(dt.parse).to eq(["tr8n", ["bold", "Hello ", ["strong", "World"]]])
24
-
25
- dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello [strong: World]")
26
- expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello ", "[strong:", " World", "]", "[/tr8n]"])
27
- expect(dt.parse).to eq(["tr8n", ["bold", "Hello ", ["strong", "World"]]])
28
-
29
- dt = Tr8n::Tokens::DecorationTokenizer.new("[bold1: Hello [strong22: World]]")
30
- expect(dt.fragments).to eq(["[tr8n]", "[bold1:", " Hello ", "[strong22:", " World", "]", "]", "[/tr8n]"])
31
- expect(dt.parse).to eq(["tr8n", ["bold1", "Hello ", ["strong22", "World"]]])
32
-
33
- dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello, [strong: how] [weak: are] you?]")
34
- expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello, ", "[strong:", " how", "]", " ", "[weak:", " are", "]", " you?", "]", "[/tr8n]"])
35
- expect(dt.parse).to eq(["tr8n", ["bold", "Hello, ", ["strong", "how"], " ", ["weak", "are"], " you?"]])
36
-
37
- dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello, [strong: how [weak: are] you?]")
38
- expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello, ", "[strong:", " how ", "[weak:", " are", "]", " you?", "]", "[/tr8n]"])
39
- expect(dt.parse).to eq(["tr8n", ["bold", "Hello, ", ["strong", "how ", ["weak", "are"], " you?"]]])
40
-
41
- dt = Tr8n::Tokens::DecorationTokenizer.new("[link: you have [italic: [bold: {count}] messages] [light: in your mailbox]]")
42
- expect(dt.fragments).to eq(["[tr8n]", "[link:", " you have ", "[italic:", " ", "[bold:", " {count}", "]", " messages", "]", " ", "[light:", " in your mailbox", "]", "]", "[/tr8n]"])
43
- expect(dt.parse).to eq(["tr8n", ["link", "you have ", ["italic", "", ["bold", "{count}"], " messages"], " ", ["light", "in your mailbox"]]])
44
-
45
- dt = Tr8n::Tokens::DecorationTokenizer.new("[link] you have [italic: [bold: {count}] messages] [light: in your mailbox] [/link]")
46
- expect(dt.fragments).to eq(["[tr8n]", "[link]", " you have ", "[italic:", " ", "[bold:", " {count}", "]", " messages", "]", " ", "[light:", " in your mailbox", "]", " ", "[/link]", "[/tr8n]"])
47
- expect(dt.parse).to eq( ["tr8n", ["link", " you have ", ["italic", "", ["bold", "{count}"], " messages"], " ", ["light", "in your mailbox"], " "]])
48
- end
49
- end
50
-
51
- describe "substitute" do
52
- it "should correctly substitute tokens" do
53
- dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello World]")
54
- expect(dt.substitute).to eq("<strong>Hello World</strong>")
55
-
56
- dt = Tr8n::Tokens::DecorationTokenizer.new("[bold]Hello World[/bold]")
57
- expect(dt.substitute).to eq("<strong>Hello World</strong>")
58
-
59
- dt = Tr8n::Tokens::DecorationTokenizer.new("[bold] Hello World [/bold]")
60
- expect(dt.substitute).to eq("<strong> Hello World </strong>")
61
-
62
- dt = Tr8n::Tokens::DecorationTokenizer.new("[p: Hello World]", :p => '<p>{$0}</p>')
63
- expect(dt.substitute).to eq("<p>Hello World</p>")
64
-
65
- dt = Tr8n::Tokens::DecorationTokenizer.new("[p: Hello World]", :p => lambda{|text| "<p>#{text}</p>"})
66
- expect(dt.substitute).to eq("<p>Hello World</p>")
67
-
68
- dt = Tr8n::Tokens::DecorationTokenizer.new("[p]Hello World[/p]", :p => lambda{|text| "<p>#{text}</p>"})
69
- expect(dt.substitute).to eq("<p>Hello World</p>")
70
-
71
- dt = Tr8n::Tokens::DecorationTokenizer.new("[link: you have 5 messages]", "link" => '<a href="http://mail.google.com">{$0}</a>')
72
- expect(dt.substitute).to eq("<a href=\"http://mail.google.com\">you have 5 messages</a>")
73
-
74
- dt = Tr8n::Tokens::DecorationTokenizer.new("[link: you have {clount} messages]", "link" => '<a href="http://mail.google.com">{$0}</a>')
75
- expect(dt.substitute).to eq("<a href=\"http://mail.google.com\">you have {clount} messages</a>")
76
-
77
- end
78
- end
79
-
80
- end
81
-
@@ -1,24 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Tr8n::Tokens::Hidden do
6
- before do
7
- @app = init_application
8
- @english = @app.language('en-US')
9
- @tkey = Tr8n::TranslationKey.new({
10
- :label => "{_he_she}",
11
- :application => @app,
12
- :locale => 'en-US'
13
- })
14
- @token = @tkey.data_tokens.first
15
- end
16
-
17
- describe "initialize" do
18
- it "should parse data token" do
19
- expect(@token.class.name).to eq("Tr8n::Tokens::Hidden")
20
- expect(@token.context_keys).to eq([])
21
- expect(@token.allowed_in_translation?).to be_falsey
22
- end
23
- end
24
- end
@@ -1,84 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Tr8n::Tokens::Method do
6
- #before do
7
- # @app = init_application
8
- # @english = @app.language('en-US')
9
- # @tkey = Tr8n::TranslationKey.new({
10
- # :label => "Hello {user.first_name}",
11
- # :application => @app,
12
- # :locale => 'en-US'
13
- # })
14
- # @tlabel = @tkey.tokenized_label
15
- #end
16
- #
17
- #describe "initialize" do
18
- # it "should parse token info" do
19
- # token = @tlabel.tokens.first
20
- # expect(token.class.name).to eq("Tr8n::Tokens::Method")
21
- # expect(token.original_label).to eq(@tlabel.label)
22
- # expect(token.full_name).to eq("{user.first_name}")
23
- # expect(token.declared_name).to eq("user.first_name")
24
- # expect(token.name).to eq("user.first_name")
25
- # expect(token.sanitized_name).to eq("{user.first_name}")
26
- # expect(token.name_key).to eq(:"user.first_name")
27
- # expect(token.pipeless_name).to eq("user.first_name")
28
- # expect(token.case_key).to be_nil
29
- # expect(token.supports_cases?).to be_truthy
30
- # expect(token.has_case_key?).to be_falsey
31
- # expect(token.caseless_name).to eq("user.first_name")
32
- #
33
- # expect(token.types).to be_nil
34
- # expect(token.has_types?).to be_falsey
35
- # expect(token.associated_rule_types).to eq([:value])
36
- # expect(token.language_rule_classes).to eq([Tr8n::Rules::Value])
37
- # expect(token.transformable_language_rule_classes).to eq([])
38
- # expect(token.decoration?).to be_falsey
39
- # end
40
- #end
41
- #
42
- #describe "substitute" do
43
- # it "should substitute values" do
44
- # token = @tlabel.tokens.first
45
- #
46
- # user = stub_object({:first_name => "Tom", :last_name => "Anderson", :gender => "Male", :to_s => "Tom Anderson"})
47
- #
48
- # # tr("Hello {user}", "", {:user => current_user}}
49
- # expect(token.token_value(user, {}, @english)).to eq(user.to_s)
50
- #
51
- # # tr("Hello {user}", "", {:user => [current_user]}}
52
- # expect(token.token_value([user], {}, @english)).to eq(user.to_s)
53
- #
54
- # # tr("Hello {user}", "", {:user => [current_user, current_user.name]}}
55
- # expect(token.token_value([user, user.first_name], {}, @english)).to eq(user.first_name)
56
- #
57
- # # tr("Hello {user}", "", {:user => [current_user, "{$0} {$1}", "param1"]}}
58
- # expect(token.token_value([user, "{$0} {$1}", "param1"], {}, @english)).to eq(user.to_s + " param1")
59
- # expect(token.token_value([user, "{$0} {$1} {$2}", "param1", "param2"], {}, @english)).to eq(user.to_s + " param1 param2")
60
- #
61
- # # tr("Hello {user}", "", {:user => [current_user, :name]}}
62
- # expect(token.token_value([user, :first_name], {}, @english)).to eq(user.first_name)
63
- #
64
- # # tr("Hello {user}", "", {:user => [current_user, :method_name, "param1"]}}
65
- # user.stub(:last_name_with_prefix) {|prefix| "#{prefix} #{user.last_name}"}
66
- # expect(token.token_value([user, :last_name_with_prefix, 'Mr.'], {}, @english)).to eq("Mr. Anderson")
67
- #
68
- # # tr("Hello {user}", "", {:user => [current_user, lambda{|user| user.name}]}}
69
- # expect(token.token_value([user, lambda{|user| user.to_s}], {}, @english)).to eq(user.to_s)
70
- #
71
- # # tr("Hello {user}", "", {:user => [current_user, lambda{|user, param1| user.name}, "param1"]}}
72
- # expect(token.token_value([user, lambda{|user, param1| user.to_s + " " + param1}, "extra_param1"], {}, @english)).to eq(user.to_s + " extra_param1")
73
- #
74
- # # tr("Hello {user}", "", {:user => {:object => current_user, :value => current_user.name}]}}
75
- # expect(token.token_value({:object => user, :value => user.to_s}, {}, @english)).to eq(user.to_s)
76
- #
77
- # # tr("Hello {user}", "", {:user => {:object => current_user, :attribute => :first_name}]}}
78
- # expect(token.token_value({:object => user, :attribute => :first_name}, {}, @english)).to eq(user.first_name)
79
- # expect(token.token_value({:object => {:first_name => "Michael"}, :attribute => :first_name}, {}, @english)).to eq("Michael")
80
- # end
81
- #end
82
-
83
-
84
- end