[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#762951: debsources: increase/maximize test coverage



Attached is a patch adding tests for debsources.filetype. They were all
created from Matthieu's assertions in __main__.

I'm not sure whether I was right to create a new file and a new attribute
('filetype') for these tests; I'll amend the commit according to your
comments.

filetype.py's coverage went from 80% to 96%.

The patch is also available here:
https://github.com/clemux/debsources/commit/a946736342b945da393216879f5b27a2baa499ca

>From a946736342b945da393216879f5b27a2baa499ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Schreiner?= <clement@mux.me>
Date: Wed, 1 Apr 2015 16:57:22 +0200
Subject: [PATCH] filetype: move assertions to test_filetype

Moved the assertions in debsources.filetype.__main__ into a new test
file, tests_filetype.

Code coverage for debsources.filetype is now 96%.
---
 debsources/filetype.py            | 17 --------
 debsources/tests/test_filetype.py | 84 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 17 deletions(-)
 create mode 100644 debsources/tests/test_filetype.py

diff --git a/debsources/filetype.py b/debsources/filetype.py
index 0e55843..938b318 100644
--- a/debsources/filetype.py
+++ b/debsources/filetype.py
@@ -237,20 +237,3 @@ def is_text_file(mimetype):
         if text_mime in mimetype:
             return True
     return False
-
-if __name__ == "__main__":
-    assert get_filetype("foo", "#!/usr/bin/env python") == PYTHON
-    assert get_filetype("foo", "#!/usr/bin/python") == PYTHON
-    assert get_filetype("foo.py", "foobar") == PYTHON
-
-    assert get_filetype("foo", "<html><head>") == HTML
-    assert get_filetype("foo", "<?xml>") == XML
-    assert get_filetype("foo", "<?php echo('hello') ?>") == PHP
-
-    assert get_filetype("foo", "#!/usr/bin/env ruby") == RUBY
-    assert get_filetype("foo.rb", "foobar") == RUBY
-
-    assert get_highlightjs_language("foo.html", "foobar") == "django"
-    assert get_highlightjs_language("foo", "#!/bin/perl\n") == "perl"
-
-    assert get_filetype("foo", "foobar") is None
diff --git a/debsources/tests/test_filetype.py b/debsources/tests/test_filetype.py
new file mode 100644
index 0000000..c49922e
--- /dev/null
+++ b/debsources/tests/test_filetype.py
@@ -0,0 +1,84 @@
+# Copyright (C) 2013  Matthieu Caneill <matthieu.caneill@gmail.com>
+# Copyright (C) 2015  Clement Schreiner <clement@mux.me>
+#
+# This file is part of Debsources.
+#
+# Debsources is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+from __future__ import absolute_import
+
+import unittest
+
+from nose.tools import istest
+from nose.plugins.attrib import attr
+
+from debsources.filetype import get_filetype, get_highlightjs_language
+from debsources.filetype import HTML, PHP, PYTHON, RUBY, XML
+
+
+@attr('filetype')
+class FiletypeTests(unittest.TestCase):
+    """ Unit tests for debsources.filetype """
+
+    @istest
+    def pythonShebang(self):
+        self.assertEqual(get_filetype('foo', '#!/usr/bin/python'), PYTHON)
+
+    @istest
+    def envPythonShebang(self):
+        self.assertEqual(get_filetype('foo', '#!/usr/bin/env python'), PYTHON)
+
+    @istest
+    def envRubyShebang(self):
+        self.assertEqual(get_filetype('foo', '#!/usr/bin/env ruby'), RUBY)
+
+    @istest
+    def testUnknownShebang(self):
+        self.assertIsNone(get_filetype('foo', '#!/usr/bin/foobar'))
+
+    @istest
+    def pythonExtension(self):
+        self.assertEqual(get_filetype('foo.py', 'foobar'), PYTHON)
+
+    @istest
+    def rubyExtension(self):
+        self.assertEqual(get_filetype('foo.rb', 'foobar'), RUBY)
+
+    @istest
+    def unknownExtension(self):
+        self.assertIsNone(get_filetype('foo.bar', 'foobar'))
+
+    @istest
+    def htmlTag(self):
+        self.assertEqual(get_filetype('foo', '<html><head>'), HTML)
+
+    @istest
+    def xmlTag(self):
+        self.assertEqual(get_filetype('foo', '<?xml>'), XML)
+
+    @istest
+    def phpTag(self):
+        self.assertEqual(get_filetype('foo', "<?php echo('hello') ?>"), PHP)
+
+    @istest
+    def hilightjsLanguageDjango(self):
+        self.assertEqual(get_highlightjs_language("foo.html", "foobar", None),
+                         "django")
+
+    @istest
+    def hilightjsLanguagePerl(self):
+        self.assertEqual(get_highlightjs_language("foo",
+                                                  "#!/bin/perl\n",
+                                                  None),
+                         "perl")
-- 
2.1.4


Reply to: