Implemented Set-Get simple functions included test

bzr-revno: 70
This commit is contained in:
Carsten Tinggaard 2008-11-02 15:19:49 +00:00
parent b666d823f1
commit 4d42c84ddb
3 changed files with 467 additions and 27 deletions

View File

@ -18,17 +18,23 @@ Place, Suite 330, Boston, MA 02111-1307 USA
import sys
import os
from types import StringType, ListType, NoneType
sys.path.append(os.path.abspath("./../.."))
from openlp.core.xmlrootclass import XmlRootClass
class SongException(Exception):
pass
class SongException(Exception):
pass
class SongTitleError(SongException):
pass
blankSongXml = \
class SongTypeError(SongException):
pass
_blankSongXml = \
'''<?xml version="1.0" encoding="iso-8859-1"?>
<Song>
<title>BlankSong</title>
@ -38,7 +44,7 @@ blankSongXml = \
<copyright></copyright>
<showTitle>1</showTitle>
<showAuthorList>1</showAuthorList>
<showSongCcli>1</showSongCcli>
<showSongCcliNo>1</showSongCcliNo>
<showCopyright>1</showCopyright>
<theme></theme>
<categoryArray></categoryArray>
@ -57,32 +63,33 @@ class Song(XmlRootClass) :
"""Initialize from given xml content
xmlContent (string) -- xml formatted string
attributes
title -- title of the song
searchableTitle -- title without punctuation chars
authorList -- list of authors
songCcliNo -- CCLI number for this song
copyright -- copyright string
showTitle -- 0: no show, 1: show
showAuthorList -- 0: no show, 1: show
showCopyright -- 0: no show, 1: show
showCcliNo -- 0: no show, 1: show
theme -- name of theme or blank
categoryArray -- list of user defined properties (hymn, gospel)
songBook -- name of originating book
songNumber -- number of the song, related to a songbook
comments -- free comment
verseOrder -- presentation order of the slides
lyrics -- simple or formatted (tbd)
title -- title of the song
searchableTitle -- title without punctuation chars
authorList -- list of authors
songCcliNo -- CCLI number for this song
copyright -- copyright string
showTitle -- 0: no show, 1: show
showAuthorList -- 0: no show, 1: show
showCopyright -- 0: no show, 1: show
showSongCcliNo -- 0: no show, 1: show
theme -- name of theme or blank
categoryArray -- list of user defined properties (hymn, gospel)
songBook -- name of originating book
songNumber -- number of the song, related to a songbook
comments -- free comment
verseOrder -- presentation order of the slides
lyrics -- simple or formatted (tbd)
"""
super(Song, self).__init__()
self._reset()
if xmlContent != None :
self._setFromXml(xmlContent, "Song")
def _reset(self):
"""Reset all song attributes"""
global blankSongXml
self._setFromXml(blankSongXml, "Song")
global _blankSongXml
self._setFromXml(_blankSongXml, "Song")
def _RemovePunctuation(self, title):
"""Remove the puntuation chars from title
@ -135,4 +142,234 @@ class Song(XmlRootClass) :
f.close()
self.FromText(textList)
def _assureString(self, s):
"""Force a string is returned"""
if s == None :
r = ""
else :
r = str(s)
return r
def _splitToList(self, aString):
"""Split a string into a list - comma separated"""
res = []
if aString != None :
lst = aString.split(',')
for l in lst :
# remove whitespace
res.append(l.strip())
return res
def _listToString(self, strOrList):
"""Force a possibly list into a string"""
if type(strOrList) == StringType :
lst = self._splitToList(strOrList)
elif type(strOrList) == ListType :
lst = strOrList
elif type(strOrList) == NoneType :
lst = []
else :
raise SongTypeError("Variable not String or List")
s = ", ".join(lst)
return s
def GetCopyright(self):
"""Return copyright info string"""
return self._assureString(self.copyright)
def SetCopyright(self, copyright):
"""Set the copyright string"""
self.copyright = copyright
def GetSongCcliNo(self):
"""Return the songCclino"""
return self._assureString(self.songCcliNo)
def SetSongCcliNo(self, songCcliNo):
"""Set the songCcliNo"""
self.songCcliNo = songCcliNo
def GetTheme(self):
"""Return the theme name for the song"""
return self._assureString(self.theme)
def SetTheme(self, theme):
"""Set the theme name (string)"""
self.theme = theme
def GetSongBook(self):
"""Return the songBook (string)"""
return self._assureString(self.songBook)
def SetSongBook(self, songBook):
"""Set the songBook (string)"""
self.songBook = songBook
def GetSongNumber(self):
"""Return the songNumber (string)"""
return self._assureString(self.songNumber)
def SetSongNumber(self, songNumber):
"""Set the songNumber (string)"""
self.songNumber = songNumber
def GetComments(self):
"""Return the comments (string)"""
return self._assureString(self.comments)
def SetComments(self, comments):
"""Set the comments (string)"""
self.comments = comments
def GetVerseOrder(self):
"""Get the verseOrder (string) - preferably space delimited"""
return self._assureString(self.verseOrder)
def SetVerseOrder(self, verseOrder):
"""Set the verseOrder (string) - space delimited"""
self.verseOrder = verseOrder
def GetAuthorList(self, asOneString = True):
"""Return the list of authors as a string
asOneString
True -- string:
"John Newton, A Parker"
False -- list of strings
["John Newton", "A Parker"]
"""
if asOneString :
res = self._assureString(self.authorList)
else :
res = self._splitToList(self.authorList)
return res
def SetAuthorList(self, authorList):
"""Set the authorList
authorList -- a string or list of strings
"""
if authorList == None :
self.authorList = None
else :
self.authorList = self._listToString(authorList)
def GetCategoryArray(self, asOneString = True):
"""Return the list of categories as a string
asOneString
True -- string:
"Hymn, Gospel"
False -- list of strings
["Hymn", "Gospel"]
"""
if asOneString :
res = self._assureString(self.categoryArray)
else :
res = self._splitToList(self.categoryArray)
return res
def SetCategoryArray(self, categoryArray):
"""Set the categoryArray
categoryArray -- a string or list of strings
"""
if categoryArray == None :
self.categoryArray = None
else :
self.categoryArray = self._listToString(categoryArray)
def GetShowTitle(self):
"""Return the showTitle flag (bool)"""
return self.showTitle
def SetShowTitle(self, showTitle):
"""Set the showTitle flag (bool)"""
self.showTitle = showTitle
def GetShowAuthorList(self):
"""Return the showAuthorList flag"""
return self.showAuthorList
def SetShowAuthorList(self, showAuthorList):
"""Set the showAuthorList flag (bool)"""
self.showAuthorList = showAuthorList
def GetShowCopyright(self):
"""Return the showCopyright flag"""
return self.showCopyright
def SetShowCopyright(self, showCopyright):
"""Set the showCopyright flag (bool)"""
self.showCopyright = showCopyright
def GetShowSongCcliNo(self):
"""Return the showSongCclino (string)"""
return self.showSongCcliNo
def SetShowSongCcliNo(self, showSongCcliNo):
"""Set the showSongCcliNo flag (bool)"""
self.showSongCcliNo = showSongCcliNo
def GetLyrics(self):
"""Return the lyrics as a list of strings
this will return all the strings in the song
"""
return self.lyrics
def SetLyrics(self, lyrics):
"""Set the lyrics as a list of strings"""
# TODO: check font formatting
self.lyrics = lyrics
def GetNumberOfVerses(self):
"""Return the number of verses in the song (int)"""
numOfVerses = 0
#
return numOfVerses
def GetPreviewVerse(self, verseNumber):
"""Return the preview text for specified verse number
verseNumber -- 0: all verses, 1..n : specific verse
a list of strings are returned
"""
return []
def GetRenderVerse(self, verseNumber):
"""Return the verse to be rendered including the additional
properties
Returns a list as:
[theme (string),
title (string),
authorlist (string),
copyright (string),
cclino (string),
lyric-verse as a list of strings]
"""
res = []
res.append(self.GetTheme())
if self.showTitle :
title = self.GetTitle()
else :
title = ""
res.append(title)
if self.showAuthorList :
author = self.GetAuthorList(True)
else :
author = ""
res.append(author)
if self.showCopyright :
cpright = self.GetCopyright()
else :
cpright = ""
res.append(cpright)
if self.showSongCcliNo :
ccli = self.GetSongCcliNo()
else :
ccli = ""
res.append(ccli)
return res

View File

@ -38,8 +38,7 @@ class Test_Basic(object):
s = Song()
r = s.__str__()
l = r.split("\n")
#print r
assert(len(l) == 21)
assert(len(l) == 52)
def test_asString(self):
"""Init: Empty asString - initial values"""
@ -84,3 +83,97 @@ class Test_Basic(object):
s = Song()
py.test.raises(SongTitleError, s.SetTitle, ",*")
def test_Copyright(self):
"""Set a copyright string"""
s = Song()
assert(s.GetCopyright() == "")
s.SetCopyright("A B Car")
assert(s.GetCopyright() == "A B Car")
def test_SongCclino(self):
"""Set a SongCcliNo"""
s = Song()
assert(s.GetSongCcliNo() == "")
s.SetSongCcliNo(12345)
assert(s.GetSongCcliNo() == "12345")
def test_SongBook(self):
"""Set a songbook value"""
s = Song()
assert(s.GetSongBook() == "")
s.SetSongBook("Hymns")
assert(s.GetSongBook() == "Hymns")
def test_SongNumber(self):
"""Set a song number"""
s = Song()
assert(s.GetSongNumber() == "")
s.SetSongNumber(278)
assert(s.GetSongNumber() == "278")
def test_Theme(self):
"""Set a theme name"""
s = Song()
assert(s.GetTheme() == "")
s.SetTheme("Red")
assert(s.GetTheme() == "Red")
def test_VerseOrder(self):
"""Set a verse order"""
s = Song()
assert(s.GetVerseOrder() == "")
s.SetVerseOrder("V1 C V2")
assert(s.GetVerseOrder() == "V1 C V2")
def test_Comments(self):
"""Set a comment"""
s = Song()
assert(s.GetComments() == "")
s.SetComments("a comment")
assert(s.GetComments() == "a comment")
def test_AuthorList(self):
"""Set author lists"""
s = Song()
assert(s.GetAuthorList(True) == "")
assert(s.GetAuthorList(False) == [])
t1 = "John Newton"
s.SetAuthorList(t1)
assert(s.GetAuthorList(True) == t1)
assert(s.GetAuthorList(False) == [t1])
s.SetAuthorList(" Peter Done , John Newton")
assert(s.GetAuthorList(True)== "Peter Done, John Newton")
assert(s.GetAuthorList(False) == ["Peter Done", "John Newton"])
s.SetAuthorList(None)
assert(s.GetAuthorList(True) == "")
assert(s.GetAuthorList(False) == [])
s.SetAuthorList("")
assert(s.GetAuthorList(True) == "")
assert(s.GetAuthorList(False) == [""])
s.SetAuthorList([])
assert(s.GetAuthorList(True) == "")
assert(s.GetAuthorList(False) == [""])
def test_CategoryArray(self):
"""Set categories"""
s = Song()
assert(s.GetCategoryArray(True) == "")
assert(s.GetCategoryArray(False) == [])
t1 = "Gospel"
s.SetCategoryArray(t1)
assert(s.GetCategoryArray(True) == t1)
assert(s.GetCategoryArray(False) == [t1])
s.SetCategoryArray(" Gospel, Hymns ")
assert(s.GetCategoryArray(True) == "Gospel, Hymns")
assert(s.GetCategoryArray(False) == ["Gospel", "Hymns"])
s.SetCategoryArray(None)
assert(s.GetCategoryArray(True) == "")
assert(s.GetCategoryArray(False) == [])
s.SetCategoryArray("")
assert(s.GetCategoryArray(True) == "")
assert(s.GetCategoryArray(False) == [""])
s.SetCategoryArray([])
assert(s.GetCategoryArray(True) == "")
assert(s.GetCategoryArray(False) == [""])

View File

@ -0,0 +1,110 @@
"""
OpenLP - Open Source Lyrics Projection
Copyright (c) 2008 Raoul Snyman
Portions copyright (c) 2008 Carsten Tinggaard
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
"""
import py.test
import os
import sys
sys.path.append(os.path.abspath("./../../.."))
from openlp.song import *
class Test_Verse(object):
"""Class for testing verses for preview and review"""
def stdSong(self):
"""Definition of a standard song"""
s = Song()
self.title = "A song"
self.author = "John Newton"
self.copyright = "Peter Hamil"
self.ccli = "123456"
s.SetLyrics(["# verse","a single line"])
s.SetTitle(self.title)
s.SetCopyright(self.copyright)
s.SetAuthorList(self.author)
s.SetSongCcliNo(self.ccli)
return s
def check_allfields(self, r, isblank = 0):
#[theme, title, author, cpright, ccli, lyrics]
if isblank == 1 :
assert(r[1] == "")
else :
assert(r[1] == self.title)
if isblank == 2 :
assert(r[2] == "")
else :
assert(r[2] == self.author)
if isblank == 3 :
assert(r[3] == "")
else :
assert(r[3] == self.copyright)
if isblank == 4 :
assert(r[4] == "")
else :
assert(r[4] == self.ccli)
def test_title_show_noshow(self):
"""Test the show title flag"""
s = self.stdSong()
r = s.GetRenderVerse(1)
self.check_allfields(r)
s.SetShowTitle(False)
r = s.GetRenderVerse(1)
self.check_allfields(r, 1)
s.SetShowTitle(True)
r = s.GetRenderVerse(1)
self.check_allfields(r)
def test_author_show_noshow(self):
"""Test the show author flag"""
s = self.stdSong()
r = s.GetRenderVerse(1)
self.check_allfields(r)
s.SetShowAuthorList(False)
r = s.GetRenderVerse(1)
self.check_allfields(r, 2)
s.SetShowAuthorList(True)
r = s.GetRenderVerse(1)
self.check_allfields(r)
def test_copyright_show_noshow(self):
"""Test the show copyright flag"""
s = self.stdSong()
r = s.GetRenderVerse(1)
self.check_allfields(r)
s.SetShowCopyright(False)
r = s.GetRenderVerse(1)
self.check_allfields(r, 3)
s.SetShowCopyright(True)
r = s.GetRenderVerse(1)
self.check_allfields(r)
def test_ccli_show_noshow(self):
"""Test the show copyright flag"""
s = self.stdSong()
r = s.GetRenderVerse(1)
self.check_allfields(r)
s.SetShowSongCcliNo(False)
r = s.GetRenderVerse(1)
self.check_allfields(r, 4)
s.SetShowSongCcliNo(True)
r = s.GetRenderVerse(1)
self.check_allfields(r)