# Copyright (C) 2020 Freie Universität Berlin
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.


class MockSpawn():
    def __init__(self, *args, **kwargs):
        # set some expected attributes
        self.before = None
        self.echo = False
        self.output = None
        self.last_command = None

    def sendline(self, line, *args, **kwargs):
        self.last_command = line
        if self.output is None:
            # just echo last input for before (what replwrap is assembling
            # output from)
            self.before = line
        else:
            # use pre-configured output in case command expects a specific
            # output
            self.before = self.output

    def expect_exact(self, *args, **kwargs):
        # always match on prompt with replwrap
        return 0


class MockRIOTCtrl():
    """
    Mock RIOT ctrl
    """
    def __init__(self, *args, **kwargs):
        self.term = MockSpawn()
        self.last_command = None


def init_ctrl(output=None):
    rc = MockRIOTCtrl("foobar", env={"BOARD": "native"})
    rc.term.output = output
    return rc

@@ -210,16 +210,35 @@
 tails.%0A%0A
+import contextlib%0A%0A
 %0Aclass M
@@ -263,32 +263,38 @@
 f __init__(self,
+ ctrl,
  *args, **kwargs
@@ -288,32 +288,90 @@
 rgs, **kwargs):%0A
+        self.ctrl = ctrl%0A        self.last_command = None%0A
         # set so
@@ -449,68 +449,8 @@
 alse
-%0A        self.output = None%0A        self.last_command = None
 %0A%0A  
@@ -539,24 +539,29 @@
     if self.
+ctrl.
 output is No
@@ -838,16 +838,21 @@
  = self.
+ctrl.
 output%0A%0A
@@ -1080,27 +1080,47 @@
 .term = 
-MockSpawn()
+None%0A        self.output = None
 %0A       
@@ -1146,16 +1146,346 @@
 = None%0A%0A
+    @contextlib.contextmanager%0A    def run_term(self, reset=True, **startkwargs):%0A        try:%0A            self.start_term(**startkwargs)%0A            yield self.term%0A        finally:%0A            self.stop_term()%0A%0A    def start_term(self, **spawnkwargs):%0A        self.term = MockSpawn(self)%0A%0A    def stop_term(self):%0A        pass%0A%0A
 %0Adef ini
@@ -1573,13 +1573,8 @@
  rc.
-term.
 outp

rewrite Mock to detect missing check_term