Source

options / test / test.py

Diff from to

test/test.py

 
 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)