-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlinter_spec.rb
101 lines (85 loc) · 2.76 KB
/
linter_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
require_relative '../lib/sqlint'
RSpec.describe SQLint::Linter do
WIBBLE_ERROR = 'syntax error at or near "WIBBLE"'
let(:filename) { "some/file/here.sql" }
let(:input) { "SELECT 1" }
let(:input_stream) { StringIO.new(input) }
subject(:linter) { SQLint::Linter.new(filename, input_stream) }
let(:results) { linter.run.to_a }
def error(line, col, msg)
SQLint::Linter::Lint.new(filename, line, col, :error, msg)
end
def warning(line, col, msg)
SQLint::Linter::Lint.new(filename, line, col, :warning, msg)
end
context "with empty input" do
let(:input) { "" }
it "reports no errors" do
expect(results).to be_empty
end
end
context "with valid PG 12 syntax" do
let(:input) do <<-EOF
CREATE TABLE things (
x_in numeric,
x_cm numeric GENERATED ALWAYS AS (x / 2.54) STORED
);
EOF
end
it "reports no errors" do
expect(results).to be_empty
end
end
describe "single errors" do
context "with a single valid statement" do
it "reports no errors" do
expect(results).to be_empty
end
end
context "with a single invalid keyword" do
let(:input) { "WIBBLE" }
it "reports one error" do
expect(results).to eq([error(1, 1, WIBBLE_ERROR)])
end
end
context "with two successive invalid keywords" do
let(:input) { "WIBBLE WIBBLE" }
it "report 2 errors" do
expect(results).to eq([error(1, 1, WIBBLE_ERROR)])
end
end
context "with a single invalid keyword on a later line" do
let(:input) { "SELECT 1;\nWIBBLE" }
it "reports one error" do
expect(results).to eq([error(2, 1, WIBBLE_ERROR)])
end
end
context "with a single error part-way through a line" do
let(:input) { "SELECT '" }
it "reports one error" do
expect(results).to eq([error(1, 8, 'unterminated quoted string at or near "\'"')])
end
end
context "when there are 2 errors in separate statements" do
let(:input) { "WIBBLE; WIBBLE" }
it "report 2 errors" do
expect(results).to eq([error(1, 1, WIBBLE_ERROR), error(1, 9, WIBBLE_ERROR)])
end
end
context "when there is a second error at the end of the file" do
let(:input) { "WIBBLE; SELECT 1 FROM" }
it "reports 2 errors" do
expect(results).to eq([error(1, 1, WIBBLE_ERROR),
error(1, 21, "syntax error at end of input")])
end
end
end
describe "long error messages" do
context "when the 'at or near' fragment is longer than 50 characters" do
let(:input) { "SELECT '" + 'x' * 100 }
it "truncates the fragment" do
expect(results).to eq([error(1, 8, "unterminated quoted string at or near \"'#{'x' * 49}\"")])
end
end
end
end