Beispiel #1
0
 def test_contract_state_switch_default_param(self):
     func = deal.pre(lambda x: x > 0)(lambda x: x * 2)
     deal.switch(main=False)
     func(-2)
     deal.switch(main=True)
     with pytest.raises(deal.PreContractError):
         func(-2)
Beispiel #2
0
 def test_contract_state_switch_custom_param(self):
     func = deal.pre(lambda x: x > 0, debug=True)(lambda x: x * 2)
     deal.switch(debug=False)
     func(-2)
     deal.switch(debug=True)
     with pytest.raises(deal.PreContractError):
         func(-2)
Beispiel #3
0
 def test_main(self):
     func = deal.pre(lambda x: x > 0)(lambda x: x * 2)
     deal.switch(main=False)
     with self.subTest(text='good'):
         func(-2)
     deal.switch(main=True)
     with self.subTest(text='error'):
         with self.assertRaises(deal.PreContractError):
             func(-2)
Beispiel #4
0
 def test_debug(self):
     func = deal.pre(lambda x: x > 0, debug=True)(lambda x: x * 2)
     deal.switch(debug=False)
     with self.subTest(text='good'):
         func(-2)
     deal.switch(debug=True)
     with self.subTest(text='error'):
         with pytest.raises(deal.PreContractError):
             func(-2)
Beispiel #5
0
def test_state_switch_activate():
    try:
        assert deal.activate()
        assert deactivate()

        deal.switch(main=False)
        assert not deal.activate()
    finally:
        deactivate()
        deal.switch(main=True)
Beispiel #6
0
def test_state_switch_module_load_debug():
    with pytest.raises(RuntimeError):
        deal.module_load(debug=True)
    try:
        deal.switch(debug=False)
        deal.activate()
        deal.module_load(debug=True)
    finally:
        deactivate()
        deal.switch(debug=True)
Beispiel #7
0
def test_contract_state_switch_default_param_generator():
    @deal.pre(lambda x: x > 0)
    def func(x):
        yield x * 2

    deal.switch(main=False)
    assert list(func(-2)) == [-4]
    deal.switch(main=True)
    with pytest.raises(deal.PreContractError):
        list(func(-2))
Beispiel #8
0
def test_contract_state_switch_default_param_async():
    @deal.pre(lambda x: x > 0)
    async def func(x):
        return x * 2

    deal.switch(main=False)
    assert run_sync(func(-2)) == -4
    deal.switch(main=True)
    with pytest.raises(deal.PreContractError):
        run_sync(func(-2))