Skip to content

Commit

Permalink
Created test cases and expanded on starter code
Browse files Browse the repository at this point in the history
  • Loading branch information
digehode committed Jul 17, 2020
1 parent ebadf78 commit 81f2859
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -15,4 +18,8 @@ else
endif


test: FORCE venvcheck
py.test -v tests/


FORCE:
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 changes: 49 additions & 2 deletions src/transcoder.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -15,11 +49,24 @@ def main():
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
49 changes: 49 additions & 0 deletions tests/test_transcoder.py
Original file line number Diff line number Diff line change
@@ -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.