From 87bb42977fd0f6aedcea77280b2c0a201042e825 Mon Sep 17 00:00:00 2001 From: Ayush Parikh Date: Mon, 30 Aug 2021 21:43:50 +0530 Subject: [PATCH 1/2] Create analyze_text.py --- Lib/test/analyze_text.py | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Lib/test/analyze_text.py diff --git a/Lib/test/analyze_text.py b/Lib/test/analyze_text.py new file mode 100644 index 00000000000000..caea3f26fc302d --- /dev/null +++ b/Lib/test/analyze_text.py @@ -0,0 +1,66 @@ +import os +import unittest + + +def analyze_text(filename): + """Calculate the number of lines and characters in a file. + Args: + filename: The name of the file to analyze. + Raises: + IOError: If ``filename`` does not exist or can't be read. + Returns: A tuple where the first element is the number of lines in + the file and the second element is the number of characters. + """ + lines = 0 + chars = 0 + with open(filename, 'r') as f: + for line in f: + lines += 1 + chars += len(line) + return (lines, chars) + + +class TextAnalysisTests(unittest.TestCase): + """Tests for the ``analyze_text()`` function.""" + + def setUp(self): + """Fixture that creates a file for the text methods to use.""" + self.filename = 'text_analysis_test_file.txt' + with open(self.filename, 'w') as f: + f.write('Now we are engaged in a great civil war.\n' + 'testing whether that nation,\n' + 'or any nation so conceived and so dedicated,\n' + 'can long endure.') + + def tearDown(self): + """Fixture that deletes the files used by the test methods.""" + try: + os.remove(self.filename) + except: + pass + + def test_function_runs(self): + """Basic smoke test: does the function run.""" + analyze_text(self.filename) + + def test_line_count(self): + """Check that the line count is correct.""" + self.assertEqual(analyze_text(self.filename)[0], 4) + + def test_character_count(self): + """Check that the character count is correct.""" + self.assertEqual(analyze_text(self.filename)[1], 131) + + def test_no_such_file(self): + """Check the proper exception is thrown for a missing file.""" + with self.assertRaises(IOError): + analyze_text('foobar') + + def test_no_deletion(self): + """Check that the function doesn't delete the input file.""" + analyze_text(self.filename) + self.assertTrue(os.path.exists(self.filename)) + + +if __name__ == '__main__': + unittest.main() From f5918ffeb68ca9892b9181effc3392bc958a3706 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 30 Aug 2021 16:20:00 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Misc/NEWS.d/next/Tests/2021-08-30-16-19-59.bpo-45050.mRsMNG.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2021-08-30-16-19-59.bpo-45050.mRsMNG.rst diff --git a/Misc/NEWS.d/next/Tests/2021-08-30-16-19-59.bpo-45050.mRsMNG.rst b/Misc/NEWS.d/next/Tests/2021-08-30-16-19-59.bpo-45050.mRsMNG.rst new file mode 100644 index 00000000000000..ec3f5400b066f3 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2021-08-30-16-19-59.bpo-45050.mRsMNG.rst @@ -0,0 +1 @@ +created a unittest file(analyze_text.py) for Calculate the number of lines and characters \ No newline at end of file