|
| 1 | +require_relative '../lib/sqlint' |
| 2 | + |
| 3 | +RSpec.describe SQLint do |
| 4 | + |
| 5 | + let(:filename) { "some/file/here.sql" } |
| 6 | + let(:input) { "SELECT 1" } |
| 7 | + let(:input_stream) { StringIO.new(input) } |
| 8 | + subject(:linter) { SQLint::Linter.new(filename, input_stream) } |
| 9 | + let(:results) { linter.run } |
| 10 | + |
| 11 | + def error(line, col, msg) |
| 12 | + SQLint::Linter::Lint.new(filename, line, col, :error, msg) |
| 13 | + end |
| 14 | + |
| 15 | + def warning(line, col, msg) |
| 16 | + SQLint::Linter::Lint.new(filename, line, col, :warning, msg) |
| 17 | + end |
| 18 | + |
| 19 | + context "with empty input" do |
| 20 | + let(:input) { "" } |
| 21 | + |
| 22 | + it "reports no errors" do |
| 23 | + expect(results).to be_empty |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + describe "single errors" do |
| 28 | + context "with a single valid statement" do |
| 29 | + it "reports no errors" do |
| 30 | + expect(results).to be_empty |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + context "with a single invalid keyword" do |
| 35 | + let(:input) { "WIBBLE" } |
| 36 | + it "reports one error" do |
| 37 | + expect(results.size).to eq(1) |
| 38 | + expect(results.first).to eq(error(1, 1, 'syntax error at or near "WIBBLE"')) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + context "with a single invalid keyword on a later line" do |
| 43 | + let(:input) { "SELECT 1;\nWIBBLE" } |
| 44 | + it "reports one error" do |
| 45 | + expect(results.size).to eq(1) |
| 46 | + expect(results.first).to eq(error(2, 1, 'syntax error at or near "WIBBLE"')) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context "with a single error part-way through a line" do |
| 51 | + let(:input) { "SELECT '" } |
| 52 | + it "reports one error" do |
| 53 | + expect(results.size).to eq(1) |
| 54 | + expect(results.first).to eq(error(1, 8, 'unterminated quoted string at or near "\'"')) |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | +end |
0 commit comments