|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import hamcrest |
| 5 | +import pytest |
| 6 | +from cattrs import ClassValidationError |
| 7 | + |
| 8 | +from lsprotocol import converters as cv |
| 9 | +from lsprotocol import types as lsp |
| 10 | + |
| 11 | +TEST_DATA = [ |
| 12 | + { |
| 13 | + "id": 1, |
| 14 | + "result": [{"name": "test", "kind": 1, "location": {"uri": "test"}}], |
| 15 | + "jsonrpc": "2.0", |
| 16 | + }, |
| 17 | + { |
| 18 | + "id": 1, |
| 19 | + "result": [ |
| 20 | + { |
| 21 | + "name": "test", |
| 22 | + "kind": 1, |
| 23 | + "location": { |
| 24 | + "uri": "test", |
| 25 | + "range": { |
| 26 | + "start": {"line": 1, "character": 1}, |
| 27 | + "end": {"line": 1, "character": 1}, |
| 28 | + }, |
| 29 | + }, |
| 30 | + } |
| 31 | + ], |
| 32 | + "jsonrpc": "2.0", |
| 33 | + }, |
| 34 | + { |
| 35 | + "id": 1, |
| 36 | + "result": [{"name": "test", "kind": 1, "location": {"uri": "test"}, "data": 1}], |
| 37 | + "jsonrpc": "2.0", |
| 38 | + }, |
| 39 | + { |
| 40 | + "id": 1, |
| 41 | + "result": [ |
| 42 | + { |
| 43 | + "name": "test", |
| 44 | + "kind": 1, |
| 45 | + "location": { |
| 46 | + "uri": "test", |
| 47 | + "range": { |
| 48 | + "start": {"line": 1, "character": 1}, |
| 49 | + "end": {"line": 1, "character": 1}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + "deprecated": True, |
| 53 | + } |
| 54 | + ], |
| 55 | + "jsonrpc": "2.0", |
| 56 | + }, |
| 57 | +] |
| 58 | + |
| 59 | +BAD_TEST_DATA = [ |
| 60 | + { |
| 61 | + "id": 1, |
| 62 | + "result": [ |
| 63 | + { |
| 64 | + "name": "test", |
| 65 | + "kind": 1, |
| 66 | + "location": {"uri": "test"}, |
| 67 | + "deprecated": True, |
| 68 | + } |
| 69 | + ], |
| 70 | + "jsonrpc": "2.0", |
| 71 | + }, |
| 72 | +] |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("data", TEST_DATA) |
| 76 | +def test_workspace_symbols(data): |
| 77 | + converter = cv.get_converter() |
| 78 | + obj = converter.structure(data, lsp.WorkspaceSymbolResponse) |
| 79 | + hamcrest.assert_that(obj, hamcrest.instance_of(lsp.WorkspaceSymbolResponse)) |
| 80 | + hamcrest.assert_that( |
| 81 | + converter.unstructure(obj, lsp.WorkspaceSymbolResponse), |
| 82 | + hamcrest.is_(data), |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize("data", BAD_TEST_DATA) |
| 87 | +def test_workspace_symbols_bad(data): |
| 88 | + converter = cv.get_converter() |
| 89 | + with pytest.raises(ClassValidationError): |
| 90 | + obj = converter.structure(data, lsp.WorkspaceSymbolResponse) |
| 91 | + hamcrest.assert_that(obj, hamcrest.instance_of(lsp.WorkspaceSymbolResponse)) |
0 commit comments