Ejemplo n.º 1
0
class Test_metalineReceived:

    def setUp(self):
        self.realm = RootRealm(Mock())
        self.p = Mock()
        self.realm.addProtocol(self.p)
        self.realm.telnet = Mock()
        self.ml = simpleml('foo', sentinel.fores, sentinel.backs)
        self.ml2 = simpleml("bar", sentinel.fores, sentinel.backs)
        self.ml2_written = self.ml2.copy()
        self.ml2_written.insert(0, '\n')

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

    def test_sends_to_screen_normally(self):
        self.realm.metalineReceived(self.ml)
        assert self.lines_gotten == [self.ml]

    @binding_trigger("foo")
    def trigger_1(self, match, realm):
        realm.write(self.ml2)

    def test_write_writes_afterwards(self):
        self.realm.triggers.append(self.trigger_1)
        self.realm.metalineReceived(self.ml)
        assert self.lines_gotten == [self.ml, self.ml2_written]

    @binding_trigger("foo")
    def trigger_2(self, match, realm):
        realm.display_line = False

    def test_doesnt_display_if_asked_not_to(self):
        self.realm.triggers.append(self.trigger_2)
        self.realm.metalineReceived(self.ml)
        assert self.lines_gotten == []

    @binding_alias("spam")
    def bar_writing_alias(self, match, realm):
        realm.write("BAR BAR BAR")
        realm.send_to_mud = False
    
    @binding_trigger('foo')
    def spam_sending_trigger(self, match, realm):
        realm.send("spam")

    def test_aliases_inside_triggers_write_after_trigger_writes(self):
        self.realm.triggers.append(self.spam_sending_trigger)
        self.realm.aliases.append(self.bar_writing_alias)

        noteline = Metaline("\nBAR BAR BAR",
                            RunLengthList([(0, fg_code(WHITE, False))]),
                            RunLengthList([(0, bg_code(BLACK))]))

        self.realm.metalineReceived(self.ml)

        assert self.lines_gotten == [self.ml, noteline]
Ejemplo n.º 2
0
class Test_addProtocol:

    def setUp(self):
        self.factory = TelnetClientFactory(None, 'ascii', None)
        self.realm = RootRealm(self.factory)
        self.realm.telnet = self.telnet = Mock(spec = TelnetClient)
        self.receiver = Mock()
        self.realm.addProtocol(self.receiver)

    def test_passes_on_connection_lost(self):
        self.realm.connectionLost()
        assert self.receiver.connectionLost.called

    @patch('pymudclient.realms', 'time')
    def test_connection_lost_writes_message(self, our_time):
        our_time.strftime.return_value = 'FOOBAR'
        self.realm.write = Mock()
        self.realm.connectionLost()
        assert self.realm.write.called
        ml = self.realm.write.call_args[0][0]
        assert ml.line == 'FOOBAR'
        assert ml.fores.items() == [(0, HexFGCode(0xFF, 0xAA, 0x00))]
        assert ml.backs.items() == [(0, bg_code(BLACK))]

    @patch('pymudclient.realms', 'time')
    def test_connection_made_writes_message(self, our_time):
        our_time.strftime.return_value = 'FOOBAR'
        self.realm.write = Mock()
        self.realm.connectionMade()
        assert self.realm.write.called
        ml = self.realm.write.call_args[0][0]
        assert ml.line == 'FOOBAR'
        assert ml.fores.items() == [(0, HexFGCode(0xFF, 0xAA, 0x00))]
        assert ml.backs.items() == [(0, bg_code(BLACK))]

    def test_passes_on_connection_made(self):
        self.realm.connectionMade()
        assert self.receiver.connectionMade.called

    def test_sends_connection_lost_and_close_in_right_order(self):
        self.realm.close()
        #simulate Twisted's 'throw-it-over-the-wall' anti-guarantee
        self.realm.connectionLost()
        calls = [mname for (mname, args, kws) in self.receiver.method_calls]
        #the metalineReceived is the closing note
        assert calls == ['metalineReceived', 'connectionLost', 'close'], calls

    def test_connection_lost_then_close_works(self):
        self.realm.connectionLost()
        self.realm.close()
        calls = [mname for (mname, args, kws) in self.receiver.method_calls]
        assert calls == ['metalineReceived', 'connectionLost', 'close'], calls
Ejemplo n.º 3
0
class TestRuneguardTriggers(unittest.TestCase):
    @property
    def lines_gotten(self):
        return [line for ((line,), kwargs) in 
                    self.protocol.metalineReceived.call_args_list]
    @property
    def telnet_lines_gotten(self):
        return [line for ((line,),kwargs) in
                    self.realm.telnet.sendLine.call_args_list]
        
    def setUp(self):
        self.realm=RootRealm(None)
        self.realm.factory=TestFactory()
        self.realm.module_settings_dir='/home/dmitry/muds/settings'
        
        import sys
        sys.path.append('/home/dmitry/dev/modules')
        
        from runeguard import Runeguard
        #sr = ShieldRez(self.realm)
        self.realm.load_module(Runeguard)
        self.realm.telnet = TestPrinter()
        self.protocol = TestPrinter()
        self.realm.addProtocol(self.protocol)
        self.realm.state['target']='Iniar'
    
    def test_rune_fired(self):
        self.realm.state['target']='Emily'
        self.realm.metalineReceived(simpleml("You concentrate on the sowulu rune on an ink-stained snowy marble tablet, and its searing image suddenly flares on Emily's skin.",None,None))
        self.realm.receive_gui_line('rv')
        self.realm.metalineReceived(simpleml("You concentrate on the pithakhan rune on an ink-stained snowy marble tablet, and its searing image suddenly flares on Emily's skin.",None,None))
        self.realm.receive_gui_line('rv')
        self.realm.metalineReceived(simpleml("The residual effects of the sowulu rune around Emily fade.",None,None))
        self.realm.receive_gui_line('rv')
        
    def test_rebounding_on(self):
        self.realm.state['last_command_type']='attack'
        self.realm.metalineReceived(simpleml('You suddenly perceive the vague outline of an aura of rebounding around Iniar.',None,None))
        self.realm.state['last_command_type']='raze'
        self.realm.metalineReceived(simpleml('A shimmering translucent shield forms around Iniar.',None,None))
        self.realm.metalineReceived(simpleml('The shimmering translucent shield around Iniar fades away.',None,None))
        self.realm.metalineReceived(simpleml('Iniar\'s aura of weapons rebounding disappears.',None,None))
        
        #print([l.line for l in self.lines_gotten])
        #print([l for l in self.telnet_lines_gotten])
Ejemplo n.º 4
0
class TestTriggers(unittest.TestCase):
    def setUp(self):
        self.realm=RootRealm(None)
        self.realm.load_module(TestModule, True)
        self.realm.telnet = Mock()
        self.protocol = Mock()
        self.realm.addProtocol(self.protocol)
    
    @property
    def lines_gotten(self):
        return [line for ((line,), kwargs) in 
                    self.protocol.metalineReceived.call_args_list]
    def test_simple_trigger(self):
        self.realm.metalineReceived(simpleml('abc', None, None))
        return_line = self.lines_gotten[1].line
        assert self.lines_gotten[1].line == '\nsuccess'
        
    def test_list_trigger(self):
        self.realm.metalineReceived(simpleml('highjump', None, None))
        return_line = self.lines_gotten[1].line
        assert return_line=='\nsuccess2'
Ejemplo n.º 5
0
class Test_write_wrapping:
    
    def setUp(self):
        self.realm = RootRealm(Mock())
        self.p = Mock()
        self.realm.addProtocol(self.p)

    def test_line_insertion_with_hard_line_end_no_sls(self):
        self.realm._last_line_end = 'hard'
        self.realm.write(simpleml("foo", sentinel.fore,
                                  sentinel.back))
        rcvd = [ml for ((ml,), _) in self.p.metalineReceived.call_args_list]
        assert rcvd == [simpleml("\nfoo", sentinel.fore, sentinel.back)]

    def test_line_insertion_with_hard_line_end_sls(self):
        self.realm._last_line_end = 'hard'
        ml = simpleml("foo", sentinel.fore, sentinel.back)
        ml.soft_line_start = True
        expected = ml.copy()
        expected.insert(0, '\n')
        self.realm.write(ml)
        rcvd = [ml for ((ml,), _) in self.p.metalineReceived.call_args_list]
        assert rcvd == [expected]

    def test_line_insertion_sle_no_sls(self):
        self.realm._last_line_end = 'soft'
        self.realm.write(simpleml("foo", sentinel.fore,
                                  sentinel.back))
        rcvd = [ml for ((ml,), _) in self.p.metalineReceived.call_args_list]
        assert rcvd == [simpleml("\nfoo", sentinel.fore, sentinel.back)]
        
    def test_line_no_insertion_sle_sls(self):
        self.realm._last_line_end = 'soft'
        ml = simpleml("foo", sentinel.fore, sentinel.back)
        ml.soft_line_start = True
        expected = ml.copy()
        self.realm.write(ml)
        rcvd = [ml for ((ml,), _) in self.p.metalineReceived.call_args_list]
        assert rcvd == [expected]

    def test_respects_no_line_end(self):
        self.realm._last_line_end = None
        ml = simpleml("foo", sentinel.fore, sentinel.back)
        expected = ml
        self.realm.write(ml)
        rcvd = [ml for ((ml,), _) in self.p.metalineReceived.call_args_list]
        assert rcvd == [expected], rcvd

    def test_last_line_end_setting(self):
        ml = simpleml("foo", sentinel.fore, sentinel.back)
        ml.line_end = sentinel.line_end
        self.realm.write(ml)
        assert self.realm._last_line_end == sentinel.line_end

    def test_last_line_end_is_defaultly_None(self):
        assert self.realm._last_line_end is None

    def test_with_wrap(self):
        ml = simpleml("bar", sentinel.fore1, sentinel.fore2)
        ml.wrapped = Mock()
        ml.wrapped.return_value = ml2 = simpleml("foo", sentinel.fore,
                                                 sentinel.back)
        self.realm.write(ml)
        assert ml.wrapped.called
        rcvd = [ml for ((ml,), _) in self.p.metalineReceived.call_args_list]
        assert rcvd == [ml2]
Ejemplo n.º 6
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
Ejemplo n.º 7
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 == []