11 releases

new 0.3.0 Apr 24, 2024
0.2.8 Jan 4, 2024
0.2.7 Oct 10, 2023
0.2.6 Sep 29, 2023
0.1.0 Sep 7, 2023

#103 in Text editors

Download history 369/week @ 2024-01-03 251/week @ 2024-01-10 386/week @ 2024-01-17 397/week @ 2024-01-24 352/week @ 2024-01-31 448/week @ 2024-02-07 867/week @ 2024-02-14 442/week @ 2024-02-21 439/week @ 2024-02-28 466/week @ 2024-03-06 517/week @ 2024-03-13 522/week @ 2024-03-20 306/week @ 2024-03-27 387/week @ 2024-04-03 451/week @ 2024-04-10 378/week @ 2024-04-17

1,575 downloads per month
Used in 3 crates (via els)

MIT/Apache

38KB
869 lines

molc

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

Usage

You can see specific examples of molc use in ELS.

use lsp_types::{Url, Value};

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

pub struct Server {
    stdout_redirect: Option<std::sync::mpsc::Sender<Value>>,
    ...
}

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

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

impl Server {
    fn bind_fake_client() -> FakeClient<Self> {
        // 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<dyn std::error::Error>> {
    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(())
}

Dependencies

~2.5–3.5MB
~101K SLoC