aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Vilcans <martin@librador.com>2011-12-16 20:51:54 +0100
committerMartin Vilcans <martin@librador.com>2011-12-16 23:05:17 +0100
commit92b56357798394415ae17d4a7253b111be91e01f (patch)
tree1a61de70cc0ed39e5ad6754f8077eb30028e1f26
parent55a6e8f70b5125bdd40012f2a66b943a1169fd6b (diff)
downloadscreenplain-92b56357798394415ae17d4a7253b111be91e01f.tar.gz
Fixed some problems with old RichString tests.
Sometimes the segments of the RichString objects became RichString objects instead of Segment. That's what you get for dynamic typing I guess...
-rw-r--r--screenplain/richstring.py24
-rw-r--r--tests/richstring_test.py29
2 files changed, 40 insertions, 13 deletions
diff --git a/screenplain/richstring.py b/screenplain/richstring.py
index 92b9bdd..e126ae4 100644
--- a/screenplain/richstring.py
+++ b/screenplain/richstring.py
@@ -12,11 +12,16 @@ class RichString(object):
"""A sequence of segments where each segment can have its own style."""
def __init__(self, *segments):
- self.segments = tuple(segments)
+ self.segments = segments
def __repr__(self):
+ if not self.segments:
+ return "empty_string"
return ' + '.join(repr(s) for s in self.segments)
+ def __unicode__(self):
+ return ''.join(unicode(s) for s in self.segments)
+
def startswith(self, string):
"""Checks if the first segment in this string starts with a
specific string.
@@ -29,7 +34,7 @@ class RichString(object):
return self.segments[0].text.startswith(string)
def endswith(self, string):
- """Checks if the last segment in this string starts with a
+ """Checks if the last segment in this string ends with a
specific string.
"""
@@ -81,11 +86,20 @@ class Segment(object):
self.text
)
+ def __unicode__(self):
+ return self.text
+
def __eq__(self, other):
- return self.text == other.text and self.styles == other.styles
+ return (
+ isinstance(other, Segment) and
+ self.text == other.text and self.styles == other.styles
+ )
def __ne__(self, other):
- return self.text != other.text or self.styles != other.styles
+ return (
+ not isinstance(other, Segment) or
+ self.text != other.text or self.styles != other.styles
+ )
def to_html(self):
ordered_styles = list(self.styles)
@@ -187,6 +201,8 @@ bold = _CreateStyledString(set((Bold,)))
italic = _CreateStyledString(set((Italic,)))
underline = _CreateStyledString((Underline,))
+empty_string = RichString()
+
def parse_emphasis(source):
"""Parses emphasis markers like * and ** in a string
diff --git a/tests/richstring_test.py b/tests/richstring_test.py
index 5ce3b4e..0e83aa1 100644
--- a/tests/richstring_test.py
+++ b/tests/richstring_test.py
@@ -6,7 +6,8 @@ import unittest2
from screenplain.richstring import (
RichString, Segment,
Bold, Italic,
- plain, bold, italic, underline
+ plain, bold, italic, underline,
+ empty_string
)
from screenplain.richstring import parse_emphasis
from screenplain.types import Slug, Action, Dialog, DualDialog, Transition
@@ -15,8 +16,10 @@ from screenplain.types import Slug, Action, Dialog, DualDialog, Transition
class LowLevelRichStringTests(unittest2.TestCase):
def test_plain_string_has_one_single_segment(self):
- s = RichString(plain('hello'))
- self.assertEqual((plain('hello'),), s.segments)
+ s = plain('hello')
+ self.assertEqual(1, len(s.segments))
+ self.assertEqual('hello', s.segments[0].text)
+ self.assertEqual(set(), s.segments[0].styles)
class RichStringOperatorTests(unittest2.TestCase):
@@ -28,6 +31,16 @@ class RichStringOperatorTests(unittest2.TestCase):
repr(s)
)
+ def test_repr_on_empty_string(self):
+ self.assertEquals('empty_string', repr(empty_string))
+
+ def test_unicode(self):
+ s = bold('Hello') + plain(' there ') + bold('folks')
+ self.assertEquals(
+ u'Hello there folks',
+ unicode(s)
+ )
+
def test_eq(self):
self.assertEquals(bold('Hello'), bold('Hello'))
self.assertNotEquals(bold('Hello'), bold('Foo'))
@@ -62,14 +75,12 @@ class StyleGeneratorTests(unittest2.TestCase):
class RichStringTests(unittest2.TestCase):
def test_plain_to_html(self):
- self.assertEquals('hello', RichString(plain('hello')).to_html())
+ self.assertEquals('hello', plain('hello').to_html())
def test_to_html(self):
- s = RichString(
- bold('bold'),
- plain(' normal '),
- italic('italic'),
- underline('wonderline')
+ s = (
+ bold('bold') + plain(' normal ') +
+ italic('italic') + underline('wonderline')
)
self.assertEquals(
'<strong>bold</strong> normal <em>italic</em><u>wonderline</u>',