Ejemplo n.º 1
0
    def test_per_instance_field_attributes(self):
        # Field attributes are added to a `Results` instance, not the type.
        r1 = Results(['foo', 'bar'], [42, 'abc'])
        r2 = Results(['x'], [42.0])

        for attr in 'foo', 'bar', 'x':
            self.assertFalse(hasattr(Results, attr))

        self.assertTrue(hasattr(r1, 'foo'))
        self.assertTrue(hasattr(r1, 'bar'))
        self.assertFalse(hasattr(r1, 'x'))

        self.assertFalse(hasattr(r2, 'foo'))
        self.assertFalse(hasattr(r2, 'bar'))
        self.assertTrue(hasattr(r2, 'x'))
Ejemplo n.º 2
0
    def test_tuple_cast(self):
        r = Results(['a', 'b'], [42, 43])

        t = tuple(r)

        self.assertIs(type(t), tuple)
        self.assertEqual(t, (42, 43))
Ejemplo n.º 3
0
    def test_pickle(self):
        r = Results(['a', 'b'], [42, 'abc'])

        pickled = pickle.dumps(r)
        unpickled = pickle.loads(pickled)

        self.assertEqual(unpickled, r)
Ejemplo n.º 4
0
    def test_eq_subclass(self):
        class ResultsSubclass(Results):
            pass

        r1 = Results(['foo'], ['abc'])
        r2 = ResultsSubclass(['foo'], ['abc'])

        self.assertEqual(r1, r2)
Ejemplo n.º 5
0
    def test_field_attributes(self):
        r = Results(['foo', 'bar'], [42, 'abc'])

        self.assertEqual(r.foo, 42)
        self.assertEqual(r.bar, 'abc')

        with self.assertRaises(AttributeError):
            r.baz
Ejemplo n.º 6
0
    def test_index_access(self):
        r = Results(['foo', 'bar'], [42, 'abc'])

        self.assertEqual(r[0], 42)
        self.assertEqual(r[1], 'abc')

        with self.assertRaises(IndexError):
            r[2]
Ejemplo n.º 7
0
    def test_immutability(self):
        r = Results(['foo', 'bar'], [42, 'abc'])

        # Setter for existing attribute.
        with self.assertRaises(AttributeError):
            r.bar = 999

        # Setter for new attribute.
        with self.assertRaises(AttributeError):
            r.baz = 999

        # Deleter for existing attribute.
        with self.assertRaises(AttributeError):
            del r.bar

        # Deleter for new attribute.
        with self.assertRaises(AttributeError):
            del r.baz

        with self.assertRaises(TypeError):
            r[0] = 999
Ejemplo n.º 8
0
    def test_eq_different_source_types(self):
        r1 = Results(iter(['a', 'b']), iter([42, 43]))
        r2 = Results(['a', 'b'], [42, 43])

        self.assertEqual(r1, r2)
Ejemplo n.º 9
0
    def test_repr_single(self):
        r = Results(['a'], [42])

        self.assertTrue(repr(r).startswith('Results'))
        self.assertTrue(repr(r).endswith('a = 42'))
Ejemplo n.º 10
0
 def test_constructor_len_mismatch(self):
     with self.assertRaises(ValueError):
         Results(['a', 'b'], [42])
Ejemplo n.º 11
0
    def test_eq_empty(self):
        r1 = Results([], [])
        r2 = Results([], [])

        self.assertEqual(r1, r2)
Ejemplo n.º 12
0
 def f():
     return Results(['a', 'b'], [42, 43])
Ejemplo n.º 13
0
    def test_constructor_iterable(self):
        r = Results(iter(['a', 'b']), iter([42, 43]))

        self.assertEqual(tuple(r), (42, 43))
        self.assertEqual(r._fields, ('a', 'b'))
Ejemplo n.º 14
0
    def test_eq_nonempty(self):
        r1 = Results(['foo', 'bar'], ['abc', 'def'])
        r2 = Results(['foo', 'bar'], ['abc', 'def'])

        self.assertEqual(r1, r2)
Ejemplo n.º 15
0
    def test_ne_type(self):
        r1 = Results(['foo', 'bar'], ['abc', 'def'])
        r2 = ('abc', 'def')

        self.assertNotEqual(r1, r2)
Ejemplo n.º 16
0
 def test_tuple_subclass(self):
     self.assertTrue(issubclass(Results, tuple))
     self.assertIsInstance(Results(['a', 'b'], [42, 43]), tuple)
Ejemplo n.º 17
0
    def test_repr_empty(self):
        r = Results([], [])

        self.assertTrue(repr(r).startswith('Results'))
        self.assertTrue(repr(r).endswith('---'))
Ejemplo n.º 18
0
    def test_ne_values(self):
        r1 = Results(['foo', 'bar'], ['abc', 'def'])
        r2 = Results(['foo', 'bar'], ['abc', 'xyz'])

        self.assertNotEqual(r1, r2)
Ejemplo n.º 19
0
    def test_repr_multiple(self):
        r = Results(['a', 'foo'], [42, 'abc'])

        self.assertTrue(repr(r).startswith('Results'))
        self.assertTrue('a   = 42' in repr(r))
        self.assertTrue(repr(r).endswith("foo = 'abc'"))
Ejemplo n.º 20
0
    def test_eq_same_obj(self):
        r = Results(['a', 'b'], [1, 2])

        self.assertEqual(r, r)