Esempio n. 1
0
def test():
    assert is_subclass(object, object)
    assert is_subclass(object, (object, str))
    assert not is_subclass(object, str)

    assert not is_subclass(7, object)
    assert not is_subclass('meow', object)
def test():
    assert is_subclass(object, object)
    assert is_subclass(object, (object, str))
    assert not is_subclass(object, str)
    
    assert not is_subclass(7, object)
    assert not is_subclass('meow', object)
Esempio n. 3
0
 def test_basics(self):
     c = Counter('abcaba')
     self.assertEqual(c, Counter({'a':3 , 'b': 2, 'c': 1}))
     self.assertEqual(c, Counter(a=3, b=2, c=1))
     self.assertIsInstance(c, dict)
     self.assertIsInstance(c, Mapping)
     self.assertTrue(misc_tools.is_subclass(Counter, dict))
     self.assertTrue(misc_tools.is_subclass(Counter, Mapping))
     self.assertEqual(len(c), 3)
     self.assertEqual(sum(c.values()), 6)
     self.assertEqual(sorted(c.values()), [1, 2, 3])
     self.assertEqual(sorted(c.keys()), ['a', 'b', 'c'])
     self.assertEqual(sorted(c), ['a', 'b', 'c'])
     self.assertEqual(sorted(c.items()),
                      [('a', 3), ('b', 2), ('c', 1)])
     self.assertEqual(c['b'], 2)
     self.assertEqual(c['z'], 0)
 
     self.assertEqual('c' in c, True)
     self.assertEqual('z' in c, False)
     
     self.assertEqual(c.__contains__('c'), True)
     self.assertEqual(c.__contains__('z'), False)
     self.assertEqual(c.get('b', 10), 2)
     self.assertEqual(c.get('z', 10), 10)
     self.assertEqual(c, dict(a=3, b=2, c=1))
     self.assertEqual(repr(c), "Counter({'a': 3, 'b': 2, 'c': 1})")
     self.assertEqual(c.most_common(), [('a', 3), ('b', 2), ('c', 1)])
     for i in range(5):
         self.assertEqual(c.most_common(i),
                          [('a', 3), ('b', 2), ('c', 1)][:i])
     self.assertEqual(''.join(sorted(c.elements())), 'aaabbc')
     c['a'] += 1         # increment an existing value
     c['b'] -= 2         # sub existing value to zero
     del c['c']          # remove an entry
     del c['c']          # make sure that del doesn't raise KeyError
     c['d'] -= 2         # sub from a missing value
     c['e'] = -5         # directly assign a missing value
     c['f'] += 4         # add to a missing value
     self.assertEqual(c, dict(a=4, b=0, d=-2, e=-5, f=4))
     self.assertEqual(''.join(sorted(c.elements())), 'aaaaffff')
     self.assertEqual(c.pop('f'), 4)
     self.assertNotIn('f', c)
     for i in range(3):
         elem, cnt = c.popitem()
         self.assertNotIn(elem, c)
     c.clear()
     self.assertEqual(c, {})
     self.assertEqual(repr(c), 'Counter()')
     self.assertRaises(NotImplementedError, Counter.fromkeys, 'abc')
     self.assertRaises(TypeError, hash, c)
     c.update(dict(a=5, b=3))
     c.update(c=1)
     c.update(Counter('a' * 50 + 'b' * 30))
     c.update()          # test case with no args
     c.__init__('a' * 500 + 'b' * 300)
     c.__init__('cdc')
     c.__init__()
     self.assertEqual(c, dict(a=555, b=333, c=3, d=1))
     self.assertEqual(c.setdefault('d', 5), 1)
     self.assertEqual(c['d'], 1)
     self.assertEqual(c.setdefault('e', 5), 5)
     self.assertEqual(c['e'], 5)
 def test_basics(self):
     c = Counter('abcaba')
     self.assertEqual(c, Counter({'a':3 , 'b': 2, 'c': 1}))
     self.assertEqual(c, Counter(a=3, b=2, c=1))
     self.assertIsInstance(c, dict)
     self.assertIsInstance(c, Mapping)
     self.assertTrue(misc_tools.is_subclass(Counter, dict))
     self.assertTrue(misc_tools.is_subclass(Counter, Mapping))
     self.assertEqual(len(c), 3)
     self.assertEqual(sum(c.values()), 6)
     self.assertEqual(sorted(c.values()), [1, 2, 3])
     self.assertEqual(sorted(c.keys()), ['a', 'b', 'c'])
     self.assertEqual(sorted(c), ['a', 'b', 'c'])
     self.assertEqual(sorted(c.items()),
                      [('a', 3), ('b', 2), ('c', 1)])
     self.assertEqual(c['b'], 2)
     self.assertEqual(c['z'], 0)
 
     self.assertEqual(c.has_key('c'), True)
     self.assertEqual(c.has_key('z'), False)
     
     self.assertEqual(c.__contains__('c'), True)
     self.assertEqual(c.__contains__('z'), False)
     self.assertEqual(c.get('b', 10), 2)
     self.assertEqual(c.get('z', 10), 10)
     self.assertEqual(c, dict(a=3, b=2, c=1))
     self.assertEqual(repr(c), "Counter({'a': 3, 'b': 2, 'c': 1})")
     self.assertEqual(c.most_common(), [('a', 3), ('b', 2), ('c', 1)])
     for i in range(5):
         self.assertEqual(c.most_common(i),
                          [('a', 3), ('b', 2), ('c', 1)][:i])
     self.assertEqual(''.join(sorted(c.elements())), 'aaabbc')
     c['a'] += 1         # increment an existing value
     c['b'] -= 2         # sub existing value to zero
     del c['c']          # remove an entry
     del c['c']          # make sure that del doesn't raise KeyError
     c['d'] -= 2         # sub from a missing value
     c['e'] = -5         # directly assign a missing value
     c['f'] += 4         # add to a missing value
     self.assertEqual(c, dict(a=4, b=0, d=-2, e=-5, f=4))
     self.assertEqual(''.join(sorted(c.elements())), 'aaaaffff')
     self.assertEqual(c.pop('f'), 4)
     self.assertNotIn('f', c)
     for i in range(3):
         elem, cnt = c.popitem()
         self.assertNotIn(elem, c)
     c.clear()
     self.assertEqual(c, {})
     self.assertEqual(repr(c), 'Counter()')
     self.assertRaises(NotImplementedError, Counter.fromkeys, 'abc')
     self.assertRaises(TypeError, hash, c)
     c.update(dict(a=5, b=3))
     c.update(c=1)
     c.update(Counter('a' * 50 + 'b' * 30))
     c.update()          # test case with no args
     c.__init__('a' * 500 + 'b' * 300)
     c.__init__('cdc')
     c.__init__()
     self.assertEqual(c, dict(a=555, b=333, c=3, d=1))
     self.assertEqual(c.setdefault('d', 5), 1)
     self.assertEqual(c['d'], 1)
     self.assertEqual(c.setdefault('e', 5), 5)
     self.assertEqual(c['e'], 5)