[pygobject/overrides-glib-coverage] glib overrides: improve test coverage



commit 5d4c37febadbabcdbfe7701941ee6164cb47c699
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Tue Nov 20 21:00:27 2018 +0100

    glib overrides: improve test coverage

 gi/overrides/GLib.py         |  14 ++---
 tests/test_overrides_glib.py | 127 +++++++++++++++++++++++++++++++++++++++++++
 tests/test_subprocess.py     |  25 +++++++++
 3 files changed, 157 insertions(+), 9 deletions(-)
---
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index d717d5fe..5cdf032f 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -825,7 +825,7 @@ def _child_watch_add_get_args(priority_or_pid, pid_or_callback, *args, **kwargs)
     if 'data' in kwargs:
         if user_data:
             raise TypeError('got multiple values for "data" argument')
-        user_data = [kwargs['data']]
+        user_data = (kwargs['data'],)
 
     return priority, pid, callback, user_data
 
@@ -861,14 +861,10 @@ def filename_from_utf8(utf8string, len=-1):
 __all__.append('filename_from_utf8')
 
 
-# backwards compatible API for renamed function
-if not hasattr(GLib, 'unix_signal_add_full'):
-    def add_full_compat(*args):
-        warnings.warn('GLib.unix_signal_add_full() was renamed to GLib.unix_signal_add()',
-                      PyGIDeprecationWarning)
-        return GLib.unix_signal_add(*args)
-
-    GLib.unix_signal_add_full = add_full_compat
+if hasattr("Glib", "unix_signal_add"):
+    unix_signal_add_full = GLib.unix_signal_add
+    __all__.append('unix_signal_add_full')
+    deprecated_attr("GLib", "unix_signal_add_full", "GLib.unix_signal_add")
 
 
 # obsolete constants for backwards compatibility
diff --git a/tests/test_overrides_glib.py b/tests/test_overrides_glib.py
index 34672535..b137a260 100644
--- a/tests/test_overrides_glib.py
+++ b/tests/test_overrides_glib.py
@@ -3,13 +3,117 @@
 
 from __future__ import absolute_import
 
+import os
 import gc
 import unittest
+import tempfile
+import socket
+
+import pytest
 
 import gi
 from gi.repository import GLib
 from gi._compat import long_, integer_types
 
+from .helper import capture_gi_deprecation_warnings
+
+
+def test_io_add_watch_get_args():
+    get_args = GLib._io_add_watch_get_args
+    func = lambda: None
+
+    # create a closed channel for testing
+    fd, fn = tempfile.mkstemp()
+    os.close(fd)
+    try:
+        chan = GLib.IOChannel(filename=fn)
+        chan.shutdown(True)
+    finally:
+        os.remove(fn)
+
+    # old way
+    with capture_gi_deprecation_warnings():
+        assert get_args(chan, GLib.IOCondition.IN, func) == (
+            chan, 0, GLib.IOCondition.IN, func, tuple())
+
+        with pytest.raises(TypeError):
+            get_args(chan, GLib.IOCondition.IN, object())
+
+    # new way
+    prio = GLib.PRIORITY_DEFAULT
+    with capture_gi_deprecation_warnings():
+        assert get_args(chan, prio, GLib.IOCondition.IN, func, 99) == \
+            (chan, prio, GLib.IOCondition.IN, func, (99,))
+
+        with pytest.raises(TypeError):
+            assert get_args(chan, prio, GLib.IOCondition.IN)
+
+        with pytest.raises(TypeError):
+            assert get_args(chan, prio, 99)
+
+
+@pytest.mark.skipif(os.name != "nt", reason="windows only")
+def test_io_add_watch_get_args_win32_socket():
+    get_args = GLib._io_add_watch_get_args
+
+    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    func = lambda: None
+    prio = GLib.PRIORITY_DEFAULT
+    chan = get_args(s, prio, GLib.IOCondition.IN, func)[0]
+    assert isinstance(chan, GLib.IOChannel)
+    chan.shutdown(False)
+
+
+def test_threads_init():
+    with capture_gi_deprecation_warnings() as w:
+        GLib.threads_init()
+    assert len(w)
+
+
+def test_gerror_matches():
+    e = GLib.Error(domain=42, code=24)
+    assert e.matches(42, 24)
+
+
+def test_timeout_add_seconds():
+    h = GLib.timeout_add_seconds(
+        100, lambda *x: None, 1, 2, 3, priority=GLib.PRIORITY_HIGH_IDLE)
+    GLib.source_remove(h)
+
+
+def test_iochannel():
+    with pytest.raises(TypeError):
+        GLib.IOChannel()
+
+
+def test_iochannel_write():
+    fd, fn = tempfile.mkstemp()
+    os.close(fd)
+    chan = GLib.IOChannel(filename=fn, mode="r+")
+    try:
+        assert chan.write(b"foo", 2) == 2
+        chan.seek(0)
+        assert chan.read() == b"fo"
+    finally:
+        chan.shutdown(True)
+
+
+@pytest.mark.skipif(os.name != "nt", reason="windows only")
+def test_iochannel_win32():
+    fd, fn = tempfile.mkstemp()
+    closed = False
+    try:
+        channel = GLib.IOChannel(hwnd=fd)
+        try:
+            assert channel.read() == b""
+        finally:
+            closed = True
+            channel.shutdown(True)
+    finally:
+        if not closed:
+            os.close(fd)
+        os.remove(fn)
+
 
 class TestGVariant(unittest.TestCase):
     def test_create_simple(self):
@@ -25,6 +129,14 @@ class TestGVariant(unittest.TestCase):
         self.assertTrue(isinstance(variant, GLib.Variant))
         self.assertEqual(variant.get_string(), 'hello')
 
+    def test_simple_invalid_ops(self):
+        variant = GLib.Variant('i', 42)
+        with pytest.raises(TypeError):
+            len(variant)
+
+        with pytest.raises(TypeError):
+            variant[0]
+
     def test_create_variant(self):
         variant = GLib.Variant('v', GLib.Variant('i', 42))
         self.assertTrue(isinstance(variant, GLib.Variant))
@@ -97,6 +209,15 @@ class TestGVariant(unittest.TestCase):
         self.assertTrue(isinstance(variant, GLib.Variant))
         self.assertEqual(variant.unpack(), d)
 
+        # init with an iterable
+        variant = GLib.Variant('a{si}', [("foo", 2)])
+        assert variant.unpack() == {'foo': 2}
+
+        with pytest.raises(TypeError):
+            GLib.Variant('a{si}', [("foo",)])
+        with pytest.raises(TypeError):
+            GLib.Variant('a{si}', [("foo", 1, 2)])
+
     def test_create_array(self):
         variant = GLib.Variant('ai', [])
         self.assertEqual(variant.get_type_string(), 'ai')
@@ -186,6 +307,9 @@ class TestGVariant(unittest.TestCase):
         element = array.get_child_value(2)
         self.assertEqual(element.n_children(), 0)
 
+        variant = GLib.Variant('mai', None)
+        assert bool(variant)
+
     def test_create_complex(self):
         variant = GLib.Variant('(as)', ([],))
         self.assertEqual(variant.get_type_string(), '(as)')
@@ -471,6 +595,9 @@ class TestGVariant(unittest.TestCase):
         assert_equal('v', GLib.Variant('i', 42))
         assert_not_equal('v', GLib.Variant('i', 42), 'v', GLib.Variant('i', 43))
 
+        assert GLib.Variant('i', 42) != object()
+        assert not GLib.Variant('i', 42) == object()
+
     def test_bool(self):
         # Check if the GVariant bool matches the unpacked Pythonic bool
 
diff --git a/tests/test_subprocess.py b/tests/test_subprocess.py
index 3ffdf934..bd1056e0 100644
--- a/tests/test_subprocess.py
+++ b/tests/test_subprocess.py
@@ -7,9 +7,34 @@ import os
 import unittest
 import warnings
 
+import pytest
+
 from gi.repository import GLib
 from gi import PyGIDeprecationWarning
 
+from .helper import capture_gi_deprecation_warnings
+
+
+def test_child_watch_add_get_args_various():
+    cb = lambda pid, status: None
+    get_args = GLib._child_watch_add_get_args
+    pid = 42
+    with capture_gi_deprecation_warnings():
+        assert get_args(pid, cb, 2) == (0, pid, cb, (2,))
+
+        with pytest.raises(TypeError):
+            get_args(pid, cb, 2, 3, 4)
+
+        assert get_args(0, pid, 2, 3, function=cb) == (0, pid, cb, (2, 3))
+        assert get_args(0, pid, cb, 2, 3) == (0, pid, cb, (2, 3))
+        assert get_args(0, pid, cb, data=99) == (0, pid, cb, (99,))
+
+        with pytest.raises(TypeError):
+            get_args(0, pid, 24)
+
+        with pytest.raises(TypeError):
+            get_args(0, pid, cb, 2, 3, data=99)
+
 
 @unittest.skipIf(os.name == "nt", "not on Windows")
 class TestProcess(unittest.TestCase):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]