Skip to content

Commit

Permalink
perform a test driven development to read file_name from an ascii fil…
Browse files Browse the repository at this point in the history
…e in python
  • Loading branch information
yusufjimoh committed Jun 3, 2024
1 parent c1083da commit 078d611
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion exercises-python/template/ex01_basics/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#!/usr/bin/env python3

def read_ascii_file(param):
try:
f = open(param, 'r')
return f.read()
except FileNotFoundError:
print("Oops! That was not a valid file. Try again...")

def main():
pass
read_ascii_file("file.txt")


if __name__ == "__main__":
main()


20 changes: 20 additions & 0 deletions exercises-python/template/ex01_basics/test_wordcount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest
from unittest.mock import mock_open, patch
from main import *


class WordCountTest(unittest.TestCase):

def setUp(self):
# Create a temporary ASCII file for testing
self.filename = 'file.txt'

@patch('builtins.open', new_callable=mock_open, read_data='This is a test data')
def test_get_file_name_from_arg(self, mock_file):
content = read_ascii_file(self.filename)
expected_content = 'This is a test data'
self.assertEqual(content, expected_content)
mock_file.assert_called_once_with(self.filename, 'r')



0 comments on commit 078d611

Please sign in to comment.