forked from chaddcw/Python-CI-Testing
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocTestExample.py
More file actions
99 lines (68 loc) · 1.93 KB
/
docTestExample.py
File metadata and controls
99 lines (68 loc) · 1.93 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
#!/usr/bin/python3
################################
# File Name: docTestExample.py
# Author: Chadd Williams
# Date: 11/7/2014
# Class: CS 360
# Assignment: Demonstrate Python with Travis-CI
# Purpose: Demonstrate DocTest with Travs-CI
################################
# Run these test cases via:
# chadd@bart:~> python3 -m doctest -v DocTestExample.py
# Simple int example
def testIntAddition(left: int, right: int)->"sum of left and right":
"""Test the + operator for ints
Test a simple two in example
>>> testIntAddition(1,2)
3
Use the same int twice, no problem
>>> testIntAddition(2,2)
4
Try to add a list to a set! TypeError!
Only show the first and last lines for the error message
The ... is a wild card
The ... is tabbed in TWICE
>>> testIntAddition([2], {3})
Traceback (most recent call last):
...
TypeError: can only concatenate list (not "set") to list
"""
return left + right
# Simple List example
def printFirstItemInList(theList: "list of items"):
""" Retrieve the first item from the list and print it
Test a list of ints
>>> printFirstItemInList( [ 0, 1, 2] )
0
Test a list of strings
>>> printFirstItemInList( ["CS 360", "CS 150", "CS 300" ] )
CS 360
Generate a list comphrension
>>> printFirstItemInList( [ x+1 for x in range(10) ] )
1
Work with a list of tuples
>>> printFirstItemInList( [ (x,x+1, x-1) for x in range(10) ] )
(0, 1, -1)
"""
item = theList[0]
print(item)
# Test Output of print and return value
# that \ at the end of the line allows you to continue
# the same statement on the next line!
def printAndReturnSum(*args: "variadic param")\
->"return sum of ints provided as parameters":
""" Print and return the sum of the args that are ints
>>> printAndReturnSum(1,2,3)
6
6
>>> printAndReturnSum("bob", 1)
1
1
"""
total = 0
for x in args:
# type check at run time!
if type(x) is int :
total += x
print(total)
return total