diff options
Diffstat (limited to '')
-rw-r--r-- | tests/python/notifications/test_exit.py | 41 | ||||
-rw-r--r-- | tests/python/notifications/test_progress.py | 91 |
2 files changed, 132 insertions, 0 deletions
diff --git a/tests/python/notifications/test_exit.py b/tests/python/notifications/test_exit.py new file mode 100644 index 0000000..e42a0a9 --- /dev/null +++ b/tests/python/notifications/test_exit.py @@ -0,0 +1,41 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json + +import hamcrest +import jsonrpc +import pytest +from cattrs.errors import ClassValidationError + + +@pytest.mark.parametrize( + "data, expected", + [ + ( + {"method": "exit", "jsonrpc": "2.0"}, + json.dumps({"method": "exit", "jsonrpc": "2.0"}), + ), + ( + {"method": "exit", "params": None, "jsonrpc": "2.0"}, + json.dumps({"method": "exit", "jsonrpc": "2.0"}), + ), + ], +) +def test_exit_serialization(data, expected): + data_str = json.dumps(data) + parsed = jsonrpc.from_json(data_str) + actual_str = jsonrpc.to_json(parsed) + hamcrest.assert_that(actual_str, hamcrest.is_(expected)) + + +@pytest.mark.parametrize( + "data", + [ + json.dumps({}), # missing method and jsonrpc + json.dumps({"method": "invalid"}), # invalid method type + ], +) +def test_exit_invalid(data): + with pytest.raises((ClassValidationError, KeyError)): + jsonrpc.from_json(data) diff --git a/tests/python/notifications/test_progress.py b/tests/python/notifications/test_progress.py new file mode 100644 index 0000000..3f7e918 --- /dev/null +++ b/tests/python/notifications/test_progress.py @@ -0,0 +1,91 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json + +import hamcrest +import jsonrpc +import pytest + +from lsprotocol.types import ( + ProgressNotification, + ProgressParams, + WorkDoneProgressBegin, + WorkDoneProgressEnd, + WorkDoneProgressReport, +) + + +@pytest.mark.parametrize( + "obj, expected", + [ + ( + ProgressNotification( + params=ProgressParams( + token="id1", + value=WorkDoneProgressBegin(title="Begin Progress", percentage=0), + ) + ), + json.dumps( + { + "params": { + "token": "id1", + "value": { + "title": "Begin Progress", + "kind": "begin", + "percentage": 0, + }, + }, + "method": "$/progress", + "jsonrpc": "2.0", + } + ), + ), + ( + ProgressNotification( + params=ProgressParams( + token="id1", + value=WorkDoneProgressReport(message="Still going", percentage=50), + ) + ), + json.dumps( + { + "params": { + "token": "id1", + "value": { + "kind": "report", + "message": "Still going", + "percentage": 50, + }, + }, + "method": "$/progress", + "jsonrpc": "2.0", + } + ), + ), + ( + ProgressNotification( + params=ProgressParams( + token="id1", + value=WorkDoneProgressEnd(message="Finished"), + ) + ), + json.dumps( + { + "params": { + "token": "id1", + "value": { + "kind": "end", + "message": "Finished", + }, + }, + "method": "$/progress", + "jsonrpc": "2.0", + } + ), + ), + ], +) +def test_exit_serialization(obj, expected): + actual_str = jsonrpc.to_json(obj) + hamcrest.assert_that(actual_str, hamcrest.is_(expected)) |