Skip to content

Commit eb6cdf0

Browse files
[MERGE]
Patch to improve docstrings in tests/context.py
2 parents 98daaf4 + 93507e9 commit eb6cdf0

File tree

1 file changed

+103
-42
lines changed

1 file changed

+103
-42
lines changed

tests/context.py

Lines changed: 103 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -261,23 +261,41 @@ def checkCovCommand(args=[None]):
261261
Returns:
262262
list: The modified list of arguments with 'coverage run' options added as applicable.
263263
264-
Examples:
265-
>>> checkCovCommand(["python", "script.py"])
266-
['python', 'script.py']
264+
Meta Testing:
265+
266+
First setup test fixtures by importing test context.
267+
268+
>>> import tests.context as context
269+
>>>
270+
271+
Testcase 1: Function should return unmodified arguments if 'coverage' is not in the first argument.
267272
268-
>>> checkCovCommand(["coverage", "script.py"]) #doctest: +ELLIPSIS # missing 'run'
269-
['...', 'run', '-p', '--context=Integration', '--source=multicast', 'script.py']
273+
>>> context.checkCovCommand(["python", "script.py"])
274+
['python', 'script.py']
270275
271-
>>> checkCovCommand(["coverage run", "script.py"]) #doctest: +ELLIPSIS # NOT missing 'run'
272-
['...', 'run', '-p', '--context=Integration', '--source=multicast', 'script.py']
276+
Testcase 2: Function should modify arguments when 'coverage' is the first argument.
277+
A.) Missing 'run'
273278
274-
>>> checkCovCommand(["/usr/bin/coverage", "test.py"]) #doctest: +ELLIPSIS
275-
['...', 'run', '-p', '--context=Integration', '--source=multicast', 'test.py']
279+
>>> checkCovCommand(["coverage", "script.py"]) #doctest: +ELLIPSIS
280+
['...', 'run', '-p', '--context=Integration', '--source=multicast', 'script.py']
276281
277-
>>> import sys
278-
>>> test_fixutre = [str("{} -m coverage run").format(sys.executable), "test.py"]
279-
>>> checkCovCommand(test_fixutre) #doctest: +ELLIPSIS
280-
[..., '-m', 'coverage', 'run', '-p', '...', '--source=multicast', 'test.py']
282+
Testcase 3: Function should modify arguments when 'coverage run' is in the first argument.
283+
A.) NOT missing 'run'
284+
285+
>>> checkCovCommand(["coverage run", "script.py"]) #doctest: +ELLIPSIS
286+
['...', 'run', '-p', '--context=Integration', '--source=multicast', 'script.py']
287+
288+
Testcase 4: Function should handle coverage command with full path.
289+
290+
>>> checkCovCommand(["/usr/bin/coverage", "test.py"]) #doctest: +ELLIPSIS
291+
['...', 'run', '-p', '--context=Integration', '--source=multicast', 'test.py']
292+
293+
Testcase 5: Function should handle coverage invoked via sys.executable.
294+
295+
>>> import sys
296+
>>> test_fixture = [str("{} -m coverage run").format(sys.executable), "test.py"]
297+
>>> context.checkCovCommand(test_fixture) #doctest: +ELLIPSIS
298+
[..., '-m', 'coverage', 'run', '-p', '...', '--source=multicast', 'test.py']
281299
282300
283301
"""
@@ -318,21 +336,50 @@ def checkStrOrByte(theInput):
318336
Returns:
319337
str: If the input is already a string or can be decoded to UTF-8.
320338
bytes: If the input is bytes and cannot be decoded to UTF-8.
321-
None: If the input is None.
322-
323-
Examples:
324-
>>> checkStrOrByte("Hello")
325-
'Hello'
326-
>>> checkStrOrByte(b"Hello")
327-
'Hello'
328-
>>> checkStrOrByte(b'\\xff\\xfe') # Non-UTF-8 bytes
329-
b'\xff\xfe'
330-
>>> checkStrOrByte(None) is None
331-
True
332-
>>> checkStrOrByte("")
333-
''
334-
>>> checkStrOrByte(b"")
335-
''
339+
None: If the input is None.
340+
341+
Meta Testing:
342+
343+
First setup test fixtures by importing test context.
344+
345+
>>> import tests.context as _context
346+
>>>
347+
348+
Testcase 1: Input is a string.
349+
350+
>>> _context.checkStrOrByte("Hello")
351+
'Hello'
352+
>>>
353+
354+
Testcase 2: Input is UTF-8 decodable bytes.
355+
356+
>>> _context.checkStrOrByte(b"Hello")
357+
'Hello'
358+
>>>
359+
360+
Testcase 3: Input is bytes that are not UTF-8 decodable.
361+
362+
>>> _context.checkStrOrByte(b'\\xff\\xfe')
363+
b'\xff\xfe'
364+
>>>
365+
366+
Testcase 4: Input is None.
367+
368+
>>> _context.checkStrOrByte(None) is None
369+
True
370+
>>>
371+
372+
Testcase 5: Input is an empty string.
373+
374+
>>> _context.checkStrOrByte("")
375+
''
376+
>>>
377+
378+
Testcase 6: Input is empty bytes.
379+
380+
>>> _context.checkStrOrByte(b"")
381+
''
382+
>>>
336383
337384
338385
"""
@@ -367,23 +414,37 @@ def checkPythonCommand(args, stderr=None):
367414
Raises:
368415
subprocess.CalledProcessError: If the command returns a non-zero exit status.
369416
370-
Examples:
371-
>>> test_fixture_1 = [str(sys.executable), '-c', 'print("Hello, World!")']
372-
>>> checkPythonCommand(test_fixture_1)
373-
'Hello, World!\\n'
417+
Meta Testing:
374418
375-
>>> import subprocess
376-
>>> test_args_2 = [str(sys.executable), '-c', 'import sys; print("Error", file=sys.stderr)']
377-
>>> checkPythonCommand(test_args_2, stderr=subprocess.STDOUT)
378-
'Error\\n'
419+
First setup test fixtures by importing test context.
420+
421+
>>> import tests.context
422+
>>>
379423
380-
>>> test_fixture_e = [str(sys.executable), '-c', 'raise ValueError("Test error")']
381-
>>> checkPythonCommand(test_fixture_e, stderr=subprocess.STDOUT) #doctest: +ELLIPSIS
382-
'Traceback (most recent call last):\\n...ValueError...'
424+
Testcase 1: Function should have an output when provided valid arguments.
383425
384-
>>> test_fixture_s = [str(sys.executable), '-c', 'print(b"Bytes output")']
385-
>>> isinstance(checkPythonCommand(test_fixture_s, stderr=subprocess.STDOUT), str)
386-
True
426+
>>> test_fixture_1 = [str(sys.executable), '-c', 'print("Hello, World!")']
427+
>>> tests.context.checkPythonCommand(test_fixture_1)
428+
'Hello, World!\\n'
429+
430+
Testcase 2: Function should capture stderr when specified.
431+
432+
>>> import subprocess
433+
>>> test_args_2 = [str(sys.executable), '-c', 'import sys; print("Error", file=sys.stderr)']
434+
>>> tests.context.checkPythonCommand(test_args_2, stderr=subprocess.STDOUT)
435+
'Error\\n'
436+
437+
Testcase 3: Function should handle exceptions and return output.
438+
439+
>>> test_fixture_e = [str(sys.executable), '-c', 'raise ValueError("Test error")']
440+
>>> tests.context.checkPythonCommand(test_fixture_e, stderr=subprocess.STDOUT) #doctest: +ELLIPSIS
441+
'Traceback (most recent call last):\\n...ValueError...'
442+
443+
Testcase 4: Function should return the output as a string.
444+
445+
>>> test_fixture_s = [str(sys.executable), '-c', 'print(b"Bytes output")']
446+
>>> isinstance(tests.context.checkPythonCommand(test_fixture_s, stderr=subprocess.STDOUT), str)
447+
True
387448
388449
389450
"""

0 commit comments

Comments
 (0)