Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] refactor lsp tests #1374

Open
3 of 9 tasks
He1pa opened this issue May 28, 2024 · 2 comments · Fixed by #1379 · May be fixed by #1780
Open
3 of 9 tasks

[Refactor] refactor lsp tests #1374

He1pa opened this issue May 28, 2024 · 2 comments · Fixed by #1379 · May be fixed by #1780
Labels
good first issue Good for newcomers help wanted Extra attention is needed lsp

Comments

@He1pa
Copy link
Contributor

He1pa commented May 28, 2024

Enhancement

1 Unit test

  1. split test file, each test function corresponds to a test code file
- gotodef
  - case1
    - case1.k
  - case2
    - case2.k
  - ...
- hover
- ...
fn case1_test(){
}

fn case2_test(){
}
  1. use macros or help functions to remove duplicate test code
  2. use insta pkg instead of assert in test code

e.g. from

    #[test]
    #[bench_test]
    fn lambda_local_var_test() {
        let (file, _program, _, gs) = compile_test_file("src/test_data/goto_def_test/goto_def.k");

        let pos = KCLPos {
            filename: file.clone(),
            line: 96,
            column: Some(9),
        };

        let res = goto_def(&pos, &gs);
        compare_goto_res(res, (&file, 94, 11, 94, 12));
    }

to

    #[macro_export]
    macro_rules! goto_def_test_snapshot {
        ($name:ident, $file:expr, $line:expr, $column: expr) => {
            #[test]
            fn $name() {
                insta::assert_snapshot!(format!("{:?}", {
                    let (file, _program, _, gs) = compile_test_file($file);

                    let pos = KCLPos {
                        filename: file.clone(),
                        line: $line,
                        column: Some($column),
                    };
                    goto_def(&pos, &gs)
                }));
            }
        };
    }

    goto_def_test_snapshot!(
        lambda_local_var_test,
        "src/test_data/goto_def_test/goto_def.k",
        96,
        9
    );
  • goto def
  • completetion
  • find ref
  • format
  • hover
  • inlay hint
  • rename
  • sema token
  • signature help

e2e test

Abstraction some test function for e2e tests(in kclvm/tools/src/LSP/src/tests.rs), e.g.,

    // Mock open file
    server.notification::<lsp_types::notification::DidOpenTextDocument>(
        lsp_types::DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: Url::from_file_path(path).unwrap(),
                language_id: "KCL".to_string(),
                version: 0,
                text: src,
            },
        },
    );

    // Mock close file
    server.notification::<lsp_types::notification::DidCloseTextDocument>(
        lsp_types::DidCloseTextDocumentParams {
            text_document: TextDocumentIdentifier {
                uri: Url::from_file_path(path).unwrap(),
            },
        },
    );


 
    //  Mock for complete request
    let id = server.next_request_id.get();
    server.next_request_id.set(id.wrapping_add(1));

    let r: Request = Request::new(
        id.into(),
        "textDocument/completion".to_string(),
        CompletionParams {
            text_document_position: TextDocumentPositionParams {
                text_document: TextDocumentIdentifier {
                    uri: Url::from_file_path(path).unwrap(),
                },
                position: Position::new(11, 7),
            },
            work_done_progress_params: Default::default(),
            partial_result_params: Default::default(),
            context: Some(CompletionContext {
                trigger_kind: CompletionTriggerKind::TRIGGER_CHARACTER,
                trigger_character: Some(".".to_string()),
            }),
        },
    );

    // Send request and wait for it's response
    let res = server.send_and_receive(r);

Abstracte to

Impl Server{
    fn open_file(){}
    fn close_file(){}
    fn complete() -> Resp{}
    fn goto_def() -> Resp{}
    ...
}

And refactor e2e test

@He1pa He1pa added good first issue Good for newcomers help wanted Extra attention is needed lsp labels May 28, 2024
@Wck-iipi
Copy link
Contributor

@He1pa I would like to work on this issue

@He1pa He1pa changed the title [Enhancement] refactor lsp unit test [Refact] refact lsp unit test May 28, 2024
@He1pa He1pa changed the title [Refact] refact lsp unit test [Refactor] refactor lsp unit test May 28, 2024
@He1pa He1pa reopened this Jul 15, 2024
@He1pa He1pa changed the title [Refactor] refactor lsp unit test [Refactor] refactor lsp tests Nov 21, 2024
@SkySingh04
Copy link

@He1pa This sounds like a good issue to get started with! Will try and do this for the completion module!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed lsp
Projects
None yet
3 participants