use sqrust_core::{FileContext, Rule};
use sqrust_rules::layout::unicode_identifiers::UnicodeIdentifiers;
fn check(sql: &str) -> Vec<sqrust_core::Diagnostic> {
let ctx = FileContext::from_source(sql, "test.sql");
UnicodeIdentifiers.check(&ctx)
}
#[test]
fn rule_name_is_correct() {
let ctx = FileContext::from_source("SELECT 1", "test.sql");
let diags = UnicodeIdentifiers.check(&ctx);
let ctx2 = FileContext::from_source("SELECT * FROM café", "test.sql");
let diags2 = UnicodeIdentifiers.check(&ctx2);
assert!(!diags2.is_empty());
assert_eq!(diags2[0].rule, "Layout/UnicodeIdentifiers");
let _ = diags; }
#[test]
fn parse_error_produces_no_violations() {
let ctx = FileContext::from_source("SELECT FROM FROM", "test.sql");
assert!(!ctx.parse_errors.is_empty());
let diags = UnicodeIdentifiers.check(&ctx);
assert!(diags.is_empty());
}
#[test]
fn pure_ascii_sql_no_violations() {
let diags = check("SELECT id, name FROM users WHERE id = 1;");
assert!(diags.is_empty());
}
#[test]
fn non_ascii_in_unquoted_table_name_flagged() {
let diags = check("SELECT * FROM café");
assert!(!diags.is_empty());
}
#[test]
fn non_ascii_in_unquoted_column_name_flagged() {
let diags = check("SELECT prénom FROM t");
assert!(!diags.is_empty());
}
#[test]
fn non_ascii_inside_single_quoted_string_not_flagged() {
let diags = check("SELECT * FROM t WHERE name = 'André'");
assert!(diags.is_empty());
}
#[test]
fn non_ascii_inside_double_quoted_identifier_not_flagged() {
let diags = check("SELECT \"prénom\" FROM t");
assert!(diags.is_empty());
}
#[test]
fn non_ascii_inside_block_comment_not_flagged() {
let diags = check("/* ünde */ SELECT 1");
assert!(diags.is_empty());
}
#[test]
fn non_ascii_inside_line_comment_not_flagged() {
let diags = check("SELECT 1 -- ünde\nFROM t");
assert!(diags.is_empty());
}
#[test]
fn multiple_non_ascii_chars_in_one_word_one_violation_per_char() {
let diags = check("SELECT éà FROM t");
assert_eq!(diags.len(), 2);
}
#[test]
fn non_ascii_in_both_quoted_and_unquoted_only_unquoted_flagged() {
let diags = check("SELECT * FROM café WHERE name = 'André'");
assert!(!diags.is_empty());
for d in &diags {
assert!(d.col < 22, "col {} should be within 'café'", d.col);
}
}
#[test]
fn line_and_col_reported_correctly() {
let diags = check("SELECT 1\nFROM café");
assert!(!diags.is_empty());
assert_eq!(diags[0].line, 2);
assert_eq!(diags[0].col, 9);
}
#[test]
fn message_format_correct() {
let diags = check("SELECT * FROM café");
assert!(!diags.is_empty());
assert_eq!(
diags[0].message,
"Non-ASCII character found in SQL; use ASCII identifiers for portability"
);
}
#[test]
fn empty_sql_no_violations() {
let diags = check("");
assert!(diags.is_empty());
}