-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathtest_rubocop_heredoc.rb
153 lines (126 loc) · 4 KB
/
test_rubocop_heredoc.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
# frozen_string_literal: true
require "rubocop/cop/graphql/heredoc"
require "minitest/autorun"
class TestRubocopHeredoc < Minitest::Test
def setup
config = RuboCop::Config.new
@cop = RuboCop::Cop::GraphQL::Heredoc.new(config)
end
def test_good_graphql_heredoc
investigate(@cop, <<-RUBY)
Query = Client.parse <<'GRAPHQL'
{ version }
GRAPHQL
RUBY
assert_empty @cop.offenses.map(&:message)
end
def test_good_graphql_dash_heredoc
investigate(@cop, <<-RUBY)
Query = Client.parse <<-'GRAPHQL'
{ version }
GRAPHQL
RUBY
assert_empty @cop.offenses.map(&:message)
end
def test_good_graphql_squiggly_heredoc
investigate(@cop, <<-RUBY)
Query = Client.parse <<~'GRAPHQL'
{ version }
GRAPHQL
RUBY
assert_empty @cop.offenses.map(&:message)
end
def test_bad_graphql_heredoc
investigate(@cop, <<-RUBY)
Query = Client.parse <<GRAPHQL
{ version }
GRAPHQL
RUBY
assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses.first.message
end
def test_bad_graphql_dash_heredoc
investigate(@cop, <<-RUBY)
Query = Client.parse <<-GRAPHQL
{ version }
GRAPHQL
RUBY
assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses.first.message
end
def test_bad_graphql_squiggly_heredoc
skip if RUBY_VERSION < "2.3"
investigate(@cop, <<-RUBY)
Query = Client.parse <<~GRAPHQL
{ version }
GRAPHQL
RUBY
assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses.first.message
end
def test_bad_graphql_heredoc_with_interpolation
investigate(@cop, <<-RUBY)
field = "version"
Query = Client.parse <<-GRAPHQL
{ \#{field} }
GRAPHQL
RUBY
assert_equal 2, @cop.offenses.count
assert_equal "GraphQL/Heredoc: Do not interpolate variables into GraphQL queries, used variables instead.", @cop.offenses[0].message
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[1].message
end
def test_bad_graphql_multiline_heredoc
investigate(@cop, <<-RUBY)
Query = Client.parse <<GRAPHQL
{
version
}
GRAPHQL
RUBY
assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[0].message
end
def test_bad_graphql_multiline_dash_heredoc
investigate(@cop, <<-RUBY)
Query = Client.parse <<-GRAPHQL
{
version
}
GRAPHQL
RUBY
assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[0].message
end
def test_bad_graphql_multiline_squiggly_heredoc
skip if RUBY_VERSION < "2.3"
investigate(@cop, <<-RUBY)
Query = Client.parse <<~GRAPHQL
{
version
}
GRAPHQL
RUBY
assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[0].message
end
def test_bad_graphql_multiline_heredoc_with_interpolation
investigate(@cop, <<-RUBY)
field = "version"
Query = Client.parse <<-GRAPHQL
{
\#{field}
}
GRAPHQL
RUBY
assert_equal 2, @cop.offenses.count
assert_equal "GraphQL/Heredoc: Do not interpolate variables into GraphQL queries, used variables instead.", @cop.offenses[0].message
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[1].message
end
private
def investigate(cop, src)
processed_source = RuboCop::ProcessedSource.new(src, RUBY_VERSION.to_f)
commissioner = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
commissioner.investigate(processed_source)
commissioner
end
end