def test233_onThirdCall_random_args(self): fto = ForTestOnly() stub = SinonStub(ForTestOnly, "func1") stub.onThirdCall().returns("onThirdCall") self.assertEqual(fto.func1(), None) self.assertEqual(fto.func1(), None) self.assertEqual(fto.func1(1), "onThirdCall")
def test392_chained_function_throws(self): stub = SinonStub(C_func) stub.withArgs(42).onFirstCall().throws( Exception('A')).onSecondCall().throws(Exception('B')) with self.assertRaisesRegexp(Exception, 'A'): stub.g.C_func(42) with self.assertRaisesRegexp(Exception, 'B'): stub.g.C_func(42)
def test226_throws_with_condition(self): fto = ForTestOnly() self.assertEqual(fto.func1(), "func1") stub = SinonStub(ForTestOnly, "func1") stub.onSecondCall().throws() fto.func1() with self.assertRaises(Exception) as context: fto.func1()
def test442_withArgs_onCall_returns_does_not_reset_call_count(self): stub = SinonStub() self.assertEqual(stub(), None) self.assertEqual(stub('A'), None) stub.withArgs('A').onSecondCall().returns('second call!') self.assertEqual(stub('A'), 'second call!') self.assertEqual(stub('A'), None) self.assertEqual(stub(), None)
def test391_chained_module_throws(self): stub = SinonStub(os, 'system') stub.withArgs(42).onFirstCall().throws( Exception('A')).onSecondCall().throws(Exception('B')) with self.assertRaisesRegexp(Exception, 'A'): os.system(42) with self.assertRaisesRegexp(Exception, 'B'): os.system(42)
def test390_chained_pure_throws(self): stub = SinonStub() stub.withArgs(42).onFirstCall().throws( Exception('A')).onSecondCall().throws(Exception('B')) with self.assertRaisesRegexp(Exception, 'A'): stub(42) with self.assertRaisesRegexp(Exception, 'B'): stub(42)
def test393_chained_method_throws(self): o = A_object() stub = SinonStub(o, 'A_func') stub.withArgs(42).onFirstCall().throws( Exception('A')).onSecondCall().throws(Exception('B')) with self.assertRaisesRegexp(Exception, 'A'): o.A_func(42) with self.assertRaisesRegexp(Exception, 'B'): o.A_func(42)
def test443_withArgs_onCall_throws_does_not_reset_call_count(self): stub = SinonStub() self.assertEqual(stub(), None) self.assertEqual(stub('A'), None) stub.withArgs('A').onSecondCall().throws(Exception('second call!')) with self.assertRaisesRegexp(Exception, 'second call'): stub('A') self.assertEqual(stub('A'), None) self.assertEqual(stub(), None)
def test400_correct_precedence(self): stub = SinonStub() stub.withArgs('A').returns('Arg of A') stub.onFirstCall().returns('First call!') stub.onSecondCall().returns('Second call!') stub.returns('No args') self.assertEqual(stub(), 'First call!') self.assertEqual(stub('A'), 'Arg of A') self.assertEqual(stub(), 'No args')
def test401_correct_precedence_throws(self): stub = SinonStub() stub.withArgs('A').throws(Exception('A')) stub.onFirstCall().throws(Exception('First call!')) stub.onSecondCall().throws(Exception('Second call!')) stub.throws(Exception('No args')) with self.assertRaisesRegexp(Exception, 'First call!'): stub() with self.assertRaisesRegexp(Exception, 'A'): stub('A') with self.assertRaisesRegexp(Exception, 'No args'): stub()
def test225_onCall_plus_withArgs(self): fto = ForTestOnly() self.assertEqual(fto.func1(), "func1") stub = SinonStub(ForTestOnly, "func1") stub.withArgs(1).returns("1") stub.withArgs(1).onSecondCall().returns("oncall") stub.onThirdCall().returns("##") stub.onCall(4).returns("###") # on fifth call will return "###" self.assertEqual(fto.func1(1), "1") self.assertEqual(fto.func1(1), "oncall") self.assertEqual(fto.func1(1), "1") # the priority of onCall is lower than withArgs self.assertEqual(fto.func1(), None) self.assertEqual(fto.func1(), "###")
def test060_and_match(self): spy = SinonSpy() stub = SinonStub() m = SinonMatcher.instanceOf(spy).and_match( SinonMatcher.instanceOf(stub)) self.assertFalse(m.mtest(spy)) self.assertTrue(m.mtest(stub))
def test371_multiple_onCall_returns_named_functions(self): o = A_object() stub = SinonStub(o, 'A_func') stub.onFirstCall().returns(5) stub.onSecondCall().returns(10) stub.onThirdCall().returns(20) self.assertEqual(o.A_func(), 5) self.assertEqual(o.A_func(), 10) self.assertEqual(o.A_func(), 20)
def test220_returns(self): fto = ForTestOnly() self.assertEqual(fto.func1(), "func1") stub = SinonStub(ForTestOnly, "func1") stub.returns(1) self.assertEqual(fto.func1(), 1) stub.returns({}) self.assertEqual(fto.func1(), {}) stub.returns(TestSinonStub.my_func) self.assertEqual(fto.func1(), TestSinonStub.my_func)
def test222_withArgs(self): fto = ForTestOnly() stub = SinonStub(ForTestOnly, "func1") stub.withArgs(1).returns("#") self.assertEqual(fto.func1(1), "#") stub.withArgs(b=1).returns("##") self.assertEqual(fto.func1(b=1), "##") stub.withArgs(1, b=1).returns("###") self.assertEqual(fto.func1(1, b=1), "###") self.assertEqual(fto.func1(2), None)
def test425_returns_throws_can_be_overwritten(self): stub = SinonStub() self.assertEqual(stub(), None) stub.returns(5) self.assertEqual(stub(), 5) stub.throws() with self.assertRaises(Exception): stub() stub.returns(10) self.assertEqual(stub(), 10)
def test520_stub_is_SinonStubCondition_function(self): function_stub = SinonStub(C_func).onFirstCall().throws( BaseException('Hello World')).onSecondCall().returns(42) self.assertEqual(function_stub.callCount, 0) with self.assertRaisesRegexp(BaseException, 'Hello World'): sinon.g.C_func() self.assertEqual(sinon.g.C_func(), 42) self.assertEqual(sinon.g.C_func(), None) self.assertEqual(function_stub.callCount, 3)
def test510_stub_is_SinonStubCondition_module_function(self): module_function_stub = SinonStub(os, "system").onFirstCall().throws( BaseException('Hello World')).onSecondCall().returns(42) self.assertEqual(module_function_stub.callCount, 0) with self.assertRaisesRegexp(BaseException, 'Hello World'): os.system("cd") self.assertEqual(os.system("cd"), 42) self.assertEqual(os.system("cd"), None) self.assertEqual(module_function_stub.callCount, 3)
def test500_stub_is_SinonStubCondition_pure(self): pure_stub = SinonStub().onFirstCall().throws( BaseException('Hello World')).onSecondCall().returns(42) self.assertEqual(pure_stub.callCount, 0) with self.assertRaisesRegexp(BaseException, 'Hello World'): pure_stub() self.assertEqual(pure_stub(), 42) self.assertEqual(pure_stub(), None) self.assertEqual(pure_stub.callCount, 3)
def test370_multiple_onCall_returns(self): o = A_object() stub = SinonStub(o, 'A_func') stub.onCall(0).returns(5) stub.onCall(1).returns(10) stub.onCall(2).returns(20) stub.onCall(3).returns(30) self.assertEqual(o.A_func(), 5) self.assertEqual(o.A_func(), 10) self.assertEqual(o.A_func(), 20) self.assertEqual(o.A_func(), 30)
def test430_withArgs_onCall_combine_correctly_returns(self): stub = SinonStub() stub.withArgs('A').onFirstCall().returns(1) stub.withArgs('A').onSecondCall().returns(2) stub.withArgs('B').onFirstCall().returns(10) stub.withArgs('B').onSecondCall().returns(20) self.assertEqual(stub(), None) self.assertEqual(stub('A'), 1) self.assertEqual(stub('A'), 2) self.assertEqual(stub('A'), None) self.assertEqual(stub('B'), 10) self.assertEqual(stub('B'), 20) self.assertEqual(stub('B'), None)
def test432_withArgs_onCall_combine_correctly_class(self): fto = ForTestOnly() stub = SinonStub(ForTestOnly, "func1") stub.withArgs('A').onFirstCall().returns(1) stub.withArgs('A').onSecondCall().returns(2) stub.withArgs('B').onFirstCall().returns(10) stub.withArgs('B').onSecondCall().returns(20) self.assertEqual(stub(fto), None) self.assertEqual(stub(fto, 'A'), 1) self.assertEqual(stub(fto, 'A'), 2) self.assertEqual(stub(fto, 'A'), None) self.assertEqual(stub(fto, 'B'), 10) self.assertEqual(stub(fto, 'B'), 20) self.assertEqual(stub(fto, 'B'), None)
def test404_correct_precedence_withArgs_args_kwargs(self): stub = SinonStub() stub.withArgs('A', b='B').onSecondCall().returns('second call!') stub.withArgs('A', b='B').returns('Arg of A, Kwarg of B') self.assertEqual(stub(), None) self.assertEqual(stub('A', b='B'), 'Arg of A, Kwarg of B') self.assertEqual(stub('A', b='B'), 'second call!') self.assertEqual(stub('A', b='B'), 'Arg of A, Kwarg of B')
def test403_correct_precedence_withArgs_kwargs(self): stub = SinonStub() stub.withArgs(a='A').onSecondCall().returns('second call!') stub.withArgs(a='A').returns('Kwarg of A') self.assertEqual(stub(), None) self.assertEqual(stub(a='A'), 'Kwarg of A') self.assertEqual(stub(a='A'), 'second call!') self.assertEqual(stub(a='A'), 'Kwarg of A')
def test441_throws_does_not_reset_call_count(self): stub = SinonStub() stub.onFirstCall().throws(Exception('first call')) with self.assertRaisesRegexp(Exception, 'first call'): stub() stub.onSecondCall().throws(Exception('second call')) with self.assertRaisesRegexp(Exception, 'second call'): stub()
def test431_withArgs_onCall_combine_correctly_throws(self): stub = SinonStub() stub.withArgs('A').onFirstCall().throws(Exception('1')) stub.withArgs('A').onSecondCall().throws(Exception('2')) stub.withArgs('B').onFirstCall().throws(Exception('10')) stub.withArgs('B').onSecondCall().throws(Exception('20')) self.assertEqual(stub(), None) with self.assertRaisesRegexp(Exception, '1'): stub('A') with self.assertRaisesRegexp(Exception, '2'): stub('A') self.assertEqual(stub('A'), None) with self.assertRaisesRegexp(Exception, '10'): stub('B') with self.assertRaisesRegexp(Exception, '20'): stub('B') self.assertEqual(stub('B'), None)
def test043_callOrder_three_unique_args_call_repeated(self): spy1 = SinonSpy() stub1 = SinonStub() spy2 = SinonSpy() spy1() stub1() spy2() spy1() SinonAssertion.callOrder(spy1, stub1, spy2) SinonAssertion.callOrder(spy1, stub1, spy2, spy1) SinonAssertion.callOrder(stub1, spy2, spy1) SinonAssertion.callOrder(stub1, spy1) SinonAssertion.callOrder(spy1, stub1) SinonAssertion.callOrder(spy2, spy1) SinonAssertion.callOrder(spy1, spy2)
def test223_onCall(self): fto = ForTestOnly() self.assertEqual(fto.func1(), "func1") stub = SinonStub(ForTestOnly, "func1") stub.onSecondCall().returns("oncall") self.assertEqual(fto.func1(), None) self.assertEqual(fto.func1(), "oncall") stub.onThirdCall().returns("oncall") self.assertEqual(fto.func1(), "oncall") self.assertEqual(fto.func1(), None)
def test221_throws(self): fto = ForTestOnly() self.assertEqual(fto.func1(), "func1") stub = SinonStub(ForTestOnly, "func1") stub.throws() with self.assertRaises(Exception) as context: fto.func1() stub.throws(TypeError) with self.assertRaises(TypeError) as context: fto.func1()
def test408_chained_withArgs_onCall_returns(self): stub = SinonStub() stub.withArgs(42).onFirstCall().returns(1).onSecondCall().returns(2) stub.returns(0) self.assertEqual(stub(1), 0) self.assertEqual(stub(42), 1) self.assertEqual(stub(1), 0) self.assertEqual(stub(42), 2) self.assertEqual(stub(1), 0) self.assertEqual(stub(42), 0)