forked from puppetlabs/rubocop-i18n
/
decorate_string_spec.rb
69 lines (60 loc) · 2.98 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
# frozen_string_literal: true
require 'spec_helper'
describe RuboCop::Cop::I18n::GetText::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."', '_', 'decorator is missing around sentence'
it_behaves_like 'a_detecting_cop', 'thing("A sentence that is not decorated.")', '_', 'decorator is missing around sentence'
it_behaves_like 'a_fixing_cop', 'thing("A sentence that is not decorated.")', 'thing(_("A sentence that is not decorated."))', 'thing'
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'
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\"", '_', 'decorator is missing around sentence'
it_behaves_like 'a_detecting_cop', "b = \"line one.
A sentence line two.\"", '_', 'decorator is missing around sentence'
end
RuboCop::Cop::I18n::GetText.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
end
end