-
Notifications
You must be signed in to change notification settings - Fork 0
/
hb2post_tests.py
108 lines (92 loc) · 4.35 KB
/
hb2post_tests.py
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
# -*- mode: Python; coding: utf-8 -*-
# Automated tests for hb2post Python module, by Luis Sergio Oliveira.
# Copyright (c) 2010-2011 Contributors - see below
# All rights reserved.
# The use and distribution terms for this software are covered by the
# Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
# which can be found in the file epl-v10.html at the root of this distribution.
# By using this software in any fashion, you are agreeing to be bound by
# the terms of this license.
# You must not remove this notice, or any other, from this software.
# Contributors:
# - Luis Sergio Oliveira (euluis)
from datetime import date
import unittest
import re
from hb2post import *
class CadernosOptionsTest(unittest.TestCase):
def options_hbfilenames_match_programacao_hb(self, options):
self.assertTrue(5 <= len(options.hbfilenames()))
reStr = '/cadernos/programacao/(?:parte|ficheiro){1}\d{2}.html'
reCompiled = re.compile(reStr)
for hbfn in options.hbfilenames():
assert re.search(reCompiled, hbfn),\
"\"" + hbfn + "\" doesn't match the regular expression \"" + reStr + "\"."
def assertDateEquals(self, expectedDate, actualDate):
assert expectedDate == actualDate, \
'Expected ' + str(expectedDate) + ', but was ' + str(actualDate) + '.'
def test_hbfilenames_default(self):
options = CadernosOptions([])
self.options_hbfilenames_match_programacao_hb(options)
self.assertDateEquals(date.today(), options.startdate())
self.assertDateEquals(date.today(), options.enddate())
def test_hbfilenames_programacao_start_n_end_date_defined(self):
options = CadernosOptions(['--handbook', 'programacao', '-s2005-05-10', '-e2005-10-31'])
self.options_hbfilenames_match_programacao_hb(options)
self.assertDateEquals(date(2005,5,10), options.startdate())
self.assertDateEquals(date(2005,10,31), options.enddate())
def options_hbfilenames_match_idiota_hb(self, options):
self.assertTrue(2 <= len(options.hbfilenames()))
reStr = '/cadernos/idiota/ficheiro0[12].html'
reCompiled = re.compile(reStr)
for hbfn in options.hbfilenames():
assert re.search(reCompiled, hbfn),\
"\"" + hbfn + "\" doesn't match the regular expression \"" + reStr + "\"."
def test_hbfilenames_idiota_default(self):
options = CadernosOptions(['--handbook', 'idiota'])
self.options_hbfilenames_match_idiota_hb(options)
self.assertDateEquals(date.today(), options.startdate())
self.assertDateEquals(date.today(), options.enddate())
class HbFileAutoTest(unittest.TestCase):
def setUp(self):
self.non_existing_file = 'non_existing_file.html'
self.non_existing_file_error = IOError("IOError: No such file or directory: '"\
+ self.non_existing_file + "'")
class HbFileAutoStub(HbFileAuto):
def __init__(self, test, files):
self.test = test
self.openedfiles = []
self.hbfiles = []
HbFileAuto.__init__(self, files)
def openFile(self, fn):
if self.test.non_existing_file == fn:
raise self.test.non_existing_file_error
self.openedfiles.append(fn)
return fn
def closeFile(self, f):
self.openedfiles.remove(f)
def createHbFile(self, fn):
assert fn
self.hbfiles.append(fn)
return fn
def test_process_hbfiles_happy_path(self):
files = ['file1.html', 'file2.html']
hbfAuto = HbFileAutoTest.HbFileAutoStub(self, files)
self.assertEquals([], hbfAuto.openedfiles)
self.assertEquals(files, hbfAuto.hbfiles)
def test_process_hbfiles_non_existing_file(self):
files = ['file1.html', self.non_existing_file]
try:
hbfAuto = HbFileAutoTest.HbFileAutoStub(self, files)
self.fail("Expected " + str(self.non_existing_file_error))
except IOError as e:
pass # expected
def test_create_HbFileAuto_with_no_filenames_fails(self):
files = []
try:
HbFileAuto(files)
self.fail("Expected ValueError: filenames must have at least one element.")
except ValueError:
pass
if __name__ == "__main__":
unittest.main()