def test_multiple_dynamic_registrations(state, dummy_checks): diagnose_calls = 0 state_dec = state_dec_gen(dummy_checks) @state_dec def diagnose1(end_state): assert end_state.state_history[0] is state nonlocal diagnose_calls diagnose_calls += 1 @state_dec def diagnose2(end_state): assert end_state.state_history[0] is state nonlocal diagnose_calls diagnose_calls += 1 TestEx = ExGen(dummy_checks, state) TestF = LazyChainStart(dummy_checks) TestEx.register_chainable_function(diagnose1) TestEx.register_chainable_function(diagnose2) TestEx().diagnose1().diagnose2() TestEx() >> TestF().diagnose1().diagnose2() TestEx() >> diagnose1().diagnose2() TestEx() >> diagnose1() >> diagnose2() assert diagnose_calls == 8 TestEx().diagnose1().noop().diagnose2().noop() TestEx() >> TestF().diagnose1().noop().diagnose2().noop() TestEx() >> diagnose1().noop() >> diagnose2().noop() assert diagnose_calls == 14 TestEx().child_state().diagnose1().diagnose2()
def test_dynamic_registration_named(state, dummy_checks): diagnose_calls = 0 # this makes diagnose a LazyChain wrapper @state_dec_gen(dummy_checks) def diagnose(end_state): assert end_state.state_history[0] is state nonlocal diagnose_calls diagnose_calls += 1 TestEx = ExGen(dummy_checks, state) TestF = LazyChainStart(dummy_checks) TestEx.register_chainable_function(diagnose, "test123") TestEx().test123() TestEx() >> TestF().test123() TestEx() >> diagnose() assert diagnose_calls == 3 with pytest.raises(AttributeError): # The function is registered with a given name (test123) TestEx().diagnose() with pytest.raises(NameError): TestEx() >> test123()
def test_dynamic_registration(state, dummy_checks): diagnose_calls = 0 @state_dec_gen(dummy_checks) def diagnose(end_state): assert end_state.state_history[0] is state nonlocal diagnose_calls diagnose_calls += 1 TestEx = ExGen(dummy_checks, state) TestF = LazyChainStart(dummy_checks) TestEx.register_chainable_function(diagnose) TestEx().diagnose() TestEx() >> TestF().diagnose() TestEx() >> diagnose() assert diagnose_calls == 3 TestEx().diagnose().noop() TestEx() >> TestF().diagnose().noop() TestEx() >> diagnose().noop() assert diagnose_calls == 6 TestEx().child_state().diagnose()
def test_dynamic_registration_raising(state, dummy_checks): @state_dec_gen(dummy_checks) def diagnose(_): raise InstructorError.from_message("problem") TestEx = ExGen(dummy_checks, state) TestEx.register_chainable_function(diagnose) with pytest.raises(InstructorError) as e: TestEx().diagnose() # If calls with a state argument in state_dec would be decorated with link_to_state, # there would be a double link_to_state call when an sct decorated with state_dec # is registered for chaining assert len(re.findall("Debug", str(e.value))) == 1 assert len(e.value.state_history) == 2