Update project

This commit is contained in:
Giles Gaskell 2017-12-04 16:11:24 +11:00
parent 451a3785a2
commit f4d03025e5
4 changed files with 105 additions and 9 deletions

35
jenkins/Jenkinsfile vendored Normal file
View file

@ -0,0 +1,35 @@
pipeline {
agent none
stages {
stage('Build') {
agent {
docker {
image 'python:3.4-slim'
}
}
steps {
sh 'python -m py_compile sources/add2vals.py'
}
}
stage('Test') {
agent {
docker {
image 'python:3.4-slim'
}
}
steps {
sh 'python sources/test_calc.py'
}
}
stage('Deliver') {
agent {
docker {
image 'cdrx/pyinstaller-linux'
}
}
steps {
sh 'pyinstaller --onefile sources/add2vals.py'
}
}
}
}

23
sources/add2vals.py Normal file
View file

@ -0,0 +1,23 @@
'''
A simple command line tool that takes 2 values and adds them together using
the calc.py library's 'add2' function.
'''
import sys
import calc
argnumbers = len(sys.argv) - 1
if argnumbers == 2 :
print("")
print("The result is " + str(calc.add2(str(sys.argv[1]), str(sys.argv[2]))))
print("")
sys.exit(0)
if argnumbers != 2 :
print("")
print("Usage: add X Y, where X and Y are individual values.")
print(" If uncompiled, usage is 'python add2vals.py X Y'.")
print(" (You entered " + str(argnumbers) + " value/s.)")
print("")
sys.exit(1)

View file

@ -1,5 +1,28 @@
''' '''
A simple addition function. The 'calc' library contains the 'add2' function that takes 2 values and adds
them together. If either value is a string (or both of them are) 'add2' ensures
they are both strings, thereby resulting in a concatenated result.
NOTE: If a value submitted to the 'add2' function is a float, it must be done so
in quotes (i.e. as a string).
''' '''
def add(a, b):
return a + b # If 'value' is not an integer, convert it to a float and failing that, a string.
def conv(value):
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError:
return str(value)
# The 'add2' function itself
def add2(arg1, arg2):
# Convert 'arg1' and 'arg2' to their appropriate types
arg1conv = conv(arg1)
arg2conv = conv(arg2)
# If either 'arg1' or 'arg2' is a string, ensure they're both strings.
if isinstance(arg1conv, str) or isinstance(arg2conv, str):
arg1conv = str(arg1conv)
arg2conv = str(arg2conv)
return arg1conv + arg2conv

View file

@ -1,7 +1,7 @@
import calc
import unittest import unittest
import calc
class TestAdd(unittest.TestCase): class TestCalc(unittest.TestCase):
""" """
Test the add function from the calc library Test the add function from the calc library
""" """
@ -10,24 +10,39 @@ class TestAdd(unittest.TestCase):
""" """
Test that the addition of two integers returns the correct total Test that the addition of two integers returns the correct total
""" """
result = calc.add(1, 2) result = calc.add2(1, 2)
self.assertEqual(result, 3) self.assertEqual(result, 3)
def test_add_floats(self): def test_add_floats(self):
""" """
Test that the addition of two floats returns the correct result Test that the addition of two floats returns the correct result
""" """
result = calc.add(10.5, 2) result = calc.add2('10.5', 2)
self.assertEqual(result, 12.5) self.assertEqual(result, 12.5)
def test_add_strings(self): def test_add_strings(self):
""" """
Test the addition of two strings returns the two string as one Test the addition of two strings returns the two strings as one
concatenated string concatenated string
""" """
result = calc.add('abc', 'def') result = calc.add2('abc', 'def')
self.assertEqual(result, 'abcdef') self.assertEqual(result, 'abcdef')
def test_add_string_and_integer(self):
"""
Test the addition of a string and an integer returns them as one
concatenated string (in which the integer is converted to a string)
"""
result = calc.add2('abc', 3)
self.assertEqual(result, 'abc3')
def test_add_string_and_number(self):
"""
Test the addition of a string and a float returns them as one
concatenated string (in which the float is converted to a string)
"""
result = calc.add2('abc', '5.5')
self.assertEqual(result, 'abc5.5')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()