def test_when(self): result = [] def on_done(value): result.append(value) def on_all(*args): result.extend(args) return(sum(args)) d1 = Deferred() d2 = Deferred() d3 = When(d1, d2) d1.then(on_done) d2.then(on_done) d3.then(on_all)\ .then(on_done) self.assertEqual(result, []) d2.resolve(2) self.assertEqual(result, [2]) d1.resolve(1) self.assertEqual(result, [2, 1, 2, 3, 1])
def test_when(self): result = [] def on_done(value): result.append(value) def on_all(*args): result.extend(args) return (sum(args)) d1 = Deferred() d2 = Deferred() d3 = When(d1, d2) d1.then(on_done) d2.then(on_done) d3.then(on_all)\ .then(on_done) self.assertEqual(result, []) d2.resolve(2) self.assertEqual(result, [2]) d1.resolve(1) self.assertEqual(result, [2, 1, 2, 3, 1])
def test_plus(self): result = [] def get_value(): return 42 def got_value(value): self.assertEqual(value, 42) return value * 2 def got_value2(value): self.assertEqual(value, 84) result.append(value) d = Deferred() d.then(get_value)\ .then(got_value)\ .then(got_value2) self.assertEqual(result, []) d.resolve() self.assertEqual(result, [84])