An open API service indexing awesome lists of open source software.

https://github.com/erg-lang/molc

A mock language client for testing language servers
https://github.com/erg-lang/molc

language-server lsp lsp-client rust testing

Last synced: about 1 year ago
JSON representation

A mock language client for testing language servers

Awesome Lists containing this project

README

          

# `molc`

`molc` provides a mock (fake) language client for testing language servers.

## Usage

You can see specific examples of molc use in [ELS](https://github.com/erg-lang/erg/blob/main/crates/els/tests/test.rs).

```rust
use lsp_types::{Url, Value};

use molc::{FakeClient, LangServer, RedirectableStdout};
use molc::oneline_range;

pub struct Server {
stdout_redirect: Option>,
...
}

impl LangServer for Server {
fn dispatch(&mut self, msg: impl Into) -> Result<(), Box> {
self.dispatch(msg)
}
}

impl RedirectableStdout for Server {
fn sender(&self) -> Option<&std::sync::mpsc::Sender> {
self.stdout_redirect.as_ref()
}
}

impl Server {
fn bind_fake_client() -> FakeClient {
// The server should send responses to this channel at least during testing.
let (sender, receiver) = std::sync::mpsc::channel();
DummyClient::new(
Server::new(Some(sender)),
receiver,
)
}

fn init(&mut self, msg: &Value, id: i64) -> ELSResult<()> {
self.send_log("initializing the language server")?;
let result = InitializeResult {
...
};
self.init_services();
self.send_stdout(&json!({
"jsonrpc": "2.0",
"id": id,
"result": result,
}))
}

...
}

#[test]
fn test_references() -> Result<(), Box> {
let mut client = Server::bind_fake_client();
client.request_initialize()?;
let uri = Url::from_file_path(Path::new(FILE_A).canonicalize()?).unwrap();
client.notify_open(FILE_A)?;
let locations = client.request_references(uri, 1, 4)?.unwrap();
assert_eq!(locations.len(), 1);
assert_eq!(&locations[0].range, &oneline_range(1, 4, 5));
Ok(())
}
```