1# -*- coding: utf-8 -*-
2#
3#   Test the easyliststyle.py module
4#   Copyright (C) 2008 J. David Eisenberg
5
6#   This program is free software; you can redistribute it and/or modify
7#   it under the terms of the GNU General Public License as published by
8#   the Free Software Foundation; either version 2 of the License, or
9#   (at your option) any later version.
10
11#   This program is distributed in the hope that it will be useful,
12#   but WITHOUT ANY WARRANTY; without even the implied warranty of
13#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#   GNU General Public License for more details.
15
16#   You should have received a copy of the GNU General Public License along
17#   with this program; if not, write to the Free Software Foundation, Inc.,
18#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20# Contributor(s):
21# Søren Roug
22
23import unittest, sys
24from odf import easyliststyle
25from odf.opendocument import OpenDocumentText
26from odf.style import Style, TextProperties
27from odf.text import P, List, ListItem
28
29"""
30This program tests the easyliststyle.py module.
31It creates a file named "easylist_odfpy.odt"
32with a bulleted list, a numbered list, and a
33mixed list.
34"""
35
36bulletListSpec = '*,>,#,%'
37mixedListSpec = u'1.!\u273f!a)'
38numberListSpecArray = ('I', '1:', 'a')
39
40itemList = (
41    "Cats",
42    ">Domestic Shorthair",
43    ">Domestic Longhair",
44    ">Purebred",
45    ">>Russian Blue",
46    ">>Siamese",
47    ">>>Seal Point",
48     ">>>Flame Point",
49    "Dogs",
50    ">Retrievers",
51    ">>Golden Retriever",
52    ">>Labrador Retriever",
53    ">Poodles",
54    ">>Toy Poodle",
55    ">>Standard Poodle"
56)
57
58class TestEasyListStype(unittest.TestCase):
59
60    def createList(self, itemList, indentDelim, styleName):
61        listArray = []
62        listItem = ListItem()
63        level = 0
64        lastLevel = 0
65
66        for levCount in range(0,10):
67            listArray.append(None)
68        listArray[0] = List()
69
70        for item in itemList:
71            level = 0;
72            while (level < len(item) and item[level] == indentDelim):
73                level +=1
74            item = item[level:]
75
76            if (level > lastLevel):    # open the sub-levels
77                for levCount in range(lastLevel+1, level+1):
78                    listArray[levCount] = List()
79            elif (level < lastLevel):    # close off the intervening lists
80                for levCount in range(lastLevel, level, -1):
81                    listArray[levCount-1].childNodes[-1].addElement(listArray[levCount])
82
83            # now that we are at the proper level, add the item.
84            listArray[level].setAttribute( 'stylename', styleName );
85            listItem = ListItem()
86            para = P(text=item);
87            listItem.addElement(para);
88            listArray[level].addElement(listItem);
89            lastLevel = level;
90
91        # close off any remaining open lists
92        for levCount in range(lastLevel, 0, -1):
93            listArray[levCount-1].childNodes[-1].addElement(listArray[levCount])
94        return listArray[0]
95
96    def test_list(self):
97        textdoc = OpenDocumentText()
98
99        s = textdoc.styles
100        listStyle = easyliststyle.styleFromString(u'bullet1', bulletListSpec,
101            u',', u'0.6cm', easyliststyle.SHOW_ONE_LEVEL)
102        s.addElement(listStyle)
103        result = textdoc.stylesxml()
104        self.assertNotEqual(-1, result.find(u'''style:name="bullet1"'''))
105        self.assertNotEqual(-1, result.find(u'''text:bullet-char="*"'''))
106        self.assertNotEqual(-1, result.find(u'''text:level="1"'''))
107        self.assertNotEqual(-1, result.find(u'''style:list-level-properties'''))
108        #<text:list-style style:name="bullet1" style:display-name="bullet1">
109        #<text:list-level-style-bullet text:bullet-char="*" text:level="1">
110        #<style:list-level-properties text:min-label-width="0.6cm" text:space-before="0.6cm"/>
111        #</text:list-level-style-bullet>
112        #<text:list-level-style-bullet text:bullet-char="&gt;" text:level="2">
113        #<style:list-level-properties text:min-label-width="0.6cm" text:space-before="1.2cm"/>
114        #</text:list-level-style-bullet>
115        #<text:list-level-style-bullet text:bullet-char="#" text:level="3">
116        #<style:list-level-properties text:min-label-width="0.6cm" text:space-before="1.8cm"/>
117        #</text:list-level-style-bullet>
118        #<text:list-level-style-bullet text:bullet-char="%" text:level="4">
119        #<style:list-level-properties text:min-label-width="0.6cm" text:space-before="2.4cm"/>
120
121        listElement = self.createList(itemList, u'>', u'bullet1')
122        textdoc.text.addElement(listElement)
123
124        para = P(text="-----------------------");
125        textdoc.text.addElement(para)
126
127        listStyle = easyliststyle.styleFromList('num1', numberListSpecArray,
128            '0.25in', easyliststyle.SHOW_ALL_LEVELS)
129        s.addElement(listStyle)
130
131        listElement = self.createList(itemList, '>', 'num1')
132        textdoc.text.addElement(listElement)
133
134        para = P(text="-----------------------");
135        textdoc.text.addElement(para)
136
137        listStyle = easyliststyle.styleFromString('mix1', mixedListSpec,
138            '!', '0.8cm', easyliststyle.SHOW_ONE_LEVEL)
139        s.addElement(listStyle)
140
141        listElement = self.createList(itemList, '>', 'mix1')
142        textdoc.text.addElement(listElement)
143
144
145if __name__ == '__main__':
146    unittest.main()
147
148# vim: set expandtab sw=4 :
149