|
| 1 | +use sqlbuild_core::text::main::ignorecase_key::ignorecase_key; |
| 2 | + |
| 3 | +use crate::assembly::analysis_session::main::names_set_operation::names_set_operation; |
| 4 | +use crate::assembly::analysis_session::tests::test_types::{ |
| 5 | + KeywordFoldingTestCase, NamesSetOperationTestCase, |
| 6 | +}; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn given_sql_when_checking_for_set_operations_then_matches_whole_words_in_any_case() { |
| 10 | + let test_cases = [ |
| 11 | + NamesSetOperationTestCase { |
| 12 | + description: "a lowercase union is found", |
| 13 | + sql: "select 1 union all select 2", |
| 14 | + expected_set_operation: true, |
| 15 | + }, |
| 16 | + NamesSetOperationTestCase { |
| 17 | + description: "a mixed-case intersect is found", |
| 18 | + sql: "SELECT a FROM t InTeRsEcT SELECT a FROM u", |
| 19 | + expected_set_operation: true, |
| 20 | + }, |
| 21 | + NamesSetOperationTestCase { |
| 22 | + description: "a keyword at the end of the text is found", |
| 23 | + sql: "select a from t\nexcept", |
| 24 | + expected_set_operation: true, |
| 25 | + }, |
| 26 | + NamesSetOperationTestCase { |
| 27 | + description: "a keyword inside an identifier is ignored", |
| 28 | + sql: "select reunion_id, union_count, except_flag from t", |
| 29 | + expected_set_operation: false, |
| 30 | + }, |
| 31 | + NamesSetOperationTestCase { |
| 32 | + description: "a keyword followed by a non-ASCII letter is ignored", |
| 33 | + sql: "select 1 as unioné", |
| 34 | + expected_set_operation: false, |
| 35 | + }, |
| 36 | + NamesSetOperationTestCase { |
| 37 | + description: "a keyword after a non-ASCII letter is ignored", |
| 38 | + sql: "select 1 as éunion", |
| 39 | + expected_set_operation: false, |
| 40 | + }, |
| 41 | + NamesSetOperationTestCase { |
| 42 | + description: "a keyword after non-ASCII punctuation is found", |
| 43 | + sql: "select 1 «union» select 2", |
| 44 | + expected_set_operation: true, |
| 45 | + }, |
| 46 | + NamesSetOperationTestCase { |
| 47 | + description: "text without a keyword is not a set operation", |
| 48 | + sql: "select ü, ß from t", |
| 49 | + expected_set_operation: false, |
| 50 | + }, |
| 51 | + ]; |
| 52 | + |
| 53 | + for test_case in test_cases { |
| 54 | + assert_eq!( |
| 55 | + names_set_operation(test_case.sql), |
| 56 | + test_case.expected_set_operation, |
| 57 | + "{}", |
| 58 | + test_case.description |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[test] |
| 64 | +fn given_non_ascii_characters_when_folded_then_none_matches_a_keyword_letter() { |
| 65 | + let test_cases = [ |
| 66 | + KeywordFoldingTestCase { |
| 67 | + description: "no non-ASCII character folds to a letter of 'union'", |
| 68 | + keyword: "union", |
| 69 | + expected_non_ascii_folds: Vec::new(), |
| 70 | + }, |
| 71 | + KeywordFoldingTestCase { |
| 72 | + description: "no non-ASCII character folds to a letter of 'intersect'", |
| 73 | + keyword: "intersect", |
| 74 | + expected_non_ascii_folds: Vec::new(), |
| 75 | + }, |
| 76 | + KeywordFoldingTestCase { |
| 77 | + description: "no non-ASCII character folds to a letter of 'except'", |
| 78 | + keyword: "except", |
| 79 | + expected_non_ascii_folds: Vec::new(), |
| 80 | + }, |
| 81 | + ]; |
| 82 | + |
| 83 | + for test_case in test_cases { |
| 84 | + let folds: Vec<char> = (0x80..=0x10FFFF_u32) |
| 85 | + .filter_map(char::from_u32) |
| 86 | + .filter(|character| test_case.keyword.contains(ignorecase_key(*character))) |
| 87 | + .collect(); |
| 88 | + |
| 89 | + assert_eq!( |
| 90 | + folds, test_case.expected_non_ascii_folds, |
| 91 | + "{}", |
| 92 | + test_case.description |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
0 commit comments