#!/usr/bin/env python
#coding:utf-8
# Author:  mozman --<mozman@gmx.at>
# Purpose: test drawing module
# Created: 11.09.2010
# Copyright (C) 2010, Manfred Moitzi
# License: GPLv3
from __future__ import unicode_literals

import os
import unittest
from io import StringIO

from svgwrite.drawing import Drawing
from svgwrite.container import Group

class TestDrawingFullProfile(unittest.TestCase):
    def test_empty_drawing(self):
        dwg = Drawing()
        result = dwg.tostring()
        self.assertEqual(result, '<svg baseProfile="full" height="100%" version="1.1" '\
            'width="100%" xmlns="http://www.w3.org/2000/svg" '\
            'xmlns:ev="http://www.w3.org/2001/xml-events" '\
            'xmlns:xlink="http://www.w3.org/1999/xlink"><defs /></svg>')

    def test_stylesheet(self):
        dwg = Drawing()
        dwg.add_stylesheet('test.css', 'Test')
        f = StringIO()
        dwg.write(f)
        result = f.getvalue()
        f.close()
        self.assertEqual(result, '<?xml version="1.0" encoding="utf-8" ?>\n' \
            '<?xml-stylesheet href="test.css" type="text/css" title="Test" alternate="no" media="screen"?>\n'
            '<svg baseProfile="full" height="100%" version="1.1" width="100%" '\
            'xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" '\
            'xmlns:xlink="http://www.w3.org/1999/xlink"><defs /></svg>')

    def test_save(self):
        fn = 'test_drawing.svg'
        if os.path.exists(fn):
            os.remove(fn)
        dwg = Drawing(fn)
        dwg.save()
        self.assertTrue(os.path.exists(fn))
        os.remove(fn)

    def test_save_as(self):
        fn = 'test_drawing.svg'
        if os.path.exists(fn):
            os.remove(fn)
        dwg = Drawing()
        dwg.saveas(fn)
        self.assertTrue(os.path.exists(fn))
        os.remove(fn)

    def test_non_us_ascii_chars(self):
        dwg = Drawing()
        dwg.set_desc('öäü')
        f = StringIO()
        dwg.write(f)
        result = f.getvalue()
        f.close()
        self.assertEqual(result,
            '<?xml version="1.0" encoding="utf-8" ?>\n' \
            '<svg baseProfile="full" height="100%" version="1.1" width="100%" '\
            'xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" '\
            'xmlns:xlink="http://www.w3.org/1999/xlink">'
            '<title>öäü</title><defs /></svg>')

class TestDrawingTinyProfile(unittest.TestCase):
    def test_empty_drawing(self):
        dwg = Drawing(profile="tiny")
        result = dwg.tostring()
        self.assertEqual(result, '<svg baseProfile="tiny" height="100%" version="1.2" '\
            'width="100%" xmlns="http://www.w3.org/2000/svg" '\
            'xmlns:ev="http://www.w3.org/2001/xml-events" '\
            'xmlns:xlink="http://www.w3.org/1999/xlink"><defs /></svg>')

    def test_stylesheet(self):
        dwg = Drawing(profile="tiny")
        dwg.add_stylesheet('test.css', 'Test')
        f = StringIO()
        dwg.write(f)
        result = f.getvalue()
        f.close()
        self.assertEqual(result, '<?xml version="1.0" encoding="utf-8" ?>\n' \
            '<?xml-stylesheet href="test.css" type="text/css" title="Test" alternate="no" media="screen"?>\n'
            '<svg baseProfile="tiny" height="100%" version="1.2" width="100%" '\
            'xmlns="http://www.w3.org/2000/svg" '\
            'xmlns:ev="http://www.w3.org/2001/xml-events" '\
            'xmlns:xlink="http://www.w3.org/1999/xlink"><defs /></svg>')

class TestDefs(unittest.TestCase):
    def test_simple_defs(self):
        dwg = Drawing()
        g = dwg.defs.add(Group(id='test'))
        inner_g = g.add(Group(id='innerTest'))
        result = dwg.tostring()
        self.assertEqual(result, '<svg baseProfile="full" height="100%" version="1.1" '\
            'width="100%" xmlns="http://www.w3.org/2000/svg" '\
            'xmlns:ev="http://www.w3.org/2001/xml-events" '\
            'xmlns:xlink="http://www.w3.org/1999/xlink">' \
            '<defs><g id="test"><g id="innerTest" /></g></defs></svg>')


if __name__=='__main__':
    unittest.main()

[((108, 4, 108, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((19, 14, 19, 23), 'svgwrite.drawing.Drawing', 'Drawing', ({}, {}), '()', False, 'from svgwrite.drawing import Drawing\n'), ((27, 14, 27, 23), 'svgwrite.drawing.Drawing', 'Drawing', ({}, {}), '()', False, 'from svgwrite.drawing import Drawing\n'), ((29, 12, 29, 22), 'io.StringIO', 'StringIO', ({}, {}), '()', False, 'from io import StringIO\n'), ((41, 11, 41, 29), 'os.path.exists', 'os.path.exists', ({(41, 26, 41, 28): 'fn'}, {}), '(fn)', False, 'import os\n'), ((43, 14, 43, 25), 'svgwrite.drawing.Drawing', 'Drawing', ({(43, 22, 43, 24): 'fn'}, {}), '(fn)', False, 'from svgwrite.drawing import Drawing\n'), ((46, 8, 46, 21), 'os.remove', 'os.remove', ({(46, 18, 46, 20): 'fn'}, {}), '(fn)', False, 'import os\n'), ((50, 11, 50, 29), 'os.path.exists', 'os.path.exists', ({(50, 26, 50, 28): 'fn'}, {}), '(fn)', False, 'import os\n'), ((52, 14, 52, 23), 'svgwrite.drawing.Drawing', 'Drawing', ({}, {}), '()', False, 'from svgwrite.drawing import Drawing\n'), ((55, 8, 55, 21), 'os.remove', 'os.remove', ({(55, 18, 55, 20): 'fn'}, {}), '(fn)', False, 'import os\n'), ((58, 14, 58, 23), 'svgwrite.drawing.Drawing', 'Drawing', ({}, {}), '()', False, 'from svgwrite.drawing import Drawing\n'), ((60, 12, 60, 22), 'io.StringIO', 'StringIO', ({}, {}), '()', False, 'from io import StringIO\n'), ((73, 14, 73, 37), 'svgwrite.drawing.Drawing', 'Drawing', (), '', False, 'from svgwrite.drawing import Drawing\n'), ((81, 14, 81, 37), 'svgwrite.drawing.Drawing', 'Drawing', (), '', False, 'from svgwrite.drawing import Drawing\n'), ((83, 12, 83, 22), 'io.StringIO', 'StringIO', ({}, {}), '()', False, 'from io import StringIO\n'), ((96, 14, 96, 23), 'svgwrite.drawing.Drawing', 'Drawing', ({}, {}), '()', False, 'from svgwrite.drawing import Drawing\n'), ((42, 12, 42, 25), 'os.remove', 'os.remove', ({(42, 22, 42, 24): 'fn'}, {}), '(fn)', False, 'import os\n'), ((45, 24, 45, 42), 'os.path.exists', 'os.path.exists', ({(45, 39, 45, 41): 'fn'}, {}), '(fn)', False, 'import os\n'), ((51, 12, 51, 25), 'os.remove', 'os.remove', ({(51, 22, 51, 24): 'fn'}, {}), '(fn)', False, 'import os\n'), ((54, 24, 54, 42), 'os.path.exists', 'os.path.exists', ({(54, 39, 54, 41): 'fn'}, {}), '(fn)', False, 'import os\n'), ((97, 25, 97, 41), 'svgwrite.container.Group', 'Group', (), '', False, 'from svgwrite.container import Group\n'), ((98, 24, 98, 45), 'svgwrite.container.Group', 'Group', (), '', False, 'from svgwrite.container import Group\n')]