Commits

Jonathan Eunice committed 8ca075f

updated stuf version reqiured and tests

Comments (0)

Files changed (4)

    `pytest <http://pypi.python.org/pypi/pytest>`_
    and `tox <http://pypi.python.org/pypi/tox>`_. Now
    successfully packaged for, and tested against, Python 2.6, 2.7, 3.2, and 3.3.
+   
+ * Versions subsequent to 0.200 require a late-model version of ``stuf`` to
+   avoid a problem its earlier iterations had with file objects.
  
  * The author, `Jonathan Eunice <mailto:jonathan.eunice@gmail.com>`_ or
    `@jeunice on Twitter <http://twitter.com/jeunice>`_
 
 setup(
     name='options',
-    version=verno("0.102"),
+    version=verno("0.200"),
     author='Jonathan Eunice',
     author_email='jonathan.eunice@gmail.com',
     description='Container for flexible class, instance, and function call options',
     long_description=open('README.rst').read(),
     url='https://bitbucket.org/jeunice/options',
     py_modules=['options', 'nulltype', 'funclike'],
-    install_requires=['stuf', 'otherstuf>=0.6', 'six'],
+    install_requires=['stuf>=0.9.9', 'otherstuf>=0.65', 'six'],
     tests_require = ['tox', 'pytest'],
     zip_safe = True,
     keywords='options config configuration parameters arguments',
 
 from options import *
+import sys
+import six
     
 def test_good_chainstuf():
     """Test options class for being faithful subclass of chainstuf"""
     assert t.options.man == 'BOY'
     t.set(man='girl')
     assert t.options.man == 'GIRL'
-    
+    
+
+   
+def test_push():
+    
+    class T2(object):
+        options = Options(
+            this=1,
+            slick='slack',
+            nick='nack',
+            blik=99,
+            man='man'
+        )
+        
+        def __init__(self, *args, **kwargs):
+            self.options = T2.options.push(kwargs)
+            self.data = args
+            
+        def set(self, **kwargs):
+            """
+            Uplevel the set operation. A set() on this object is converted into
+            a set on its options.
+            """
+            self.options.set(**kwargs)
+        
+        def write(self, **kwargs):
+            opts = self.options.push(kwargs)
+            six.print_(opts.nick)
+            
+        def push1(self, **kwargs):
+            opts = self.options.push(kwargs)
+            
+            # persistent data test
+            assert T2.options.nick == 'nack'
+            assert T2.options.slick == 'slack'
+            assert t.options.nick == 'N'
+            assert t.options.slick == 'S'
+
+            # transient data test
+            assert opts.nick == 44
+            assert opts.slick == 55
+    
+    t = T2(nick='N', slick='S')
+    assert T2.options.nick == 'nack'
+    assert T2.options.slick == 'slack'
+    assert t.options.nick == 'N'
+    assert t.options.slick == 'S'
+    t.push1(nick=44, slick=55)
+    
+def test_files_ok():
+    # The underlying stuf container used to have (<0.9.9) a problem with files
+    # being assigned in a stuf() constructor. This tests that we're over that
+    # problem.
+    
+    # Get names of files that won't be munged by py.test's capturing mechanism
+    # (sys.stdout and sys.stderr definitely will be overtaken by py.test, but
+    # their primitive double-underscore names won't be)
+    f1 = sys.__stdout__
+    f2 = sys.__stderr__
+    f3 = sys.__stdin__
+    
+    o = Options(a=f1, b=f2, c=[f2, f3])
+
+    assert o.a is f1
+    assert o.b is f2
+    assert len(o.c) == 2
+    assert o.c[0] is f2
+    assert o.c[1] is f3
+    
+    # first push
+    oo = o.push(dict(b=f1, c=12))
+    assert oo.a is f1
+    assert oo.b is f1
+    assert oo.c == 12
+    
+    # second push
+    ooo = oo.push(dict(a=f2, b=f3))
+    assert ooo.a is f2
+    assert ooo.b is f3
+    assert ooo.c == 12
+    
+    # partial unset
+    ooo.set(a=Unset)
+    assert ooo.a is f1
+    assert ooo.b is f3
+    assert ooo.c == 12
+ 
+def test_files_push():
+    
+    class T3(object):
+        options = Options(
+            slick='slack',
+            nick='nack',
+            files = None
+        )
+        assert options.slick == 'slack'
+        assert options.nick  == 'nack'
+        assert options.files == None
+        flist = [ sys.stdout ]
+        assert flist[0] is sys.stdout
+        options.files = flist
+        assert options.files is flist
+        assert options.files[0] is sys.stdout
+        # options.files.append(sys.stdout)
+        
+        def __init__(self, *args, **kwargs):
+            self.options = T3.options.push(kwargs)
+            self.data = args
+            
+        def set(self, **kwargs):
+            """
+            Uplevel the set operation. A set() on this object is converted into
+            a set on its options.
+            """
+            self.options.set(**kwargs)
+        
+        def write(self, **kwargs):
+            opts = self.options.push(kwargs)
+            for f in opts.files:
+                six.print_(opts.nick, file=opts.f)
+            
+        def push1(self, **kwargs):
+            opts = self.options.push(kwargs)
+            
+            # persistent data test
+            assert T3.options.nick == 'nack'
+            assert T3.options.slick == 'slack'
+            assert t.options.nick == 'N'
+            assert t.options.slick == 'S'
+
+            # transient data test
+            assert opts.nick == 44
+            assert opts.slick == 55
+            
+            assert opts.files[0] is sys.stdout
+    
+    t = T3(nick='N', slick='S')
+    assert T3.options.nick == 'nack'
+    assert T3.options.slick == 'slack'
+    assert t.options.nick == 'N'
+    assert t.options.slick == 'S'
+    t.push1(nick=44, slick=55)

test/test_funclike.py

+
+from funclike import *
+
+### Setup
+
+def f():
+    pass
+
+class C(object):
+    
+    def __init__(self, v):
+        self.v = v
+        
+    @staticmethod
+    def f(x):
+        return x * x
+        
+    @staticmethod
+    def f4(x, a, b, c):
+        return x + a + b + c
+    
+    def m(self, x):
+        return x
+        
+
+c = C(12)
+
+### Tests
+
+def test_function_type():
+    assert type(f) is function_type
+
+def test_is_function():
+    assert is_function(f)
+    assert is_function(lambda: False)
+    assert not is_function(4)
+    assert not is_function("lsdjflkj")
+    assert not is_function([1,2,3])
+    
+def test_real_func():
+    assert real_func(f) is f
+    assert is_function(real_func(f))
+    assert is_function(real_func(C.f))
+    assert is_function(real_func(c.f))
+    
+def test_func_code():
+    assert hasattr(func_code(f), 'co_argcount')
+    assert hasattr(func_code(C.f), 'co_argcount')
+    assert hasattr(func_code(c.f), 'co_argcount')
+    assert func_code(f).co_argcount == 0
+    assert func_code(C.f).co_argcount == 1
+    assert func_code(c.f).co_argcount == 1
+    assert func_code(c.f4).co_argcount == 4
+