/
interpolation_compiler_test.rb
118 lines (100 loc) · 4.49 KB
/
interpolation_compiler_test.rb
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
110
111
112
113
114
115
116
117
118
require 'test_helper'
class InterpolationCompilerTest < I18n::TestCase
Compiler = I18n::Backend::InterpolationCompiler::Compiler
def compile_and_interpolate(str, values = {})
Compiler.compile_if_an_interpolation(str).i18n_interpolate(values)
end
def assert_escapes_interpolation_key(expected, malicious_str)
assert_equal(expected, Compiler.send(:escape_key_sym, malicious_str))
end
def test_escape_key_properly_escapes
assert_escapes_interpolation_key ':"\""', '"'
assert_escapes_interpolation_key ':"\\\\"', '\\'
assert_escapes_interpolation_key ':"\\\\\""', '\\"'
assert_escapes_interpolation_key ':"\#{}"', '#{}'
assert_escapes_interpolation_key ':"\\\\\#{}"', '\#{}'
end
def assert_escapes_plain_string(expected, plain_str)
assert_equal expected, Compiler.send(:escape_plain_str, plain_str)
end
def test_escape_plain_string_properly_escapes
assert_escapes_plain_string '\\"', '"'
assert_escapes_plain_string '\'', '\''
assert_escapes_plain_string '\\#', '#'
assert_escapes_plain_string '\\#{}', '#{}'
assert_escapes_plain_string '\\\\\\"','\\"'
end
def test_non_interpolated_strings_or_arrays_dont_get_compiled
['abc', '\\{a}}', '{a}}', []].each do |obj|
Compiler.compile_if_an_interpolation(obj)
assert_equal false, obj.respond_to?(:i18n_interpolate)
end
end
def test_interpolated_string_gets_compiled
assert_equal '-A-', compile_and_interpolate('-%{a}-', :a => 'A')
end
def assert_handles_key(str, key)
assert_equal 'A', compile_and_interpolate(str, key => 'A')
end
def test_compiles_fancy_keys
assert_handles_key('%{\}', :'\\' )
assert_handles_key('%{#}', :'#' )
assert_handles_key('%{#{}', :'#{' )
assert_handles_key('%{#$SAFE}', :'#$SAFE')
assert_handles_key('%{\000}', :'\000' )
assert_handles_key('%{\'}', :'\'' )
assert_handles_key('%{\'\'}', :'\'\'' )
assert_handles_key('%{a.b}', :'a.b' )
assert_handles_key('%{ }', :' ' )
assert_handles_key('%{:}', :':' )
assert_handles_key("%{:''}", :":''" )
assert_handles_key('%{:"}', :':"' )
end
def test_str_containing_only_escaped_interpolation_is_handled_correctly
assert_equal 'abc %{x}', compile_and_interpolate('abc %%{x}')
end
def test_handles_weird_strings
assert_equal '#{} a', compile_and_interpolate('#{} %{a}', :a => 'a')
assert_equal '"#{abc}"', compile_and_interpolate('"#{ab%{a}c}"', :a => '' )
assert_equal 'a}', compile_and_interpolate('%{{a}}', :'{a' => 'a')
assert_equal '"', compile_and_interpolate('"%{a}', :a => '' )
assert_equal 'a%{a}', compile_and_interpolate('%{a}%%{a}', :a => 'a')
assert_equal '%%{a}', compile_and_interpolate('%%%{a}')
assert_equal '\";eval("a")', compile_and_interpolate('\";eval("%{a}")', :a => 'a')
assert_equal '\";eval("a")', compile_and_interpolate('\";eval("a")%{a}', :a => '' )
assert_equal "\na", compile_and_interpolate("\n%{a}", :a => 'a')
end
def test_raises_exception_when_argument_is_missing
assert_raises(I18n::MissingInterpolationArgument) do
compile_and_interpolate('%{first} %{last}', :first => 'first')
end
end
def test_custom_missing_interpolation_argument_handler
old_handler = I18n.config.missing_interpolation_argument_handler
I18n.config.missing_interpolation_argument_handler = lambda do |key, values, string|
"missing key is #{key}, values are #{values.inspect}, given string is '#{string}'"
end
assert_equal %|first missing key is last, values are {:first=>"first"}, given string is '%{first} %{last}'|,
compile_and_interpolate('%{first} %{last}', :first => 'first')
ensure
I18n.config.missing_interpolation_argument_handler = old_handler
end
end
class I18nBackendInterpolationCompilerTest < I18n::TestCase
class Backend < I18n::Backend::Simple
include I18n::Backend::InterpolationCompiler
end
include I18n::Tests::Interpolation
def setup
I18n.backend = Backend.new
super
end
# pre-compile default strings to make sure we are testing I18n::Backend::InterpolationCompiler
def interpolate(*args)
options = args.last.kind_of?(Hash) ? args.last : {}
if default_str = options[:default]
I18n::Backend::InterpolationCompiler::Compiler.compile_if_an_interpolation(default_str)
end
super
end
end