This repository was archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_client.py
More file actions
288 lines (237 loc) · 6.99 KB
/
test_client.py
File metadata and controls
288 lines (237 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from __future__ import annotations
from itertools import count
from typing import TYPE_CHECKING
from unittest.mock import patch
import pytest
from pyk.kore.prelude import INT, int_dv
from pyk.kore.rpc import (
AbortedResult,
ImpliesResult,
JsonRpcClient,
KoreClient,
SatResult,
State,
StuckResult,
TransportType,
UnknownResult,
UnsatResult,
VacuousResult,
)
from pyk.kore.syntax import And, App, Bottom, Module, Top
if TYPE_CHECKING:
from collections.abc import Iterator
from typing import Any, Final
from unittest.mock import Mock
from pyk.kore.rpc import ExecuteResult
from pyk.kore.syntax import Pattern
int_top = Top(INT)
int_bottom = Bottom(INT)
def kore(pattern: Pattern) -> dict[str, Any]:
return {
'format': 'KORE',
'version': 1,
'term': pattern.dict,
}
class MockClient:
mock: Mock
def __init__(self, mock: Mock):
self.mock = mock
def assume_response(self, response: Any) -> None:
self.mock.request.return_value = response
def assert_request(self, method: str, **params: Any) -> None:
self.mock.request.assert_called_with(method, **params)
@pytest.fixture
def mock_class() -> Iterator[Mock]:
patcher = patch('pyk.kore.rpc.JsonRpcClient', spec=True)
yield patcher.start()
patcher.stop()
@pytest.fixture
def mock(mock_class: Mock) -> Mock:
mock = mock_class.return_value
assert isinstance(mock, JsonRpcClient)
return mock # type: ignore
@pytest.fixture
def rpc_client(mock: Mock) -> MockClient:
return MockClient(mock)
@pytest.fixture
def kore_client(mock: Mock, mock_class: Mock) -> Iterator[KoreClient]: # noqa: N803
client = KoreClient('localhost', 3000)
mock_class.assert_called_with(
'localhost', 3000, timeout=None, bug_report=None, bug_report_id=None, transport=TransportType.SINGLE_SOCKET
)
assert client._client._default_client == mock
yield client
client.close()
mock.close.assert_called()
EXECUTE_TEST_DATA: Final = (
(
App('IntAdd', (), (int_dv(1), int_dv(1))),
{'state': kore(App('IntAdd', [], [int_dv(1), int_dv(1)]))},
{
'state': {'term': kore(int_dv(2)), 'substitution': kore(int_top), 'predicate': kore(int_top)},
'depth': 1,
'reason': 'stuck',
},
StuckResult(State(int_dv(2), int_top, int_top), 1, logs=()),
),
(
App('IntAdd', (), (int_dv(1), int_dv(1))),
{'state': kore(App('IntAdd', [], [int_dv(1), int_dv(1)]))},
{
'state': {'term': kore(int_dv(2)), 'substitution': kore(int_top), 'predicate': kore(int_top)},
'depth': 1,
'reason': 'vacuous',
},
VacuousResult(State(int_dv(2), int_top, int_top), 1, logs=()),
),
(
int_dv(0),
{'state': kore(int_dv(0))},
{
'state': {'term': kore(int_dv(1)), 'substitution': kore(int_dv(2)), 'predicate': kore(int_dv(3))},
'depth': 4,
'unknown-predicate': kore(int_dv(5)),
'reason': 'aborted',
},
AbortedResult(
state=State(term=int_dv(1), substitution=int_dv(2), predicate=int_dv(3)),
depth=4,
unknown_predicate=int_dv(5),
logs=(),
),
),
)
@pytest.mark.parametrize('pattern,params,response,expected', EXECUTE_TEST_DATA, ids=count())
def test_execute(
kore_client: KoreClient,
rpc_client: MockClient,
pattern: Pattern,
params: dict[str, Any],
response: dict[str, Any],
expected: ExecuteResult,
) -> None:
# Given
rpc_client.assume_response(response)
# When
actual = kore_client.execute(pattern)
# Then
rpc_client.assert_request('execute', **params)
assert actual == expected
IMPLIES_TEST_DATA: Final = (
(
int_bottom,
int_top,
{'antecedent': kore(int_bottom), 'consequent': kore(int_top)},
{'satisfiable': True, 'implication': kore(int_top)},
ImpliesResult(True, int_top, None, None, ()),
),
)
@pytest.mark.parametrize('antecedent,consequent,params,response,expected', IMPLIES_TEST_DATA, ids=count())
def test_implies(
kore_client: KoreClient,
rpc_client: MockClient,
antecedent: Pattern,
consequent: Pattern,
params: dict[str, Any],
response: dict[str, Any],
expected: ImpliesResult,
) -> None:
# Given
rpc_client.assume_response(response)
# When
actual = kore_client.implies(antecedent, consequent)
# Then
rpc_client.assert_request('implies', **params)
assert actual == expected
SIMPLIFY_TEST_DATA: Final = (
(
And(INT, (int_top, int_top)),
{'state': kore(And(INT, (int_top, int_top)))},
{'state': kore(int_top)},
int_top,
),
)
@pytest.mark.parametrize('pattern,params,response,expected', SIMPLIFY_TEST_DATA, ids=count())
def test_simplify(
kore_client: KoreClient,
rpc_client: MockClient,
pattern: Pattern,
params: dict[str, Any],
response: dict[str, Any],
expected: Pattern,
) -> None:
# Given
rpc_client.assume_response(response)
# When
_unknwon_predicate, actual, _logs = kore_client.simplify(pattern)
# Then
rpc_client.assert_request('simplify', **params)
assert actual == expected
GET_MODEL_TEST_DATA: Final = (
(
int_dv(0),
None,
{'state': kore(int_dv(0))},
{'satisfiable': 'Unknown'},
UnknownResult(),
),
(
int_dv(1),
'TEST-MODULE',
{'state': kore(int_dv(1)), 'module': 'TEST-MODULE'},
{'satisfiable': 'Unknown'},
UnknownResult(),
),
(
int_dv(2),
None,
{'state': kore(int_dv(2))},
{'satisfiable': 'Unsat'},
UnsatResult(),
),
(
int_dv(3),
None,
{'state': kore(int_dv(3))},
{'satisfiable': 'Sat', 'substitution': kore(int_dv(0))},
SatResult(int_dv(0)),
),
)
@pytest.mark.parametrize('pattern,module_name,params,response,expected', GET_MODEL_TEST_DATA, ids=count())
def test_get_model(
kore_client: KoreClient,
rpc_client: MockClient,
pattern: Pattern,
module_name: str | None,
params: dict[str, Any],
response: dict[str, Any],
expected: Pattern,
) -> None:
# Given
rpc_client.assume_response(response)
# When
actual = kore_client.get_model(pattern, module_name)
# Then
rpc_client.assert_request('get-model', **params)
assert actual == expected
ADD_MODULE_TEST_DATA: Final = (
(
Module('HELLO'),
{'module': 'module HELLO\nendmodule []'},
),
)
@pytest.mark.parametrize('module,params', ADD_MODULE_TEST_DATA, ids=count())
def test_add_module(
kore_client: KoreClient,
rpc_client: MockClient,
module: Module,
params: dict[str, Any],
) -> None:
# Given
expected = module.name
rpc_client.assume_response({'module': module.name})
# When
actual = kore_client.add_module(module)
# Then
rpc_client.assert_request('add-module', **params)
assert actual == expected