Skip to content

Commit

Permalink
fix json-rpc tests to not depend on formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pr2502 committed Oct 18, 2023
1 parent 2226cac commit 18f7413
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/lsp/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,38 +159,63 @@ impl fmt::Debug for Message {

#[cfg(test)]
mod tests {
use serde_json::{from_value, json, to_value, Value};

use super::*;

fn test(input: &str) {
let deserialized = serde_json::from_str::<Message>(input).expect("failed to deserialize");
let serialized = serde_json::to_string(&deserialized).expect("failed to serialize");
fn test(input: Value) {
let deserialized = from_value::<Message>(input.clone()).expect("failed to deserialize");
let serialized = to_value(&deserialized).expect("failed to serialize");
assert_eq!(input, serialized);
}

#[test]
fn call_with_positional_parameters() {
test(r#"{"jsonrpc":"2.0","method":"subtract","params":[42,23],"id":1}"#)
test(json!({
"jsonrpc": "2.0",
"method": "subtract",
"params": [42, 23],
"id": 1,
}))
}

#[test]
fn call_with_named_parameters() {
test(
r#"{"jsonrpc":"2.0","method":"subtract","params":{"minuend":42,"subtrahend":23},"id":3}"#,
);
test(json!({
"jsonrpc": "2.0",
"method": "subtract",
"params": { "minuend": 42, "subtrahend": 23 },
"id": 3,
}))
}

#[test]
fn response() {
test(r#"{"jsonrpc":"2.0","result":19,"id":1}"#)
test(json!({
"jsonrpc": "2.0",
"result": 19,
"id": 1,
}))
}

#[test]
fn notification() {
test(r#"{"jsonrpc":"2.0","method":"update","params":[1,2,3,4,5]}"#)
test(json!({
"jsonrpc": "2.0",
"method": "update",
"params": [1, 2, 3, 4, 5],
}))
}

#[test]
fn error() {
test(r#"{"jsonrpc":"2.0","error":{"code":-32601,"message":"Method not found"},"id":"1"}"#)
test(json!({
"jsonrpc": "2.0",
"error": {
"code": -32601,
"message": "Method not found",
},
"id": "1",
}))
}
}

0 comments on commit 18f7413

Please sign in to comment.