Commits

Tobias Pape committed 3df200e

add missing tests. factor magic constant into RAW_ENCODING

  • Participants
  • Parent commits c0cda5d

Comments (0)

Files changed (5)

File dotviewer/drawgraph.py

 from pygame.locals import *
 
 
+RAW_ENCODING = "utf-8"
 this_dir = os.path.dirname(os.path.abspath(__file__))
 FONT = os.path.join(this_dir, 'font', 'DroidSans.ttf')
 FIXEDFONT = os.path.join(this_dir, 'font', 'DroidSansMono.ttf')
         return default
 
 def forceunicode(name):
-    return name if isinstance(name, unicode) else name.decode("utf-8")
+    return name if isinstance(name, unicode) else name.decode(RAW_ENCODING)
 
 def forcestr(name):
-    return name if isinstance(name, str) else name.encode("utf-8")
+    return name if isinstance(name, str) else name.encode(RAW_ENCODING)
 
 
 class GraphLayout:

File dotviewer/graphdisplay.py

                     if e.key == K_ESCAPE:
                         return None
                     elif e.key == K_RETURN:
-                        return forcestr(text) # return utf-8 encoded
+                        return forcestr(text) # return encoded unicode
                     elif e.key == K_BACKSPACE:
                         text = text[:-1]
                     elif e.unicode and ord(e.unicode) >= ord(' '):

File dotviewer/graphpage.py

 
 class DotFileGraphPage(GraphPage):
     def compute(self, dotfile):
-        from drawgraph import forceunicode
-        f = open(dotfile, 'r')
-        self.source = forceunicode(f.read())
+        import codecs
+        from drawgraph import RAW_ENCODING
+        f = codecs.open(dotfile, 'r', RAW_ENCODING)
+        self.source = f.read()
         f.close()

File dotviewer/test/test_interactive_unicode.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+import py
+import sys, os, signal, thread, time, codecs
+from dotviewer.conftest import option
+from dotviewer.drawgraph import RAW_ENCODING
+
+SOURCE1 = u"""digraph G{
+λ -> b
+b -> μ
+}
+"""
+
+FILENAME = 'graph1.dot'
+
+def setup_module(mod):
+    if not option.pygame:
+        py.test.skip("--pygame not enabled")
+    udir = py.path.local.make_numbered_dir(prefix='usession-dot-', keep=3)
+    f = codecs.open(str(udir.join(FILENAME)), 'wb', RAW_ENCODING)
+    f.write(SOURCE1)
+    f.close()
+
+    from dotviewer import graphclient
+    mod.pkgdir = py.path.local(graphclient.this_dir)
+    mod.udir = udir
+
+    try:
+        del os.environ['GRAPHSERVER']
+    except KeyError:
+        pass
+
+
+def test_dotviewer():
+    print "=== dotviewer.py %s" % FILENAME
+    err = os.system('"%s" "%s"' % (pkgdir.join('dotviewer.py'),
+                                   udir.join(FILENAME)))
+    assert err == 0
+
+    plain_name = FILENAME.replace('.dot','.plain')
+    
+    os.system('dot -Tplain "%s" > "%s"' % (udir.join(FILENAME),
+                                           udir.join(plain_name)))
+    print "=== dotviewer.py %s" % plain_name
+    err = os.system('"%s" "%s"' % (pkgdir.join('dotviewer.py'),
+                                   udir.join(plain_name)))
+    assert err == 0
+
+def test_display_dot_file():
+    from dotviewer.graphclient import display_dot_file
+    print "=== display_dot_file(%s) with GRAPHSERVER=%s" % (
+        FILENAME, os.environ.get('GRAPHSERVER', ''),)
+    display_dot_file(udir.join(FILENAME))
+    print "=== display_dot_file finished"
+
+
+def test_graphserver():
+    import socket
+    s = socket.socket()
+    s.listen(1)
+    host, port = s.getsockname()     # pick a random free port
+    s.close()
+
+    if hasattr(sys, 'pypy_objspaceclass'):
+        python = 'python'
+    else:
+        python = sys.executable
+
+    cmdargs = [python, str(pkgdir.join('graphserver.py')),
+               str(port)]
+    print '* starting:', ' '.join(cmdargs)
+    pid = os.spawnv(os.P_NOWAIT, cmdargs[0], cmdargs)
+    try:
+        time.sleep(1)    # hack - wait a bit to make sure the server is up
+        os.environ['GRAPHSERVER'] = '%s:%d' % (host, port)
+        try:
+            test_display_dot_file()
+        finally:
+            del os.environ['GRAPHSERVER']
+    finally:
+        os.kill(pid, signal.SIGTERM)
+
+def test_colors():
+    from dotviewer import graphpage, graphclient
+    class MyPage(graphpage.DotFileGraphPage):
+        def compute(self, dotfile):
+            super(MyPage, self).compute(dotfile)
+            self.links = {'v2721': 'Hello world',
+                          'v2720': ('Something green', (0, 192, 0)),
+                          }
+    dotfile = str(udir.join(FILENAME))
+    page = MyPage(dotfile)
+    graphclient.display_page(page)
+
+def test_fixedfont():
+    from dotviewer import graphpage, graphclient
+    class MyPage(graphpage.DotFileGraphPage):
+        fixedfont = True
+    dotfile = str(udir.join(FILENAME))
+    page = MyPage(dotfile)
+    page.fixedfont = True
+    graphclient.display_page(page)

File dotviewer/test/test_unicode_util.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+import py
+import codecs
+from dotviewer.drawgraph import RAW_ENCODING, forcestr, forceunicode
+
+SOURCE1 = u"""digraph G{
+λ -> b
+b -> μ
+}
+"""
+
+FILENAME = 'test.dot'
+
+class TestUnicodeUtil(object):
+
+    def test_idempotent(self):
+        x = u"a"
+        assert forceunicode(forcestr(x)) == x
+        
+        x = u"λ"
+        assert forceunicode(forcestr(x)) == x
+
+        assert forceunicode(forcestr(SOURCE1)) == SOURCE1
+
+        x = "a"
+        assert forcestr(forceunicode(x)) == x
+
+        # utf-8 encoded.
+        # fragile, does not consider RAW_ENCODING
+        # x = "\xef\xbb\xbf\xce\xbb"
+        # assert forcestr(forceunicode(x)) == x
+
+    def test_does_not_double_encode(self):
+        x = u"λ"
+        x_e = forcestr(x)
+        assert forcestr(x_e) == x_e
+
+        x_u = forceunicode(x_e)
+        assert forceunicode(x_u) == x_u
+
+    def test_file(self):       
+        udir = py.path.local.make_numbered_dir(prefix='usession-dot-', keep=3)
+        full_filename = str(udir.join(FILENAME))
+        f = codecs.open(full_filename, 'wb', RAW_ENCODING)
+        f.write(SOURCE1)
+        f.close()
+
+        with open(full_filename) as f1:
+            assert forceunicode(f1.read()) == SOURCE1
+
+        f3 = codecs.open(full_filename, 'r', RAW_ENCODING)
+        c = f3.read()
+        f3.close()
+        result = (c == SOURCE1)
+        assert result