#!/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()

[((4224, 4239), 'unittest.main', 'unittest.main', ([], {}), '()\n', (4237, 4239), False, 'import unittest\n'), ((461, 470), 'svgwrite.drawing.Drawing', 'Drawing', ([], {}), '()\n', (468, 470), False, 'from svgwrite.drawing import Drawing\n'), ((844, 853), 'svgwrite.drawing.Drawing', 'Drawing', ([], {}), '()\n', (851, 853), False, 'from svgwrite.drawing import Drawing\n'), ((915, 925), 'io.StringIO', 'StringIO', ([], {}), '()\n', (923, 925), False, 'from io import StringIO\n'), ((1515, 1533), 'os.path.exists', 'os.path.exists', (['fn'], {}), '(fn)\n', (1529, 1533), False, 'import os\n'), ((1577, 1588), 'svgwrite.drawing.Drawing', 'Drawing', (['fn'], {}), '(fn)\n', (1584, 1588), False, 'from svgwrite.drawing import Drawing\n'), ((1663, 1676), 'os.remove', 'os.remove', (['fn'], {}), '(fn)\n', (1672, 1676), False, 'import os\n'), ((1753, 1771), 'os.path.exists', 'os.path.exists', (['fn'], {}), '(fn)\n', (1767, 1771), False, 'import os\n'), ((1815, 1824), 'svgwrite.drawing.Drawing', 'Drawing', ([], {}), '()\n', (1822, 1824), False, 'from svgwrite.drawing import Drawing\n'), ((1903, 1916), 'os.remove', 'os.remove', (['fn'], {}), '(fn)\n', (1912, 1916), False, 'import os\n'), ((1974, 1983), 'svgwrite.drawing.Drawing', 'Drawing', ([], {}), '()\n', (1981, 1983), False, 'from svgwrite.drawing import Drawing\n'), ((2026, 2036), 'io.StringIO', 'StringIO', ([], {}), '()\n', (2034, 2036), False, 'from io import StringIO\n'), ((2591, 2614), 'svgwrite.drawing.Drawing', 'Drawing', ([], {'profile': '"""tiny"""'}), "(profile='tiny')\n", (2598, 2614), False, 'from svgwrite.drawing import Drawing\n'), ((2988, 3011), 'svgwrite.drawing.Drawing', 'Drawing', ([], {'profile': '"""tiny"""'}), "(profile='tiny')\n", (2995, 3011), False, 'from svgwrite.drawing import Drawing\n'), ((3073, 3083), 'io.StringIO', 'StringIO', ([], {}), '()\n', (3081, 3083), False, 'from io import StringIO\n'), ((3703, 3712), 'svgwrite.drawing.Drawing', 'Drawing', ([], {}), '()\n', (3710, 3712), False, 'from svgwrite.drawing import Drawing\n'), ((1548, 1561), 'os.remove', 'os.remove', (['fn'], {}), '(fn)\n', (1557, 1561), False, 'import os\n'), ((1634, 1652), 'os.path.exists', 'os.path.exists', (['fn'], {}), '(fn)\n', (1648, 1652), False, 'import os\n'), ((1786, 1799), 'os.remove', 'os.remove', (['fn'], {}), '(fn)\n', (1795, 1799), False, 'import os\n'), ((1874, 1892), 'os.path.exists', 'os.path.exists', (['fn'], {}), '(fn)\n', (1888, 1892), False, 'import os\n'), ((3739, 3755), 'svgwrite.container.Group', 'Group', ([], {'id': '"""test"""'}), "(id='test')\n", (3744, 3755), False, 'from svgwrite.container import Group\n'), ((3782, 3803), 'svgwrite.container.Group', 'Group', ([], {'id': '"""innerTest"""'}), "(id='innerTest')\n", (3787, 3803), False, 'from svgwrite.container import Group\n')]