Ejemplo n.º 1
0
    def test_memoize(self):
        dummy_fn = Mock(name='dummy_fn')
        dummy_fn.return_value = 'spam'

        wrapped = memoize(dummy_fn)

        # Första anrop ger call count == 1
        self.assertEqual(wrapped(3), 'spam')
        self.assertEqual(dummy_fn.call_count, 1)
        # Andra anrop ger inget ytterligare call - cachen användes!
        self.assertEqual(wrapped(3), 'spam')
        self.assertEqual(dummy_fn.call_count, 1)

        # Nästa anrop ger ökning av call count.
        self.assertEqual(wrapped(7), 'spam')
        self.assertEqual(dummy_fn.call_count, 2)
Ejemplo n.º 2
0
def test_memoize_decorator():
    dummy_fn = Mock(name="dummy_fn")
    dummy_fn.return_value = "spam"

    wrapped = memoize(dummy_fn)

    # Första anrop ger call count == 1
    assert wrapped(3) == "spam"
    assert dummy_fn.call_count == 1
    # Andra anrop ger inget ytterligare call - cachen användes!
    assert wrapped(3) == "spam"
    assert dummy_fn.call_count == 1

    # Nästa anrop ger ökning av call count.
    assert wrapped(7) == "spam"
    assert dummy_fn.call_count == 2