~jeremychang/linaro-image-tools/android-boottarball

« back to all changes in this revision

Viewing changes to hwpack/tests/test_testing.py

ok, this is a bit crazy:
add a MatchesStructure matcher that is a bit reminscent of destructuring-bind in common lisp
make explicit the attributes which must be compared to consider FetchedPackages equal
use the above two things to make a MatchesPackage matcher

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from testtools import TestCase
 
2
from testtools.matchers import (
 
3
    Equals,
 
4
    NotEquals,)
 
5
from hwpack.testing import (
 
6
    DummyFetchedPackage,
 
7
    MatchesPackage,
 
8
    MatchesStructure,
 
9
    )
 
10
 
 
11
class TestMatchesStructure(TestCase):
 
12
 
 
13
    class SimpleClass:
 
14
        def __init__(self, x):
 
15
            self.x = x
 
16
 
 
17
    def test_matches(self):
 
18
        self.assertThat(
 
19
            self.SimpleClass(1), MatchesStructure(x=Equals(1)))
 
20
 
 
21
    def test_mismatch(self):
 
22
        self.assertRaises(
 
23
            AssertionError, self.assertThat, self.SimpleClass(1),
 
24
            MatchesStructure(x=NotEquals(1)))
 
25
 
 
26
    def test_fromExample(self):
 
27
        self.assertThat(
 
28
            self.SimpleClass(1),
 
29
            MatchesStructure.fromExample(self.SimpleClass(1), 'x'))
 
30
 
 
31
    def test_update(self):
 
32
        self.assertThat(
 
33
            self.SimpleClass(1),
 
34
            MatchesStructure(x=NotEquals(1)).update(x=Equals(1)))
 
35
 
 
36
    def test_update_none(self):
 
37
        self.assertThat(
 
38
            self.SimpleClass(1),
 
39
            MatchesStructure(x=Equals(1), y=NotEquals(42)).update(
 
40
                y=None))
 
41
 
 
42
 
 
43
class TestMatchesPackage(TestCase):
 
44
 
 
45
    def test_simple(self):
 
46
        observed = DummyFetchedPackage("foo", "1.1", architecture="armel")
 
47
        expected = DummyFetchedPackage("foo", "1.1", architecture="armel")
 
48
        self.assertThat(
 
49
            observed, MatchesPackage.fromPackage(expected))
 
50
 
 
51
    def test_mismatch(self):
 
52
        observed = DummyFetchedPackage("foo", "1.1", depends="bar")
 
53
        expected = DummyFetchedPackage("foo", "1.1", depends="baz")
 
54
        self.assertRaises(AssertionError, self.assertThat, observed,
 
55
            MatchesPackage.fromPackage(expected))
 
56
 
 
57
    def test_skip_one_attribute(self):
 
58
        observed = DummyFetchedPackage("foo", "1.1", depends="bar")
 
59
        expected = DummyFetchedPackage("foo", "1.1", depends="baz")
 
60
        self.assertThat(
 
61
            observed,
 
62
            MatchesPackage.fromPackage(expected).update(depends=None))