Source

pygame / test / midi_test.py

Diff from to

File test/midi_test.py

     from test.test_utils import test_not_implemented, unittest
 import pygame
 import pygame.midi
+import pygame.compat
 from pygame.locals import *
 
 
 
         self.fail() 
 
-    def todo_test_message(self):
+    def test_MidiException(self):
 
         # __doc__ (as of 2009-05-19) for pygame.midi.MidiException.message:
 
-          
+        def raiseit():
+            raise pygame.midi.MidiException(0)
 
-        self.fail() 
+        self.assertRaises(pygame.midi.MidiException, raiseit)
+        try:
+            raise pygame.midi.MidiException(0) 
+        except pygame.midi.MidiException:
+            e = pygame.compat.geterror()
+            self.assertEqual(e.parameter, 0)
 
-    def todo_test_note_off(self):
+
+    def test_note_off(self):
+        """|tags: interactive|
+        """
 
         # __doc__ (as of 2009-05-19) for pygame.midi.Output.note_off:
 
           # Turn a note off in the output stream.  The note must already
           # be on for this to work correctly.
 
-        self.fail() 
+        i = pygame.midi.get_default_output_id()
+        if i != -1:
+            o = pygame.midi.Output(i)
+            o.note_on(5, 30, 0)
+            o.note_off(5, 30, 0)
 
-    def todo_test_note_on(self):
+
+
+    def test_note_on(self):
+        """|tags: interactive|
+        """
 
         # __doc__ (as of 2009-05-19) for pygame.midi.Output.note_on:
 
           # Turn a note on in the output stream.  The note must already
           # be off for this to work correctly.
 
-        self.fail() 
+
+        i = pygame.midi.get_default_output_id()
+        if i != -1:
+            o = pygame.midi.Output(i)
+            o.note_on(5, 30, 0)
+
 
     def todo_test_set_instrument(self):
 
 
         self.fail() 
 
-    def todo_test_write_short(self):
-
+    def test_write_short(self):
+        """|tags: interactive|
+        """
         # __doc__ (as of 2009-05-19) for pygame.midi.Output.write_short:
 
           # write_short(status <, data1><, data2>)
           # example: note 65 on with velocity 100
           #      write_short(0x90,65,100)
 
-        self.fail() 
+        i = pygame.midi.get_default_output_id()
+        if i != -1:
+            o = pygame.midi.Output(i)
+            # put a note on, then off.
+            o.write_short(0x90,65,100)
+            o.write_short(0x80,65,100)
+
+
+
+
+    def test_Input(self):
+        """|tags: interactive|
+        """
+
+        i = pygame.midi.get_default_input_id()
+        if i != -1:
+            o = pygame.midi.Input(i)
+            del o
+
+        # try feeding it an input id.
+        i = pygame.midi.get_default_output_id()
+
+        # can handle some invalid input too.
+        self.assertRaises(pygame.midi.MidiException, pygame.midi.Input, i)
+        self.assertRaises(pygame.midi.MidiException, pygame.midi.Input, 9009)
+        self.assertRaises(pygame.midi.MidiException, pygame.midi.Input, -1)
+        self.assertRaises(TypeError, pygame.midi.Input,"1234")
+        self.assertRaises(OverflowError, pygame.midi.Input, pow(2,99))
+
+
+    def test_Output(self):
+        """|tags: interactive|
+        """
+        i = pygame.midi.get_default_output_id()
+        if i != -1:
+            o = pygame.midi.Output(i)
+            del o
+
+        # try feeding it an input id.
+        i = pygame.midi.get_default_input_id()
+
+        # can handle some invalid input too.
+        self.assertRaises(pygame.midi.MidiException, pygame.midi.Output, i)
+        self.assertRaises(pygame.midi.MidiException, pygame.midi.Output, 9009)
+        self.assertRaises(pygame.midi.MidiException, pygame.midi.Output, -1)
+        self.assertRaises(TypeError, pygame.midi.Output,"1234")
+        self.assertRaises(OverflowError, pygame.midi.Output, pow(2,99))
+
 
     def todo_test_write_sys_ex(self):
 
           #     (the input or output device with the lowest PmDeviceID).
 
         c = pygame.midi.get_default_output_id()
-        if not (c is None):
-            # if there is a not None return make sure it is an int.
-
-            self.assertEqual(type(c), type(1))
-            self.failUnless(c >= 0 or c == -1)
+        self.assertEqual(type(c), type(1))
+        self.failUnless(c >= 0 or c == -1)
 
 
 
           # returns (interf, name, input, output, opened)
           # pygame.midi.get_device_info(an_id): return (interf, name, input,
           # output, opened)
-          # 
-          # 
+          #
+          #
           # If the id is out of range, the function returns None.
 
         an_id = pygame.midi.get_default_output_id()
             self.assertEqual(opened, 0)
 
 
-        an_in_id = pygame.midi.get_default_input_id() 
-        if an_id != -1:
-            interf, name, input, output, opened = pygame.midi.get_device_info(an_in_id)
+        an_in_id = pygame.midi.get_default_input_id()
+        if an_in_id != -1:
+            r = pygame.midi.get_device_info(an_in_id)
+            # if r is None, it means that the id is out of range.
+            try:
+                interf, name, input, output, opened = r
+            except TypeError:
+                raise Exception(repr(r))
+
             self.assertEqual(output, 0)
             self.assertEqual(input, 1)
             self.assertEqual(opened, 0)