()
| 91 | |
| 92 | |
| 93 | def test_code_runner(): |
| 94 | assert should_quiet("1+1;") |
| 95 | assert not should_quiet("1+1#;") |
| 96 | assert not should_quiet("5-2 # comment with trailing semicolon ;") |
| 97 | |
| 98 | # Normal usage |
| 99 | assert CodeRunner("1+1").compile().run() == 2 |
| 100 | assert CodeRunner("1+1\n1+1").compile().run() == 2 |
| 101 | assert CodeRunner("x + 7").compile().run({"x": 3}) == 10 |
| 102 | |
| 103 | # Use constants > 255 so they are stored in co_consts rather than loaded |
| 104 | # via LOAD_SMALL_INT (Python 3.14+ loads ints 0-255 directly via opcode). |
| 105 | cr = CodeRunner("x + 700") |
| 106 | |
| 107 | # Ast transform: change "x + 700" to "x * 300 + 700" |
| 108 | import ast |
| 109 | |
| 110 | l = cr.ast.body[0].value.left # type: ignore[attr-defined] |
| 111 | cr.ast.body[0].value.left = ast.BinOp( # type: ignore[attr-defined] |
| 112 | left=l, op=ast.Mult(), right=ast.Constant(value=300) |
| 113 | ) |
| 114 | assert cr.compile().run({"x": 3}) == 1600 |
| 115 | |
| 116 | # Code transform: change "x * 300 + 700" to "x * 400 + 500" |
| 117 | assert cr.code |
| 118 | co_consts = cr.code.co_consts |
| 119 | new_consts = tuple({300: 400, 700: 500}.get(c, c) for c in co_consts) |
| 120 | cr.code = cr.code.replace(co_consts=new_consts) |
| 121 | assert cr.run({"x": 4}) == 2100 |
| 122 | |
| 123 | |
| 124 | def test_code_runner_mode(): |
nothing calls this directly
no test coverage detected
searching dependent graphs…