|
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 | import yosys_mau.task_loop as tl
|
| 5 | +from yosys_mau.task_loop.context import TaskContextDict |
5 | 6 |
|
6 | 7 |
|
7 | 8 | def test_local_override_stays_local():
|
@@ -205,4 +206,95 @@ def on_task3():
|
205 | 206 | assert order == [1, 3, 3]
|
206 | 207 |
|
207 | 208 |
|
| 209 | +def test_TaskContextDict_with_default(): |
| 210 | + @tl.task_context |
| 211 | + class SomeContext: |
| 212 | + some_var: TaskContextDict[str, str] = TaskContextDict() |
| 213 | + |
| 214 | + def main(): |
| 215 | + # iterate default values |
| 216 | + for _, _ in SomeContext.some_var.items(): |
| 217 | + pass |
| 218 | + |
| 219 | + # iterate non-default values |
| 220 | + SomeContext.some_var["a"] = "b" |
| 221 | + for _, _ in SomeContext.some_var.items(): |
| 222 | + pass |
| 223 | + |
| 224 | + assert SomeContext.some_var["a"] == "b" |
| 225 | + |
| 226 | + tl.run_task_loop(main) |
| 227 | + |
| 228 | + |
| 229 | +def test_override_TaskContextDict(): |
| 230 | + order: list[dict[str, str]] = [] |
| 231 | + |
| 232 | + @tl.task_context |
| 233 | + class SomeContext: |
| 234 | + some_var: TaskContextDict[str, str] = TaskContextDict() |
| 235 | + |
| 236 | + def main(): |
| 237 | + def on_task1(): |
| 238 | + SomeContext.some_var["a"] = "b" |
| 239 | + order.append(SomeContext.some_var.as_dict()) |
| 240 | + |
| 241 | + def on_task2(): |
| 242 | + order.append(SomeContext.some_var.as_dict()) |
| 243 | + |
| 244 | + with tl.root_task().as_current_task(): |
| 245 | + SomeContext.some_var["b"] = "d" |
| 246 | + |
| 247 | + def on_task3(): |
| 248 | + order.append(SomeContext.some_var.as_dict()) |
| 249 | + |
| 250 | + task1 = tl.Task(on_run=on_task1) |
| 251 | + task2 = tl.Task(on_run=on_task2) |
| 252 | + task3 = tl.Task(on_run=on_task3) |
| 253 | + |
| 254 | + task2.depends_on(task1) |
| 255 | + task3.depends_on(task2) |
| 256 | + |
| 257 | + SomeContext.some_var["b"] = "c" |
| 258 | + |
| 259 | + tl.run_task_loop(main) |
| 260 | + |
| 261 | + assert order == [ |
| 262 | + {"a": "b", "b": "c"}, |
| 263 | + {"b": "c"}, |
| 264 | + {"b": "d"}, |
| 265 | + ] |
| 266 | + |
| 267 | + |
| 268 | +def test_child_TaskContextDict(): |
| 269 | + order: list[dict[str, str]] = [] |
| 270 | + |
| 271 | + @tl.task_context |
| 272 | + class SomeContext: |
| 273 | + some_var: TaskContextDict[str, str] = TaskContextDict() |
| 274 | + |
| 275 | + def main(): |
| 276 | + async def on_task1(): |
| 277 | + SomeContext.some_var["a"] = "b" |
| 278 | + order.append(SomeContext.some_var.as_dict()) |
| 279 | + t2 = tl.Task(on_run=on_task2) |
| 280 | + await t2.finished |
| 281 | + order.append(SomeContext.some_var.as_dict()) |
| 282 | + |
| 283 | + def on_task2(): |
| 284 | + SomeContext.some_var["b"] = "d" |
| 285 | + order.append(SomeContext.some_var.as_dict()) |
| 286 | + |
| 287 | + tl.Task(on_run=on_task1) |
| 288 | + |
| 289 | + SomeContext.some_var["b"] = "c" |
| 290 | + |
| 291 | + tl.run_task_loop(main) |
| 292 | + |
| 293 | + assert order == [ |
| 294 | + {"a": "b", "b": "c"}, |
| 295 | + {"a": "b", "b": "d"}, |
| 296 | + {"a": "b", "b": "c"}, |
| 297 | + ] |
| 298 | + |
| 299 | + |
208 | 300 | # TODO tests
|
0 commit comments