python-ctypes: implement comparison operators for enum classes
authorOlivier Aubert <olivier.aubert@liris.cnrs.fr>
Thu, 3 Sep 2009 08:31:36 +0000 (10:31 +0200)
committerOlivier Aubert <olivier.aubert@liris.cnrs.fr>
Thu, 3 Sep 2009 08:53:08 +0000 (10:53 +0200)
TODO
generate.py
test.py

diff --git a/TODO b/TODO
index 087d87b..07ddd07 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,6 +1,4 @@
 * Investigate memory management
 
-* Write a test suite
-
 * Support multiple VLC versions: define a front-end module which will
    load the appropriate versionned module from a subdirectory.
index 355536f..a1870e0 100755 (executable)
@@ -478,7 +478,7 @@ class PythonGenerator(object):
                 conv[k]=n
 
             for k, v in values:
-                self.output("    %s=%s" % (conv[k], v))
+                self.output("    %s=ctypes.c_uint(%s)" % (conv[k], v))
 
             self.output("    _names={")
             for k, v in values:
@@ -488,6 +488,13 @@ class PythonGenerator(object):
             self.output("""
     def __repr__(self):
         return ".".join((self.__class__.__module__, self.__class__.__name__, self._names[self.value]))
+
+    def __eq__(self, other):
+        return (isinstance(other, ctypes.c_uint) and self.value == other.value)
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
+
     """)
 
     def output_ctypes(self, rtype, method, params, comment):
diff --git a/test.py b/test.py
index bd0c4a3..97899fa 100755 (executable)
--- a/test.py
+++ b/test.py
@@ -37,37 +37,37 @@ class TestVLCAPI(unittest.TestCase):
     # failure, check that the reason is not a change in the .h
     # definitions.
     def test_enum_event_type(self):
-        self.assertEqual(vlc.EventType.MediaStateChanged, 5)
+        self.assertEqual(vlc.EventType.MediaStateChanged.value, 5)
 
     def test_enum_meta(self):
-        self.assertEqual(vlc.Meta.Description, 6)
+        self.assertEqual(vlc.Meta.Description.value, 6)
 
     def test_enum_state(self):
-        self.assertEqual(vlc.State.Playing, 3)
+        self.assertEqual(vlc.State.Playing.value, 3)
 
     def test_enum_media_option(self):
-        self.assertEqual(vlc.MediaOption.unique, 256)
+        self.assertEqual(vlc.MediaOption.unique.value, 256)
 
     def test_enum_playback_mode(self):
-        self.assertEqual(vlc.PlaybackMode.repeat, 2)
+        self.assertEqual(vlc.PlaybackMode.repeat.value, 2)
 
     def test_enum_marquee_int_option(self):
-        self.assertEqual(vlc.VideoMarqueeIntOption.Size, 5)
+        self.assertEqual(vlc.VideoMarqueeIntOption.Size.value, 5)
 
     def test_enum_output_device_type(self):
-        self.assertEqual(vlc.AudioOutputDeviceTypes._2F2R, 4)
+        self.assertEqual(vlc.AudioOutputDeviceTypes._2F2R.value, 4)
 
     def test_enum_output_channel(self):
-        self.assertEqual(vlc.AudioOutputChannel.Dolbys, 5)
+        self.assertEqual(vlc.AudioOutputChannel.Dolbys.value, 5)
 
     def test_enum_position_origin(self):
-        self.assertEqual(vlc.PositionOrigin.ModuloPosition, 2)
+        self.assertEqual(vlc.PositionOrigin.ModuloPosition.value, 2)
 
     def test_enum_position_key(self):
-        self.assertEqual(vlc.PositionKey.MediaTime, 2)
+        self.assertEqual(vlc.PositionKey.MediaTime.value, 2)
 
     def test_enum_player_status(self):
-        self.assertEqual(vlc.PlayerStatus.StopStatus, 5)
+        self.assertEqual(vlc.PlayerStatus.StopStatus.value, 5)
 
     # Basic MediaControl tests
     def test_mediacontrol_creation(self):
@@ -101,5 +101,11 @@ class TestVLCAPI(unittest.TestCase):
         p=i.media_player_new(mrl)
         self.assertEqual(p.get_media().get_mrl(), mrl)
 
+    def test_libvlc_player_state(self):
+        mrl='/tmp/foo.avi'
+        i=vlc.Instance()
+        p=i.media_player_new(mrl)
+        self.assertEqual(p.get_state(), vlc.State.Ended)
+
 if __name__ == '__main__':
     unittest.main()