[pyrepl-checkins] pyrepl/pyrepl/tests __init__.py,NONE,1.1 basic.py,NONE,1.1 bugs.py,NONE,1.1 infrastructure.py,NONE,1.1 wishes.py,NONE,1.1

mwh@codespeak.net mwh@codespeak.net
Tue, 4 Feb 2003 19:47:09 +0100 (MET)


Update of /cvs/pyrepl/pyrepl/pyrepl/tests
In directory thoth.codespeak.net:/tmp/cvs-serv1169

Added Files:
	__init__.py basic.py bugs.py infrastructure.py wishes.py 
Log Message:
Added the glimmerings of a test suite.

Bet you didn't expect that :-)


--- NEW FILE: __init__.py ---
# moo

--- NEW FILE: basic.py ---
from pyrepl.console import Event
from pyrepl.tests.infrastructure import ReaderTestCase, EA, run_testcase

class SimpleTestCase(ReaderTestCase):

    def test_basic(self):
        self.run_test([(('self-insert', 'a'), ['a']),
                       ( 'accept',            ['a'])])

    def test_repeat(self):
        self.run_test([(('digit-arg', '3'),   ['']),
                       (('self-insert', 'a'), ['aaa']),
                       ( 'accept',            ['aaa'])])

    def test_kill_line(self):
        self.run_test([(('self-insert', 'abc'), ['abc']),
                       ( 'left',                None),
                       ( 'kill-line',           ['ab']),
                       ( 'accept',              ['ab'])])

    def test_unix_line_discard(self):
        self.run_test([(('self-insert', 'abc'), ['abc']),
                       ( 'left',                None),
                       ( 'unix-word-rubout',    ['c']),
                       ( 'accept',              ['c'])])

    def test_kill_word(self):
        self.run_test([(('self-insert', 'ab cd'), ['ab cd']),
                       ( 'beginning-of-line',     ['ab cd']),
                       ( 'kill-word',             [' cd']),
                       ( 'accept',                [' cd'])])

    def test_backward_kill_word(self):
        self.run_test([(('self-insert', 'ab cd'), ['ab cd']),
                       ( 'backward-kill-word',    ['ab ']),
                       ( 'accept',                ['ab '])])

    def test_yank(self):
        self.run_test([(('self-insert', 'ab cd'), ['ab cd']),
                       ( 'backward-kill-word',    ['ab ']),
                       ( 'beginning-of-line',     ['ab ']),
                       ( 'yank',                  ['cdab ']),
                       ( 'accept',                ['cdab '])])
        
    def test_yank_pop(self):
        self.run_test([(('self-insert', 'ab cd'), ['ab cd']),
                       ( 'backward-kill-word',    ['ab ']),
                       ( 'left',                  ['ab ']),
                       ( 'backward-kill-word',    [' ']),
                       ( 'yank',                  ['ab ']),
                       ( 'yank-pop',              ['cd ']),
                       ( 'accept',                ['cd '])])

    def test_interrupt(self):
        try:
            self.run_test([( 'interrupt',  [''])])
        except KeyboardInterrupt:
            pass
        else:
            self.fail('KeyboardInterrupt got lost')

    # test_suspend -- hah

    def test_up(self):
        self.run_test([(('self-insert', 'ab\ncd'), ['ab', 'cd']),
                       ( 'up',                     ['ab', 'cd']),
                       (('self-insert', 'e'),      ['abe', 'cd']),
                       ( 'accept',                 ['abe', 'cd'])])

    def test_down(self):
        self.run_test([(('self-insert', 'ab\ncd'), ['ab', 'cd']),
                       ( 'up',                     ['ab', 'cd']),
                       (('self-insert', 'e'),      ['abe', 'cd']),
                       ( 'down',                   ['abe', 'cd']),
                       (('self-insert', 'f'),      ['abe', 'cdf']),
                       ( 'accept',                 ['abe', 'cdf'])])

    def test_left(self):
        self.run_test([(('self-insert', 'ab'), ['ab']),
                       ( 'left',               ['ab']),
                       (('self-insert', 'c'),  ['acb']),
                       ( 'accept',             ['acb'])])

    def test_right(self):
        self.run_test([(('self-insert', 'ab'), ['ab']),
                       ( 'left',               ['ab']),
                       (('self-insert', 'c'),  ['acb']),
                       ( 'right',              ['acb']),
                       (('self-insert', 'd'),  ['acbd']),
                       ( 'accept',             ['acbd'])])
        
def test():
    run_testcase(SimpleTestCase)

if __name__ == '__main__':
    test()

--- NEW FILE: bugs.py ---
from pyrepl.console import Event
from pyrepl.tests.infrastructure import ReaderTestCase, EA, run_testcase

# this test case should contain as-verbatim-as-possible versions of
# (applicable) bug reports

class BugsTestCase(ReaderTestCase):

    def test_transpose_at_start(self):
        self.run_test([( 'transpose', [EA, '']),
                       ( 'accept',    [''])])

def test():
    run_testcase(BugsTestCase)

if __name__ == '__main__':
    test()

--- NEW FILE: infrastructure.py ---
from pyrepl.reader import Reader
from pyrepl.console import Console, Event
import unittest
import sys

class EqualsAnything(object):
    def __eq__(self, other):
        return True
EA = EqualsAnything()

class TestConsole(Console):
    height = 24
    width = 80
    encoding = 'utf-8'

    def __init__(self, events, testcase, verbose=False):
        self.events = events
        self.next_screen = None
        self.verbose = verbose
        self.testcase = testcase

    def refresh(self, screen, xy):
        if self.next_screen is not None:
            self.testcase.assertEqual(
                screen, self.next_screen,
                "[ %s != %s after %r ]"%(screen, self.next_screen,
                                         self.last_event_name))

    def get_event(self, block=1):
        ev, sc = self.events.pop(0)
        self.next_screen = sc
        if not isinstance(ev, tuple):
            ev = (ev,)
        self.last_event_name = ev[0]
        if self.verbose:
            print "event", ev
        return Event(*ev)

class TestReader(Reader):
    def get_prompt(self, lineno, cursor_on_line):
        return ''
    def refresh(self):
        Reader.refresh(self)
        self.dirty = True

class ReaderTestCase(unittest.TestCase):
    def run_test(self, test_spec, reader_class=TestReader):
        # remember to finish your test_spec with 'accept' or similar!
        con = TestConsole(test_spec, self)
        reader = reader_class(con)
        reader.readline()

class BasicTestRunner:
    def run(self, test):
        result = unittest.TestResult()
        test(result)
        return result

def run_testcase(testclass):
    suite = unittest.makeSuite(testclass)
    runner = unittest.TextTestRunner(sys.stdout, verbosity=1)
    result = runner.run(suite)
    

--- NEW FILE: wishes.py ---
from pyrepl.console import Event
from pyrepl.tests.infrastructure import ReaderTestCase, EA, run_testcase

# this test case should contain as-verbatim-as-possible versions of
# (applicable) feature requests

class WishesTestCase(ReaderTestCase):

    def test_quoted_insert_repeat(self):
        self.run_test([(('digit-arg', '3'),      ['']),
                       ( 'quoted-insert',        ['']),
                       (('self-insert', '\033'), ['^[^[^[']),
                       ( 'accept',               None)])

def test():
    run_testcase(WishesTestCase)

if __name__ == '__main__':
    test()