store messages in tests

This commit is contained in:
Aleksey Kladov 2018-09-02 12:34:06 +03:00
parent d752455637
commit 7fad13de73
2 changed files with 17 additions and 7 deletions

View file

@ -9,7 +9,7 @@ use languageserver_types::{
use Result;
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum RawMessage {
Request(RawRequest),
@ -17,14 +17,14 @@ pub enum RawMessage {
Response(RawResponse),
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RawRequest {
pub id: u64,
pub method: String,
pub params: Value,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RawResponse {
// JSON RPC allows this to be null if it was impossible
// to decode the request's id. Ignore this special case
@ -36,7 +36,7 @@ pub struct RawResponse {
pub error: Option<RawResponseError>,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RawResponseError {
pub code: i32,
pub message: String,
@ -44,6 +44,7 @@ pub struct RawResponseError {
pub data: Option<Value>,
}
#[derive(Clone, Copy, Debug)]
#[allow(unused)]
pub enum ErrorCode {
ParseError = -32700,
@ -58,7 +59,7 @@ pub enum ErrorCode {
RequestCancelled = -32800,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RawNotification {
pub method: String,
pub params: Value,

View file

@ -1,7 +1,7 @@
use std::{
fs,
thread,
cell::Cell,
cell::{Cell, RefCell},
path::PathBuf,
};
@ -56,6 +56,7 @@ pub fn project(fixture: &str) -> Server {
pub struct Server {
req_id: Cell<u64>,
messages: RefCell<Vec<RawMessage>>,
dir: TempDir,
sender: Option<Sender<RawMessage>>,
receiver: Receiver<RawMessage>,
@ -71,6 +72,7 @@ impl Server {
let res = Server {
req_id: Cell::new(1),
dir,
messages: Default::default(),
sender: Some(client_sender),
receiver: client_receiver,
server: Some(server),
@ -129,7 +131,7 @@ impl Server {
.unwrap()
.send(RawMessage::Request(r));
while let Some(msg) = self.receiver.recv() {
while let Some(msg) = self.recv() {
match msg {
RawMessage::Request(req) => panic!("unexpected request: {:?}", req),
RawMessage::Notification(_) => (),
@ -144,6 +146,13 @@ impl Server {
}
panic!("no response");
}
fn recv(&self) -> Option<RawMessage> {
self.receiver.recv()
.map(|msg| {
self.messages.borrow_mut().push(msg.clone());
msg
})
}
fn send_notification(&self, not: RawNotification) {
self.sender.as_ref()