|
| 1 | +import unittest |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | +from extliner.main import LineCounter |
| 5 | + |
| 6 | + |
| 7 | +class TestLineCounter(unittest.TestCase): |
| 8 | + |
| 9 | + # inbuilt setup and teardown methods to create a temporary directory for testing |
| 10 | + def setUp(self): |
| 11 | + self.temp_dir = tempfile.TemporaryDirectory() |
| 12 | + self.base_path = Path(self.temp_dir.name) |
| 13 | + |
| 14 | + # it will call the cleanup method of TemporaryDirectory to delete the directory after tests |
| 15 | + def tearDown(self): |
| 16 | + self.temp_dir.cleanup() |
| 17 | + |
| 18 | + def create_file(self, relative_path: str, content: str): |
| 19 | + file_path = self.base_path / relative_path |
| 20 | + file_path.parent.mkdir(parents=True, exist_ok=True) |
| 21 | + file_path.write_text(content, encoding="utf-8") |
| 22 | + |
| 23 | + def test_single_file_with_and_without_spaces(self): |
| 24 | + self.create_file("hello.py", "print('Hello')\n\nprint('World')\n") |
| 25 | + |
| 26 | + counter = LineCounter() |
| 27 | + result = counter.count_lines(self.base_path) |
| 28 | + |
| 29 | + self.assertIn(".py", result) |
| 30 | + self.assertEqual(result[".py"]["with_spaces"], 3) |
| 31 | + self.assertEqual(result[".py"]["without_spaces"], 2) |
| 32 | + |
| 33 | + def test_multiple_extensions(self): |
| 34 | + self.create_file("a.py", "print('a')\n\n") |
| 35 | + self.create_file("b.js", "console.log('b');\n") |
| 36 | + self.create_file("c.txt", "\n\ntext\n\n") |
| 37 | + |
| 38 | + counter = LineCounter() |
| 39 | + result = counter.count_lines(self.base_path) |
| 40 | + |
| 41 | + self.assertEqual(result[".py"]["with_spaces"], 2) |
| 42 | + self.assertEqual(result[".py"]["without_spaces"], 1) |
| 43 | + self.assertEqual(result[".js"]["with_spaces"], 1) |
| 44 | + self.assertEqual(result[".js"]["without_spaces"], 1) |
| 45 | + self.assertEqual(result[".txt"]["with_spaces"], 4) |
| 46 | + self.assertEqual(result[".txt"]["without_spaces"], 1) |
| 47 | + |
| 48 | + def test_ignore_extensions(self): |
| 49 | + self.create_file("a.py", "code\ncode\n") |
| 50 | + self.create_file("b.txt", "text\ntext\n") |
| 51 | + |
| 52 | + counter = LineCounter(ignore_extensions=[".txt"]) |
| 53 | + result = counter.count_lines(self.base_path) |
| 54 | + |
| 55 | + self.assertIn(".py", result) |
| 56 | + self.assertNotIn(".txt", result) |
| 57 | + |
| 58 | + def test_no_extension_file(self): |
| 59 | + self.create_file("README", "line1\nline2\n") |
| 60 | + |
| 61 | + counter = LineCounter() |
| 62 | + result = counter.count_lines(self.base_path) |
| 63 | + |
| 64 | + self.assertIn("NO_EXT", result) |
| 65 | + self.assertEqual(result["NO_EXT"]["with_spaces"], 2) |
| 66 | + self.assertEqual(result["NO_EXT"]["without_spaces"], 2) |
| 67 | + |
| 68 | + def test_to_json_format(self): |
| 69 | + self.create_file("sample.py", "x = 1\n\ny = 2\n") |
| 70 | + |
| 71 | + counter = LineCounter() |
| 72 | + data = counter.count_lines(self.base_path) |
| 73 | + json_str = counter.to_json(data) |
| 74 | + |
| 75 | + self.assertTrue(json_str.startswith("{")) |
| 76 | + self.assertIn('"with_spaces":', json_str) |
| 77 | + self.assertIn('"without_spaces":', json_str) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + unittest.main() |
0 commit comments