Skip to content

Commit 24043e9

Browse files
committed
Factorize tests
1 parent 9b88152 commit 24043e9

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

src/lexer/sql/test.rs

+28-29
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,21 @@ fn count_named_placeholders() {
4444

4545
#[test]
4646
fn duplicate_column() {
47-
expect_parser_err(
47+
expect_parser_err_msg(
4848
b"CREATE TABLE t (x TEXT, x TEXT)",
4949
"duplicate column name: x",
5050
);
5151
}
5252

5353
#[test]
5454
fn create_table_without_column() {
55-
let r = parse(b"CREATE TABLE t ()");
56-
let Error::ParserError(
55+
expect_parser_err(
56+
b"CREATE TABLE t ()",
5757
ParserError::SyntaxError {
5858
token_type: "RP",
5959
found: None,
6060
},
61-
_,
62-
) = r.unwrap_err()
63-
else {
64-
panic!("unexpected error type")
65-
};
61+
);
6662
}
6763

6864
#[test]
@@ -147,44 +143,44 @@ fn extra_comments_between_statements() {
147143

148144
#[test]
149145
fn insert_mismatch_count() {
150-
expect_parser_err(b"INSERT INTO t (a, b) VALUES (1)", "1 values for 2 columns");
146+
expect_parser_err_msg(b"INSERT INTO t (a, b) VALUES (1)", "1 values for 2 columns");
151147
}
152148

153149
#[test]
154150
fn insert_default_values() {
155-
expect_parser_err(
151+
expect_parser_err_msg(
156152
b"INSERT INTO t (a) DEFAULT VALUES",
157153
"0 values for 1 columns",
158154
);
159155
}
160156

161157
#[test]
162158
fn create_view_mismatch_count() {
163-
expect_parser_err(
159+
expect_parser_err_msg(
164160
b"CREATE VIEW v (c1, c2) AS SELECT 1",
165161
"expected 2 columns for v but got 1",
166162
);
167163
}
168164

169165
#[test]
170166
fn create_view_duplicate_column_name() {
171-
expect_parser_err(
167+
expect_parser_err_msg(
172168
b"CREATE VIEW v (c1, c1) AS SELECT 1, 2",
173169
"duplicate column name: c1",
174170
);
175171
}
176172

177173
#[test]
178174
fn create_table_without_rowid_missing_pk() {
179-
expect_parser_err(
175+
expect_parser_err_msg(
180176
b"CREATE TABLE t (c1) WITHOUT ROWID",
181177
"PRIMARY KEY missing on table t",
182178
);
183179
}
184180

185181
#[test]
186182
fn create_temporary_table_with_qualified_name() {
187-
expect_parser_err(
183+
expect_parser_err_msg(
188184
b"CREATE TEMPORARY TABLE mem.x AS SELECT 1",
189185
"temporary table name must be unqualified",
190186
);
@@ -193,20 +189,20 @@ fn create_temporary_table_with_qualified_name() {
193189

194190
#[test]
195191
fn create_table_with_only_generated_column() {
196-
expect_parser_err(
192+
expect_parser_err_msg(
197193
b"CREATE TABLE test(data AS (1))",
198194
"must have at least one non-generated column",
199195
);
200196
}
201197

202198
#[test]
203199
fn create_strict_table_missing_datatype() {
204-
expect_parser_err(b"CREATE TABLE t (c1) STRICT", "missing datatype for t.c1");
200+
expect_parser_err_msg(b"CREATE TABLE t (c1) STRICT", "missing datatype for t.c1");
205201
}
206202

207203
#[test]
208204
fn create_strict_table_unknown_datatype() {
209-
expect_parser_err(
205+
expect_parser_err_msg(
210206
b"CREATE TABLE t (c1 BOOL) STRICT",
211207
"unknown datatype for t.c1: \"BOOL\"",
212208
);
@@ -225,76 +221,79 @@ fn create_strict_table_generated_column() {
225221

226222
#[test]
227223
fn selects_compound_mismatch_columns_count() {
228-
expect_parser_err(
224+
expect_parser_err_msg(
229225
b"SELECT 1 UNION SELECT 1, 2",
230226
"SELECTs to the left and right of UNION do not have the same number of result columns",
231227
);
232228
}
233229

234230
#[test]
235231
fn delete_order_by_without_limit() {
236-
expect_parser_err(
232+
expect_parser_err_msg(
237233
b"DELETE FROM t ORDER BY x",
238234
"ORDER BY without LIMIT on DELETE",
239235
);
240236
}
241237

242238
#[test]
243239
fn update_order_by_without_limit() {
244-
expect_parser_err(
240+
expect_parser_err_msg(
245241
b"UPDATE t SET x = 1 ORDER BY x",
246242
"ORDER BY without LIMIT on UPDATE",
247243
);
248244
}
249245

250246
#[test]
251247
fn values_mismatch_columns_count() {
252-
expect_parser_err(
248+
expect_parser_err_msg(
253249
b"INSERT INTO t VALUES (1), (1,2)",
254250
"all VALUES must have the same number of terms",
255251
);
256252
}
257253

258254
#[test]
259255
fn alter_add_column_primary_key() {
260-
expect_parser_err(
256+
expect_parser_err_msg(
261257
b"ALTER TABLE t ADD COLUMN c PRIMARY KEY",
262258
"Cannot add a PRIMARY KEY column",
263259
);
264260
}
265261

266262
#[test]
267263
fn alter_add_column_unique() {
268-
expect_parser_err(
264+
expect_parser_err_msg(
269265
b"ALTER TABLE t ADD COLUMN c UNIQUE",
270266
"Cannot add a UNIQUE column",
271267
);
272268
}
273269

274270
#[test]
275271
fn alter_rename_same() {
276-
expect_parser_err(
272+
expect_parser_err_msg(
277273
b"ALTER TABLE t RENAME TO t",
278274
"there is already another table or index with this name: t",
279275
);
280276
}
281277

282278
#[test]
283279
fn natural_join_on() {
284-
expect_parser_err(
280+
expect_parser_err_msg(
285281
b"SELECT x FROM t NATURAL JOIN t USING (x)",
286282
"a NATURAL join may not have an ON or USING clause",
287283
);
288-
expect_parser_err(
284+
expect_parser_err_msg(
289285
b"SELECT x FROM t NATURAL JOIN t ON t.x = t.x",
290286
"a NATURAL join may not have an ON or USING clause",
291287
);
292288
}
293289

294-
fn expect_parser_err(input: &[u8], error_msg: &str) {
290+
fn expect_parser_err_msg(input: &[u8], error_msg: &str) {
291+
expect_parser_err(input, ParserError::Custom(error_msg.to_owned()))
292+
}
293+
fn expect_parser_err(input: &[u8], err: ParserError) {
295294
let r = parse(input);
296-
if let Error::ParserError(ParserError::Custom(ref msg), _) = r.unwrap_err() {
297-
assert_eq!(msg, error_msg);
295+
if let Error::ParserError(e, _) = r.unwrap_err() {
296+
assert_eq!(e, err);
298297
} else {
299298
panic!("unexpected error type")
300299
};

src/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::dialect::Token;
1919
use ast::{Cmd, ExplainKind, Name, Stmt};
2020

2121
/// Parser error
22-
#[derive(Debug)]
22+
#[derive(Debug, PartialEq)]
2323
pub enum ParserError {
2424
/// Stack overflow
2525
StackOverflow,

0 commit comments

Comments
 (0)