File: test_debug_sequences.py

package info (click to toggle)
pyocd 0.36.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 341,216 kB
  • sloc: xml: 3,682,260; python: 59,213; ansic: 112; makefile: 87; asm: 25; sh: 14
file content (773 lines) | stat: -rw-r--r-- 26,861 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# pyOCD debugger
# Copyright (c) 2020 Arm Limited
# Copyright (c) 2021-2023 Chris Reed
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import logging
import pytest
from unittest import mock
from lark.lexer import Token as LarkToken
from lark.tree import Tree as LarkTree

from pyocd.core import exceptions
from pyocd.debug.sequences.scope import Scope
from pyocd.debug.sequences.sequences import (
    DebugSequenceSemanticError,
    DebugSequenceExecutionContext,
    DebugSequence,
    Block,
    WhileControl,
    IfControl,
    Parser,
    SemanticChecker,
    _ConstantFolder,
)
from pyocd.core.session import Session
from pyocd.probe.debug_probe import DebugProbe

# Substitute for SequenceFunctionDelegate for testing.
class SequenceFunctionsDelegateForTesting:
    def valid_fn_no_args(self):
        pass

    def valid_fn_1_arg(self, a: int):
        pass

    def valid_fn_3_arg(self, a: int, b: int, c: int):
        pass

    def valid_fn_varg(self, a: int, *vargs):
        pass

    def fn_with_str_parm(self, foo: str):
        pass

    def sequence(self, name: str):
        pass

class SequenceDelegateForTesting:
    # This same root scope instance must be return from all .get_root_scope() calls since it is
    # checked for by id in some tests.
    _root = Scope(name="unit-test-root")

    @property
    def sequences(self):
        return set()

    @property
    def cmsis_pack_device(self):
        return mock.MagicMock()

    def get_root_scope(self, context) -> Scope:
        self._root.set('rootvar', 42)
        return self._root

    def has_sequence_with_name(self, name, pname=None):
        return name == "valid"

    def get_sequence_with_name(self, name, pname=None):
        print(f"SequenceDelegateForTesting.get_sequence_by_name: name={name} pname={pname}")
        if name == "valid":
            x = mock.Mock()
            print(f"returning {x}")
            return x
        else:
            return None

    def get_protocol(self):
        return 2

    def get_connection_type(self):
        return 1

    def get_traceout(self):
        return 0

    def get_sequence_functions(self) -> SequenceFunctionsDelegateForTesting:
        return SequenceFunctionsDelegateForTesting()


class MockProbe:
    def __init__(self):
        self.wire_protocol = DebugProbe.Protocol.SWD

@pytest.fixture(scope='function')
def session():
    s = Session(None)
    setattr(s, '_probe', MockProbe())
    return s

@pytest.fixture(scope='function')
def delegate():
    return SequenceDelegateForTesting()

@pytest.fixture(scope='function')
def scope():
    s = Scope(name="fixture")
    s.set("a", 0)
    s.set("b", 128)
    return s

@pytest.fixture(scope='function')
def context(session, delegate):
    c = DebugSequenceExecutionContext(session, delegate, pname=None)
    return c

@pytest.fixture(scope='function')
def block_context(context: DebugSequenceExecutionContext):
    # Return a context set up to be able to run a Block.
    #
    # First create a sequence node with no children. Use the private _create_scope() method to get
    # the new sequence scope with the delegate's root scope at parent. Then push the sequence node
    # and scope onto the context stack. We can't just execute the sequence to do all that because
    # it will pop the context before returning, leaving us without the proper context for running
    # a block.
    seq = DebugSequence('test_sequence')
    scope = seq._create_scope(context)
    # Must manually enter the push context manager.
    ctxmgr = context._push(seq, scope)
    return context

class TestDebugSequenceScope:
    def test_name(self):
        s = Scope(name='test')
        assert s.name == 'test'

    def test_get_1(self, scope):
        assert scope.get('a') == 0
        assert scope.get('b') == 128
        assert len(scope) == 2

    def test_get_invalid(self, scope):
        with pytest.raises(KeyError):
            scope.get('missing')
        with pytest.raises(KeyError):
            scope.get('')

    def test_set_1(self, scope):
        scope.set('a', 10)
        assert scope.get('a') == 10
        assert scope.get('b') == 128

    def test_set_ro(self, scope):
        scope.set('x', 32, readonly=True)
        assert scope.is_read_only('x')
        assert scope.get('x') == 32
        with pytest.raises(RuntimeError):
            scope.set('x', 50)

    def test_freeze(self, scope):
        scope.set('y', 16)
        scope.freeze()
        with pytest.raises(RuntimeError):
            scope.set('a', 1)
        assert len(scope) == 3
        assert scope.is_read_only('b')

    def test_parent_1(self, scope):
        sub = Scope(parent=scope, name='subscope')
        assert sub.parent is scope
        assert sub.get('a') == 0
        assert sub.get('b') == 128
        assert sub.is_defined('b')

    def test_parent_2(self, scope):
        sub = Scope(parent=scope, name='subscope')
        sub.set('x', 1)
        assert sub.parent is scope
        assert sub.get('a') == 0
        assert sub.get('x') == 1
        assert sub.is_defined('b')
        assert not scope.is_defined('x')

    def test_parent_3(self, scope):
        sub = Scope(parent=scope, name='subscope')
        sub.set('a', 1)
        assert sub.parent is scope
        assert sub.get('a') == 1
        assert scope.get('a') == 1 # Make sure it was set in parent scope.
        assert sub.is_defined('a')
        assert 'a' not in sub._variables
        assert scope.is_defined('a')

    def test_parent_4(self, scope):
        sub = Scope(parent=scope, name='subscope')
        scope.set('y', 12, readonly=True)
        with pytest.raises(RuntimeError):
            sub.set('y', 1)
        assert sub.get('y') == 12
        assert scope.get('y') == 12

    def test_copy(self, scope):
        other = Scope(name='other')
        other.copy_variables(from_scope=scope, variables=['a', 'b'])
        assert other.is_defined('a') \
            and other.is_defined('b')
        assert other.get('a') == scope.get('a')
        assert other.get('b') == scope.get('b')

    def test_copy_undefined(self, scope):
        other = Scope(name='other')
        other.copy_variables(from_scope=scope, variables=['a', 'missing'])
        assert other.is_defined('a') \
            and not other.is_defined('b') \
            and not other.is_defined('missing')
        assert other.get('a') == scope.get('a')


class TestDebugSequenceParser:
    def test_semicolons(self):
        # first with semicolon
        ast = Parser().parse("12;")
        print("ast=", ast)
        assert ast.children[0].children[0] == 12

        # now without semicolon
        ast = Parser().parse("12")
        print("ast=", ast)
        assert ast.children[0].children[0] == 12

    def test_fncall_no_args(self):
        ast = Parser().parse("myfunc();")
        print("ast=", ast)
        fncall = ast.children[0].children[0] # start.expr_stmt...
        assert fncall.data == 'fncall'
        assert len(fncall.children) == 1 # IDENT only
        assert fncall.children[0] == LarkToken('IDENT', 'myfunc')

    def test_fncall_3_arg(self):
        ast = Parser().parse("myfunc(1, 2, 3);")
        print("ast=", ast)
        fncall = ast.children[0].children[0] # start.expr_stmt...
        assert fncall.data == 'fncall'
        assert len(fncall.children) == 4 # IDENT, arg1, arg2, arg3
        assert fncall.children[0] == LarkToken('IDENT', 'myfunc')
        assert fncall.children[1] == 1
        assert fncall.children[2] == 2
        assert fncall.children[3] == 3

    def test_bad_input(self):
        with pytest.raises(exceptions.Error):
            Parser().parse("bad input ••• wooo")

    def test_unary_op_assign(self):
        # Statement from Infineon.PSoC6_DFP
        ast = Parser().parse("__Result = -1; // DAP is unavailable")
        print("ast=", ast)

    def test_assign_minus_negative(self):
        ast = Parser().parse("a = 1 - -1;")
        print("ast=", ast)
        e = ast.children[0].children[0].children[2] # start.expr_stmt.assign_expr.binary_expr
        assert e.children[0] == 1
        assert e.children[1] == LarkToken('MINUS', '-')
        assert e.children[2].data == 'unary_expr'
        assert e.children[2].children[0] == LarkToken('MINUS', '-')
        assert e.children[2].children[1] == 1

    def test_assign_minus_positive(self):
        ast = Parser().parse("a = 1 - +1;")
        print("ast=", ast)
        e = ast.children[0].children[0].children[2] # start.expr_stmt.assign_expr.binary_expr
        assert e.children[0] == 1
        assert e.children[1] == LarkToken('MINUS', '-')
        assert e.children[2].data == 'unary_expr'
        assert e.children[2].children[0] == LarkToken('PLUS', '+')
        assert e.children[2].children[1] == 1

    @pytest.mark.parametrize("literal", [
            "1000", "0x3e8", "0x3E8", "0b1111101000",
            "1000u", "1000U", "0x3e8u", "0x3e8U", "0b1111101000u", "0b1111101000U",
        ])
    def test_unsigned_int_lit(self, literal):
        ast = Parser().parse(f"{literal};")
        print("ast=", ast)
        assert ast.children[0].children[0] == 1000

    def test_simple_assign_expr(self):
        ast = Parser().parse("x = 10;")
        print("ast=", ast)
        e = ast.children[0].children[0] # start.expr_stmt.assign_expr
        assert e.children[2] == 10

    def test_multi_assign_expr(self):
        # ast= start
        # expr_stmt
        #     assign_expr
        #     x
        #     =
        #     binary_expr
        #         assign_expr
        #         y
        #         +=
        #         binary_expr
        #             1
        #             +
        #             1
        #         +
        #         2
        ast = Parser().parse("x = (y += (1 + 1)) + 2;")
        print("ast=", ast.pretty())
        e = ast.children[0].children[0] # start.expr_stmt.assign_expr
        assert e.data == LarkToken('RULE', 'assign_expr')
        assert e.children[0] == 'x'
        e = e.children[2].children[0]
        assert e.data == LarkToken('RULE', 'assign_expr')
        assert e.children[0] == 'y'
        assert e.children[1] == "+="

    def test_assign_expr_precedence_1(self):
        ast = Parser().parse("x = 1 ? 2 : 3;")
        print("ast=", ast.pretty())
        e = ast.children[0].children[0] # start.expr_stmt.assign_expr
        assert e.data == LarkToken('RULE', 'assign_expr')

    def test_assign_expr_precedence_2(self):
        ast = Parser().parse("1 ? x = 2 : 3;")
        print("ast=", ast.pretty())
        e = ast.children[0].children[0] # start.expr_stmt.assign_expr
        assert e.data == LarkToken('RULE', 'ternary_expr')
        assert e.children[1].data == LarkToken('RULE', 'assign_expr')

class TestDebugSequenceBlockExecute:
    def test_semicolons(self, block_context):
        block_context.current_scope.set("a", 0)

        # First with semicolon.
        s = Block("a == 0;")
        s.execute(block_context)
        assert block_context.current_scope.get("a") == 0

        # Now without semicolon.
        s = Block("a == 0")
        s.execute(block_context)
        assert block_context.current_scope.get("a") == 0

    def test_set_var(self, block_context):
        s = Block("__var x = 100;")
        s.execute(block_context)
        assert block_context.current_scope.get("x") == 100

    def test_var_no_expr(self, block_context):
        s = Block("__var x;")
        s.execute(block_context)
        assert block_context.current_scope.get("x") == 0

    def test_var_no_expr_separate_set(self, block_context):
        s = Block("__var x; x = 123;")
        s.execute(block_context)
        assert block_context.current_scope.get("x") == 123

    @pytest.mark.parametrize(("expr", "result"), [
            ("-1", 0xffffffffffffffff),
            ("-2", 0xfffffffffffffffe),
            ("!1", 0),
            ("!0", 1),
            ("+1", 1),
            ("~0xffff", 0xffffffffffff0000),
        ])
    def test_int_unary_ops(self, block_context, expr, result):
        s = Block("__var x = %s;" % expr)
        s.execute(block_context)
        assert block_context.current_scope.get("x") == result

    @pytest.mark.parametrize(("expr", "result"), [
            ("1 + 1", 2),
            ("2 - 1", 1),
            ("2 * 4", 8),
            ("4 / 2", 2),
            ("5 % 4", 1),
            ("1 << 12", 4096),
            ("0x80 >> 4", 0x8),
            ("0b1000 | 0x2", 0b1010),
            ("0b1100 & 0b0100", 0b0100),
        ])
    def test_int_expr(self, block_context, expr, result):
        s = Block("__var x = %s;" % expr)
        s.execute(block_context)
        assert block_context.current_scope.get("x") == result

    @pytest.mark.parametrize(("expr", "result"), [
            ("1 == 1", 1),
            ("1 == 0", 0),
            ("0 == 1", 0),
            ("1 != 1", 0),
            ("1 != 0", 1),
            ("0 != 1", 1),
            ("20 > 10", 1),
            ("20 > 20", 0),
            ("20 > 100", 0),
            ("5 >= 2", 1),
            ("5 >= 5", 1),
            ("5 >= 100", 0),
            ("10 < 20", 1),
            ("10 < 10", 0),
            ("10 < 4", 0),
            ("10 <= 20", 1),
            ("10 <= 10", 1),
            ("10 <= 5", 0),
        ])
    def test_bool_cmp_expr(self, block_context, expr, result):
        s = Block("__var x = %s;" % expr)
        s.execute(block_context)
        assert block_context.current_scope.get("x") == result

    # Aside from the obvious, verify that && and || are evaluated as in C rather than Python.
    # That is, they must produce a 1 or 0 and not the value of either operand.
    @pytest.mark.parametrize(("expr", "result"), [
            ("1 && 1", 1),
            ("1 && 0", 0),
            ("0 && 1", 0),
            ("0 && 0", 0),
            ("1 || 1", 1),
            ("1 || 0", 1),
            ("0 || 1", 1),
            ("0 || 0", 0),
            ("5 && 1000", 1),
            ("432 && 0", 0),
            ("0 && 2", 0),
            ("0 && 0", 0),
            ("348 || 4536", 1),
            ("5 || 0", 1),
            ("0 || 199", 1),
            ("0 || 0", 0),
        ])
    def test_bool_and_or_expr(self, block_context, expr, result):
        s = Block("__var x = %s;" % expr)
        s.execute(block_context)
        assert block_context.current_scope.get("x") == result

    @pytest.mark.parametrize(("expr", "result"), [
            ("1 + 2 * 5", 11),
            ("7 * 12 + 5", 89),
            ("1 + 5 - 3", 3),
            ("(1 + 2) * 5", 15),
            ("1 + (2 * 5)", 11),
            ("2 + 16 / 2", 10),
            ("1 + 17 % 3", 3),
            ("2 * 3 * 4", 24),
            ("0 || 1 && 1", 1),
            ("0 && 1 || 0", 0),
            ("1 == 6 > 5", 1),
            ("1 != 6 < 12", 0),
            ("1 << 4 > 1 << 2", 1),
            ("1 << (4 > 1) << 2", 8),
            ("!1 == 0", 1),
        ])
    def test_precedence(self, block_context, expr, result):
        s = Block("__var x = %s;" % expr)
        logging.info("Block: %s", s._ast.pretty())
        s.execute(block_context)
        actual = block_context.current_scope.get("x")
        assert actual == result

    @pytest.mark.parametrize(("expr", "result"), [
            ("(7 * (1 << 3) + 1) >> 1", 28),
        ])
    def test_longer_expr(self, block_context, expr, result):
        s = Block("__var x = %s;" % expr)
        logging.info("Block: %s", s._ast.pretty())
        s.execute(block_context)
        actual = block_context.current_scope.get("x")
        assert actual == result

    def test_unary_op_assign_block(self, block_context):
        # Statement from Infineon.PSoC6_DFP
        s = Block("__Result = -1; // DAP is unavailable")
        logging.info(f"Block: {s._ast.pretty()}")
        s.execute(block_context)
        actual = block_context.current_scope.get("__Result")
        assert actual == 0xffffffffffffffff

    @pytest.mark.parametrize(("expr", "result"), [
            ("1 ? (1 + 1) : (1 - 1)", 2),
            ("0 ? 10: 20", 20),
            ("1 << 5 ? 17 * (2 + 1) : 0", 51),
        ])
    def test_ternary_expr(self, block_context, expr, result):
        s = Block(f"__var x = {expr};")
        logging.info("Block: %s", s._ast.pretty())
        s.execute(block_context)
        actual = block_context.current_scope.get("x")
        assert actual == result

    @pytest.mark.parametrize(("expr", "result"), [
            ("x += 1", 2),
            ("x -= 1", 0),
            ("x *= 10", 10),
            ("x /= 1", 1),
            ("x %= 1", 0),
            ("x &= 3", 1),
            ("x |= 0x40", 0x41),
            ("x ^= 3", 2),
            ("x <<= 5", 1 << 5),
            ("x >>= 0", 1),
        ])
    def test_compound_assign(self, block_context, expr, result):
        s = Block("__var x = 1; %s;" % expr)
        s.execute(block_context)
        assert block_context.current_scope.get("x") == result

    def test_assign_expr_block(self, block_context):
        s = Block("__var x; __var y; x = 1 + (y = 2);")
        logging.info(f"Block: {s._ast.pretty()}")
        s.execute(block_context)
        assert block_context.current_scope.get("x") == 3
        assert block_context.current_scope.get("y") == 2

class TestConstantFolder:
    def _get_folded_ast(self, expr):
        ast = Parser.parse(expr)
        logging.info("Unoptimized AST:\n%s", ast.pretty())
        ast_opt = _ConstantFolder().transform(ast)
        logging.info("Optimized AST:\n%s", ast_opt.pretty())
        return ast_opt

    def _do_fold_test(self, expr, expected):
        ast_opt = self._get_folded_ast(expr)
        assert isinstance(ast_opt, LarkTree)
        assert ast_opt.data == 'start'
        assert ast_opt.children[0].data == 'expr_stmt'
        assert ast_opt.children[0].children[0] == expected

    @pytest.mark.parametrize(("op", "expected"), [
            # return left
            ("+",   "x"),
            ("-",   "x"),
            ("|",   "x"),
            ("^",   "x"),
            ("<<",  "x"),
            (">>",  "x"),
            ("||",  "x"),
            # return 0
            ("*",   0),
            ("/",   0),
            ("%",   0),
            ("&",   0),
            ("&&",  0),
        ])
    def test_fold_left_0(self, op, expected):
        self._do_fold_test(f"x {op} 0", expected)

    @pytest.mark.parametrize(("op", "expected"), [
            # return right
            ("+",   "x"),
            ("-",   "x"),
            ("|",   "x"),
            ("^",   "x"),
            ("||",  "x"),
            # return 0
            ("*",   0),
            ("/",   0),
            ("%",   0),
            ("&",   0),
            ("<<",  0),
            (">>",  0),
            ("&&",  0),
        ])
    def test_fold_right_0(self, op, expected):
        self._do_fold_test(f"0 {op} x", expected)

    @pytest.mark.parametrize(("op", "expected"), [
            # return left
            ("*",   "x"),
            ("/",   "x"),
            # return 1
            ("||",  1),
            # return 0
            ("%",   0),
        ])
    def test_fold_left_1(self, op, expected):
        self._do_fold_test(f"x {op} 1", expected)

    # Really more of a parser test.
    def test_unary_parens(self):
        self._do_fold_test(f"! (1)", 0)

    # Really more of a parser test.
    def test_unary_unfolded(self):
        ast_opt = self._get_folded_ast("! x")

        assert isinstance(ast_opt, LarkTree)
        assert ast_opt.data == 'start'
        assert ast_opt.children[0].data == 'expr_stmt'
        assert ast_opt.children[0].children[0].data == 'unary_expr'

    @pytest.mark.parametrize(("expr", "expected"), [
            ("-1",   0xffffffffffffffff),
            ("-0",   0),
        ])
    def test_unary_fold(self, expr, expected):
        self._do_fold_test(expr, expected)

    @pytest.mark.parametrize(("expr", "expected"), [
            ("1 ? 10 : 20",     10),
            ("0 ? 1 : 2",       2),
            ("0 ? 1 : 2 + 2",   4),
            ("1 + 1 ? 99 : 77", 99),
        ])
    def test_ternary_fold(self, expr, expected):
        self._do_fold_test(expr, expected)

class TestSemanticChecker:

    @pytest.mark.parametrize("expr", [
            "funkymonkey();",                   # fncall: invalid function name
            "valid_fn_1_arg(2134, x << 4);",    # fncall: too many args
            "valid_fn_1_arg(\"a-string\");",    # fncall: string arg for int param
            "valid_fn_3_arg(1, 2);",            # fncall: too few args
            "fn_with_str_parm(123);",           # fncall: int arg for string param
            "valid_fn_varg();",                 # vararg fn: too few args
            '"just a string in this expr";',    # expr consisting of only a string
            "__var a = \"string value\";",      # decl: attempt to assign string to variable
            "b = \"string value\";",            # assign: attempt to assign string to variable
            "Sequence();",                      # fn-specific check: no args
            "Sequence(1000);",                  # fn-specific check: int arg for str arg
        ])
    def test_sem_checker_raises(self, expr, context, scope):
        ast = Parser.parse(expr)
        logging.info("ast:\n%s", ast.pretty())
        c = SemanticChecker(ast, scope, context)
        with pytest.raises(DebugSequenceSemanticError):
            c.check()

    @pytest.mark.parametrize("expr", [
            "valid_fn_no_args();",
            "valid_fn_1_arg(2134);",
            "valid_fn_3_arg(1, 2, 3);",
            "fn_with_str_parm(\"bubblegum\");", # fncall: string arg for string param
            "valid_fn_varg(a);",                # vararg fn: exact number of positional args
            "valid_fn_varg(123, x, q + 1, \"hi there\", 99);",  # vararg fn: several var args
            "__var a = 3 + x * 2;",             # decl: valid assignment
            "b = 3 + x * 2;",                   # assign: valid assignment
            "Sequence(\"valid\");",             # fn-specific check: valid call
        ])
    def test_sem_checker_passes(self, expr, context, scope):
        ast = Parser.parse(expr)
        logging.info("ast:\n%s", ast.pretty())
        c = SemanticChecker(ast, scope, context)
        c.check()

class TestDebugSequences:
    def test_pname(self):
        seq = DebugSequence('test', pname="cm4")
        assert seq.name == 'test'
        assert seq.pname == 'cm4'

        assert DebugSequence('test').pname is None

    def test_info(self):
        assert DebugSequence('test').info == ''
        assert DebugSequence('test', info='hi there').info == 'hi there'

    def test_enable(self):
        assert DebugSequence('test').is_enabled
        assert not DebugSequence('test', is_enabled=False).is_enabled

    def test_scope_create(self, context):
        seq = DebugSequence('test')
        scope = seq._create_scope(context)
        assert scope.parent is context.delegate.get_root_scope(context)
        assert scope.get('__Result') == 0
        assert not scope.is_read_only('_Result')
        assert scope.get('__dp') == 0
        assert not scope.is_read_only('__dp')
        assert scope.get('__ap') == 0
        assert not scope.is_read_only('__ap')
        assert scope.get('__apid') == 0
        assert not scope.is_read_only('__apid')
        assert scope.get('__errorcontrol') == 0
        assert not scope.is_read_only('__errorcontrol')
        assert scope.get('__protocol') == 2
        assert scope.is_read_only('__protocol')
        assert scope.get('__connection') == 1
        assert scope.is_read_only('__connection')
        assert scope.get('__traceout') == 0
        assert scope.is_read_only('__traceout')
        assert scope.get('__FlashOp') == 0
        assert scope.is_read_only('__FlashOp')
        assert scope.get('__FlashAddr') == 0
        assert scope.is_read_only('__FlashAddr')
        assert scope.get('__FlashLen') == 0
        assert scope.is_read_only('__FlashLen')
        assert scope.get('__FlashArg') == 0
        assert scope.is_read_only('__FlashArg')

    def test_scope_create_with_calling_seq(self, block_context):
        # Modify some of the stacked scope's special vars.
        super_scope = block_context.current_scope
        super_scope.set('__dp', 1)
        super_scope.set('__ap', 2)
        super_scope.set('__apid', 3)
        super_scope.set('__errorcontrol', 1)
        super_scope.set('__Result', 333)

        # Create a sub-sequence.
        seq = DebugSequence('test')
        # _create_scope() will see the stacked sequence in the context.
        scope = seq._create_scope(block_context)

        # Certain vars are propagated to subsequences.
        assert scope.get('__dp') == 1
        assert scope.get('__ap') == 2
        assert scope.get('__apid') == 3
        assert scope.get('__errorcontrol') == 1
        # __Result should not have been propagated.
        assert scope.get('__Result') == 0

        # Modify the propagated variables.
        scope.set('__dp', 2)
        scope.set('__ap', 1)
        scope.set('__apid', 5)
        scope.set('__errorcontrol', 0)
        scope.set('__Result', 555)

        # Verify the variables weren't set in the supersequences.
        assert super_scope.get('__dp') == 1
        assert super_scope.get('__ap') == 2
        assert super_scope.get('__apid') == 3
        assert super_scope.get('__errorcontrol') == 1
        assert super_scope.get('__Result') == 333

    def test_exec_block(self, context):
        seq = DebugSequence('test')
        seq.add_child(Block("__var x = 1 + 1;"))
        seq.execute(context)

    def test_exec_if(self, context):
        seq = DebugSequence('test')
        seq.add_child(Block("__var x = 1;"))
        seq.add_child(IfControl("x"))
        seq.execute(context)

    def test_exec_while(self, context):
        seq = DebugSequence('test')
        seq.add_child(Block("__var x = 0;"))
        w = WhileControl("x < 2")
        w.add_child(Block("x += 1;"))
        seq.add_child(w)
        seq.execute(context)

    def test_exec_while_with_assign(self, context: DebugSequenceExecutionContext):
        seq = DebugSequence('test')
        seq.add_child(Block("__var x = 0; __var y = 0;"))
        w = WhileControl("x < (y = 2)")
        w.add_child(Block("x += 1;"))
        seq.add_child(w)
        seq.execute(context)