|
1
|
|
|
import sys |
|
2
|
|
|
sys.path.append('..') |
|
3
|
|
|
|
|
4
|
|
|
from hamcrest.core.base_matcher import BaseMatcher |
|
5
|
|
|
from hamcrest.core.helpers.hasmethod import hasmethod |
|
6
|
|
|
|
|
7
|
|
|
from hamcrest import * |
|
8
|
|
|
import unittest |
|
9
|
|
|
import datetime |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class IsGivenDayOfWeek(BaseMatcher): |
|
13
|
|
|
"""Matches dates that fall on a given day of the week.""" |
|
14
|
|
|
|
|
15
|
|
|
def __init__(self, day): |
|
16
|
|
|
self.day = day # Monday is 0, Sunday is 6 |
|
17
|
|
|
|
|
18
|
|
|
def _matches(self, item): |
|
19
|
|
|
"""Test whether item matches.""" |
|
20
|
|
|
if not hasmethod(item, 'weekday'): |
|
21
|
|
|
return False |
|
22
|
|
|
return item.weekday() == self.day |
|
23
|
|
|
|
|
24
|
|
|
def describe_to(self, description): |
|
25
|
|
|
"""Describe the matcher.""" |
|
26
|
|
|
day_as_string = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', |
|
27
|
|
|
'Friday', 'Saturday', 'Sunday'] |
|
28
|
|
|
description.append_text('calendar date falling on ') \ |
|
29
|
|
|
.append_text(day_as_string[self.day]) |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
def on_a_saturday(): |
|
33
|
|
|
"""Factory function to generate Saturday matcher.""" |
|
34
|
|
|
return IsGivenDayOfWeek(5) |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
class SampleTest(unittest.TestCase): |
|
38
|
|
|
def testDateIsOnASaturday(self): |
|
39
|
|
|
"""Example of successful match.""" |
|
40
|
|
|
d = datetime.date(2008, 04, 26) |
|
41
|
|
|
assert_that(d, is_(on_a_saturday())) |
|
42
|
|
|
|
|
43
|
|
|
def testFailsWithMismatchedDate(self): |
|
44
|
|
|
"""Example of what happens with date that doesn't match.""" |
|
45
|
|
|
d = datetime.date(2008, 04, 06) |
|
46
|
|
|
assert_that(d, is_(on_a_saturday())) |
|
47
|
|
|
|
|
48
|
|
|
def testFailsWithNonDate(self): |
|
49
|
|
|
"""Example of what happens with object that isn't a date.""" |
|
50
|
|
|
d = 'oops' |
|
51
|
|
|
assert_that(d, is_(on_a_saturday())) |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
if __name__ == '__main__': |
|
55
|
|
|
unittest.main() |
|
56
|
|
|
|