-
Notifications
You must be signed in to change notification settings - Fork 0
Preserve user-supplied grouping parentheses in boolean expressions #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gmr
wants to merge
1
commit into
main
Choose a base branch
from
fix/preserve-grouping-parens
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+230
−36
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| use libpgfmt::{format, style::Style}; | ||
| use std::path::Path; | ||
|
|
||
| fn run_fixture(style: Style, style_name: &str, name: &str) { | ||
| let base = Path::new(env!("CARGO_MANIFEST_DIR")) | ||
| .join("tests") | ||
| .join("fixtures") | ||
| .join(style_name); | ||
| let sql_path = base.join(format!("{name}.sql")); | ||
| let expected_path = base.join(format!("{name}.expected")); | ||
|
|
||
| let sql = std::fs::read_to_string(&sql_path) | ||
| .unwrap_or_else(|e| panic!("Failed to read {}: {e}", sql_path.display())); | ||
| let expected = std::fs::read_to_string(&expected_path) | ||
| .unwrap_or_else(|e| panic!("Failed to read {}: {e}", expected_path.display())); | ||
|
|
||
| let result = format(sql.trim(), style); | ||
| match result { | ||
| Ok(formatted) => { | ||
| pretty_assertions::assert_eq!( | ||
| formatted.trim(), | ||
| expected.trim(), | ||
| "\n\nStyle: {style_name}, Fixture: {name}" | ||
| ); | ||
| } | ||
| Err(e) => { | ||
| panic!("Failed to format {style_name}/{name}: {e}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Known fixtures that don't match expected output yet due to grammar | ||
| /// limitations or incomplete formatting support. These parse successfully | ||
| /// but produce different output than the pgfmt reference. | ||
| const KNOWN_FAILING: &[&str] = &[ | ||
| "river/create_domain", | ||
| "river/create_foreign_table", | ||
| "river/create_function", | ||
| "river/create_matview", | ||
| "river/create_table_with", | ||
| "river/create_view_cte", | ||
| "aweber/select_case_join", | ||
| "aweber/select_cte_nested", | ||
| ]; | ||
|
|
||
| /// Discover all .sql files in each style directory and run them. | ||
| #[test] | ||
| fn all_fixture_pairs() { | ||
| let fixtures_dir = Path::new(env!("CARGO_MANIFEST_DIR")) | ||
| .join("tests") | ||
| .join("fixtures"); | ||
|
|
||
| let styles: &[(&str, Style)] = &[ | ||
| ("river", Style::River), | ||
| ("mozilla", Style::Mozilla), | ||
| ("aweber", Style::Aweber), | ||
| ("dbt", Style::Dbt), | ||
| ("gitlab", Style::Gitlab), | ||
| ("kickstarter", Style::Kickstarter), | ||
| ("mattmc3", Style::Mattmc3), | ||
| ]; | ||
|
|
||
| let mut total = 0; | ||
| let mut passed = 0; | ||
| let mut failures = Vec::new(); | ||
|
|
||
| for (style_name, style) in styles { | ||
| let style_dir = fixtures_dir.join(style_name); | ||
| if !style_dir.exists() { | ||
| continue; | ||
| } | ||
| let mut entries: Vec<_> = std::fs::read_dir(&style_dir) | ||
| .unwrap() | ||
| .filter_map(|e| e.ok()) | ||
| .filter(|e| e.path().extension().is_some_and(|ext| ext == "sql")) | ||
| .collect(); | ||
| entries.sort_by_key(|e| e.file_name()); | ||
|
|
||
| for entry in entries { | ||
| let stem = entry | ||
| .path() | ||
| .file_stem() | ||
| .unwrap() | ||
| .to_string_lossy() | ||
| .to_string(); | ||
| let expected_path = style_dir.join(format!("{stem}.expected")); | ||
| if !expected_path.exists() { | ||
| eprintln!("SKIP {style_name}/{stem}: no .expected file"); | ||
| continue; | ||
| } | ||
| let fixture_key = format!("{style_name}/{stem}"); | ||
| let is_known_failing = KNOWN_FAILING.contains(&fixture_key.as_str()); | ||
| total += 1; | ||
| let result = std::panic::catch_unwind(|| { | ||
| run_fixture(*style, style_name, &stem); | ||
| }); | ||
| match result { | ||
| Ok(()) => { | ||
| passed += 1; | ||
| if is_known_failing { | ||
| eprintln!("UNEXPECTED PASS {fixture_key}: remove from KNOWN_FAILING"); | ||
| } | ||
| } | ||
| Err(e) => { | ||
| if is_known_failing { | ||
| eprintln!("EXPECTED FAIL {fixture_key}"); | ||
| passed += 1; // Don't count as failure. | ||
| } else { | ||
| let msg = if let Some(s) = e.downcast_ref::<String>() { | ||
| s.clone() | ||
| } else if let Some(s) = e.downcast_ref::<&str>() { | ||
| s.to_string() | ||
| } else { | ||
| "unknown panic".to_string() | ||
| }; | ||
| let short = if msg.chars().count() > 200 { | ||
| let truncated: String = msg.chars().take(200).collect(); | ||
| format!("{truncated}...") | ||
| } else { | ||
| msg | ||
| }; | ||
| failures.push(format!("{fixture_key}: {short}")); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| eprintln!("\n=== Fixture Results: {passed}/{total} passed ==="); | ||
| if !failures.is_empty() { | ||
| eprintln!("\nFailures:"); | ||
| for f in &failures { | ||
| eprintln!(" FAIL: {f}"); | ||
| } | ||
| panic!("{} of {} fixtures failed", failures.len(), total); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| use libpgfmt::{format, style::Style}; | ||
|
|
||
| #[test] | ||
| fn preserve_parens_around_or_in_and() { | ||
| let sql = "SELECT 1 FROM t WHERE (a IS NULL OR b > 1) AND c = 'x'"; | ||
| let result = format(sql, Style::River).unwrap(); | ||
| assert!( | ||
| result.contains("(a IS NULL OR b > 1)"), | ||
| "Parentheses around OR were dropped:\n{result}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn no_unnecessary_parens() { | ||
| let sql = "SELECT 1 FROM t WHERE a = 1 AND b = 2"; | ||
| let result = format(sql, Style::River).unwrap(); | ||
| assert!(!result.contains('('), "Unexpected parens added:\n{result}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn preserve_adjacent_parens() { | ||
| let sql = "SELECT 1 FROM t WHERE (a = 1) AND (b = 2)"; | ||
| let result = format(sql, Style::River).unwrap(); | ||
| assert!( | ||
| result.contains("(a = 1)") && result.contains("(b = 2)"), | ||
| "Adjacent parens corrupted:\n{result}" | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.