Exemplo n.º 1
0
class TestStaff(unittest.TestCase):
    def setUp(self):
        self.staff = Staff()

    def testEmptyStaff(self):
        self.assertEqual(self.staff.numMeasures(), 0)
        self.assertEqual(len(self.staff), 0)

    def testAddMeasure(self):
        self.staff.addMeasure(Measure(1))
        self.assertEqual(self.staff.numMeasures(), 1)
        self.assertEqual(len(self.staff), 1)
        self.staff.addMeasure(Measure(2))
        self.assertEqual(self.staff.numMeasures(), 2)
        self.assertEqual(len(self.staff), 3)

    def testIterateOverMeasures(self):
        for i in range(1, 16):
            self.staff.addMeasure(Measure(i))
        for i, measure in enumerate(self.staff):
            self.assertEqual(len(measure), i + 1)

    def testGetMeasureByIndex(self):
        for i in range(1, 16):
            self.staff.addMeasure(Measure(i))
        for i in range(1, 16):
            self.assertEqual(len(self.staff[i - 1]), i)

    def testDeleteLastMeasure(self):
        self.staff.addMeasure(Measure(1))
        self.staff.addMeasure(Measure(2))
        self.staff.addMeasure(Measure(3))
        self.staff.addMeasure(Measure(4))
        self.assertEqual(self.staff.numMeasures(), 4)
        self.staff.deleteLastMeasure()
        self.assertEqual(self.staff.numMeasures(), 3)
        for index in range(1, 4):
            self.assertEqual(len(self.staff[index - 1]), index)


    def testDeleteMeasure(self):
        self.staff.addMeasure(Measure(1))
        self.staff.addMeasure(Measure(2))
        self.staff.deleteMeasure(NotePosition(measureIndex = 0))
        self.assertEqual(self.staff.numMeasures(), 1)
        self.assertEqual(len(self.staff), 2)

    def testDeleteMeasure_BadIndex(self):
        self.staff.addMeasure(Measure(1))
        self.staff.addMeasure(Measure(2))
        self.assertRaises(BadTimeError, self.staff.deleteMeasure,
                          NotePosition(measureIndex = -1))
        self.assertRaises(BadTimeError, self.staff.deleteMeasure,
                          NotePosition(measureIndex = 2))

    def testInsertMeasure(self):
        self.staff.addMeasure(Measure(1))
        self.staff.addMeasure(Measure(3))
        self.staff.insertMeasure(NotePosition(measureIndex = 1), Measure(2))
        for i, measure in enumerate(self.staff):
            self.assertEqual(len(measure), i + 1)

    def testInsertMeasureAtStart(self):
        self.staff.addMeasure(Measure(2))
        self.staff.insertMeasure(NotePosition(measureIndex = 0), Measure(1))
        for i, measure in enumerate(self.staff):
            self.assertEqual(len(measure), i + 1)

    def testInsertMeasureAtEnd(self):
        self.staff.addMeasure(Measure(1))
        self.staff.insertMeasure(NotePosition(measureIndex = 1), Measure(2))
        for i, measure in enumerate(self.staff):
            self.assertEqual(len(measure), i + 1)

    def testInsertMeasure_BadIndex(self):
        self.staff.addMeasure(Measure(1))
        self.staff.addMeasure(Measure(2))
        m = Measure(3)
        self.assertRaises(BadTimeError,
                          self.staff.insertMeasure,
                          NotePosition(measureIndex = -1), m)
        self.assertRaises(BadTimeError,
                          self.staff.insertMeasure,
                          NotePosition(measureIndex = 3), m)

    def testClearStaff(self):
        self.staff.addMeasure(Measure(16))
        self.staff.addMeasure(Measure(16))
        self.staff.addMeasure(Measure(16))
        self.staff.addMeasure(Measure(16))
        self.staff.clear()
        self.assertEqual(len(self.staff), 0)
        self.assertEqual(self.staff.numMeasures(), 0)
Exemplo n.º 2
0
class TestStaff(unittest.TestCase):
    def setUp(self):
        self.staff = Staff()

    def testEmptyStaff(self):
        self.assertEqual(self.staff.numMeasures(), 0)
        self.assertEqual(len(self.staff), 0)

    def testAddMeasure(self):
        self.staff.addMeasure(Measure(1))
        self.assertEqual(self.staff.numMeasures(), 1)
        self.assertEqual(len(self.staff), 1)
        self.staff.addMeasure(Measure(2))
        self.assertEqual(self.staff.numMeasures(), 2)
        self.assertEqual(len(self.staff), 3)

    def testIterateOverMeasures(self):
        for i in range(1, 16):
            self.staff.addMeasure(Measure(i))
        for i, measure in enumerate(self.staff):
            self.assertEqual(len(measure), i + 1)

    def testGetMeasureByIndex(self):
        for i in range(1, 16):
            self.staff.addMeasure(Measure(i))
        for i in range(1, 16):
            self.assertEqual(len(self.staff[i - 1]), i)

    def testDeleteLastMeasure(self):
        self.staff.addMeasure(Measure(1))
        self.staff.addMeasure(Measure(2))
        self.staff.addMeasure(Measure(3))
        self.staff.addMeasure(Measure(4))
        self.assertEqual(self.staff.numMeasures(), 4)
        self.staff.deleteLastMeasure()
        self.assertEqual(self.staff.numMeasures(), 3)
        for index in range(1, 4):
            self.assertEqual(len(self.staff[index - 1]), index)

    def testDeleteMeasure(self):
        self.staff.addMeasure(Measure(1))
        self.staff.addMeasure(Measure(2))
        self.staff.deleteMeasure(NotePosition(measureIndex=0))
        self.assertEqual(self.staff.numMeasures(), 1)
        self.assertEqual(len(self.staff), 2)

    def testDeleteMeasure_BadIndex(self):
        self.staff.addMeasure(Measure(1))
        self.staff.addMeasure(Measure(2))
        self.assertRaises(BadTimeError, self.staff.deleteMeasure,
                          NotePosition(measureIndex=-1))
        self.assertRaises(BadTimeError, self.staff.deleteMeasure,
                          NotePosition(measureIndex=2))

    def testInsertMeasure(self):
        self.staff.addMeasure(Measure(1))
        self.staff.addMeasure(Measure(3))
        self.staff.insertMeasure(NotePosition(measureIndex=1), Measure(2))
        for i, measure in enumerate(self.staff):
            self.assertEqual(len(measure), i + 1)

    def testInsertMeasureAtStart(self):
        self.staff.addMeasure(Measure(2))
        self.staff.insertMeasure(NotePosition(measureIndex=0), Measure(1))
        for i, measure in enumerate(self.staff):
            self.assertEqual(len(measure), i + 1)

    def testInsertMeasureAtEnd(self):
        self.staff.addMeasure(Measure(1))
        self.staff.insertMeasure(NotePosition(measureIndex=1), Measure(2))
        for i, measure in enumerate(self.staff):
            self.assertEqual(len(measure), i + 1)

    def testInsertMeasure_BadIndex(self):
        self.staff.addMeasure(Measure(1))
        self.staff.addMeasure(Measure(2))
        m = Measure(3)
        self.assertRaises(BadTimeError, self.staff.insertMeasure,
                          NotePosition(measureIndex=-1), m)
        self.assertRaises(BadTimeError, self.staff.insertMeasure,
                          NotePosition(measureIndex=3), m)

    def testClearStaff(self):
        self.staff.addMeasure(Measure(16))
        self.staff.addMeasure(Measure(16))
        self.staff.addMeasure(Measure(16))
        self.staff.addMeasure(Measure(16))
        self.staff.clear()
        self.assertEqual(len(self.staff), 0)
        self.assertEqual(self.staff.numMeasures(), 0)
Exemplo n.º 3
0
class TestCallBack(unittest.TestCase):
    def setUp(self):
        self.staff = Staff()
        self.staff.addMeasure(Measure(16))
        self.calls = []
        def myCallBack(position):
            self.calls.append((position.measureIndex,
                               position.noteTime,
                               position.drumIndex))
        self.staff.setCallBack(myCallBack)

    def testAddNoteCallBack(self):
        np = NotePosition(measureIndex = 0, noteTime = 0, drumIndex = 0)
        self.staff.addNote(np, "x")
        self.assertEqual(len(self.calls), 1)
        self.assertEqual(self.calls[0], (0, 0, 0))
        self.staff.addNote(np, "x")
        self.assertEqual(len(self.calls), 1)
        self.staff.addNote(np, "o")
        self.assertEqual(len(self.calls), 2)
        self.assertEqual(self.calls[1], (0, 0, 0))

    def testDeleteNoteCallBack(self):
        np = NotePosition(measureIndex = 0, noteTime = 0, drumIndex = 0)
        self.staff.deleteNote(np)
        self.assertEqual(len(self.calls), 0)
        self.staff.addNote(np, "x")
        self.assertEqual(len(self.calls), 1)
        self.assertEqual(self.calls[0], (0, 0, 0))
        self.staff.deleteNote(np)
        self.assertEqual(len(self.calls), 2)
        self.assertEqual(self.calls[1], (0, 0, 0))

    def testToggleNoteCallBack(self):
        np = NotePosition(measureIndex = 0, noteTime = 0, drumIndex = 0)
        self.staff.toggleNote(np, "x")
        self.assertEqual(len(self.calls), 1)
        self.assertEqual(self.calls[0], (0, 0, 0))
        self.staff.toggleNote(np, "x")
        self.assertEqual(len(self.calls), 2)
        self.assertEqual(self.calls[1], (0, 0, 0))
        self.staff.toggleNote(np, "x")
        self.assertEqual(len(self.calls), 3)
        self.assertEqual(self.calls[2], (0, 0, 0))
        self.staff.toggleNote(np, "o")
        self.assertEqual(len(self.calls), 4)
        self.assertEqual(self.calls[2], (0, 0, 0))

    def testClearCallBack(self):
        self.staff.clearCallBack()
        self.staff.addNote(NotePosition(measureIndex = 0,
                                        noteTime = 0,
                                        drumIndex = 0), "x")
        self.assertEqual(len(self.calls), 0)

    def testAddMeasureCallBack(self):
        np = NotePosition(measureIndex = 1, noteTime = 0, drumIndex = 0)
        self.staff.addMeasure(Measure(16))
        self.staff.addNote(np, "x")
        self.assertEqual(len(self.calls), 1)
        self.assertEqual(self.calls[0], (1, 0, 0))

    def testDeleteMeasureCallBack(self):
        self.staff.addMeasure(Measure(16))
        self.staff.addMeasure(Measure(16))
        np = NotePosition(measureIndex = 1,
                          noteTime = 0,
                          drumIndex = 0)
        self.staff.addNote(np, "x")
        self.staff.deleteMeasure(NotePosition(measureIndex = 0))
        self.staff.deleteNote(NotePosition(measureIndex = 0,
                                           noteTime = 0,
                                           drumIndex = 0))
        self.staff.addNote(np, "x")
        self.assertEqual(len(self.calls), 3)
        self.assertEqual(self.calls[0], (1, 0, 0))
        self.assertEqual(self.calls[1], (0, 0, 0))
        self.assertEqual(self.calls[2], (1, 0, 0))

    def testInsertMeasureCallBack(self):
        np0 = NotePosition(measureIndex = 0,
                          noteTime = 0,
                          drumIndex = 0)
        np1 = NotePosition(measureIndex = 1,
                          noteTime = 0,
                          drumIndex = 0)
        self.staff.addNote(np0, "x")
        self.staff.insertMeasure(np0, Measure(8))
        self.staff.addNote(np0, "x")
        self.staff.deleteNote(np1)
        self.assertEqual(len(self.calls), 3)
        self.assertEqual(self.calls[0], (0, 0, 0))
        self.assertEqual(self.calls[1], (0, 0, 0))
        self.assertEqual(self.calls[2], (1, 0, 0))
Exemplo n.º 4
0
class TestCallBack(unittest.TestCase):
    def setUp(self):
        self.staff = Staff()
        self.staff.addMeasure(Measure(16))
        self.calls = []

        def myCallBack(position):
            self.calls.append(
                (position.measureIndex, position.noteTime, position.drumIndex))

        self.staff.setCallBack(myCallBack)

    def testAddNoteCallBack(self):
        np = NotePosition(measureIndex=0, noteTime=0, drumIndex=0)
        self.staff.addNote(np, "x")
        self.assertEqual(len(self.calls), 1)
        self.assertEqual(self.calls[0], (0, 0, 0))
        self.staff.addNote(np, "x")
        self.assertEqual(len(self.calls), 1)
        self.staff.addNote(np, "o")
        self.assertEqual(len(self.calls), 2)
        self.assertEqual(self.calls[1], (0, 0, 0))

    def testDeleteNoteCallBack(self):
        np = NotePosition(measureIndex=0, noteTime=0, drumIndex=0)
        self.staff.deleteNote(np)
        self.assertEqual(len(self.calls), 0)
        self.staff.addNote(np, "x")
        self.assertEqual(len(self.calls), 1)
        self.assertEqual(self.calls[0], (0, 0, 0))
        self.staff.deleteNote(np)
        self.assertEqual(len(self.calls), 2)
        self.assertEqual(self.calls[1], (0, 0, 0))

    def testToggleNoteCallBack(self):
        np = NotePosition(measureIndex=0, noteTime=0, drumIndex=0)
        self.staff.toggleNote(np, "x")
        self.assertEqual(len(self.calls), 1)
        self.assertEqual(self.calls[0], (0, 0, 0))
        self.staff.toggleNote(np, "x")
        self.assertEqual(len(self.calls), 2)
        self.assertEqual(self.calls[1], (0, 0, 0))
        self.staff.toggleNote(np, "x")
        self.assertEqual(len(self.calls), 3)
        self.assertEqual(self.calls[2], (0, 0, 0))
        self.staff.toggleNote(np, "o")
        self.assertEqual(len(self.calls), 4)
        self.assertEqual(self.calls[2], (0, 0, 0))

    def testClearCallBack(self):
        self.staff.clearCallBack()
        self.staff.addNote(
            NotePosition(measureIndex=0, noteTime=0, drumIndex=0), "x")
        self.assertEqual(len(self.calls), 0)

    def testAddMeasureCallBack(self):
        np = NotePosition(measureIndex=1, noteTime=0, drumIndex=0)
        self.staff.addMeasure(Measure(16))
        self.staff.addNote(np, "x")
        self.assertEqual(len(self.calls), 1)
        self.assertEqual(self.calls[0], (1, 0, 0))

    def testDeleteMeasureCallBack(self):
        self.staff.addMeasure(Measure(16))
        self.staff.addMeasure(Measure(16))
        np = NotePosition(measureIndex=1, noteTime=0, drumIndex=0)
        self.staff.addNote(np, "x")
        self.staff.deleteMeasure(NotePosition(measureIndex=0))
        self.staff.deleteNote(
            NotePosition(measureIndex=0, noteTime=0, drumIndex=0))
        self.staff.addNote(np, "x")
        self.assertEqual(len(self.calls), 3)
        self.assertEqual(self.calls[0], (1, 0, 0))
        self.assertEqual(self.calls[1], (0, 0, 0))
        self.assertEqual(self.calls[2], (1, 0, 0))

    def testInsertMeasureCallBack(self):
        np0 = NotePosition(measureIndex=0, noteTime=0, drumIndex=0)
        np1 = NotePosition(measureIndex=1, noteTime=0, drumIndex=0)
        self.staff.addNote(np0, "x")
        self.staff.insertMeasure(np0, Measure(8))
        self.staff.addNote(np0, "x")
        self.staff.deleteNote(np1)
        self.assertEqual(len(self.calls), 3)
        self.assertEqual(self.calls[0], (0, 0, 0))
        self.assertEqual(self.calls[1], (0, 0, 0))
        self.assertEqual(self.calls[2], (1, 0, 0))