Module SongTest
[hide private]
[frames] | no frames]

Source Code for Module SongTest

 1  ##################################################################### 
 2  # -*- coding: iso-8859-1 -*-                                        # 
 3  #                                                                   # 
 4  # Frets on Fire                                                     # 
 5  # Copyright (C) 2006 Sami Kyöstilä                                  # 
 6  #                                                                   # 
 7  # This program is free software; you can redistribute it and/or     # 
 8  # modify it under the terms of the GNU General Public License       # 
 9  # as published by the Free Software Foundation; either version 2    # 
10  # of the License, or (at your option) any later version.            # 
11  #                                                                   # 
12  # This program is distributed in the hope that it will be useful,   # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of    # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     # 
15  # GNU General Public License for more details.                      # 
16  #                                                                   # 
17  # You should have received a copy of the GNU General Public License # 
18  # along with this program; if not, write to the Free Software       # 
19  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        # 
20  # MA  02110-1301, USA.                                              # 
21  ##################################################################### 
22   
23  import unittest, pygame 
24  import shutil, os, sys 
25   
26  from GameEngine import GameEngine 
27  from Song import Song, Note 
28   
29 -class SongTest(unittest.TestCase):
30 - def testLoading(self):
31 e = GameEngine() 32 infoFile = e.resource.fileName("songs", "defy", "song.ini") 33 guitarFile = e.resource.fileName("songs", "defy", "guitar.ogg") 34 songFile = e.resource.fileName("songs", "defy", "song.ogg") 35 noteFile = e.resource.fileName("songs", "defy", "notes.mid") 36 song = Song(e, infoFile, guitarFile, songFile, None, noteFile) 37 38 assert int(song.bpm) == 122
39
40 - def testSaving(self):
41 e = GameEngine() 42 43 # Make a temp copy 44 tmp = "songtest_tmp" 45 files = ["song.ini", "guitar.ogg", "song.ogg", "notes.mid"] 46 try: 47 os.mkdir(tmp) 48 for f in files: 49 shutil.copy(e.resource.fileName("songs", "defy", f), tmp) 50 51 infoFile = os.path.join(tmp, "song.ini") 52 guitarFile = os.path.join(tmp, "guitar.ogg") 53 songFile = os.path.join(tmp, "song.ogg") 54 noteFile = os.path.join(tmp, "notes.mid") 55 song = Song(e, infoFile, guitarFile, songFile, None, noteFile) 56 57 events1 = song.track.allEvents[:] 58 59 song.save() 60 song = Song(e, infoFile, guitarFile, songFile, None, noteFile) 61 62 events2 = song.track.allEvents[:] 63 64 notes1 = [(time, event) for time, event in events1 if isinstance(event, Note)] 65 notes2 = [(time, event) for time, event in events2 if isinstance(event, Note)] 66 67 for i, event in enumerate(zip(notes1, notes2)): 68 t1, n1 = event[0] 69 t2, n2 = event[1] 70 71 if "-v" in sys.argv: 72 print "%8d. %.3f + %.3f\t%2d\t %.3f + %.3f\t%2d" % (i, t1, n1.length, n1.number, t2, n2.length, n2.number) 73 74 # Allow 2ms of rounding error 75 assert abs(t1 - t2) < 2 76 assert abs(n1.length - n2.length) < 2 77 assert n1.number == n2.number 78 finally: 79 # Load another song to free the copy 80 pygame.mixer.music.load(e.resource.fileName("songs", "defy", "guitar.ogg")) 81 shutil.rmtree(tmp)
82 83 84 if __name__ == "__main__": 85 unittest.main() 86