Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Use ServerMessages in tests#323

Merged
nrc merged 3 commits intorust-lang:masterfrom
Xanewok:use-structs-in-tests
May 23, 2017
Merged

Use ServerMessages in tests#323
nrc merged 3 commits intorust-lang:masterfrom
Xanewok:use-structs-in-tests

Conversation

@Xanewok
Copy link
Copy Markdown
Contributor

@Xanewok Xanewok commented May 22, 2017

Implemented #305 (comment).

  • Changed Notification/Method enum variant names to match those implicitly-defined in languageserver-types crate (used for fetching LSP method name from ServerMessage)
  • Implemented simple serialize method for ServerMessages
  • Removed string-based Message in tests, those now use ServerMessage in both mock_(raw_)server scenarios

* Changed Notification/Method enum variant names to match those implicitly-defined in languageserver-types crate (used for fetching LSP method name from ServerMessage)
* Implemented simple serialize method for ServerMessages
* Removed string-based Message in tests, those now use ServerMessage in both mock_(raw_)server scenarios
Copy link
Copy Markdown
Member

@nrc nrc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I think this improves the tests a lot.

Could we remove the 'raw' mock message reader and use this setup for those tests too?

src/test/mod.rs Outdated
const TEST_TIMEOUT_IN_SEC: u64 = 10;

impl ServerMessage {
pub fn simple_test_initialize(id: usize, root_path: Option<String>) -> ServerMessage {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just call this initialize (and does it need to be pub?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rename it. It's pub because I also used it here. Should I get rid of that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No pub is fine if it needs to be, I missed that usage.

src/test/mod.rs Outdated

let messages = vec![
ServerMessage::simple_test_initialize(0,root_path.as_os_str().to_str().map(|x| x.to_owned())),
ServerMessage::Request(Request { id: 11, method: Method::GotoDefinition(TextDocumentPositionParams {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit: put some newlines between the fields (or run it through rustfmt)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could probably factor out a request helper method (or macro)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would something like:

fn request(id: usize, method: Method) -> ServerMessage {
    ServerMessage::Request(Request { id: id, method: method })
}

e.g.

ServerMessage::request(11, Method::GotoDefinition(TextDocumentPositionParams {
    text_document: text_doc,
    position: pos
}))

be good enough (also asking about style)?
I think it strikes a good balance between reducing redundancy and simplification while allowing to express every request type.
To get rid of explicit need to pass method params struct type I'd have to enumerate over variants (which I don't know how to do yet - I assume it needs macros 1.1 like here?) to get their payload types.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would something like:

fn request<F>(id: usize, ..., mk_method: F) -> ServerMessage
    where F: FnOnce(Position) -> Method
{
    let pos = ...;
    ServerMessage::Request(Request { id: id, method: mk_method(pos) })
}

work?

Looks fine, style-wise.

@Xanewok
Copy link
Copy Markdown
Contributor Author

Xanewok commented May 23, 2017

@nrc yes, pushed mock_raw_server -> mock_server change just now.

@nrc nrc merged commit e833b6d into rust-lang:master May 23, 2017
@nrc
Copy link
Copy Markdown
Member

nrc commented May 23, 2017

Awesome, thank you!

@Xanewok Xanewok deleted the use-structs-in-tests branch May 24, 2017 06:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants