Skip to content
Permalink
Browse files
Created test cases and expanded on starter code
  • Loading branch information
digehode committed Jul 17, 2020
1 parent ebadf78 commit 81f285985e4415a8248699eb16ea14ca3cb3c5a1
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
@@ -7,6 +7,9 @@ prereqs: venvcheck FORCE
venv: FORCE
python3 -m venv venv

docserve:
pydoc -p 5555

venvcheck:
ifeq ($(INVENV),)
$(error You should only run this from within the venv. Use '. ./venv/bin/activate')
@@ -15,4 +18,8 @@ else
endif


test: FORCE venvcheck
py.test -v tests/


FORCE:
@@ -116,6 +116,16 @@ it (including yourself) and docstrings for people that have to make
use of it without viewing the internals. That could be users, or
people calling on your classes and functions in their own code.

### Prettier documentation

To view the documentation in your browser, you have some options. The
basic methods are: generate HTML versions of your documentation and
store it to be viewed in your browser *or* use the built-in web server
that `pydoc` contains to generate your documentation on the fly and
point your browser at it.

TODO: this bit

## Virtualenv

Python is a very popular programming language. This means there are
@@ -187,3 +197,19 @@ This is OK to test that you at least have your environment set up, but
it's not really enough to test the software is working perfectly.

TODO: write about testing.


# Completing Transcoder

TODO:

- asASCII (basic)
- User input (intermediate)
- wrapping (advanced)

# TODO - things left to do before students get access

- Clear up TODO items
- Put proper pydoc generation in and fill in all docstrings
- Write bit about cloning repo
- Write tests for asASCII (but not give the function)
51 src/transcoder.py 100644 → 100755
@@ -1,4 +1,38 @@
#!python
import colored

def asHex(data):
#Start with an empty string
output=""

#Now for each character...
for c in data:
#Add the hex representation of the character to the string
#The ord function returns the ASCII value of a character
#The hex function returns the hexadecimal representation of a number
output+=hex(ord(c))+" "

return output.strip() #Take off any spare spaces at the start/end

def asOctal(data):
output=""

for c in data:
output+=" "+oct(ord(c))

return output.strip()

def asBinary(data):
output=""

for c in data:
output+=" "+bin(ord(c))

return output.strip()


def colourise(text, colour):
return colored.fg(colour)+text+colored.attr('reset')


def main():
@@ -15,11 +49,24 @@ three in a row to signal the end. You can also go across multiple
lines, like I have here.
"""
print("Hello World")

print(colourise("Transcoder V0.1 pre-alpha","#FF0000"))
inputData="Any text could go here..."
print(f"Input: {inputData}")
hexData=asHex(inputData)
octData=asOctal(inputData)
binData=asBinary(inputData)
print(f"Hex : {hexData}")
print(f"Octal : {octData}")
print(f"Binary: {binData}")




if __name__=="__main__":
main()



# JS: You can ignore the stuff below. It's just for my spell-checker.
# LocalWords: Transcoder pre asHex JS inputData hexData asOctal
# LocalWords: octData binData asBinary
@@ -0,0 +1,49 @@
import pytest
import sys
sys.path.append("./src/")
import transcoder






def test_asHex():
""" Test the 'asHex' function """

#First a simple string
assert transcoder.asHex("ABC")=="0x41 0x42 0x43"
#An empty string
assert transcoder.asHex("")==""
#The highest byte value
assert transcoder.asHex(chr(255))=="0xff"
#Lowest byte value
assert transcoder.asHex(chr(0))=="0x0"




def test_asOctal():
""" Test the 'asOctal' function """

#First a simple string
assert transcoder.asOctal("ABC")=="0o101 0o102 0o103"
#An empty string
assert transcoder.asOctal("")==""
#The highest byte value
assert transcoder.asOctal(chr(255))=="0o377"
#Lowest byte value
assert transcoder.asOctal(chr(0))=="0o0"


def test_asBinary():
""" Test the 'asBinary' function """

#First a simple string
assert transcoder.asBinary("ABC")=="0b1000001 0b1000010 0b1000011"
#An empty string
assert transcoder.asBinary("")==""
#The highest byte value
assert transcoder.asBinary(chr(255))=="0b11111111"
#Lowest byte value
assert transcoder.asBinary(chr(0))=="0b0"

0 comments on commit 81f2859

Please sign in to comment.