Example #1
0
 def __init__(self, gui):
     gtk.Entry.__init__(self)
     self.realm = gui.realm
     self.gui = gui
     self.tabdict = Trie()
     self.hist = CommandHistory(200)
     self.connect('key-press-event', self.key_pressed_cb)
     self.modify_font(pango.FontDescription('monospace 8'))
Example #2
0
 def setUp(self):
     self.trie = Trie()
     self.words = []
Example #3
0
 def setUp(self):
     self.sample = ['barbells', 'foobinate', 'bazaar', 'spam', 
                    'spamandeggs', 'baza']
     self.trie = Trie()
     for s in self.sample:
         self.trie.add_word(s)
Example #4
0
class Test_complete_and_keys:

    def setUp(self):
        self.sample = ['barbells', 'foobinate', 'bazaar', 'spam', 
                       'spamandeggs', 'baza']
        self.trie = Trie()
        for s in self.sample:
            self.trie.add_word(s)

    def test_simple_word_replace(self):
        line, ind = self.trie.complete('foo bar baz', 5)
        assert line == 'foo barbells baz'

    def test_simple_index_setting(self):
        line, ind = self.trie.complete('foo bar baz', 5)
        assert ind == 12 #the end of barbells

    def test_nothing_happen_on_space(self):
        line = 'foo bar'
        ind = 4
        assert self.trie.complete(line, ind) == (line, ind)

    def test_one_word_not_in_tabdict(self):
        line = 'qux'
        ind = 3
        res = self.trie.complete(line, ind)
        print res
        assert res == (line, ind)

    def test_word_and_longer_word_in_tabdict(self):
        line = 'spam'
        ind = 2
        res, ind = self.trie.complete(line, ind)
        assert res == 'spamandeggs'

    def test_longer_word_not_in_tabdict(self):
        line = 'Spamandeggs'
        ind = 2
        res, ind = self.trie.complete(line, ind)
        assert res == 'Spamandeggs'

    def test_preserves_user_capitalisation(self):
        line = 'foo Bar baz'
        ind = 5
        res, ind = self.trie.complete(line, ind)
        assert res == 'foo Barbells baz', res

    def test_cursor_on_very_first_character_no_op(self):
        line = 'foo'
        ind = 0
        res, ind = self.trie.complete(line, ind)
        assert res == 'foo'
        assert ind == 0

    def test_on_long_but_unextendable_word(self):
        line = 'bazaar'
        ind = 3
        res, ind = self.trie.complete(line, ind)
        assert line == 'bazaar'
        assert ind == 6, ind

    def test_longer_word_also_in_tabdict(self):
        line = 'baza'
        ind = 3
        res, ind = self.trie.complete(line, ind)
        assert res == 'bazaar', res
        assert ind == 6

    def test_extend_special_case(self):
        line = 'baz'
        ind = 3
        res, ind = self.trie.complete(line, ind)
        assert res == 'baza', res
        assert ind == 4

    def test_does_right_thing_with_ordering(self):
        self.trie.add_word("frobble")
        self.trie.add_word("frobulate")
        self.trie.add_word("frobble")
        res, ind = self.trie.complete('frob', 4)
        assert res == 'frobble', res

    def test_retains_capitalisation(self):
        res, ind = self.trie.complete("FOoBI", 3)
        assert res == "FOoBInate"

    def test_makes_all_caps_if_all_caps_previously(self):
        res, ind = self.trie.complete("FOO", 2)
        assert res == "FOOBINATE"

    def test_doesnt_make_all_caps_if_only_one_char(self):
        res, ind = self.trie.complete("F", 1)
        assert res == "Foobinate"
Example #5
0
def test_add_line_only_adds_words_not_splitting_characters():
    trie = Trie()
    trie.add_word = Mock()
    trie.add_line('foo, bar  ')
    assert trie.add_word.call_args_list == [(('foo',), {}), 
                                            (('bar',), {})]
Example #6
0
class Test_Adding:

    def setUp(self):
        self.trie = Trie()
        self.words = []

    def test_add_word_adds_to_the_trie(self):
        self.trie.add_word('foo')
        assert self.trie._fetch('foo') == 'foo'

    def test_add_word_is_caseless(self):
        self.trie.add_word("FOO")
        assert self.trie._fetch('foo') == 'foo'

    def test_add_line_adds_all_words_in_line(self):
        self.trie.add_line('foo bar baz')
        assert self.trie._fetch('foo') == 'foo'
        assert self.trie._fetch('bar') == 'bar'
        assert self.trie._fetch('baz') == 'baz'

    def test_add_line_adds_words_in_the_order_they_appear(self):
        self.trie.add_line('bar, baz')
        assert self.trie._fetch('ba') == 'baz'

    def test_add_line_works_nicely_with_complete(self):
        self.trie.add_line(' foo bar ')
        line, ind = self.trie.complete('fo', 2)
        assert line == 'foo'
        assert ind == 3
Example #7
0
def test_retrieves_shorter_but_more_recent_word_without_extend():
    trie = Trie()
    trie.add_word("zeepf")
    trie.add_word("zee")
    res, ind = trie.complete("ze", 1)
    assert res == "zee", res
Example #8
0
class CommandView(gtk.Entry):
    """The area where the user enters commands to be sent to the MUD."""
    def __init__(self, gui):
        gtk.Entry.__init__(self)
        self.realm = gui.realm
        self.gui = gui
        self.tabdict = Trie()
        self.hist = CommandHistory(200)
        self.connect('key-press-event', self.key_pressed_cb)
        self.modify_font(pango.FontDescription('monospace 8'))

    def key_pressed_cb(self, widget, event):
        """The user's pressed a key.

        First, this checks to see if there is a macro bound for the keychord,
        and if there is then it is run; if not, the key is handled by PyGTK.
        """
        chord = from_gtk_event(event)

        if not self.gui.realm.maybe_do_macro(chord):
            #not a macro, so keep processing.
            return False
        return True

    def history_up(self):
        """Move up (ie, back in time) one command in the history."""
        #cursor will be at the end of the line, as it has no left gravity.
        self.set_text(self.hist.advance())
        self.set_position(-1)

    def history_down(self):
        """Move down (ie, forwards in time) one command in the history."""
        self.set_text(self.hist.retreat())
        self.set_position(-1)

    def get_all_text(self):
        """Finger-saving mathod to get all the text from the buffer."""
        bytes = self.get_chars(0, -1)
        return bytes.decode('utf-8')

    def escape_pressed(self):
        """Add the current line to the list of previous commands, and clear
        the buffer.
        """
        self.hist.add_command(self.get_all_text())
        self.set_text('')

    def submit_line(self):
        """Send the current line to the MUD and clear the buffer."""
        text = self.get_all_text()
        self.set_text('')
        self.realm.receive_gui_line(text)
        if not self.realm.server_echo:
            self.hist.add_command(text)

    def tab_complete(self):
        """Tab-completion."""
        line = self.get_all_text()
        #cursor position as an integer from the start of the line
        ind = self.get_position()
        line, ind = self.tabdict.complete(line, ind)
        self.set_text(line)
        #move the cursor to where the tabdict wants it
        self.set_position(ind)

    def add_line_to_tabdict(self, line):
        """Add all the new words in the line to our tabdict."""
        self.tabdict.add_line(line)