forked from rubocop/rubocop
/
encoding_spec.rb
135 lines (115 loc) · 3.34 KB
/
encoding_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::Encoding, :config do
it 'does not register an offense when no encoding present' do
expect_no_offenses(<<~RUBY)
def foo() end
RUBY
end
it 'does not register an offense when encoding present but not UTF-8' do
expect_no_offenses(<<~RUBY)
# encoding: us-ascii
def foo() end
RUBY
end
it 'does not register an offense on a different magic comment type' do
expect_no_offenses(<<~RUBY)
# frozen-string-literal: true
def foo() end
RUBY
end
it 'registers an offense when encoding present and UTF-8' do
expect_offense(<<~RUBY)
# encoding: utf-8
^^^^^^^^^^^^^^^^^ Unnecessary utf-8 encoding comment.
def foo() end
RUBY
expect_correction(<<~RUBY)
def foo() end
RUBY
end
it 'registers an offense when encoding present on 2nd line after shebang' do
expect_offense(<<~RUBY)
#!/usr/bin/env ruby
# encoding: utf-8
^^^^^^^^^^^^^^^^^ Unnecessary utf-8 encoding comment.
def foo() end
RUBY
expect_correction(<<~RUBY)
#!/usr/bin/env ruby
def foo() end
RUBY
end
it 'registers an offense and corrects if there are multiple encoding magic comments' do
expect_offense(<<~RUBY)
# encoding: utf-8
^^^^^^^^^^^^^^^^^ Unnecessary utf-8 encoding comment.
# coding: utf-8
^^^^^^^^^^^^^^^ Unnecessary utf-8 encoding comment.
def foo() end
RUBY
expect_correction(<<~RUBY)
def foo() end
RUBY
end
it 'registers an offense and corrects the magic comment follows another magic comment' do
expect_offense(<<~RUBY)
# frozen-string-literal: true
# encoding: utf-8
^^^^^^^^^^^^^^^^^ Unnecessary utf-8 encoding comment.
def foo() end
RUBY
expect_correction(<<~RUBY)
# frozen-string-literal: true
def foo() end
RUBY
end
it 'does not register an offense when encoding is not at the top of the file' do
expect_no_offenses(<<~RUBY)
# frozen-string-literal: true
# encoding: utf-8
def foo() end
RUBY
end
it 'does not register an offense when encoding is in the wrong place' do
expect_no_offenses(<<~RUBY)
def foo() end
# encoding: utf-8
RUBY
end
context 'vim comments' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
# vim:filetype=ruby, fileencoding=utf-8
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary utf-8 encoding comment.
def foo() end
RUBY
expect_correction(<<~RUBY)
# vim: filetype=ruby
def foo() end
RUBY
end
end
context 'emacs comment' do
it 'registers an offense for encoding' do
expect_offense(<<~RUBY)
# -*- encoding : utf-8 -*-
^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary utf-8 encoding comment.
def foo() 'ä' end
RUBY
expect_correction(<<~RUBY)
def foo() 'ä' end
RUBY
end
it 'only removes encoding if there are other editor comments' do
expect_offense(<<~RUBY)
# -*- encoding : utf-8; mode: enh-ruby -*-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary utf-8 encoding comment.
def foo() 'ä' end
RUBY
expect_correction(<<~RUBY)
# -*- mode: enh-ruby -*-
def foo() 'ä' end
RUBY
end
end
end