def test_add_message(self): r = roost.Roost(mocks.Context()) r.drop_cache = Mock() r.redisplay = Mock() m = messages.SnipeMessage(r, 'foo', 1.0) r.add_message(m) r.drop_cache.assert_called() r.redisplay.assert_called_with(m, m) m = messages.SnipeMessage(r, 'bar', 1.0) r.add_message(m) self.assertEqual(2, len(r.messages)) self.assertEqual(1.00001, r.messages[-1].time)
def test_body(self): context = mocks.Context() s = SyntheticBackend(context, 'synthetic') m = messages.SnipeMessage(s, 'foo', 0.0) self.assertEqual(str(m.sender), 'synthetic') self.assertEqual(m.OnelineDecor.body(m), []) self.assertEqual(m.Decor.body(m), [(set(), 'foo\n')]) s.indent = 'X ' self.assertEqual(m.Decor.body(m), [(set(), 'X foo\n')])
def test(self): os.environ['TZ'] = 'GMT' context = mocks.Context() s = SyntheticBackend(context, 'synthetic') m = messages.SnipeMessage(s, 'foo', 0.0) self.assertEqual(str(m.sender), 'synthetic') self.assertEqual(str(m), '00:00 synthetic\nfoo') self.assertEqual( m.display({}), [ ({'bold'}, 'synthetic'), ({'right'}, ' 00:00:00'), (set(), 'foo\n'), ]) self.assertIsNone(m.canon('foo', None)) self.assertEqual(m.field('foo'), '') m.data['bar'] = 5 self.assertEqual(m.field('bar'), 5) m.data['baz'] = 'quux' self.assertEqual(m.field('baz'), 'quux') timeything = collections.namedtuple('TimeyThing', ['time'])(0.0) self.assertEqual(m._coerce(timeything), 0.0) class IntyThing: __int__ = lambda self: 0 # noqa: E731 intything = IntyThing() self.assertEqual(m._coerce(intything), 0) class FloatyThing: __float__ = lambda self: 0.0 # noqa: E731 floatything = FloatyThing() self.assertEqual(m._coerce(floatything), 0.0) self.assertEqual(m._coerce('foo'), 'foo') self.assertEqual(m.reply(), s.name) self.assertEqual(m.followup(), s.name) self.assertEqual(m.filter(), filters.Compare('==', 'backend', s.name)) self.assertEqual(float(m), m.time) m.transform('foo', 'bar') self.assertEqual(m.transformed, 'foo') self.assertEqual(m.body, 'bar') self.assertIs( m.get_decor({'decor': 'test_messages.TestMessage'}), TestMessage) self.assertIs( m.get_decor({'decor': 'nonexistent.object'}), messages.SnipeMessage.Decor)
def start(self): super().start() count = self.conf.get('count', 1) string = self.conf.get('string', '0123456789') width = self.conf.get('width', 72) if self.myname is None: self.myname = '%s-%d-%s-%d' % (self.name, count, string, width) now = int(time.time()) self.messages = [ messages.SnipeMessage( self, ''.join(itertools.islice(itertools.cycle(string), i, i + width)), now - count + i) for i in range(count) ]
def test_replymode(self): w = prompt.LongPrompt( None, modes=[prompt.ReplyMode(messages.SnipeMessage(None, 'pants'))]) w.keymap[chr(ord('C') - ord('@'))][chr(ord('Y') - ord('@'))](w) self.assertEqual(w.input(), '> pants')