Exemplo n.º 1
0
    def test_traceback(self):
        i, a = self.interp_errlog()

        def f():
            return 1 / 0

        def gfunc():
            return f()

        i.runsource("gfunc()")

        global_not_found = "name 'gfunc' is not defined"

        expected = (
            "Traceback (most recent call last):\n  File "
            + green('"<input>"')
            + ", line "
            + bold(magenta("1"))
            + ", in "
            + cyan("<module>")
            + "\n    gfunc()\n"
            + bold(red("NameError"))
            + ": "
            + cyan(global_not_found)
            + "\n"
        )

        self.assertMultiLineEqual(str(plain("").join(a)), str(expected))
        self.assertEqual(plain("").join(a), expected)
Exemplo n.º 2
0
    def test_traceback(self):
        i = interpreter.Interp()
        a = []

        def append_to_a(message):
            a.append(message)

        i.write = append_to_a

        def f():
            return 1 / 0

        def g():
            return f()

        i.runsource('g()', encode=False)

        if pypy:
            global_not_found = "global name 'g' is not defined"
        else:
            global_not_found = "name 'g' is not defined"

        expected = 'Traceback (most recent call last):\n  File ' + \
            green('"%s"' % _last_console_filename()) + ', line ' + bold(magenta('1')) + ', in ' + \
            cyan('<module>') + '\n    g()\n' + bold(red('NameError')) + ': ' + \
            cyan(global_not_found) + '\n'

        self.assertMultiLineEqual(str(plain('').join(a)), str(expected))
        self.assertEquals(plain('').join(a), expected)
Exemplo n.º 3
0
    def test_traceback(self):
        i = interpreter.Interp()
        a = []

        def append_to_a(message):
            a.append(message)

        i.write = append_to_a

        def f():
            return 1/0

        def g():
            return f()

        i.runsource('g()', encode=False)

        if pypy:
            global_not_found = "global name 'g' is not defined"
        else:
            global_not_found = "name 'g' is not defined"

        expected = 'Traceback (most recent call last):\n  File ' + \
            green('"%s"' % _last_console_filename()) + ', line ' + bold(magenta('1')) + ', in ' + \
            cyan('<module>') + '\n    g()\n' + bold(red('NameError')) + ': ' + \
            cyan(global_not_found) + '\n'

        self.assertMultiLineEqual(str(plain('').join(a)), str(expected))
        self.assertEquals(plain('').join(a), expected)
Exemplo n.º 4
0
    def test_syntaxerror(self):
        i = interpreter.Interp()
        a = []

        def append_to_a(message):
            a.append(message)

        i.write = append_to_a
        i.runsource('1.1.1.1')

        if pypy:
            expected = (
                '  File ' + green('"<input>"') +
                ', line ' + bold(magenta('1')) + '\n    1.1.1.1\n      ^\n' +
                bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') +
                '\n')
        else:
            expected = (
                '  File ' + green('"<input>"') +
                ', line ' + bold(magenta('1')) + '\n    1.1.1.1\n        ^\n' +
                bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') +
                '\n')

        self.assertMultiLineEqual(str(plain('').join(a)), str(expected))
        self.assertEquals(plain('').join(a), expected)
Exemplo n.º 5
0
    def draw_confirm_new(self) -> None:
        """Confirms we want to restart the game"""
        items = [
            ff.red("A game is already in-progress!"),
            ff.plain("Press ") + fmtstr("n", fg="yellow", underline=True) +
            " again to start a new",
            ff.plain("game, or ") + fmtstr("c", fg="yellow", underline=True) +
            " to cancel",
        ]

        self.draw_menu(ff.plain("Restart?"), items)
Exemplo n.º 6
0
    def test_syntaxerror(self):
        i = interpreter.Interp()
        a = []

        def append_to_a(message):
            a.append(message)

        i.write = append_to_a
        i.runsource('1.1.1.1')

        expected = ''+u''+u'  File '+green(u'"<input>"')+u', line '+bold(magenta(u'1'))+u'\n'+u'    '+u'1.1'+u'.'+u'1.1'+u'\n'+u'    '+u'    '+u'^'+u'\n'+bold(red(u'SyntaxError'))+u': '+cyan(u'invalid syntax')+u'\n'

        self.assertEquals(str(plain('').join(a)), str(expected))
        self.assertEquals(plain('').join(a), expected)
Exemplo n.º 7
0
    def draw_lost(self) -> None:
        """Confirms we want to restart the game"""
        assert self.field is not None

        items = [
            ff.plain(f"Alas, you appear to have ") +
            fmtstr("exploded", fg="red", bold=True) + ".",
            ff.plain(f"Press ") + fmtstr("n", fg="yellow", underline=True) +
            " to start a new game,",
            ff.plain("or ") + fmtstr("c", fg="yellow", underline=True) +
            " to learn from failure.",
        ]

        self.draw_menu(ff.red("Defeat!"), items)
Exemplo n.º 8
0
    def draw_won(self) -> None:
        """Confirms we want to restart the game"""
        assert self.field is not None

        items = [
            ff.plain(
                f"Congratulations! You won in {self.field.game_time.in_words()}"
            ),
            ff.plain(f"Press ") + fmtstr("n", fg="yellow", underline=True) +
            " to start a new game,",
            ff.plain("or ") + fmtstr("c", fg="yellow", underline=True) +
            " to savor your success.",
        ]

        self.draw_menu(ff.green("Victory!"), items)
Exemplo n.º 9
0
    def clock(self) -> FmtStr:
        """game clock as a formatted string"""
        clock = f"{self.game_time.minutes:02d}:{(self.game_time.seconds % 60):02d}"
        if self.game_time.hours > 0:
            clock = f"{self.game_time.hours:02d}:{clock}"

        return ff.plain(clock)
Exemplo n.º 10
0
    def draw_header(self) -> None:
        """renders the header into our array"""
        title = fmtstr(" No-Guess Sweeper :", fg="blue", underline=True)
        self.chars[1, 1:1 + title.width] = [title]

        clock = ff.plain('┊ ') + ff.green(
            self.field.clock if self.field else "00:00")
        self.chars[1, (self.max_cols - 1 - clock.width):(self.max_cols -
                                                         1)] = [clock]

        avail = self.max_cols - 2 - title.width - clock.width

        instructions: List[FmtStr] = [
            ff.yellow(' h: ') + ff.gray("Help "),
            ff.yellow(' q: ') + ff.gray("Quit "),
            ff.yellow(' n: ') + ff.gray("New "),
        ]

        # drop instructions until they fit on top line
        while sum(i.width for i in instructions) > avail:
            instructions.pop()

        per = int(avail / len(instructions))
        istr = FmtStr().join(i.ljust(per) for i in instructions)

        self.chars[1, title.width:(title.width + istr.width)] = [istr]
Exemplo n.º 11
0
    def test_syntaxerror(self):
        i, a = self.interp_errlog()

        i.runsource('1.1.1.1')

        if pypy:
            expected = ('  File ' + green('"<input>"') + ', line ' +
                        bold(magenta('1')) + '\n    1.1.1.1\n      ^\n' +
                        bold(red('SyntaxError')) + ': ' +
                        cyan('invalid syntax') + '\n')
        else:
            expected = ('  File ' + green('"<input>"') + ', line ' +
                        bold(magenta('1')) + '\n    1.1.1.1\n        ^\n' +
                        bold(red('SyntaxError')) + ': ' +
                        cyan('invalid syntax') + '\n')

        self.assertMultiLineEqual(str(plain('').join(a)), str(expected))
        self.assertEqual(plain('').join(a), expected)
Exemplo n.º 12
0
    def test_syntaxerror(self):
        i, a = self.interp_errlog()

        i.runsource("1.1.1.1")

        if sys.version_info[:2] >= (3, 8):
            expected = (
                "  File "
                + green('"<input>"')
                + ", line "
                + bold(magenta("1"))
                + "\n    1.1.1.1\n       ^\n"
                + bold(red("SyntaxError"))
                + ": "
                + cyan("invalid syntax")
                + "\n"
            )
        elif pypy:
            expected = (
                "  File "
                + green('"<input>"')
                + ", line "
                + bold(magenta("1"))
                + "\n    1.1.1.1\n      ^\n"
                + bold(red("SyntaxError"))
                + ": "
                + cyan("invalid syntax")
                + "\n"
            )
        else:
            expected = (
                "  File "
                + green('"<input>"')
                + ", line "
                + bold(magenta("1"))
                + "\n    1.1.1.1\n        ^\n"
                + bold(red("SyntaxError"))
                + ": "
                + cyan("invalid syntax")
                + "\n"
            )

        self.assertMultiLineEqual(str(plain("").join(a)), str(expected))
        self.assertEqual(plain("").join(a), expected)
Exemplo n.º 13
0
    def test_syntaxerror(self):
        i, a = self.interp_errlog()

        i.runsource('1.1.1.1')

        if pypy:
            expected = (
                '  File ' + green('"<input>"') +
                ', line ' + bold(magenta('1')) + '\n    1.1.1.1\n      ^\n' +
                bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') +
                '\n')
        else:
            expected = (
                '  File ' + green('"<input>"') +
                ', line ' + bold(magenta('1')) + '\n    1.1.1.1\n        ^\n' +
                bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') +
                '\n')

        self.assertMultiLineEqual(str(plain('').join(a)), str(expected))
        self.assertEqual(plain('').join(a), expected)
Exemplo n.º 14
0
    def test_traceback(self):
        i = interpreter.Interp()
        a = []

        def append_to_a(message):
            a.append(message)

        i.write = append_to_a

        def f():
            return 1/0

        def g():
            return f()

        i.runsource('g()')

        expected = u'Traceback (most recent call last):\n'+''+u'  File '+green(u'"<input>"')+u', line '+bold (magenta(u'1'))+u', in '+cyan(u'<module>')+u'\n'+''+bold(red(u'NameError'))+u': '+cyan(u"name 'g' is not defined")+u'\n'

        self.assertEquals(str(plain('').join(a)), str(expected))
        self.assertEquals(plain('').join(a), expected)
    def test_traceback(self):
        i = interpreter.Interp()
        a = []

        def append_to_a(message):
            a.append(message)

        i.write = append_to_a

        def f():
            return 1/0

        def g():
            return f()

        i.runsource('g()')

        expected = 'Traceback (most recent call last):\n  File ' + \
            green('"<input>"') + ', line ' + bold(magenta('1')) + ', in ' + \
            cyan('<module>') + '\n' + bold(red('NameError')) + ': ' + \
            cyan("name 'g' is not defined") + '\n'

        self.assertEquals(str(plain('').join(a)), str(expected))
        self.assertEquals(plain('').join(a), expected)
Exemplo n.º 16
0
    def test_traceback(self):
        i, a = self.interp_errlog()

        def f():
            return 1 / 0

        def gfunc():
            return f()

        i.runsource('gfunc()')

        if pypy and not py3:
            global_not_found = "global name 'gfunc' is not defined"
        else:
            global_not_found = "name 'gfunc' is not defined"

        expected = (
            'Traceback (most recent call last):\n  File ' +
            green('"<input>"') + ', line ' +
            bold(magenta('1')) + ', in ' + cyan('<module>') + '\n    gfunc()\n' +
            bold(red('NameError')) + ': ' + cyan(global_not_found) + '\n')

        self.assertMultiLineEqual(str(plain('').join(a)), str(expected))
        self.assertEqual(plain('').join(a), expected)
Exemplo n.º 17
0
    def test_traceback(self):
        i, a = self.interp_errlog()

        def f():
            return 1 / 0

        def gfunc():
            return f()

        i.runsource('gfunc()')

        if pypy:
            global_not_found = "global name 'gfunc' is not defined"
        else:
            global_not_found = "name 'gfunc' is not defined"

        expected = ('Traceback (most recent call last):\n  File ' +
                    green('"<input>"') + ', line ' + bold(magenta('1')) +
                    ', in ' + cyan('<module>') + '\n    gfunc()\n' +
                    bold(red('NameError')) + ': ' + cyan(global_not_found) +
                    '\n')

        self.assertMultiLineEqual(str(plain('').join(a)), str(expected))
        self.assertEqual(plain('').join(a), expected)