Example #1
0
class Test_write:

    def setUp(self):
        self.realm = RootRealm(Mock())
        self.realm.telnet = Mock()
        self.p = Mock()
        self.realm.addProtocol(self.p)
        self.noting_line = simpleml("foo", Mock(), Mock())
        
    def writer(self, match, realm):
        print 'writer called!'
        realm.write(self.noting_line)

    @property
    def lines_gotten(self):
        return [line for ((line,), kwargs) in 
                            self.p.metalineReceived.call_args_list]

    def test_from_not_a_string(self):
        self.realm.write(42)
        assert len(self.lines_gotten) == 1
        assert self.lines_gotten[0].line == '42'

    def test_from_string(self):
        self.realm.write('spam')
        assert len(self.lines_gotten) == 1
        assert self.lines_gotten[0].line == 'spam'

    def test_from_metaline(self):
        ml = Metaline('foo', None, None)
        self.realm.write(ml)
        assert self.lines_gotten == [ml]

    def test_no_colourbleed_fg(self):
        self.realm.write("eggs")
        cols = self.lines_gotten[0].fores.items()
        expected = [(0, fg_code(WHITE, False))]
        assert cols == expected, (cols, expected)

    def test_no_colourbleed_bg(self):
        self.realm.write("eggs")
        cols = self.lines_gotten[0].backs.items()
        assert cols ==  [(0, bg_code(BLACK))], cols

    def test_passes_on_wrap_default(self):
        self.realm.write("eggs")
        assert not self.lines_gotten[0].wrap

    def test_soft_line_start_default_is_off(self):
        self.realm.write("barbaz")
        assert not self.lines_gotten[0].soft_line_start

    def test_passes_on_soft_line_start(self):
        self.realm.write('foo', soft_line_start = True)
        assert self.lines_gotten[0].soft_line_start

    noting_trigger = binding_trigger('bar')(writer)
    noting_alias = binding_alias('bar')(writer)

    def test_write_writes_after_during_matching_triggers(self):
        self.realm.triggers.append(self.noting_trigger)
        inline = Metaline('bar', set(), set())
        self.realm.metalineReceived(inline)
        assert self.lines_gotten == [inline, self.noting_line], \
               self.lines_gotten

    def test_write_writes_after_during_alias_matching(self):
        self.realm.aliases.append(self.noting_alias)
        inline = Metaline('bar', RunLengthList([(0, fg_code(WHITE, False))]),
                          RunLengthList([(0, bg_code(BLACK))]),
                          soft_line_start = True)
        self.realm.send('bar')
        print self.lines_gotten
        print
        expected = [inline, self.noting_line]
        print expected
        assert self.lines_gotten == expected

    def tracer(self, match, realm):
        realm.trace("Foo")

    tracing_trigger = binding_trigger("baz")(tracer)
    tracing_alias = binding_alias("baz")(tracer)

    def test_trace_writes_after_during_matching_triggers(self):
        self.realm.tracing = True
        self.realm.triggers.append(self.tracing_trigger)
        inline = Metaline('baz', set(), set())
        self.realm.metalineReceived(inline)
        expected_lines = [simpleml("\nTRACE: %s matched!" % self.tracing_trigger,
                                   fg_code(WHITE, False), bg_code(BLACK)),
                          simpleml("\nTRACE: Foo", fg_code(WHITE, False),
                                   bg_code(BLACK))]
        print self.lines_gotten
        print
        expected = [inline] + expected_lines
        print expected
        assert self.lines_gotten == expected

    def test_trace_writes_after_during_alias_matching(self):
        self.realm.tracing = True
        self.realm.aliases.append(self.tracing_alias)
        inline = Metaline('baz', RunLengthList([(0, fg_code(WHITE, False))]),
                          RunLengthList([(0, bg_code(BLACK))]),
                          soft_line_start = True)
        self.realm.send('baz')
        expected_lines = [simpleml("\nTRACE: %s matched!" % self.tracing_alias,
                                   fg_code(WHITE, False), bg_code(BLACK)),
                          simpleml("\nTRACE: Foo", fg_code(WHITE, False),
                                   bg_code(BLACK))]
        print self.lines_gotten
        print
        expected = [inline] + expected_lines
        print expected
        assert self.lines_gotten == expected
Example #2
0
class Test_send:

    def setUp(self):
        self.realm = RootRealm(Mock())
        self.p = Mock()
        self.realm.addProtocol(self.p)
        self.realm.telnet = Mock()

    @property
    def lines_gotten(self):
        return [line for ((line,), kwargs) in 
                            self.p.metalineReceived.call_args_list]

    def test_send_sends_to_the_mud(self):
        self.realm.send("bar")
        assert self.realm.telnet.sendLine.call_args_list == [(('bar',), {})]

    def test_send_echos_by_default(self):
        self.realm.send("bar")
        expected = Metaline('bar', 
                            RunLengthList([(0, fg_code(WHITE, False))]),
                            RunLengthList([(0, bg_code(BLACK))]),
                            soft_line_start = True)
        assert self.lines_gotten == [expected]

    def test_send_doesnt_echo_if_told_not_to(self):
        self.realm.send("bar", False)
        assert self.lines_gotten == []

    def test_send_uses_echoes_with_soft_line_start(self):
        self.realm.send("spam")
        expected = Metaline('spam', 
                            RunLengthList([(0, fg_code(WHITE, False))]),
                            RunLengthList([(0, bg_code(BLACK))]),
                            soft_line_start = True)
        assert self.lines_gotten == [expected]

    #TODO: (not tested yet)
    #  - test calling send in a trigger and echoing then
    #  - test recursive calls to send properly
    #  - actually test matching properly

    @binding_alias("bar")
    def our_alias_1(self, match, realm):
        print 'sending after!'
        realm.send_after("foo")

    def test_send_after_sends_afterwards(self):
        self.realm.aliases.append(self.our_alias_1)
        self.realm.send('bar')

        assert self.realm.telnet.sendLine.call_args_list == [(('bar',), {}), 
                                                             (('foo',), {})]

    def test_send_after_default_echoing_is_off(self):
        self.realm.aliases.append(self.our_alias_1)
        self.realm.send("bar")
        expected = [Metaline('bar', 
                             RunLengthList([(0, fg_code(WHITE, False))]),
                             RunLengthList([(0, bg_code(BLACK))]),
                             soft_line_start = True)]
        assert self.lines_gotten == expected

    @binding_alias('baz')
    def our_alias_2(self, match, realm):
        realm.send_after("eggs", echo = False)

    def test_send_after_doesnt_echo_if_asked_not_to(self):
        self.realm.aliases.append(self.our_alias_2)
        self.realm.send("baz")
        expected = [Metaline('baz', 
                             RunLengthList([(0, fg_code(WHITE, False))]),
                             RunLengthList([(0, bg_code(BLACK))]),
                             soft_line_start = True)]
        assert self.lines_gotten == expected

    @binding_alias("foo")
    def foo_alias_sends_bar(self, match, realm):
        print 'Foo alias going!'
        realm.send('bar', echo = True)

    @binding_alias("bar")
    def bar_alias_sends_baz(self, match, realm):
        print 'Bar alias going'
        realm.send('baz', echo = True)

    def test_sends_and_writes_in_a_consistent_order(self):
        self.realm.aliases.append(self.foo_alias_sends_bar)
        self.realm.aliases.append(self.bar_alias_sends_baz)
        self.realm.send("foo", echo = True)

        expect_write = [Metaline("baz",
                                 RunLengthList([(0, fg_code(WHITE, False))]),
                                 RunLengthList([(0, bg_code(BLACK))]),
                                 soft_line_start = True),
                        Metaline("\nbar",
                                 RunLengthList([(0, fg_code(WHITE, False))]),
                                 RunLengthList([(0, bg_code(BLACK))]),
                                 soft_line_start = True),
                        Metaline("\nfoo",
                                 RunLengthList([(0, fg_code(WHITE, False))]),
                                 RunLengthList([(0, bg_code(BLACK))]),
                                 soft_line_start = True)]
        expect_send = ['baz', 'bar', 'foo']
        sent = [line for ((line,), kwargs)
                in self.realm.telnet.sendLine.call_args_list]

        assert self.lines_gotten == expect_write
        assert sent == expect_send

    @binding_alias('spam')
    def noisy_alias(self, match, realm):
        realm.write("FOO FOO FOO")

    def test_writes_come_after_echoing(self):
        self.realm.aliases.append(self.noisy_alias)
        self.realm.send("spam")

        expecting = [Metaline("spam",
                              RunLengthList([(0, fg_code(WHITE, False))]),
                              RunLengthList([(0, bg_code(BLACK))]),
                              soft_line_start = True),
                     Metaline("\nFOO FOO FOO",
                              RunLengthList([(0, fg_code(WHITE, False))]),
                              RunLengthList([(0, bg_code(BLACK))]))]

        assert self.lines_gotten == expecting

    def test_server_echo_defaultly_False(self):
        assert not self.realm.server_echo

    def test_doesnt_echo_if_server_echo_is_True(self):
        self.realm.server_echo = True
        self.realm.send("Foo")
        assert self.lines_gotten == []