Check that an interface runs the example code correctly First argument is a callable accepting the initial globals and using them to create the actual namespace Second argument is the expected result
(self, create_namespace, expected_namespace)
| 126 | self.assertEqual(actual, expected) |
| 127 | |
| 128 | def check_code_execution(self, create_namespace, expected_namespace): |
| 129 | """Check that an interface runs the example code correctly |
| 130 | |
| 131 | First argument is a callable accepting the initial globals and |
| 132 | using them to create the actual namespace |
| 133 | Second argument is the expected result |
| 134 | """ |
| 135 | sentinel = object() |
| 136 | expected_ns = expected_namespace.copy() |
| 137 | run_name = expected_ns["__name__"] |
| 138 | saved_argv0 = sys.argv[0] |
| 139 | saved_mod = sys.modules.get(run_name, sentinel) |
| 140 | # Check without initial globals |
| 141 | result_ns = create_namespace(None) |
| 142 | self.assertNamespaceMatches(result_ns, expected_ns) |
| 143 | self.assertIs(sys.argv[0], saved_argv0) |
| 144 | self.assertIs(sys.modules.get(run_name, sentinel), saved_mod) |
| 145 | # And then with initial globals |
| 146 | initial_ns = {"sentinel": sentinel} |
| 147 | expected_ns["sentinel"] = sentinel |
| 148 | result_ns = create_namespace(initial_ns) |
| 149 | self.assertIsNot(result_ns, initial_ns) |
| 150 | self.assertNamespaceMatches(result_ns, expected_ns) |
| 151 | self.assertIs(sys.argv[0], saved_argv0) |
| 152 | self.assertIs(sys.modules.get(run_name, sentinel), saved_mod) |
| 153 | |
| 154 | |
| 155 | class ExecutionLayerTestCase(unittest.TestCase, CodeExecutionMixin): |
no test coverage detected