forked from puppetlabs/rubocop-i18n
/
decorate_string_spec.rb
165 lines (139 loc) · 8.14 KB
/
decorate_string_spec.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true
require 'spec_helper'
describe RuboCop::Cop::I18n::RailsI18n::DecorateString, :config do
before(:each) do
@offenses = investigate(cop, source)
end
context 'decoration needed for string' do
it_behaves_like 'a_detecting_cop', 'a = "A sentence that is not decorated."', 't', 'decorator is missing around sentence'
it_behaves_like 'a_detecting_cop', 'thing("A sentence that is not decorated.")', 't', 'decorator is missing around sentence'
end
context 'decoration not needed for string' do
it_behaves_like 'a_no_cop_required', "'keyword'"
# a regexp is a string
it_behaves_like 'a_no_cop_required', '@regexp = /[ =]/'
it_behaves_like 'a_no_cop_required', 'Dir[File.dirname(__FILE__) + "/parser/compiler/catalog_validator/*.rb"].each { |f| require f }'
it_behaves_like 'a_no_cop_required', 'stream.puts "#@version\n" if @version'
it_behaves_like 'a_no_cop_required', '
f.puts(<<-YAML)
---
:tag: yaml
:yaml:
:something: #{variable}
YAML'
end
context 'decoration not needed for a hash key' do
# a string as an hash key is ok
it_behaves_like 'a_no_cop_required', 'memo["#{class_path}##{method}-#{source_line}"] += 1' # rubocop:disable Lint/InterpolationCheck
end
context 'string with invalid UTF-8' do
it_behaves_like 'a_no_cop_required', '
STRING_MAP = {
Encoding::UTF_8 => "\uFFFD",
Encoding::UTF_16LE => "\xFD\xFF".force_encoding(Encoding::UTF_16LE),
}'
end
context 'decoration missing for dstr' do
it_behaves_like 'a_detecting_cop', "b = \"A sentence line one.
line two\"", 't', 'decorator is missing around sentence'
it_behaves_like 'a_detecting_cop', "b = \"line one.
A sentence line two.\"", 't', 'decorator is missing around sentence'
end
RuboCop::Cop::I18n::RailsI18n.supported_decorators.each do |decorator|
context "#{decorator} already present" do
it_behaves_like 'a_no_cop_required', "#{decorator}('a string')"
it_behaves_like 'a_no_cop_required', "#{decorator} \"a string\""
it_behaves_like 'a_no_cop_required', "a = #{decorator}('a string')"
it_behaves_like 'a_no_cop_required', "#{decorator}(\"a %-5.2.s thing s string\")"
it_behaves_like 'a_no_cop_required', "Log.warning #{decorator}(\"could not change to group %<group>: %<detail>\", group: group, detail: detail)"
it_behaves_like 'a_no_cop_required', "Log.warning #{decorator}(\"could not change to group %<group>: %<detail>\",
group: #{decorator}(\"group\"), detail: #{decorator}(\"detail\"))"
end
context "#{decorator} around dstr" do
it_behaves_like 'a_no_cop_required', "a = #{decorator}(\"A sentence line one.
line two\")"
it_behaves_like 'a_no_cop_required', "a = #{decorator}(\"line one.
A sentence line two.\")"
end
context "I18n.#{decorator} already present" do
it_behaves_like 'a_no_cop_required', "I18n.#{decorator}('a string')"
it_behaves_like 'a_no_cop_required', "I18n.#{decorator} \"a string\""
it_behaves_like 'a_no_cop_required', "a = I18n.#{decorator}('a string')"
it_behaves_like 'a_no_cop_required', "I18n.#{decorator}(\"a %-5.2.s thing s string\")"
it_behaves_like 'a_no_cop_required', "Log.warning I18n.#{decorator}(\"could not change to group %<group>: %<detail>\", group: group, detail: detail)"
it_behaves_like 'a_no_cop_required', "Log.warning I18n.#{decorator}(\"could not change to group %<group>: %<detail>\",
group: I18n.#{decorator}(\"group\"), detail: I18n.#{decorator}(\"detail\"))"
end
context "I18n.#{decorator} around dstr" do
it_behaves_like 'a_no_cop_required', "a = I18n.#{decorator}(\"A sentence line one.
line two\")"
it_behaves_like 'a_no_cop_required', "a = I18n.#{decorator}(\"line one.
A sentence line two.\")"
end
context "SomeOtherMod.#{decorator} is used" do
it_behaves_like 'a_no_cop_required', "SomeOtherMod.#{decorator}('a string')"
it_behaves_like 'a_no_cop_required', "SomeOtherMod.#{decorator} \"a string\""
it_behaves_like 'a_no_cop_required', "a = SomeOtherMod.#{decorator}('a string')"
it_behaves_like 'a_no_cop_required', "SomeOtherMod.#{decorator}(\"a %-5.2.s thing s string\")"
it_behaves_like 'a_no_cop_required', "Log.warning SomeOtherMod.#{decorator}(\"could not change to group %<group>: %<detail>\", group: group, detail: detail)"
it_behaves_like 'a_no_cop_required', "Log.warning SomeOtherMod.#{decorator}(\"could not change to group %<group>: %<detail>\",
group: SomeOtherMod.#{decorator}(\"group\"), detail: SomeOtherMod.#{decorator}(\"detail\"))"
it_behaves_like 'a_detecting_cop', "SomeOtherMod.#{decorator}('Some sentence like text.')", decorator, 'decorator is missing around sentence'
it_behaves_like 'a_detecting_cop', "SomeOtherMod.#{decorator} \"Some sentence like text.\"", decorator, 'decorator is missing around sentence'
end
context "SomeOtherMod.#{decorator} around dstr" do
it_behaves_like 'a_detecting_cop', "a = SomeOtherMod.#{decorator}(\"A sentence line one.
line two\")", decorator, 'decorator is missing around sentence'
it_behaves_like 'a_detecting_cop', "a = SomeOtherMod.#{decorator}(\"line one.
A sentence line two.\")", decorator, 'decorator is missing around sentence'
it_behaves_like 'a_no_cop_required', "a = SomeOtherMod.#{decorator}(\"line one.
a sentence line two\")"
end
end
context 'when ignoring raised exceptions' do
let(:config) do
RuboCop::Config.new('I18n/RailsI18n/DecorateString' => { 'IgnoreExceptions' => true })
end
%w[fail raise].each do |type|
it_behaves_like 'a_no_cop_required', "#{type} \"A sentence that is not decorated.\""
it_behaves_like 'a_no_cop_required', "#{type} StandardError, \"A sentence that is not decorated.\""
it_behaves_like 'a_no_cop_required', "#{type} StandardError.new(\"A sentence that is not decorated.\")"
end
end
context 'when configuring a different regex' do
let(:config) do
RuboCop::Config.new('I18n/RailsI18n/DecorateString' => { 'Regexp' => '^test-test-test$' })
end
it_behaves_like 'a_no_cop_required', "not_t('A sentence.')"
it_behaves_like 'a_detecting_cop', "not_t('test-test-test')", 't', 'decorator is missing around sentence'
end
context 'when string type' do
let(:config) do
RuboCop::Config.new('I18n/RailsI18n/DecorateString' => { 'EnforcedSentenceType' => type })
end
context 'is sentence' do
let(:type) { 'sentence' }
it_behaves_like 'a_no_cop_required', "not_t('word')"
it_behaves_like 'a_no_cop_required', "not_t('a fragment')"
it_behaves_like 'a_no_cop_required', "not_t('A sentence fragment')"
it_behaves_like 'a_no_cop_required', "not_t('a sentence fragment.')"
it_behaves_like 'a_detecting_cop', "not_t('A real sentence.')", 't', 'decorator is missing around sentence'
end
context 'is fragmented sentence' do
let(:type) { 'fragmented_sentence' }
it_behaves_like 'a_no_cop_required', "not_t('word')"
it_behaves_like 'a_no_cop_required', "not_t('a fragment')"
it_behaves_like 'a_detecting_cop', "not_t('A sentence fragment')", 't', 'decorator is missing around sentence'
it_behaves_like 'a_detecting_cop', "not_t('a sentence fragment.')", 't', 'decorator is missing around sentence'
it_behaves_like 'a_detecting_cop', "not_t('A real sentence.')", 't', 'decorator is missing around sentence'
end
context 'is fragment' do
let(:type) { 'fragment' }
it_behaves_like 'a_no_cop_required', "not_t('word')"
it_behaves_like 'a_detecting_cop', "not_t('a fragment')", 't', 'decorator is missing around sentence'
it_behaves_like 'a_detecting_cop', "not_t('A sentence fragment')", 't', 'decorator is missing around sentence'
it_behaves_like 'a_detecting_cop', "not_t('a sentence fragment.')", 't', 'decorator is missing around sentence'
it_behaves_like 'a_detecting_cop', "not_t('A real sentence.')", 't', 'decorator is missing around sentence'
end
end
end