moo.core.tests.test_code

Comprehensive tests for moo/core/code.py

Covers:
  • ContextManager lifecycle and get()

  • ContextManager caller stack

  • ContextManager per-session caches

  • get_default_globals() and get_restricted_environment()

  • Each underscore key in the restricted environment

  • _print_ / _print mechanism (RestrictedPython print protocol)

  • compile_verb_code() and its LRU cache

  • do_eval() string path (compile_restricted) and compiled-code path

  • r_exec() / r_eval()

  • Restricted import security (allowed, forbidden, wizard-only)

Functions

test_apply_calls_function()

RestrictedPython still generates _apply_ for star-expansion calls (f(*args)) in Python 3 — it is not the Python 2 apply() builtin artifact we initially assumed.

test_caller_stack(t_init, t_wizard)

test_compile_verb_code_different_args()

test_compile_verb_code_is_cached()

test_compile_verb_code_returns_object()

test_context_manager_caches_are_dicts_inside_session()

test_context_manager_caches_are_none_outside_session()

test_context_manager_caches_isolated_between_sessions()

test_context_manager_caller_stack_outside_session()

test_context_manager_defaults()

test_context_manager_get_parser_default()

test_context_manager_get_unknown_raises()

test_context_manager_is_active()

test_context_manager_nested()

test_context_manager_override_and_pop_caller()

test_context_manager_override_caller_stack_contents()

test_context_manager_player_explicit()

test_context_manager_pop_caller_empty_raises()

test_context_manager_set_parser()

test_context_manager_task_id()

test_context_manager_task_time_inside_session()

test_context_manager_task_time_outside_session()

test_do_eval_string_exec()

test_do_eval_string_expression()

test_eval_simple_command(t_init, t_wizard)

test_get_default_globals()

test_get_protected_attribute_allows_public()

test_get_protected_attribute_private_blocked()

test_get_restricted_environment_exact_keys()

test_getitem()

test_getiter()

test_inplace_var_addition()

test_inplace_var_unsupported_raises()

test_print_and_print_factory_both_present()

RestrictedPython transforms print(x) into _print_(_print)._call_print(x).

test_print_collector_buffer_does_not_leak_between_invocations(...)

Each r_exec builds a fresh environment with a fresh collector — a stranded buffer from one invocation never appears in the next.

test_print_default_end_emits_one_writer_call_per_print(...)

print("a"); print("b") produces two writer calls — one per line.

test_print_embedded_newline_preserved_in_writer_entry(...)

Internal \n in a single print stays inside one writer call — multi-line content (letters, room descriptions) reaches the renderer intact so the shell can render the embedded newlines as line breaks.

test_print_end_empty_coalesces_with_next_print(...)

print("a", end=""); print("b") is a single line — the writer is called once with the concatenated text.

test_print_sep_kwarg_honored(t_init, t_wizard)

sep= joins multiple positional args within a single print.

test_print_three_fragments_coalesce(t_init, ...)

The exact pattern the ZIL translator emits — three fragments with no newlines between — collapses into one writer call.

test_print_trailing_end_empty_flushes_at_verb_exit(...)

A verb ending mid-buffer (print("x", end="") with no follow-up) flushes the pending fragment as a final writer call.

test_printing_imported_caller(t_init, t_wizard)

test_r_eval_returns_value(t_init, t_wizard)

test_r_exec_executes_code(t_init, t_wizard)

test_r_exec_print_calls_writer(t_init, t_wizard)

test_restricted_import_allowed_module()

test_restricted_import_forbidden_module()

test_restricted_import_wizard_module_as_non_wizard()

test_restricted_import_wizard_module_as_wizard()

test_set_protected_attribute_private_blocked()

test_trivial_printing(t_init, t_wizard)

test_write_setitem_passthrough()