mod harness;
use harness::TestClient;
use harness::runtime::run_lsp_test;
use indoc::indoc;
use insta::assert_json_snapshot;
use serde_json::json;
#[test]
fn test_format_basic_select() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
client
.open_document("file:///test.sparql", "SELECT*WHERE{?a ?b ?c}")
.await;
let id = client.format("file:///test.sparql").await;
let response = client
.get_response(id)
.expect("Should receive format response");
let edits = response["result"]
.as_array()
.expect("Result should be an array");
assert!(!edits.is_empty(), "Should have formatting edits");
assert_json_snapshot!("format_basic_select", response["result"]);
});
}
#[test]
fn test_format_with_prefix() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
let query = "PREFIX wdt:<http://example.org/>SELECT?a?b WHERE{?a wdt:P31 ?b}";
client.open_document("file:///test.sparql", query).await;
let id = client.format("file:///test.sparql").await;
let response = client
.get_response(id)
.expect("Should receive format response");
assert_json_snapshot!("format_with_prefix", response["result"]);
});
}
#[test]
fn test_format_complex_query() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
let query = indoc!(
"PREFIX wdt:<http://example.org/>
SELECT?a?b WHERE{
?a wdt:P31 ?b.
OPTIONAL{?a wdt:P18 ?c}
FILTER(?b>0)
}GROUP BY ?a ORDER BY DESC(?b)LIMIT 10"
);
client.open_document("file:///test.sparql", query).await;
let id = client.format("file:///test.sparql").await;
let response = client
.get_response(id)
.expect("Should receive format response");
assert_json_snapshot!("format_complex_query", response["result"]);
});
}
#[test]
fn test_format_ask_query() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
client
.open_document("file:///test.sparql", "ASK{?s ?p ?o}")
.await;
let id = client.format("file:///test.sparql").await;
let response = client
.get_response(id)
.expect("Should receive format response");
assert_json_snapshot!("format_ask_query", response["result"]);
});
}
#[test]
fn test_format_construct_query() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
let query = "CONSTRUCT{?s ?p ?o}WHERE{?s ?p ?o}";
client.open_document("file:///test.sparql", query).await;
let id = client.format("file:///test.sparql").await;
let response = client
.get_response(id)
.expect("Should receive format response");
assert_json_snapshot!("format_construct_query", response["result"]);
});
}
#[test]
fn test_format_describe_query() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
client
.open_document(
"file:///test.sparql",
"DESCRIBE <http://example.org/resource>",
)
.await;
let id = client.format("file:///test.sparql").await;
let response = client
.get_response(id)
.expect("Should receive format response");
assert_json_snapshot!("format_describe_query", response["result"]);
});
}
#[test]
fn test_format_with_values_clause() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
let query = "SELECT*WHERE{VALUES ?x{1 2 3}?x ?p ?o}";
client.open_document("file:///test.sparql", query).await;
let id = client.format("file:///test.sparql").await;
let response = client
.get_response(id)
.expect("Should receive format response");
assert_json_snapshot!("format_values_clause", response["result"]);
});
}
#[test]
fn test_format_with_subquery() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
let query = "SELECT*WHERE{{SELECT ?a WHERE{?a ?b ?c}}?a ?p ?o}";
client.open_document("file:///test.sparql", query).await;
let id = client.format("file:///test.sparql").await;
let response = client
.get_response(id)
.expect("Should receive format response");
assert_json_snapshot!("format_subquery", response["result"]);
});
}
#[test]
fn test_format_with_union() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
let query = "SELECT*WHERE{{?a ?b ?c}UNION{?x ?y ?z}}";
client.open_document("file:///test.sparql", query).await;
let id = client.format("file:///test.sparql").await;
let response = client
.get_response(id)
.expect("Should receive format response");
assert_json_snapshot!("format_union", response["result"]);
});
}
#[test]
fn test_format_with_custom_options() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
client
.open_document("file:///test.sparql", "SELECT*WHERE{?a ?b ?c}")
.await;
let id = client
.send_request(
"textDocument/formatting",
json!({
"textDocument": { "uri": "file:///test.sparql" },
"options": {
"tabSize": 4,
"insertSpaces": true
}
}),
)
.await;
let response = client
.get_response(id)
.expect("Should receive format response");
assert!(response["result"].is_array());
});
}
#[test]
fn test_format_idempotent() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
client
.open_document("file:///test.sparql", "SELECT*WHERE{?a ?b ?c}")
.await;
let id = client.format("file:///test.sparql").await;
let response = client
.get_response(id)
.expect("Should receive format response");
let edits = response["result"]
.as_array()
.expect("Result should be an array");
assert!(!edits.is_empty(), "Should have formatting edits");
assert!(response["result"].is_array());
});
}
#[test]
fn test_format_empty_document() {
run_lsp_test(|| async {
let client = TestClient::new();
client.initialize().await;
client.open_document("file:///test.sparql", "").await;
let id = client.format("file:///test.sparql").await;
let response = client
.get_response(id)
.expect("Should receive format response");
let edits = response["result"]
.as_array()
.expect("Result should be an array");
assert!(
edits.is_empty(),
"Empty document should have no edits, got {}",
edits.len()
);
});
}