worktoy 1.2.1.dev0__py3-none-any.whl → 1.2.1.dev2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- worktoy/__init__.py +1 -1
- worktoy/work_test/_base_test.py +118 -63
- worktoy/work_test/_complex_mixin.py +41 -6
- worktoy/work_test/_complex_test.py +1 -1
- worktoy/work_test/_sub_test.py +24 -8
- worktoy/work_test/_test_meta.py +56 -40
- worktoy/work_test/_test_space.py +11 -10
- worktoy/work_test/samplers/__init__.py +2 -2
- worktoy/work_test/samplers/_base_sampler.py +184 -60
- worktoy/work_test/samplers/_int_sampler.py +47 -7
- worktoy/work_test/samplers/_lorem_text.py +90 -17
- worktoy/work_test/samplers/_sampler_meta.py +8 -6
- worktoy/work_test/samplers/_symbolic_sampler.py +4 -5
- {worktoy-1.2.1.dev0.dist-info → worktoy-1.2.1.dev2.dist-info}/METADATA +2 -2
- {worktoy-1.2.1.dev0.dist-info → worktoy-1.2.1.dev2.dist-info}/RECORD +18 -18
- {worktoy-1.2.1.dev0.dist-info → worktoy-1.2.1.dev2.dist-info}/WHEEL +0 -0
- {worktoy-1.2.1.dev0.dist-info → worktoy-1.2.1.dev2.dist-info}/licenses/LICENSE +0 -0
- {worktoy-1.2.1.dev0.dist-info → worktoy-1.2.1.dev2.dist-info}/top_level.txt +0 -0
worktoy/__init__.py
CHANGED
|
@@ -48,7 +48,7 @@ and the enclosing class is left without the decoration.
|
|
|
48
48
|
# Copyright (c) 2025-2026 Asger Jon Vistisen
|
|
49
49
|
from __future__ import annotations
|
|
50
50
|
|
|
51
|
-
__version__ = '1.2.1-
|
|
51
|
+
__version__ = '1.2.1-dev2'
|
|
52
52
|
|
|
53
53
|
from . import utilities
|
|
54
54
|
from . import waitaminute
|
worktoy/work_test/_base_test.py
CHANGED
|
@@ -5,9 +5,7 @@ BaseTest is the 'unittest.TestCase' subclass shared by the worktoy tests.
|
|
|
5
5
|
# Copyright (c) 2025-2026 Asger Jon Vistisen
|
|
6
6
|
from __future__ import annotations
|
|
7
7
|
|
|
8
|
-
import gc
|
|
9
8
|
import os
|
|
10
|
-
import sys
|
|
11
9
|
import traceback
|
|
12
10
|
from functools import wraps
|
|
13
11
|
from inspect import iscoroutinefunction
|
|
@@ -35,11 +33,9 @@ try:
|
|
|
35
33
|
except AttributeError: # pragma: no cover, no mutate
|
|
36
34
|
class _Temp(TestCase):
|
|
37
35
|
def assertIsSubclass(self, cls: type, base: type, msg=None) -> None:
|
|
38
|
-
"""Asserts that 'cls' is a subclass of 'base'."""
|
|
39
36
|
self.assertTrue(issubclass(cls, base), msg)
|
|
40
37
|
|
|
41
38
|
def assertNotIsSubclass(self, cls: type, base: type, msg=None) -> None:
|
|
42
|
-
"""Asserts that 'cls' is not a subclass of 'base'."""
|
|
43
39
|
self.assertFalse(issubclass(cls, base), msg)
|
|
44
40
|
else: # version >= 3.14
|
|
45
41
|
_Temp = TestCase
|
|
@@ -200,19 +196,16 @@ class BaseTest(_Temp, metaclass=BaseMeta):
|
|
|
200
196
|
one instance of a test class per test method gives each test samplers
|
|
201
197
|
of its own, built at the first read, and a sampler narrowed or seeded
|
|
202
198
|
in 'setUp' keeps its settings for that test alone; 'seedSamplers'
|
|
203
|
-
seeds all six with one seed. 'subTest' is the
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
'
|
|
213
|
-
garbage collector after its tests, for the reason its docstring
|
|
214
|
-
gives. 'exceptions' yields the common builtin exception classes.
|
|
215
|
-
'argReport'
|
|
199
|
+
seeds all six with one seed. 'subTest' is the 'SubTest' descriptor,
|
|
200
|
+
which builds one accumulator per test instance; '_reportSubTests'
|
|
201
|
+
reads it without building one and fails the test once, with
|
|
202
|
+
'FAIL [label]: message' or 'ERROR [label]: repr' per recorded block,
|
|
203
|
+
each followed by the traceback of what the block raised.
|
|
204
|
+
'__class_init__' gives every test class test methods that run it as
|
|
205
|
+
they end, and a 'doCleanups' and a 'debug' that run it for what a
|
|
206
|
+
'tearDown' or a cleanup recorded, so a 'tearDown', a 'doCleanups', a
|
|
207
|
+
'debug' or a 'run' of a test class need not hand on for it.
|
|
208
|
+
'exceptions' yields the common builtin exception classes. 'argReport'
|
|
216
209
|
puts each argument on a line of its own as '<type: value>'.
|
|
217
210
|
'assertIsNotSubclass' and 'assertIsNotInstance' are the negative
|
|
218
211
|
assertions under the names the suite uses; below Python 3.14 the
|
|
@@ -267,12 +260,39 @@ class BaseTest(_Temp, metaclass=BaseMeta):
|
|
|
267
260
|
@classmethod
|
|
268
261
|
def argReport(cls, *args, **kwargs) -> str:
|
|
269
262
|
"""
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
263
|
+
A failing assertion on several values is read best with each value
|
|
264
|
+
on a line of its own, named by its type and short enough to take in
|
|
265
|
+
at a glance, and a test should not have to build such a message by
|
|
266
|
+
hand.
|
|
267
|
+
|
|
268
|
+
Implementation
|
|
269
|
+
--------------
|
|
270
|
+
|
|
271
|
+
Each positional argument becomes '<type: value>', the value as 'str'
|
|
272
|
+
gives it. A line as long as the limit or longer is cut to three
|
|
273
|
+
characters fewer and closed with an ellipsis, so it is as long as
|
|
274
|
+
the limit. The lines are joined by the line separator, each after
|
|
275
|
+
the first indented by two spaces. The text passes through 'textFmt',
|
|
276
|
+
so a run of whitespace inside a value shows as one space, and a
|
|
277
|
+
'<br>' or a '<tab>' in a value is expanded.
|
|
278
|
+
|
|
279
|
+
Parameters
|
|
280
|
+
----------
|
|
281
|
+
|
|
282
|
+
*args
|
|
283
|
+
The values to report.
|
|
284
|
+
|
|
285
|
+
**kwargs
|
|
286
|
+
chars : int, optional
|
|
287
|
+
The limit of a line. Defaults to 48.
|
|
288
|
+
newLine : str, optional
|
|
289
|
+
The line separator. Defaults to 'os.linesep'.
|
|
290
|
+
|
|
291
|
+
Returns
|
|
292
|
+
-------
|
|
293
|
+
|
|
294
|
+
str
|
|
295
|
+
The report, empty when no argument is given.
|
|
276
296
|
"""
|
|
277
297
|
if 'chars' in kwargs:
|
|
278
298
|
lineLength: int = kwargs['chars']
|
|
@@ -299,10 +319,31 @@ class BaseTest(_Temp, metaclass=BaseMeta):
|
|
|
299
319
|
|
|
300
320
|
def seedSamplers(self, seed: int) -> None:
|
|
301
321
|
"""
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
322
|
+
A test drawing random data must be able to draw the same data again,
|
|
323
|
+
at the next run and on another machine, or a failure seen once
|
|
324
|
+
cannot be looked at twice. The test has six samplers, and one line
|
|
325
|
+
should seed them all.
|
|
326
|
+
|
|
327
|
+
Implementation
|
|
328
|
+
--------------
|
|
329
|
+
|
|
330
|
+
The seed is assigned to 'seed' of each of the six samplers,
|
|
331
|
+
'randomInteger' first, which seeds the generator of that sampler. A
|
|
332
|
+
test calling the method in its 'setUp' repeats its draws at every
|
|
333
|
+
run.
|
|
334
|
+
|
|
335
|
+
Parameters
|
|
336
|
+
----------
|
|
337
|
+
|
|
338
|
+
seed : int
|
|
339
|
+
The seed every sampler receives.
|
|
340
|
+
|
|
341
|
+
Raises
|
|
342
|
+
------
|
|
343
|
+
|
|
344
|
+
TypeException
|
|
345
|
+
'seed' is no 'int'. The first sampler refuses it, so none is
|
|
346
|
+
seeded.
|
|
306
347
|
"""
|
|
307
348
|
samplers = (
|
|
308
349
|
self.randomInteger,
|
|
@@ -318,40 +359,33 @@ class BaseTest(_Temp, metaclass=BaseMeta):
|
|
|
318
359
|
@staticmethod
|
|
319
360
|
def _formatRaised(exception: BaseException) -> str:
|
|
320
361
|
"""
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
362
|
+
A failed block is caught where it happens, and its report must still
|
|
363
|
+
name the line that raised, as the traceback of an uncaught exception
|
|
364
|
+
does.
|
|
365
|
+
|
|
366
|
+
Implementation
|
|
367
|
+
--------------
|
|
368
|
+
|
|
369
|
+
'traceback.format_exception' renders the exception with the
|
|
370
|
+
traceback it carries, and the trailing whitespace is dropped.
|
|
371
|
+
|
|
372
|
+
Parameters
|
|
373
|
+
----------
|
|
374
|
+
|
|
375
|
+
exception : BaseException
|
|
376
|
+
What a block raised.
|
|
377
|
+
|
|
378
|
+
Returns
|
|
379
|
+
-------
|
|
380
|
+
|
|
381
|
+
str
|
|
382
|
+
The traceback as the interpreter prints it.
|
|
324
383
|
"""
|
|
325
384
|
lines = traceback.format_exception(
|
|
326
385
|
type(exception), exception, exception.__traceback__,
|
|
327
386
|
)
|
|
328
387
|
return str.join('', lines).rstrip()
|
|
329
388
|
|
|
330
|
-
@classmethod
|
|
331
|
-
def tearDownClass(cls) -> None:
|
|
332
|
-
"""
|
|
333
|
-
Unloads the test module and runs the garbage collector to evict
|
|
334
|
-
the class object before the next test class is built.
|
|
335
|
-
|
|
336
|
-
This is here because 'unittest' pins every test class it discovers
|
|
337
|
-
for the entire run via 'TestLoader' and 'TestResult', and that
|
|
338
|
-
pinning causes real, observable metaclass context leakage between
|
|
339
|
-
otherwise-independent test classes. Run TestA and TestB in
|
|
340
|
-
isolation: both pass. Run them in sequence: TestB inherits stale
|
|
341
|
-
descriptor state, mismatched owners, or cached registry entries
|
|
342
|
-
that 'unittest' has refused to release. Popping the module from
|
|
343
|
-
sys.modules and forcing a GC pass drops the refcount on the
|
|
344
|
-
outgoing class so its metaclass finalizers actually fire before
|
|
345
|
-
the next class's body executes. The bug only exists in the
|
|
346
|
-
intersection of two innocent tests, which is why this looks
|
|
347
|
-
paranoid until you have spent a weekend debugging it.
|
|
348
|
-
|
|
349
|
-
Blame: 'unittest'. The metaclass machinery is fine.
|
|
350
|
-
"""
|
|
351
|
-
super().tearDownClass()
|
|
352
|
-
sys.modules.pop(cls.__module__, None)
|
|
353
|
-
gc.collect()
|
|
354
|
-
|
|
355
389
|
@classmethod
|
|
356
390
|
def __class_init__(cls, name: str, bases: tuple, space, **kw) -> None:
|
|
357
391
|
"""
|
|
@@ -423,14 +457,35 @@ class BaseTest(_Temp, metaclass=BaseMeta):
|
|
|
423
457
|
|
|
424
458
|
def _reportSubTests(self, ) -> None:
|
|
425
459
|
"""
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
460
|
+
A failed 'subTest' block is swallowed where it happens, so that the
|
|
461
|
+
rest of the test runs, and the test must still fail for it
|
|
462
|
+
afterwards. One failure must carry every block recorded since the
|
|
463
|
+
last report, and tell of each what is needed to find it: its label,
|
|
464
|
+
what it raised and the line that raised it. A test that recorded
|
|
465
|
+
nothing, or never opened a block, must pass through untouched. And
|
|
466
|
+
since the report is asked for at more than one moment of a test,
|
|
467
|
+
nothing may be reported twice.
|
|
468
|
+
|
|
469
|
+
Implementation
|
|
470
|
+
--------------
|
|
471
|
+
|
|
472
|
+
The accumulator is read through the 'subTest' descriptor in the way
|
|
473
|
+
that builds none, and a test without one returns at once, as does
|
|
474
|
+
one whose accumulator holds no fail and no error. Otherwise the
|
|
475
|
+
message gets 'FAIL [label]: message' for each fail and
|
|
476
|
+
'ERROR [label]: repr' for each error, each followed by its traceback
|
|
477
|
+
from '_formatRaised'; the accumulator is removed from the instance,
|
|
478
|
+
and 'fail' raises the message. Every test method a test class is
|
|
479
|
+
given calls this method as it ends, the 'doCleanups' it is given
|
|
480
|
+
runs it after the cleanups of the test, and the 'debug' it is given
|
|
481
|
+
calls it at its end; '__class_init__' gives the class the three.
|
|
482
|
+
|
|
483
|
+
Raises
|
|
484
|
+
------
|
|
485
|
+
|
|
486
|
+
AssertionError
|
|
487
|
+
A block failed or raised since the last report. The exception is
|
|
488
|
+
the 'failureException' of the test.
|
|
434
489
|
"""
|
|
435
490
|
cls = type(self)
|
|
436
491
|
try:
|
|
@@ -140,10 +140,33 @@ class ComplexMixin:
|
|
|
140
140
|
@classmethod
|
|
141
141
|
def _resolveOther(cls, other: Any) -> Union[NotImpType, Self]:
|
|
142
142
|
"""
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
143
|
+
Every operator of the mixin takes its other operand in several
|
|
144
|
+
shapes, and must defer to that operand, as Python expects of an
|
|
145
|
+
operator, when it can make nothing of the value. One place decides
|
|
146
|
+
what counts as an operand, so the operators agree.
|
|
147
|
+
|
|
148
|
+
Implementation
|
|
149
|
+
--------------
|
|
150
|
+
|
|
151
|
+
An instance of the class is returned as it is. A real number and a
|
|
152
|
+
'complex' give their parts. A string goes to the constructor, which
|
|
153
|
+
parses it as 'complex' does. Any other value is unpacked as the
|
|
154
|
+
parts when it can be iterated, and handed to the constructor whole
|
|
155
|
+
when it cannot. A value the constructor refuses with 'TypeError' or
|
|
156
|
+
'ValueError' gives 'NotImplemented'.
|
|
157
|
+
|
|
158
|
+
Parameters
|
|
159
|
+
----------
|
|
160
|
+
|
|
161
|
+
other : Any
|
|
162
|
+
The other operand.
|
|
163
|
+
|
|
164
|
+
Returns
|
|
165
|
+
-------
|
|
166
|
+
|
|
167
|
+
Self or NotImplemented
|
|
168
|
+
An instance of the class holding the parts of 'other', or
|
|
169
|
+
'NotImplemented' when 'other' is no operand.
|
|
147
170
|
"""
|
|
148
171
|
if isinstance(other, cls):
|
|
149
172
|
return other
|
|
@@ -172,8 +195,20 @@ class ComplexMixin:
|
|
|
172
195
|
|
|
173
196
|
def conjugate(self) -> Self:
|
|
174
197
|
"""
|
|
175
|
-
|
|
176
|
-
|
|
198
|
+
'complex' offers its conjugate as a method, and a class standing for
|
|
199
|
+
a complex number is expected to offer the same.
|
|
200
|
+
|
|
201
|
+
Implementation
|
|
202
|
+
--------------
|
|
203
|
+
|
|
204
|
+
A new instance of the class holds the same real part and the
|
|
205
|
+
negated imaginary part, as the '~' operator gives.
|
|
206
|
+
|
|
207
|
+
Returns
|
|
208
|
+
-------
|
|
209
|
+
|
|
210
|
+
Self
|
|
211
|
+
The conjugate.
|
|
177
212
|
"""
|
|
178
213
|
cls = type(self)
|
|
179
214
|
out = cls(self.REAL, -self.IMAG)
|
|
@@ -32,7 +32,7 @@ class ComplexTest:
|
|
|
32
32
|
--------------
|
|
33
33
|
|
|
34
34
|
'ComplexTest' has no base of its own, so no loader collects the bank
|
|
35
|
-
itself and no test
|
|
35
|
+
itself and no test file importing it pays twenty-five skipped
|
|
36
36
|
tests; the assertions and 'subTest' come from the test case listed
|
|
37
37
|
after it. The twenty-five test methods are each a loop over 'targets'
|
|
38
38
|
with one block of 'subTest' per target labelled by the target's name,
|
worktoy/work_test/_sub_test.py
CHANGED
|
@@ -30,8 +30,8 @@ class SubTest(TestCase):
|
|
|
30
30
|
block's label, lets the test method run to its end, and leaves it to
|
|
31
31
|
'BaseTest' to fail the test there with every record and its traceback,
|
|
32
32
|
whatever the test class does in 'tearDown' or 'doCleanups'. 'BaseTest'
|
|
33
|
-
hosts it as the 'subTest' descriptor, so a test
|
|
34
|
-
|
|
33
|
+
hosts it as the 'subTest' descriptor, so a test writes
|
|
34
|
+
'with self.subTest(target=x):' as it would with 'unittest'.
|
|
35
35
|
|
|
36
36
|
Implementation
|
|
37
37
|
--------------
|
|
@@ -66,8 +66,10 @@ class SubTest(TestCase):
|
|
|
66
66
|
__fallback_current__: str = '<sub test>'
|
|
67
67
|
|
|
68
68
|
# Private Variables
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
# The descriptor holds the name 'BaseTest' places it at and 'BaseTest'
|
|
70
|
+
# itself; an accumulator holds '__sub_test__' and its test class.
|
|
71
|
+
__field_name__: Optional[str] = None
|
|
72
|
+
__field_owner__: Optional[Type[BaseTest]] = None
|
|
71
73
|
__current_tests__: Optional[tuple[str, ...]] = None
|
|
72
74
|
__test_fails__: Optional[tuple[AssertionError, ...]] = None
|
|
73
75
|
__test_errors__: Optional[tuple[Exception, ...]] = None
|
|
@@ -179,16 +181,30 @@ class SubTest(TestCase):
|
|
|
179
181
|
|
|
180
182
|
def _addFail(self, fail: AssertionError) -> None:
|
|
181
183
|
"""
|
|
182
|
-
The
|
|
183
|
-
|
|
184
|
+
The report names each failed block, so a fail must be kept together
|
|
185
|
+
with the label of the block that raised it.
|
|
186
|
+
|
|
187
|
+
Implementation
|
|
188
|
+
--------------
|
|
189
|
+
|
|
190
|
+
The fail joins 'fails', and the chained label of the block in
|
|
191
|
+
progress joins 'failLabels' at the same position, '<sub test>' when
|
|
192
|
+
no block is open.
|
|
184
193
|
"""
|
|
185
194
|
self.__test_fails__ = (*self.fails, fail)
|
|
186
195
|
self.__fail_labels__ = (*self.failLabels, self.current)
|
|
187
196
|
|
|
188
197
|
def _addError(self, error: Exception) -> None:
|
|
189
198
|
"""
|
|
190
|
-
|
|
191
|
-
|
|
199
|
+
An error is kept with the label of its block as a fail is, and apart
|
|
200
|
+
from the fails, since the report tells an assertion that failed from
|
|
201
|
+
an exception nobody expected.
|
|
202
|
+
|
|
203
|
+
Implementation
|
|
204
|
+
--------------
|
|
205
|
+
|
|
206
|
+
The error joins 'errors', and the chained label of the block in
|
|
207
|
+
progress joins 'errorLabels' at the same position.
|
|
192
208
|
"""
|
|
193
209
|
self.__test_errors__ = (*self.errors, error)
|
|
194
210
|
self.__error_labels__ = (*self.errorLabels, self.current)
|
worktoy/work_test/_test_meta.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
|
-
TestMeta is a metaclass a test base may choose.
|
|
3
|
-
|
|
4
|
-
the reach of a test class.
|
|
2
|
+
TestMeta is a metaclass a test base may choose. Under it, a class body
|
|
3
|
+
that binds 'doCleanups' or 'debug' is refused at that line.
|
|
5
4
|
"""
|
|
6
5
|
# Apache-2.0 license
|
|
7
6
|
# Copyright (c) 2026 Asger Jon Vistisen
|
|
@@ -19,23 +18,16 @@ if TYPE_CHECKING: # pragma: no cover, no mutate
|
|
|
19
18
|
|
|
20
19
|
class TestMeta(BaseMeta):
|
|
21
20
|
"""
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
writes itself. A test base that would rather have such a class body
|
|
33
|
-
refused at its line chooses TestMeta as its metaclass: it puts the
|
|
34
|
-
library's own 'doCleanups' and 'debug' into the namespace of every
|
|
35
|
-
class it builds, before the class body runs, and the namespace
|
|
36
|
-
refuses a class body that binds either name. 'BaseTest' itself is not
|
|
37
|
-
built by it, so that a test base may bring any metaclass based on
|
|
38
|
-
'BaseMeta'.
|
|
21
|
+
'BaseTest' reports the failed 'subTest' blocks of a test from three
|
|
22
|
+
places: each test method as it ends, 'doCleanups' after the cleanups
|
|
23
|
+
of the test, and 'debug' at its end. It gives every test class a
|
|
24
|
+
'doCleanups' and a 'debug' that report, built around the ones the
|
|
25
|
+
class writes itself when it writes any, so a class that writes its
|
|
26
|
+
own loses no report. A test base may still prefer that the bodies of
|
|
27
|
+
its test classes leave the two names alone, and that a body binding
|
|
28
|
+
one is refused at that line. TestMeta is the metaclass such a base
|
|
29
|
+
chooses. 'BaseTest' itself is not built by it, so that a test base
|
|
30
|
+
may bring any metaclass based on 'BaseMeta'.
|
|
39
31
|
|
|
40
32
|
class StrictTest(BaseTest, metaclass=TestMeta):
|
|
41
33
|
pass
|
|
@@ -50,29 +42,43 @@ class TestMeta(BaseMeta):
|
|
|
50
42
|
|
|
51
43
|
'__prepare__' returns a 'TestSpace' in which 'doCleanups' and 'debug'
|
|
52
44
|
are bound already, to the functions '_doCleanupsFactory' and
|
|
53
|
-
'_debugFactory' make,
|
|
54
|
-
'
|
|
55
|
-
|
|
56
|
-
'
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
'
|
|
45
|
+
'_debugFactory' make, and which refuses a class body binding either
|
|
46
|
+
name. The 'doCleanups' placed registers 'BaseTest._reportSubTests'
|
|
47
|
+
with 'addCleanup' and calls 'TestCase.doCleanups': an exception
|
|
48
|
+
raised in 'doCleanups' itself would leave 'TestCase.run', where one
|
|
49
|
+
raised by a registered cleanup is collected as a failure of the test.
|
|
50
|
+
Registered last, the report runs first, before the cleanups of the
|
|
51
|
+
test. The 'debug' placed calls 'TestCase.debug' and then the report,
|
|
52
|
+
whose exception leaves 'debug' as any exception of the test does.
|
|
53
|
+
'BaseTest.__class_init__' builds its own 'doCleanups' and 'debug'
|
|
54
|
+
around the two, as around any a class body binds, so those are what
|
|
55
|
+
the class holds in its '__dict__', and the report runs once more
|
|
56
|
+
after the cleanups of the test, for a block that failed inside one.
|
|
57
|
+
Only the class body is refused: a name assigned on the class after
|
|
58
|
+
the class statement is not.
|
|
62
59
|
"""
|
|
63
60
|
|
|
64
61
|
@staticmethod
|
|
65
62
|
def _doCleanupsFactory() -> Callable:
|
|
66
63
|
"""
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
Every class 'TestMeta' builds must find a 'doCleanups' of the
|
|
65
|
+
library in its namespace before its body runs.
|
|
66
|
+
|
|
67
|
+
Implementation
|
|
68
|
+
--------------
|
|
69
|
+
|
|
70
|
+
A new function is made at each call, so each class holds one of its
|
|
71
|
+
own. It registers 'BaseTest._reportSubTests' as a cleanup, the last,
|
|
72
|
+
and returns what 'TestCase.doCleanups' returns.
|
|
73
|
+
|
|
74
|
+
Returns
|
|
75
|
+
-------
|
|
76
|
+
|
|
77
|
+
Callable
|
|
78
|
+
Called as '(self)': the 'doCleanups' of one test class.
|
|
69
79
|
"""
|
|
70
80
|
|
|
71
81
|
def doCleanups(self: Any) -> bool:
|
|
72
|
-
"""
|
|
73
|
-
Registers the report of the 'subTest' blocks as the last cleanup,
|
|
74
|
-
then runs the cleanups as 'TestCase.doCleanups' does.
|
|
75
|
-
"""
|
|
76
82
|
self.addCleanup(self._reportSubTests)
|
|
77
83
|
return TestCase.doCleanups(self)
|
|
78
84
|
|
|
@@ -81,14 +87,24 @@ class TestMeta(BaseMeta):
|
|
|
81
87
|
@staticmethod
|
|
82
88
|
def _debugFactory() -> Callable:
|
|
83
89
|
"""
|
|
84
|
-
|
|
90
|
+
Every class 'TestMeta' builds must find a 'debug' of the library in
|
|
91
|
+
its namespace before its body runs, as it finds a 'doCleanups'.
|
|
92
|
+
|
|
93
|
+
Implementation
|
|
94
|
+
--------------
|
|
95
|
+
|
|
96
|
+
A new function is made at each call, so each class holds one of its
|
|
97
|
+
own. It calls 'TestCase.debug', which collects no errors and runs no
|
|
98
|
+
'doCleanups', and then 'BaseTest._reportSubTests'.
|
|
99
|
+
|
|
100
|
+
Returns
|
|
101
|
+
-------
|
|
102
|
+
|
|
103
|
+
Callable
|
|
104
|
+
Called as '(self)': the 'debug' of one test class.
|
|
85
105
|
"""
|
|
86
106
|
|
|
87
107
|
def debug(self: Any) -> None:
|
|
88
|
-
"""
|
|
89
|
-
Runs the test as 'TestCase.debug' does, which collects no errors
|
|
90
|
-
and runs no 'doCleanups', then the report of the 'subTest' blocks.
|
|
91
|
-
"""
|
|
92
108
|
TestCase.debug(self)
|
|
93
109
|
self._reportSubTests()
|
|
94
110
|
|
worktoy/work_test/_test_space.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
|
-
TestSpace is the namespace a test class
|
|
3
|
-
'doCleanups' and 'debug' for the library.
|
|
2
|
+
TestSpace is the namespace 'TestMeta' prepares for a test class. It
|
|
3
|
+
keeps the names 'doCleanups' and 'debug' for the library.
|
|
4
4
|
"""
|
|
5
5
|
# Apache-2.0 license
|
|
6
6
|
# Copyright (c) 2026 Asger Jon Vistisen
|
|
@@ -17,14 +17,15 @@ if TYPE_CHECKING: # pragma: no cover, no mutate
|
|
|
17
17
|
|
|
18
18
|
class TestSpace(BaseSpace):
|
|
19
19
|
"""
|
|
20
|
-
A test base built by 'TestMeta' has chosen that
|
|
21
|
-
not
|
|
22
|
-
'BaseTest' reports the failed 'subTest' blocks of a test,
|
|
23
|
-
place to say so is the line of
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
'TestMeta' bind the two names
|
|
27
|
-
every binding of either by
|
|
20
|
+
A test base built by 'TestMeta' has chosen that the bodies of its
|
|
21
|
+
test classes do not bind 'doCleanups' or 'debug', two of the places
|
|
22
|
+
from which 'BaseTest' reports the failed 'subTest' blocks of a test,
|
|
23
|
+
each test method being the third. The place to say so is the line of
|
|
24
|
+
the class body that tries: the builder is then shown that line, not
|
|
25
|
+
the end of a class statement. TestSpace is the namespace 'TestMeta'
|
|
26
|
+
prepares for a test class. It lets 'TestMeta' bind the two names
|
|
27
|
+
before the class body runs and refuses every binding of either by
|
|
28
|
+
the class body.
|
|
28
29
|
|
|
29
30
|
class StrictTest(BaseTest, metaclass=TestMeta):
|
|
30
31
|
pass
|
|
@@ -3,8 +3,8 @@ The 'worktoy.work_test.samplers' package provides random sample-data
|
|
|
3
3
|
generators for the test suite. 'BaseSampler' defines the shared shape, a
|
|
4
4
|
value type plus a single-sample recipe sized into items, rows, and
|
|
5
5
|
tables, with a generator and a seed of its own; each concrete sampler
|
|
6
|
-
fills in one data type. 'SamplerMeta' builds them, so a sampler
|
|
7
|
-
|
|
6
|
+
fills in one data type. 'SamplerMeta' builds them, so a sampler class
|
|
7
|
+
that leaves either hook out cannot be instantiated.
|
|
8
8
|
|
|
9
9
|
- LOREM_WORDS, randomWord, loremSentence
|
|
10
10
|
- SamplerMeta
|
|
@@ -48,29 +48,30 @@ class BaseSampler(BaseObject, metaclass=SamplerMeta):
|
|
|
48
48
|
Implementation
|
|
49
49
|
--------------
|
|
50
50
|
|
|
51
|
-
The settings are boxes. 'colCount' and 'rowCount',
|
|
52
|
-
set, size a row and a table, with 'width', 'size',
|
|
53
|
-
'count' as aliases and as keywords, and a count below
|
|
54
|
-
with 'ValueError'; a subclass declares its own
|
|
55
|
-
The keyword constructor takes every setting
|
|
56
|
-
'__key_groups__' lists, merged along the resolution
|
|
57
|
-
subclass adds its own groups,
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
values found, so a subclass
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
'
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
'
|
|
72
|
-
|
|
73
|
-
|
|
51
|
+
The settings are boxes, apart from 'seed'. 'colCount' and 'rowCount',
|
|
52
|
+
one each unless set, size a row and a table, with 'width', 'size',
|
|
53
|
+
'height' and 'count' as aliases and as keywords, and a count below
|
|
54
|
+
one is refused with 'ValueError'; a subclass declares its own
|
|
55
|
+
settings the same way. The keyword constructor takes every setting
|
|
56
|
+
by the synonyms '__key_groups__' lists, merged along the resolution
|
|
57
|
+
order so that a subclass adds its own groups, assigns the first
|
|
58
|
+
synonym present of each, and refuses a keyword that names no setting
|
|
59
|
+
with the 'TypeError' Python raises for an unexpected keyword
|
|
60
|
+
argument. A box casts the value assigned when it converts without
|
|
61
|
+
loss and refuses it otherwise with 'TypeException' naming the
|
|
62
|
+
setting. '_applySettings' receives the values found, so a subclass
|
|
63
|
+
holding a pair of settings may write them together. Every draw goes
|
|
64
|
+
to 'random', a 'random.Random' built for each sampler apart. 'seed'
|
|
65
|
+
is a field over the seed last given, 'None' until an 'int' is given,
|
|
66
|
+
by keyword or by assignment, which seeds that generator, so two
|
|
67
|
+
samplers seeded alike draw alike whatever else draws in between; it
|
|
68
|
+
takes an 'int' alone and casts nothing. 'item', 'row' and 'table'
|
|
69
|
+
draw one value, a row of 'colCount' values and a table of 'rowCount'
|
|
70
|
+
rows; 'row', and 'table' through it, check each value against
|
|
71
|
+
'valueType', while 'item' and a call of the sampler hand out what
|
|
72
|
+
'_getItem' gives as it is. Iterating the sampler yields 'rowCount'
|
|
73
|
+
rows. The two hooks are abstract, and a class that leaves either out
|
|
74
|
+
cannot be instantiated.
|
|
74
75
|
|
|
75
76
|
Attributes
|
|
76
77
|
----------
|
|
@@ -134,10 +135,24 @@ class BaseSampler(BaseObject, metaclass=SamplerMeta):
|
|
|
134
135
|
@classmethod
|
|
135
136
|
def _getKeyGroups(cls) -> KeyGroups:
|
|
136
137
|
"""
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
A sampler class declares the settings it adds and inherits the rest,
|
|
139
|
+
so the keyword constructor needs the synonyms of every setting along
|
|
140
|
+
the resolution order, and a subclass must be able to give a setting
|
|
141
|
+
it inherits other synonyms.
|
|
142
|
+
|
|
143
|
+
Implementation
|
|
144
|
+
--------------
|
|
145
|
+
|
|
146
|
+
'__key_groups__' is read from the own dictionary of each class
|
|
147
|
+
along the resolution order, and the nearest class declaring a
|
|
148
|
+
setting answers for it.
|
|
149
|
+
|
|
150
|
+
Returns
|
|
151
|
+
-------
|
|
152
|
+
|
|
153
|
+
KeyGroups
|
|
154
|
+
A 'dict[str, tuple[str, ...]]' from each setting to its synonyms,
|
|
155
|
+
the settings of the nearest class first.
|
|
141
156
|
"""
|
|
142
157
|
out = dict()
|
|
143
158
|
for base in cls.__mro__:
|
|
@@ -181,19 +196,59 @@ class BaseSampler(BaseObject, metaclass=SamplerMeta):
|
|
|
181
196
|
@overload()
|
|
182
197
|
def __init__(self, **kwargs) -> None:
|
|
183
198
|
"""
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
199
|
+
A sampler is sized, narrowed and seeded in the expression that
|
|
200
|
+
creates it, each setting under whichever of its usual names the test
|
|
201
|
+
writes, and a name that is no setting, most often a misspelled one,
|
|
202
|
+
must not pass in silence.
|
|
203
|
+
|
|
204
|
+
Implementation
|
|
205
|
+
--------------
|
|
206
|
+
|
|
207
|
+
A keyword naming no setting is refused first, as Python refuses an
|
|
208
|
+
unexpected keyword argument. Each setting then takes the first of
|
|
209
|
+
its synonyms present, by assignment. A box casts what converts
|
|
210
|
+
without loss, so an 'int' bound suits a float sampler, and refuses a
|
|
211
|
+
lossy value with 'TypeException' naming the setting; 'seed' takes an
|
|
212
|
+
'int' alone.
|
|
213
|
+
|
|
214
|
+
Parameters
|
|
215
|
+
----------
|
|
216
|
+
|
|
217
|
+
**kwargs
|
|
218
|
+
The settings, each under any of its synonyms.
|
|
219
|
+
|
|
220
|
+
Raises
|
|
221
|
+
------
|
|
222
|
+
|
|
223
|
+
TypeError
|
|
224
|
+
A keyword names no setting.
|
|
225
|
+
|
|
226
|
+
TypeException
|
|
227
|
+
A setting refuses its value.
|
|
228
|
+
|
|
229
|
+
ValueError
|
|
230
|
+
A count is below one.
|
|
189
231
|
"""
|
|
190
232
|
self._refuseUnknown(**kwargs)
|
|
191
233
|
self._applySettings(self._collectSettings(**kwargs))
|
|
192
234
|
|
|
193
235
|
def _refuseUnknown(self, **kwargs) -> None:
|
|
194
236
|
"""
|
|
195
|
-
|
|
196
|
-
|
|
237
|
+
A keyword the sampler does not know, most often a misspelled
|
|
238
|
+
setting, must not pass in silence: the test would run on a default
|
|
239
|
+
it believes it replaced.
|
|
240
|
+
|
|
241
|
+
Implementation
|
|
242
|
+
--------------
|
|
243
|
+
|
|
244
|
+
A keyword among the synonyms of no setting raises, worded as Python
|
|
245
|
+
words an unexpected keyword argument.
|
|
246
|
+
|
|
247
|
+
Raises
|
|
248
|
+
------
|
|
249
|
+
|
|
250
|
+
TypeError
|
|
251
|
+
A keyword names no setting.
|
|
197
252
|
"""
|
|
198
253
|
groups = self._getKeyGroups()
|
|
199
254
|
accepted = [key for keys in groups.values() for key in keys]
|
|
@@ -204,9 +259,23 @@ class BaseSampler(BaseObject, metaclass=SamplerMeta):
|
|
|
204
259
|
|
|
205
260
|
def _collectSettings(self, **kwargs) -> Settings:
|
|
206
261
|
"""
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
262
|
+
A setting may be written under several names, and the constructor
|
|
263
|
+
needs each setting once, under its own name, whichever synonym the
|
|
264
|
+
caller used.
|
|
265
|
+
|
|
266
|
+
Implementation
|
|
267
|
+
--------------
|
|
268
|
+
|
|
269
|
+
For each setting the synonyms are tried in the order
|
|
270
|
+
'__key_groups__' lists them, and the first one present gives the
|
|
271
|
+
value; a further synonym of the same setting is passed over.
|
|
272
|
+
|
|
273
|
+
Returns
|
|
274
|
+
-------
|
|
275
|
+
|
|
276
|
+
Settings
|
|
277
|
+
A 'dict[str, Any]' from the name of each setting found to its
|
|
278
|
+
value.
|
|
210
279
|
"""
|
|
211
280
|
out = dict()
|
|
212
281
|
for name, keys in self._getKeyGroups().items():
|
|
@@ -218,10 +287,25 @@ class BaseSampler(BaseObject, metaclass=SamplerMeta):
|
|
|
218
287
|
|
|
219
288
|
def _applySettings(self, settings: Settings) -> None:
|
|
220
289
|
"""
|
|
221
|
-
The
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
290
|
+
The settings found among the keywords must reach the sampler, and a
|
|
291
|
+
subclass whose settings belong together, as two bounds do, must be
|
|
292
|
+
able to write them in one step, so that neither is checked against
|
|
293
|
+
a value of the other that is about to be replaced.
|
|
294
|
+
|
|
295
|
+
Implementation
|
|
296
|
+
--------------
|
|
297
|
+
|
|
298
|
+
Each setting is assigned under its own name: a box casts or refuses
|
|
299
|
+
the value, and 'seed' takes an 'int' alone. A subclass holding such
|
|
300
|
+
a pair reimplements the method, takes the pair out of the settings,
|
|
301
|
+
writes it, and hands the rest on to this one.
|
|
302
|
+
|
|
303
|
+
Parameters
|
|
304
|
+
----------
|
|
305
|
+
|
|
306
|
+
settings : Settings
|
|
307
|
+
A 'dict[str, Any]' from the name of each setting found to its
|
|
308
|
+
value.
|
|
225
309
|
"""
|
|
226
310
|
for name, value in settings.items():
|
|
227
311
|
setattr(self, name, value)
|
|
@@ -248,6 +332,7 @@ class BaseSampler(BaseObject, metaclass=SamplerMeta):
|
|
|
248
332
|
|
|
249
333
|
Returns
|
|
250
334
|
-------
|
|
335
|
+
|
|
251
336
|
Any
|
|
252
337
|
A single sample value of the type specified by the '_getValueType'
|
|
253
338
|
method.
|
|
@@ -260,16 +345,27 @@ class BaseSampler(BaseObject, metaclass=SamplerMeta):
|
|
|
260
345
|
@row.GET
|
|
261
346
|
def _getRow(self, ) -> Row:
|
|
262
347
|
"""
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
348
|
+
A test often wants several values at once, and wants to trust that
|
|
349
|
+
every one of them is of the type the sampler names.
|
|
350
|
+
|
|
351
|
+
Implementation
|
|
352
|
+
--------------
|
|
353
|
+
|
|
354
|
+
'colCount' values are drawn through '_getItem', and each is checked
|
|
355
|
+
against 'valueType'. A subclass reimplementing the method sees to
|
|
356
|
+
the number of values, and to their type, itself.
|
|
267
357
|
|
|
268
358
|
Returns
|
|
269
359
|
-------
|
|
360
|
+
|
|
270
361
|
Row
|
|
271
|
-
A tuple
|
|
272
|
-
|
|
362
|
+
A 'tuple[Any, ...]' of 'colCount' values.
|
|
363
|
+
|
|
364
|
+
Raises
|
|
365
|
+
------
|
|
366
|
+
|
|
367
|
+
TypeException
|
|
368
|
+
'_getItem' gave a value that is no instance of 'valueType'.
|
|
273
369
|
"""
|
|
274
370
|
out = []
|
|
275
371
|
for _ in range(self.colCount):
|
|
@@ -282,17 +378,21 @@ class BaseSampler(BaseObject, metaclass=SamplerMeta):
|
|
|
282
378
|
@table.GET
|
|
283
379
|
def _getTable(self, ) -> Table:
|
|
284
380
|
"""
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
381
|
+
A test of anything laid out in rows wants a whole table of values
|
|
382
|
+
from one read.
|
|
383
|
+
|
|
384
|
+
Implementation
|
|
385
|
+
--------------
|
|
386
|
+
|
|
387
|
+
'rowCount' rows are drawn through '_getRow'. A subclass
|
|
388
|
+
reimplementing the method sees to the number of rows itself.
|
|
290
389
|
|
|
291
390
|
Returns
|
|
292
391
|
-------
|
|
392
|
+
|
|
293
393
|
Table
|
|
294
|
-
A tuple
|
|
295
|
-
'
|
|
394
|
+
A 'tuple[tuple[Any, ...], ...]' of 'rowCount' rows, each of
|
|
395
|
+
'colCount' values.
|
|
296
396
|
"""
|
|
297
397
|
out = []
|
|
298
398
|
while len(out) < self.rowCount:
|
|
@@ -305,14 +405,32 @@ class BaseSampler(BaseObject, metaclass=SamplerMeta):
|
|
|
305
405
|
|
|
306
406
|
def __call__(self, ) -> Any:
|
|
307
407
|
"""
|
|
308
|
-
|
|
408
|
+
A sampler handed over where a function of no arguments is expected,
|
|
409
|
+
as a factory of values, must give one value at each call.
|
|
410
|
+
|
|
411
|
+
Implementation
|
|
412
|
+
--------------
|
|
413
|
+
|
|
414
|
+
The call returns what '_getItem' gives, unchecked, as 'item' does.
|
|
309
415
|
"""
|
|
310
416
|
return self._getItem()
|
|
311
417
|
|
|
312
418
|
def __iter__(self) -> Iterator[Any]:
|
|
313
419
|
"""
|
|
314
|
-
|
|
315
|
-
|
|
420
|
+
A loop over a sampler must end, so iterating one is a loop over a
|
|
421
|
+
table of its rows, not over values without end.
|
|
422
|
+
|
|
423
|
+
Implementation
|
|
424
|
+
--------------
|
|
425
|
+
|
|
426
|
+
'rowCount' is read once, as the iteration starts, and that many rows
|
|
427
|
+
are yielded, each drawn by '_getRow' when the loop asks for it.
|
|
428
|
+
|
|
429
|
+
Yields
|
|
430
|
+
------
|
|
431
|
+
|
|
432
|
+
Row
|
|
433
|
+
A 'tuple[Any, ...]' of 'colCount' values.
|
|
316
434
|
"""
|
|
317
435
|
_c = int(self.rowCount)
|
|
318
436
|
while _c:
|
|
@@ -327,9 +445,15 @@ class BaseSampler(BaseObject, metaclass=SamplerMeta):
|
|
|
327
445
|
|
|
328
446
|
def __repr__(self, ) -> str:
|
|
329
447
|
"""
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
448
|
+
A sampler shown in a failure message should say how it is set, so
|
|
449
|
+
that the data of the failing test can be drawn again.
|
|
450
|
+
|
|
451
|
+
Implementation
|
|
452
|
+
--------------
|
|
453
|
+
|
|
454
|
+
Every setting of the key groups is named with its value, the
|
|
455
|
+
settings of the nearest class first, as 'IntSampler(minVal=0,
|
|
456
|
+
maxVal=255, colCount=1, rowCount=1, seed=None)'.
|
|
333
457
|
"""
|
|
334
458
|
parts = []
|
|
335
459
|
for name in self._getKeyGroups():
|
|
@@ -70,9 +70,27 @@ class IntSampler(BaseSampler):
|
|
|
70
70
|
|
|
71
71
|
def _setBounds(self, low: Any, high: Any) -> None:
|
|
72
72
|
"""
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
Two bounds given together must be ordered against each other, never
|
|
74
|
+
one of them against the bound the other is about to replace. Writing
|
|
75
|
+
300 and 400 over the range from 0 to 255 one bound at a time would
|
|
76
|
+
find the new lower bound above the old upper one, swap the two, and
|
|
77
|
+
end at 255 and 400.
|
|
78
|
+
|
|
79
|
+
Implementation
|
|
80
|
+
--------------
|
|
81
|
+
|
|
82
|
+
Both bounds are written, each cast by its box, while
|
|
83
|
+
'__bounds_pending__' makes '_orderBounds' stand down, and
|
|
84
|
+
'_orderBounds' orders the pair once both are stored.
|
|
85
|
+
|
|
86
|
+
Parameters
|
|
87
|
+
----------
|
|
88
|
+
|
|
89
|
+
low : Any
|
|
90
|
+
The one bound.
|
|
91
|
+
|
|
92
|
+
high : Any
|
|
93
|
+
The other bound.
|
|
76
94
|
"""
|
|
77
95
|
self.__bounds_pending__ = True
|
|
78
96
|
try:
|
|
@@ -84,8 +102,22 @@ class IntSampler(BaseSampler):
|
|
|
84
102
|
|
|
85
103
|
def _applySettings(self, settings: Settings) -> None:
|
|
86
104
|
"""
|
|
87
|
-
|
|
88
|
-
|
|
105
|
+
Two bounds given by keyword belong together as two given by position
|
|
106
|
+
do, and must be written as a pair.
|
|
107
|
+
|
|
108
|
+
Implementation
|
|
109
|
+
--------------
|
|
110
|
+
|
|
111
|
+
When the settings hold both bounds, the two are taken out and
|
|
112
|
+
written through '_setBounds'. The rest, a lone bound included, goes
|
|
113
|
+
to 'BaseSampler._applySettings'.
|
|
114
|
+
|
|
115
|
+
Parameters
|
|
116
|
+
----------
|
|
117
|
+
|
|
118
|
+
settings : Settings
|
|
119
|
+
A 'dict[str, Any]' from the name of each setting found to its
|
|
120
|
+
value.
|
|
89
121
|
"""
|
|
90
122
|
if 'minVal' in settings and 'maxVal' in settings:
|
|
91
123
|
self._setBounds(settings.pop('minVal'), settings.pop('maxVal'))
|
|
@@ -99,8 +131,16 @@ class IntSampler(BaseSampler):
|
|
|
99
131
|
@maxVal.onSet
|
|
100
132
|
def _orderBounds(self, *args) -> None:
|
|
101
133
|
"""
|
|
102
|
-
|
|
103
|
-
|
|
134
|
+
A test names a range by its two ends and should not have to care
|
|
135
|
+
which it wrote first, and a bound assigned later must not leave the
|
|
136
|
+
range empty.
|
|
137
|
+
|
|
138
|
+
Implementation
|
|
139
|
+
--------------
|
|
140
|
+
|
|
141
|
+
The method is the 'onSet' notifier of both boxes. Unless
|
|
142
|
+
'_setBounds' is writing a pair, an upper bound found below the lower
|
|
143
|
+
one makes it write the two swapped, through '_setBounds'.
|
|
104
144
|
"""
|
|
105
145
|
if self.__bounds_pending__:
|
|
106
146
|
# This branch is reached by the following:
|
|
@@ -47,9 +47,15 @@ _UP_TO_LENGTH: dict[int, tuple[str, ...]] = {
|
|
|
47
47
|
|
|
48
48
|
def _choose(words: tuple[str, ...], generator: Optional[Random]) -> str:
|
|
49
49
|
"""
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
A sampler draws from a generator of its own, so that its seed repeats
|
|
51
|
+
its words, while a caller with no generator draws as 'random.choice'
|
|
52
|
+
does.
|
|
53
|
+
|
|
54
|
+
Implementation
|
|
55
|
+
--------------
|
|
56
|
+
|
|
57
|
+
One of 'words' is drawn from 'generator', a 'random.Random', or from
|
|
58
|
+
Python's own 'random' when none is given.
|
|
53
59
|
"""
|
|
54
60
|
if generator is None:
|
|
55
61
|
return random.choice(words)
|
|
@@ -58,20 +64,60 @@ def _choose(words: tuple[str, ...], generator: Optional[Random]) -> str:
|
|
|
58
64
|
|
|
59
65
|
def randomWord(generator: Optional[Random] = None) -> str:
|
|
60
66
|
"""
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
67
|
+
A test that needs a short string wants a real word rather than random
|
|
68
|
+
letters, and a seeded sampler asking for one must get the same word
|
|
69
|
+
again.
|
|
70
|
+
|
|
71
|
+
Implementation
|
|
72
|
+
--------------
|
|
73
|
+
|
|
74
|
+
One word of 'LOREM_WORDS' is drawn, each as likely as any other, from
|
|
75
|
+
'generator' when one is given and from Python's own 'random'
|
|
76
|
+
otherwise.
|
|
77
|
+
|
|
78
|
+
Parameters
|
|
79
|
+
----------
|
|
80
|
+
|
|
81
|
+
generator : random.Random, optional
|
|
82
|
+
The generator to draw from.
|
|
83
|
+
|
|
84
|
+
Returns
|
|
85
|
+
-------
|
|
86
|
+
|
|
87
|
+
str
|
|
88
|
+
The word.
|
|
64
89
|
"""
|
|
65
90
|
return _choose(LOREM_WORDS, generator)
|
|
66
91
|
|
|
67
92
|
|
|
68
93
|
def _fittingWord(room: int, generator: Optional[Random] = None) -> str:
|
|
69
94
|
"""
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
95
|
+
A sentence of an exact length is built word by word, and each word
|
|
96
|
+
must leave the rest closable: after it either nothing is left, or
|
|
97
|
+
room for a space and at least one more letter.
|
|
98
|
+
|
|
99
|
+
Implementation
|
|
100
|
+
--------------
|
|
101
|
+
|
|
102
|
+
With room for no more than the longest word, a word of exactly that
|
|
103
|
+
many letters is drawn, which closes the body; the vocabulary holds a
|
|
104
|
+
word of every length for that. With more room, a word of at most two
|
|
105
|
+
letters fewer than the room is drawn.
|
|
106
|
+
|
|
107
|
+
Parameters
|
|
108
|
+
----------
|
|
109
|
+
|
|
110
|
+
room : int
|
|
111
|
+
The letters left in the body of the sentence.
|
|
112
|
+
|
|
113
|
+
generator : random.Random, optional
|
|
114
|
+
The generator to draw from.
|
|
115
|
+
|
|
116
|
+
Returns
|
|
117
|
+
-------
|
|
118
|
+
|
|
119
|
+
str
|
|
120
|
+
The word.
|
|
75
121
|
"""
|
|
76
122
|
if room <= _LONGEST:
|
|
77
123
|
return _choose(_OF_LENGTH[room], generator)
|
|
@@ -80,12 +126,39 @@ def _fittingWord(room: int, generator: Optional[Random] = None) -> str:
|
|
|
80
126
|
|
|
81
127
|
def loremSentence(charCount: int, generator: Optional[Random] = None) -> str:
|
|
82
128
|
"""
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
129
|
+
A test of anything that lays out, wraps or cuts text wants text of a
|
|
130
|
+
length it chooses to the character, which still reads as a sentence.
|
|
131
|
+
|
|
132
|
+
Implementation
|
|
133
|
+
--------------
|
|
134
|
+
|
|
135
|
+
Words of 'LOREM_WORDS' are joined by single spaces, each chosen by
|
|
136
|
+
'_fittingWord' so that the last one ends the body one character short
|
|
137
|
+
of the count; the first letter is capitalised and a period closes the
|
|
138
|
+
sentence. A count of one gives the period alone. The words are drawn
|
|
139
|
+
from 'generator' when one is given and from Python's own 'random'
|
|
140
|
+
otherwise.
|
|
141
|
+
|
|
142
|
+
Parameters
|
|
143
|
+
----------
|
|
144
|
+
|
|
145
|
+
charCount : int
|
|
146
|
+
The length of the sentence, period included.
|
|
147
|
+
|
|
148
|
+
generator : random.Random, optional
|
|
149
|
+
The generator to draw from.
|
|
150
|
+
|
|
151
|
+
Returns
|
|
152
|
+
-------
|
|
153
|
+
|
|
154
|
+
str
|
|
155
|
+
The sentence.
|
|
156
|
+
|
|
157
|
+
Raises
|
|
158
|
+
------
|
|
159
|
+
|
|
160
|
+
ValueError
|
|
161
|
+
'charCount' is below one.
|
|
89
162
|
"""
|
|
90
163
|
if charCount < 1:
|
|
91
164
|
infoSpec = """A sentence needs at least one character, but %d were
|
|
@@ -14,10 +14,11 @@ from ...mcls import BaseMeta
|
|
|
14
14
|
class SamplerMeta(BaseMeta, ABCMeta):
|
|
15
15
|
"""
|
|
16
16
|
A sampler must supply a value type and a single-sample recipe, and a
|
|
17
|
-
sampler that leaves either out must
|
|
18
|
-
|
|
19
|
-
'
|
|
20
|
-
|
|
17
|
+
sampler class that leaves either out must give no sampler at all: the
|
|
18
|
+
mistake must show where the sampler is created, not at its first row.
|
|
19
|
+
'abc.ABC' cannot stand beside 'BaseObject', since 'ABCMeta' is outside
|
|
20
|
+
the 'MetaType' family and Python refuses the metaclass conflict, so
|
|
21
|
+
the samplers need one metaclass that is both.
|
|
21
22
|
|
|
22
23
|
Implementation
|
|
23
24
|
--------------
|
|
@@ -26,6 +27,7 @@ class SamplerMeta(BaseMeta, ABCMeta):
|
|
|
26
27
|
the way the 'mcls' package documents for a metaclass of another
|
|
27
28
|
library. 'BaseMeta' answers first along the resolution order, so a
|
|
28
29
|
sampler body keeps its overloads and class hooks, and 'ABCMeta'
|
|
29
|
-
collects the methods marked with 'abstractmethod'
|
|
30
|
-
|
|
30
|
+
collects the methods marked with 'abstractmethod'. A class that
|
|
31
|
+
leaves one of them abstract is created all the same, and calling it
|
|
32
|
+
raises 'TypeError' naming the method. 'BaseSampler' is built by it.
|
|
31
33
|
"""
|
|
@@ -30,11 +30,10 @@ class SymbolicSampler(BaseSampler):
|
|
|
30
30
|
|
|
31
31
|
'wordCount' is an 'AttriBox[int]' with the synonyms 'numWords',
|
|
32
32
|
'num_words' and 'word_count' for the keyword constructor ('count' is
|
|
33
|
-
the row count, as on every sampler);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
'
|
|
37
|
-
'self.random'.
|
|
33
|
+
the row count, as on every sampler); a 'preSet' notifier refuses a
|
|
34
|
+
count below one with 'ValueError'. The one-argument constructor takes
|
|
35
|
+
the count. Each value is a 'SymbolicName' of 'wordCount' words from
|
|
36
|
+
'randomWord' drawing from 'self.random'.
|
|
38
37
|
"""
|
|
39
38
|
|
|
40
39
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: worktoy
|
|
3
|
-
Version: 1.2.1.
|
|
3
|
+
Version: 1.2.1.dev2
|
|
4
4
|
Summary: Declarative structure for Python that enforces types at runtime and enables true function overloading
|
|
5
5
|
Author-email: Asger Jon Vistisen <asgerjon2@gmail.com>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -32,7 +32,7 @@ Dynamic: license-file
|
|
|
32
32
|
[](https://pypi.org/project/worktoy/)
|
|
33
33
|
[](https://opensource.org/licenses/Apache-2.0)
|
|
34
34
|
|
|
35
|
-
# worktoy v1.2.1-
|
|
35
|
+
# worktoy v1.2.1-dev2 (development build)
|
|
36
36
|
|
|
37
37
|
**worktoy** provides utilities for Python development focused on
|
|
38
38
|
reducing boilerplate while improving type safety and readability. Each
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
worktoy/__init__.py,sha256=
|
|
1
|
+
worktoy/__init__.py,sha256=LgFXPEBiiRiwVXDoIpwPH697ZNtwp5W1jAtUBHMuD9Q,2899
|
|
2
2
|
worktoy/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
3
|
worktoy/core/__init__.py,sha256=dUNFlZa37R95JPn_6spjQDhI_A3LBJV34NGiyElV5gs,604
|
|
4
4
|
worktoy/core/_instance_owner.py,sha256=Nb0j0Wp8uXROUTLMVGWS_Krl6mlUcelo24vu07U4_bo,2144
|
|
@@ -178,24 +178,24 @@ worktoy/waitaminute/meta/_shadowed_class_hook.py,sha256=SEEPHdlPnKgiekig5rYIHTs3
|
|
|
178
178
|
worktoy/waitaminute/meta/_slots_refused.py,sha256=IEvHCiDP29qjzMQW5Q9aZwNfi0KUOC5owJTO9mRerrY,1986
|
|
179
179
|
worktoy/waitaminute/meta/_unbound_class_hook.py,sha256=s8sCKCcJ97wZAqcZ6_0kM1fyFGd5mTHm-OsJOOBnKhA,2851
|
|
180
180
|
worktoy/work_test/__init__.py,sha256=xYPCovvILuKvklNN9eRXUuNDF2K-SEFrfVYvIv2zYCY,1248
|
|
181
|
-
worktoy/work_test/_base_test.py,sha256=
|
|
182
|
-
worktoy/work_test/_complex_mixin.py,sha256
|
|
183
|
-
worktoy/work_test/_complex_test.py,sha256=
|
|
184
|
-
worktoy/work_test/_sub_test.py,sha256=
|
|
185
|
-
worktoy/work_test/_test_meta.py,sha256=
|
|
186
|
-
worktoy/work_test/_test_space.py,sha256=
|
|
187
|
-
worktoy/work_test/samplers/__init__.py,sha256=
|
|
188
|
-
worktoy/work_test/samplers/_base_sampler.py,sha256=
|
|
181
|
+
worktoy/work_test/_base_test.py,sha256=RcuuLaYaFB9OyaMYIm7MIcYIZX2MUTCi-ICIyQg9_KY,17346
|
|
182
|
+
worktoy/work_test/_complex_mixin.py,sha256=9QFDbcQbj2e4oEV47TYTOOUfuiIh-EdnN5HCiz8YpSM,12322
|
|
183
|
+
worktoy/work_test/_complex_test.py,sha256=kaCywbiXkXjQUtlKEtf_cuoIaF0D7p-8eAU_7d6S_DQ,12476
|
|
184
|
+
worktoy/work_test/_sub_test.py,sha256=2YE-P54K0wLfEx1gkQiKnBl_PKZOAqkdCG5T5-ELIKQ,13876
|
|
185
|
+
worktoy/work_test/_test_meta.py,sha256=DAGMqDniWB0_5Zb_W81K8HJJ1kQdf1xTJC9s8-e5NDg,4486
|
|
186
|
+
worktoy/work_test/_test_space.py,sha256=s_rYCmVA-6iVS5PDGLfaPGWJvT64jz6yP6I1uQmxdwQ,2080
|
|
187
|
+
worktoy/work_test/samplers/__init__.py,sha256=ufhNeBPvRqJ7lVQwmTI7xbkGUp-345wSHvdVp-QZ0Kc,1277
|
|
188
|
+
worktoy/work_test/samplers/_base_sampler.py,sha256=O9DtXOflA5CsRhNtFJrMzjlsUZJY0jyH3QaoCmRutpc,14740
|
|
189
189
|
worktoy/work_test/samplers/_float_sampler.py,sha256=LUlFllZuPKislQp2MX4Mw6HtuSkkGLqaAWNmAe8sqDw,2924
|
|
190
190
|
worktoy/work_test/samplers/_gaussian_sampler.py,sha256=tvdI0rehBWgsZD9WFBkpJV3NHniYxvGc4x92l3mQLe4,4582
|
|
191
|
-
worktoy/work_test/samplers/_int_sampler.py,sha256=
|
|
191
|
+
worktoy/work_test/samplers/_int_sampler.py,sha256=gZgxoAaeeyp1aOHoKTTIJOa63qMDNvomcryNLHxuNTo,6140
|
|
192
192
|
worktoy/work_test/samplers/_lorem_sampler.py,sha256=mMCq24GwRTxf3m1rtm1RIr3LgBM9kihU--GWfUv8dMY,2333
|
|
193
|
-
worktoy/work_test/samplers/_lorem_text.py,sha256=
|
|
194
|
-
worktoy/work_test/samplers/_sampler_meta.py,sha256=
|
|
195
|
-
worktoy/work_test/samplers/_symbolic_sampler.py,sha256=
|
|
193
|
+
worktoy/work_test/samplers/_lorem_text.py,sha256=KQgBKQ5iLS_lU7HGvfoLTVsFyDcm-JRCCdA__xM4oQs,5060
|
|
194
|
+
worktoy/work_test/samplers/_sampler_meta.py,sha256=9AodXKw3Ww-Eu1qzqJsRm-AbYolgON9TKA5fdfBZ9bY,1216
|
|
195
|
+
worktoy/work_test/samplers/_symbolic_sampler.py,sha256=VgheImGi_-WiUYa1o-Htu1OSjoOqTzx8PycDzf-es4c,3042
|
|
196
196
|
worktoy/work_test/samplers/_word_sampler.py,sha256=9K5ib8Wt2APUEMh2ZGMRNSkHjnUpl_HDl8OdFSgw1BU,1044
|
|
197
|
-
worktoy-1.2.1.
|
|
198
|
-
worktoy-1.2.1.
|
|
199
|
-
worktoy-1.2.1.
|
|
200
|
-
worktoy-1.2.1.
|
|
201
|
-
worktoy-1.2.1.
|
|
197
|
+
worktoy-1.2.1.dev2.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
198
|
+
worktoy-1.2.1.dev2.dist-info/METADATA,sha256=YVk_TQPQ7ZAdeto71MzPM5R0Cq5iP7y3Ib6bZi2otTE,10656
|
|
199
|
+
worktoy-1.2.1.dev2.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
200
|
+
worktoy-1.2.1.dev2.dist-info/top_level.txt,sha256=6yO8xnr7WakfINq91qO3rs5ECubDOGvAQ9ztVgoG0j4,8
|
|
201
|
+
worktoy-1.2.1.dev2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|