Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions crates/squawk_ide/src/code_actions/rewrite_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ mod test {
use insta::assert_snapshot;

use crate::code_actions::test_utils::{
apply_code_action, code_action_not_applicable, code_action_not_applicable_with_errors,
apply_code_action_with_errors, code_action_not_applicable,
code_action_not_applicable_with_errors,
};

use super::rewrite_from;

#[test]
fn rewrite_from_simple() {
assert_snapshot!(apply_code_action(
assert_snapshot!(apply_code_action_with_errors(
rewrite_from,
"from$0 t;"),
@"select * from t;"
Expand All @@ -55,7 +56,7 @@ mod test {

#[test]
fn rewrite_from_qualified() {
assert_snapshot!(apply_code_action(
assert_snapshot!(apply_code_action_with_errors(
rewrite_from,
"from$0 s.t;"),
@"select * from s.t;"
Expand All @@ -64,7 +65,7 @@ mod test {

#[test]
fn rewrite_from_on_name() {
assert_snapshot!(apply_code_action(
assert_snapshot!(apply_code_action_with_errors(
rewrite_from,
"from t$0;"),
@"select * from t;"
Expand Down
12 changes: 7 additions & 5 deletions crates/squawk_ide/src/code_actions/rewrite_leading_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ pub(super) fn rewrite_leading_from(
mod test {
use insta::assert_snapshot;

use crate::code_actions::test_utils::{apply_code_action, code_action_not_applicable};
use crate::code_actions::test_utils::{
apply_code_action_with_errors, code_action_not_applicable,
};

use super::rewrite_leading_from;

#[test]
fn rewrite_leading_from_simple() {
assert_snapshot!(apply_code_action(
assert_snapshot!(apply_code_action_with_errors(
rewrite_leading_from,
"from$0 t select c;"),
@"select c from t;"
Expand All @@ -70,7 +72,7 @@ mod test {

#[test]
fn rewrite_leading_from_multiple_cols() {
assert_snapshot!(apply_code_action(
assert_snapshot!(apply_code_action_with_errors(
rewrite_leading_from,
"from$0 t select a, b;"),
@"select a, b from t;"
Expand All @@ -79,7 +81,7 @@ mod test {

#[test]
fn rewrite_leading_from_with_where() {
assert_snapshot!(apply_code_action(
assert_snapshot!(apply_code_action_with_errors(
rewrite_leading_from,
"from$0 t select c where x = 1;"),
@"select c from t where x = 1;"
Expand All @@ -88,7 +90,7 @@ mod test {

#[test]
fn rewrite_leading_from_on_select() {
assert_snapshot!(apply_code_action(
assert_snapshot!(apply_code_action_with_errors(
rewrite_leading_from,
"from t sel$0ect c;"),
@"select c from t;"
Expand Down
60 changes: 34 additions & 26 deletions crates/squawk_ide/src/code_actions/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use salsa::Database as Db;
use squawk_linter::Edit;
use squawk_syntax::ast;

use crate::db::{Database, File};
use crate::db::File;
use crate::test_utils::Fixture;

use super::{ActionKind, CodeAction};
Expand All @@ -13,15 +13,33 @@ pub(super) fn apply_code_action(
f: impl Fn(&dyn Db, File, &mut Vec<CodeAction>, TextSize) -> Option<()>,
sql: &str,
) -> String {
let fixture = Fixture::new(sql);
apply_code_action_(f, sql, false)
}

#[must_use]
pub(super) fn apply_code_action_with_errors(
f: impl Fn(&dyn Db, File, &mut Vec<CodeAction>, TextSize) -> Option<()>,
sql: &str,
) -> String {
apply_code_action_(f, sql, true)
}

fn apply_code_action_(
f: impl Fn(&dyn Db, File, &mut Vec<CodeAction>, TextSize) -> Option<()>,
sql: &str,
allow_errors: bool,
) -> String {
let fixture = if allow_errors {
Fixture::new_allow_errors(sql)
} else {
Fixture::new(sql)
};
let offset = fixture.marker().offset_before();
let sql = fixture.sql();
let db = Database::default();
let file = File::new(&db, sql.into());
let parse_result = crate::db::parse(&db, file);
let db = fixture.db();
let file = fixture.file();

let mut actions = vec![];
f(&db, file, &mut actions, offset);
f(db, file, &mut actions, offset);

assert!(
!actions.is_empty(),
Expand All @@ -30,16 +48,7 @@ pub(super) fn apply_code_action(

let action = &actions[0];

match action.kind {
ActionKind::QuickFix => {
// Quickfixes can fix syntax errors so we don't assert
}
ActionKind::RefactorRewrite => {
assert_eq!(parse_result.errors(), vec![]);
}
}

let mut result = sql.to_string();
let mut result = file.content(db).to_string();

let mut edits = action.edits.clone();
edits.sort_by_key(|e| e.text_range.start());
Expand Down Expand Up @@ -94,18 +103,17 @@ fn code_action_not_applicable_(
sql: &str,
allow_errors: bool,
) -> bool {
let fixture = Fixture::new(sql);
let fixture = if allow_errors {
Fixture::new_allow_errors(sql)
} else {
Fixture::new(sql)
};
let offset = fixture.marker().offset_before();
let sql = fixture.sql();
let db = Database::default();
let file = File::new(&db, sql.into());
let parse_result = crate::db::parse(&db, file);
if !allow_errors {
assert_eq!(parse_result.errors(), vec![]);
}
let db = fixture.db();
let file = fixture.file();

let mut actions = vec![];
f(&db, file, &mut actions, offset);
f(db, file, &mut actions, offset);
actions.is_empty()
}

Expand Down
15 changes: 4 additions & 11 deletions crates/squawk_ide/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,20 +935,16 @@ pub struct CompletionItem {
#[cfg(test)]
mod tests {
use super::completion;
use crate::db::{Database, File};
use crate::test_utils::Fixture;
use insta::assert_snapshot;
use tabled::builder::Builder;
use tabled::settings::Style;

#[must_use]
fn completions(sql: &str) -> String {
let fixture = Fixture::new(sql);
let fixture = Fixture::new_allow_errors(sql);
let offset = fixture.marker().offset();
let sql = fixture.sql();
let db = Database::default();
let file = File::new(&db, sql.into());
let items = completion(&db, file, offset);
let items = completion(fixture.db(), fixture.file(), offset);
assert!(
!items.is_empty(),
"No completions found. If this was intended, use `completions_not_found` instead."
Expand All @@ -957,12 +953,9 @@ mod tests {
}

fn completions_not_found(sql: &str) {
let fixture = Fixture::new(sql);
let fixture = Fixture::new_allow_errors(sql);
let offset = fixture.marker().offset();
let sql = fixture.sql();
let db = Database::default();
let file = File::new(&db, sql.into());
let items = completion(&db, file, offset);
let items = completion(fixture.db(), fixture.file(), offset);
assert_eq!(
items,
vec![],
Expand Down
19 changes: 9 additions & 10 deletions crates/squawk_ide/src/expand_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,15 @@ mod tests {
use super::*;
use crate::test_utils::Fixture;
use insta::assert_debug_snapshot;
use squawk_syntax::{SourceFile, ast::AstNode};
use squawk_syntax::ast::AstNode;

#[must_use]
fn expand(sql: &str) -> Vec<String> {
let fixture = Fixture::new(sql);
let offset = fixture.marker().offset();
let sql = fixture.sql();
let parse = SourceFile::parse(sql);
let file = parse.tree();
let root = file.syntax();
let sql = fixture.file().content(fixture.db()).clone();
let tree = crate::db::parse(fixture.db(), fixture.file()).tree();
let root = tree.syntax();

let mut range = TextRange::empty(offset);
let mut results = vec![];
Expand Down Expand Up @@ -346,15 +345,15 @@ select 'some stret$0ched out words in a string'
#[test]
fn string() {
assert_debug_snapshot!(expand(r"
select b'foo$0 bar'
select e'foo$0 bar'
'buzz';
"), @r#"
[
"foo",
"b'foo bar'",
"b'foo bar'\n'buzz'",
"select b'foo bar'\n'buzz'",
"\nselect b'foo bar'\n'buzz';\n",
"e'foo bar'",
"e'foo bar'\n'buzz'",
"select e'foo bar'\n'buzz'",
"\nselect e'foo bar'\n'buzz';\n",
]
"#);
}
Expand Down
16 changes: 7 additions & 9 deletions crates/squawk_ide/src/find_references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn find_references(db: &dyn Db, file: File, offset: TextSize) -> Vec<Locatio
#[cfg(test)]
mod test {
use crate::builtins::builtins_file;
use crate::db::{Database, File};
use crate::db::File;
use crate::find_references::find_references;
use crate::test_utils::Fixture;
use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet, renderer::DecorStyle};
Expand All @@ -54,16 +54,14 @@ mod test {
let marker = fixture.marker();
let offset = marker.offset_before();
let query_span = marker.range();
let sql = fixture.sql();
let db = Database::default();
let current_file = File::new(&db, sql.into());
assert_eq!(crate::db::parse(&db, current_file).errors(), vec![]);
let db = fixture.db();
let current_file = fixture.file();

let references = find_references(&db, current_file, offset);
let references = find_references(db, current_file, offset);

let mut file_paths = FxHashMap::default();
file_paths.insert(current_file, "current.sql");
file_paths.insert(builtins_file(&db), "builtins.sql");
file_paths.insert(builtins_file(db), "builtins.sql");

let mut refs_by_file: FxHashMap<File, Vec<(usize, TextRange)>> = FxHashMap::default();
for (i, location) in references.iter().enumerate() {
Expand All @@ -75,7 +73,7 @@ mod test {

let multi_file = refs_by_file.len() > 1 || !refs_by_file.contains_key(&current_file);

let mut snippet = Snippet::source(sql).fold(true);
let mut snippet = Snippet::source(current_file.content(db).as_ref()).fold(true);
if multi_file {
snippet = snippet.path(*file_paths.get(&current_file).unwrap());
}
Expand All @@ -88,7 +86,7 @@ mod test {

for (ref_file, refs) in refs_by_file {
let path = file_paths.get(&ref_file).unwrap();
let other_snippet = Snippet::source(ref_file.content(&db).as_ref())
let other_snippet = Snippet::source(ref_file.content(db).as_ref())
.path(*path)
.fold(true);
let other_snippet = annotate_refs(other_snippet, refs);
Expand Down
19 changes: 8 additions & 11 deletions crates/squawk_ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,11 @@ fn find_following_commit_or_rollback(
#[cfg(test)]
mod test {
use crate::builtins::builtins_file;
use crate::db::{Database, File};
use crate::db::File;
use crate::goto_definition::goto_definition;
use crate::test_utils::Fixture;
use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet, renderer::DecorStyle};
use insta::assert_snapshot;
use log::info;
use rowan::TextRange;
use rustc_hash::FxHashMap;

Expand All @@ -149,25 +148,23 @@ mod test {

#[track_caller]
fn goto_(sql: &str) -> Option<String> {
info!("starting");
let fixture = Fixture::new(sql);
// For go to def we want the previous character since we usually put the
// marker after the item we're trying to go to def on.
let marker = fixture.marker();
let offset = marker.offset_before();
let source_span = marker.range();
let sql = fixture.sql();
let db = Database::default();
let current_file = File::new(&db, sql.into());
assert_eq!(crate::db::parse(&db, current_file).errors(), vec![]);
let results = goto_definition(&db, current_file, offset);
let db = fixture.db();
let current_file = fixture.file();

let results = goto_definition(db, current_file, offset);
if results.is_empty() {
return None;
}

let mut file_paths = FxHashMap::default();
file_paths.insert(current_file, "current.sql");
file_paths.insert(builtins_file(&db), "builtins.sql");
file_paths.insert(builtins_file(db), "builtins.sql");

let mut dests_by_file: FxHashMap<File, Vec<(usize, TextRange)>> = FxHashMap::default();
for (i, location) in results.iter().enumerate() {
Expand All @@ -179,7 +176,7 @@ mod test {

let multi_file = dests_by_file.len() > 1 || !dests_by_file.contains_key(&current_file);

let mut snippet = Snippet::source(current_file.content(&db).as_ref()).fold(true);
let mut snippet = Snippet::source(current_file.content(db).as_ref()).fold(true);
if multi_file {
snippet = snippet.path(*file_paths.get(&current_file).unwrap());
}
Expand All @@ -192,7 +189,7 @@ mod test {

for (dest_file, dests) in dests_by_file {
let path = file_paths.get(&dest_file).unwrap();
let other_snippet = Snippet::source(dest_file.content(&db).as_ref())
let other_snippet = Snippet::source(dest_file.content(db).as_ref())
.path(*path)
.fold(true);
let other_snippet = annotate_destinations(other_snippet, dests);
Expand Down
Loading
Loading