File size: 9,064 Bytes
d086025 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
--- /dev/null
+++ b/tests/__init__.py
@@ -0,0 +1,5 @@
+# -*- coding: utf-8 -*-
+"""
+Declare this directory a package so that 'python setup.py test' finds
+the unittests here.
+"""
diff --git a/tests/test_File.py b/tests/test_File.py
new file mode 100644
--- /dev/null
+++ b/tests/test_File.py
@@ -0,0 +1,91 @@
+# -*- coding: utf-8 -*-
+"""
+Test File Class
+"""
+
+import unittest
+# import code to be tested here (add to your python path!)
+import ppf.jabref as dut
+
+
+class Test_File(unittest.TestCase):
+ """Test File class."""
+
+ def test_File_init(self):
+ """Test File creation."""
+ link1 = dut.Link(name='x', path='/bla/di/bla', filetype='DOCX')
+ link2 = dut.Link(name='whatever', path=r'..\what\ever', filetype='PDF')
+
+ list_of_links = [link1, link2]
+ file = dut.File(list_of_links)
+
+ self.assertTrue(file._links is list_of_links)
+
+ def test_File_init_rejects_non_links(self):
+ """Test File creation rejects non-Link entries."""
+ with self.assertRaises(TypeError):
+ dut.File([dut.Link('x', 'p', 'PDF'), 'not-a-link'])
+
+ def test_Link_from_string(self):
+ """Test Link creation from string."""
+ string = 'Pref:Bks\\Chpt0.pdf:PDF;Chapter 1:Bks\\Chpt1.pdf:PDF'
+ file = dut.File.from_string(string)
+
+ self.assertTrue(file._links[0].name == 'Pref')
+ self.assertTrue(file._links[0].path == 'Bks\\Chpt0.pdf')
+ self.assertTrue(file._links[0].filetype == 'PDF')
+ self.assertTrue(file._links[1].name == 'Chapter 1')
+ self.assertTrue(file._links[1].path == 'Bks\\Chpt1.pdf')
+ self.assertTrue(file._links[1].filetype == 'PDF')
+
+ def test_File_repr(self):
+ """Test File string representation."""
+ link1 = dut.Link(name='a', path='b:c', filetype='d')
+ link2 = dut.Link(name='e', path='f', filetype='g;h')
+ file = dut.File([link1, link2])
+ self.assertTrue(str(file) == r'a:b\:c:d;e:f:g\;h')
+
+
+class Test_File_List_Behavior(unittest.TestCase):
+ """Test list operations on File"""
+
+ def setUp(self):
+ self.list_of_links = [dut.Link('A', './docs/A.docx', 'DOCX'),
+ dut.Link('B', 'pdfs/B.pdf', 'PDF')]
+ self.file = dut.File(self.list_of_links)
+
+ def test_indexing(self):
+ self.assertTrue(self.file[0] == self.list_of_links[0])
+
+ def test_len(self):
+ self.assertTrue(len(self.file) == 2)
+
+ def test_append(self):
+ new_link = dut.Link('C', r'.\presentations\C.pptx', 'PPTX')
+ self.file.append(new_link)
+ self.assertTrue(len(self.file) == 3)
+
+ def test_append_rejects_non_link(self):
+ with self.assertRaises(TypeError):
+ self.file.append('not-a-link')
+
+ def test_setitem_rejects_non_link(self):
+ with self.assertRaises(TypeError):
+ self.file[0] = 'not-a-link'
+
+ def test_setitem(self):
+ new_link = dut.Link('C', 'docs/C.pdf', 'PDF')
+ self.file[1] = new_link
+ self.assertTrue(self.file[1] is new_link)
+
+ def test_remove(self):
+ original_link = self.list_of_links[0]
+ popped_link = self.file.pop(0)
+ self.assertTrue(len(self.file) == 1)
+ self.assertTrue(popped_link is original_link)
+
+
+if __name__ == '__main__':
+ # This enables running the unit tests by running this script which is
+ # much more convenient than 'python setup.py test' while developing tests.
+ unittest.main()
diff --git a/tests/test_Link.py b/tests/test_Link.py
new file mode 100644
--- /dev/null
+++ b/tests/test_Link.py
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+"""
+Test Link Class
+"""
+
+import unittest
+# import code to be tested here (add to your python path!)
+import ppf.jabref as dut
+
+
+class Test_Link(unittest.TestCase):
+ """Test Link class."""
+
+ def test_Link_init(self):
+ """Test Link creation."""
+ link = dut.Link(name='Preface',
+ path='/There/is/a/preface/somewhere.pdf',
+ filetype='PDF')
+ self.assertTrue(link.name == 'Preface')
+ self.assertTrue(link.path == '/There/is/a/preface/somewhere.pdf')
+ self.assertTrue(link.filetype == 'PDF')
+
+ def test_Link_from_string(self):
+ """Test Link creation from string."""
+ string = 'Preface:/There/is/a/preface/somewhere.pdf:PDF'
+ link = dut.Link.from_string(string)
+ self.assertTrue(link.name == 'Preface')
+ self.assertTrue(link.path == '/There/is/a/preface/somewhere.pdf')
+ self.assertTrue(link.filetype == 'PDF')
+
+ def test_escape(self):
+ """Test Link.escape() for special characters."""
+ self.assertTrue(dut.Link.escape('a:b') == r'a\:b')
+ self.assertTrue(dut.Link.escape('a;b') == r'a\;b')
+ self.assertTrue(dut.Link.escape(r'a\b') == r'a\\b')
+ self.assertTrue(dut.Link.escape(r'a\:b') == r'a\\\:b')
+ self.assertTrue(dut.Link.escape(r'a\;b') == r'a\\\;b')
+ self.assertTrue(dut.Link.escape(r'a\:b\;c') == r'a\\\:b\\\;c')
+
+ def test_repr(self):
+ """Test Link string representation."""
+ link = dut.Link(name='A', path='B:C', filetype='D;E')
+ self.assertTrue(str(link) == r'A:B\:C:D\;E')
+
+
+if __name__ == '__main__':
+ # This enables running the unit tests by running this script which is
+ # much more convenient than 'python setup.py test' while developing tests.
+ unittest.main()
diff --git a/tests/test_citationkeymapping.py b/tests/test_citationkeymapping.py
new file mode 100644
--- /dev/null
+++ b/tests/test_citationkeymapping.py
@@ -0,0 +1,41 @@
+# -*- coding: utf-8 -*-
+"""
+Test citationkey mapping Functions.
+"""
+
+import unittest
+# import code to be tested here (add to your python path!)
+import ppf.jabref as dut
+from functools import reduce
+
+
+class Test_CitationkeyMapping(unittest.TestCase):
+ """Test counter2citationkey() and citationkey2counter functions."""
+
+ def test_counter2citationkey(self):
+ """Test counter2citationkey with known values."""
+ numbers = [0, 26, 26 + 26**2]
+ ckeys_true = ['a', 'aa', 'aaa']
+ ckeys = [dut.counter2citationkey(c) for c in numbers]
+
+ equal = reduce(lambda a, b: a and b,
+ map(lambda p, q: p == q, ckeys_true, ckeys),
+ True)
+ self.assertTrue(equal)
+
+ def test_citationkey2counter(self):
+ """Test citationkey2counter with known values."""
+ numbers_true = [0, 26, 26 + 26**2]
+ ckeys = ['a', 'aa', 'aaa']
+ numbers = [dut.citationkey2counter(c) for c in ckeys]
+
+ equal = reduce(lambda a, b: a and b,
+ map(lambda p, q: p == q, numbers_true, numbers),
+ True)
+ self.assertTrue(equal)
+
+
+if __name__ == '__main__':
+ # This enables running the unit tests by running this script which is
+ # much more convenient than 'python setup.py test' while developing tests.
+ unittest.main()
diff --git a/tests/test_split_by_unescaped_sep.py b/tests/test_split_by_unescaped_sep.py
new file mode 100644
--- /dev/null
+++ b/tests/test_split_by_unescaped_sep.py
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+"""
+Test Link Class
+"""
+
+import unittest
+# import code to be tested here (add to your python path!)
+import ppf.jabref as dut
+
+
+class Test_split_by_unescaped_sep(unittest.TestCase):
+ """Test split_by_unescaped_sep() function."""
+
+ def test_harmless(self):
+ """Test string without escaped chars."""
+ str = 'bla:di:bla'
+ split = dut.split_by_unescaped_sep(str, sep=':')
+ self.assertTrue(split[0] == 'bla')
+ self.assertTrue(split[1] == 'di')
+ self.assertTrue(split[2] == 'bla')
+
+ def test_escaped_colon(self):
+ """Test string with escaped colon."""
+ str = r'bla\:bla:di:bla'
+ split = dut.split_by_unescaped_sep(str, sep=':')
+ self.assertTrue(split[0] == r'bla\:bla')
+ self.assertTrue(split[1] == 'di')
+ self.assertTrue(split[2] == 'bla')
+
+ def test_backslash_before_colon(self):
+ """Test string with backslash before colon."""
+ str = r'bla:di\\:bla'
+ split = dut.split_by_unescaped_sep(str, sep=':')
+ self.assertTrue(split[0] == 'bla')
+ self.assertTrue(split[1] == r'di\\')
+ self.assertTrue(split[2] == 'bla')
+
+ def test_multibackslash(self):
+ """Test string with multiple backslashs."""
+ str = r'bla:di\\\::\\bla'
+ split = dut.split_by_unescaped_sep(str, sep=':')
+ self.assertTrue(split[0] == 'bla')
+ self.assertTrue(split[1] == r'di\\\:')
+ self.assertTrue(split[2] == r'\\bla')
+
+
+if __name__ == '__main__':
+ # This enables running the unit tests by running this script which is
+ # much more convenient than 'python setup.py test' while developing tests.
+ unittest.main()
|