test_traitlets.py
814 lines
| 21.3 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r2175 | #!/usr/bin/env python | ||
# encoding: utf-8 | ||||
""" | ||||
Tests for IPython.utils.traitlets. | ||||
Authors: | ||||
* Brian Granger | ||||
* Enthought, Inc. Some of the code in this file comes from enthought.traits | ||||
and is licensed under the BSD license. Also, many of the ideas also come | ||||
from enthought.traits even though our implementation is very different. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (C) 2008-2009 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
from unittest import TestCase | ||||
from IPython.utils.traitlets import ( | ||||
MinRK
|
r3870 | HasTraits, MetaHasTraits, TraitType, Any, CStr, | ||
Dav Clark
|
r2537 | Int, Long, Float, Complex, Str, Unicode, TraitError, | ||
MinRK
|
r3870 | Undefined, Type, This, Instance, TCPAddress, List, Tuple | ||
Brian Granger
|
r2175 | ) | ||
#----------------------------------------------------------------------------- | ||||
# Helper classes for testing | ||||
#----------------------------------------------------------------------------- | ||||
Dav Clark
|
r2385 | class HasTraitsStub(HasTraits): | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2385 | def _notify_trait(self, name, old, new): | ||
Brian Granger
|
r2175 | self._notify_name = name | ||
self._notify_old = old | ||||
self._notify_new = new | ||||
#----------------------------------------------------------------------------- | ||||
# Test classes | ||||
#----------------------------------------------------------------------------- | ||||
Dav Clark
|
r2385 | class TestTraitType(TestCase): | ||
Brian Granger
|
r2175 | |||
Brian Granger
|
r2177 | def test_get_undefined(self): | ||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Dav Clark
|
r2385 | a = TraitType | ||
Brian Granger
|
r2182 | a = A() | ||
self.assertEquals(a.a, Undefined) | ||||
Brian Granger
|
r2175 | |||
def test_set(self): | ||||
Dav Clark
|
r2385 | class A(HasTraitsStub): | ||
a = TraitType | ||||
Brian Granger
|
r2182 | |||
a = A() | ||||
a.a = 10 | ||||
self.assertEquals(a.a, 10) | ||||
self.assertEquals(a._notify_name, 'a') | ||||
self.assertEquals(a._notify_old, Undefined) | ||||
self.assertEquals(a._notify_new, 10) | ||||
Brian Granger
|
r2175 | |||
def test_validate(self): | ||||
Dav Clark
|
r2385 | class MyTT(TraitType): | ||
Brian Granger
|
r2175 | def validate(self, inst, value): | ||
return -1 | ||||
Dav Clark
|
r2385 | class A(HasTraitsStub): | ||
Brian Granger
|
r2182 | tt = MyTT | ||
a = A() | ||||
a.tt = 10 | ||||
self.assertEquals(a.tt, -1) | ||||
Brian Granger
|
r2175 | |||
Brian Granger
|
r2177 | def test_default_validate(self): | ||
Dav Clark
|
r2385 | class MyIntTT(TraitType): | ||
Brian Granger
|
r2177 | def validate(self, obj, value): | ||
if isinstance(value, int): | ||||
return value | ||||
self.error(obj, value) | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2182 | tt = MyIntTT(10) | ||
a = A() | ||||
self.assertEquals(a.tt, 10) | ||||
Dav Clark
|
r2384 | # Defaults are validated when the HasTraits is instantiated | ||
class B(HasTraits): | ||||
Brian Granger
|
r2182 | tt = MyIntTT('bad default') | ||
Dav Clark
|
r2385 | self.assertRaises(TraitError, B) | ||
Brian Granger
|
r2177 | |||
Brian Granger
|
r2175 | def test_is_valid_for(self): | ||
Dav Clark
|
r2385 | class MyTT(TraitType): | ||
Brian Granger
|
r2175 | def is_valid_for(self, value): | ||
return True | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2182 | tt = MyTT | ||
a = A() | ||||
a.tt = 10 | ||||
self.assertEquals(a.tt, 10) | ||||
Brian Granger
|
r2175 | |||
def test_value_for(self): | ||||
Dav Clark
|
r2385 | class MyTT(TraitType): | ||
Brian Granger
|
r2175 | def value_for(self, value): | ||
return 20 | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2182 | tt = MyTT | ||
a = A() | ||||
a.tt = 10 | ||||
self.assertEquals(a.tt, 20) | ||||
Brian Granger
|
r2175 | |||
def test_info(self): | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Dav Clark
|
r2385 | tt = TraitType | ||
Brian Granger
|
r2182 | a = A() | ||
self.assertEquals(A.tt.info(), 'any value') | ||||
Brian Granger
|
r2175 | |||
def test_error(self): | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Dav Clark
|
r2385 | tt = TraitType | ||
Brian Granger
|
r2182 | a = A() | ||
Dav Clark
|
r2385 | self.assertRaises(TraitError, A.tt.error, a, 10) | ||
Brian Granger
|
r2175 | |||
Robert Kern
|
r3207 | def test_dynamic_initializer(self): | ||
class A(HasTraits): | ||||
x = Int(10) | ||||
def _x_default(self): | ||||
return 11 | ||||
class B(A): | ||||
x = Int(20) | ||||
class C(A): | ||||
def _x_default(self): | ||||
return 21 | ||||
a = A() | ||||
self.assertEquals(a._trait_values, {}) | ||||
Robert Kern
|
r3336 | self.assertEquals(a._trait_dyn_inits.keys(), ['x']) | ||
Robert Kern
|
r3207 | self.assertEquals(a.x, 11) | ||
self.assertEquals(a._trait_values, {'x': 11}) | ||||
b = B() | ||||
self.assertEquals(b._trait_values, {'x': 20}) | ||||
Robert Kern
|
r3336 | self.assertEquals(a._trait_dyn_inits.keys(), ['x']) | ||
Robert Kern
|
r3207 | self.assertEquals(b.x, 20) | ||
c = C() | ||||
self.assertEquals(c._trait_values, {}) | ||||
Robert Kern
|
r3336 | self.assertEquals(a._trait_dyn_inits.keys(), ['x']) | ||
Robert Kern
|
r3207 | self.assertEquals(c.x, 21) | ||
self.assertEquals(c._trait_values, {'x': 21}) | ||||
Robert Kern
|
r3336 | # Ensure that the base class remains unmolested when the _default | ||
# initializer gets overridden in a subclass. | ||||
a = A() | ||||
c = C() | ||||
self.assertEquals(a._trait_values, {}) | ||||
self.assertEquals(a._trait_dyn_inits.keys(), ['x']) | ||||
self.assertEquals(a.x, 11) | ||||
self.assertEquals(a._trait_values, {'x': 11}) | ||||
Robert Kern
|
r3207 | |||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2384 | class TestHasTraitsMeta(TestCase): | ||
Brian Granger
|
r2175 | |||
def test_metaclass(self): | ||||
Dav Clark
|
r2384 | self.assertEquals(type(HasTraits), MetaHasTraits) | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2175 | a = Int | ||
a = A() | ||||
Dav Clark
|
r2384 | self.assertEquals(type(a.__class__), MetaHasTraits) | ||
Brian Granger
|
r2175 | self.assertEquals(a.a,0) | ||
a.a = 10 | ||||
self.assertEquals(a.a,10) | ||||
Dav Clark
|
r2384 | class B(HasTraits): | ||
Brian Granger
|
r2175 | b = Int() | ||
b = B() | ||||
self.assertEquals(b.b,0) | ||||
b.b = 10 | ||||
self.assertEquals(b.b,10) | ||||
Dav Clark
|
r2384 | class C(HasTraits): | ||
Brian Granger
|
r2175 | c = Int(30) | ||
c = C() | ||||
self.assertEquals(c.c,30) | ||||
c.c = 10 | ||||
self.assertEquals(c.c,10) | ||||
Brian Granger
|
r2183 | def test_this_class(self): | ||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2183 | t = This() | ||
tt = This() | ||||
class B(A): | ||||
tt = This() | ||||
ttt = This() | ||||
self.assertEquals(A.t.this_class, A) | ||||
self.assertEquals(B.t.this_class, A) | ||||
self.assertEquals(B.tt.this_class, B) | ||||
self.assertEquals(B.ttt.this_class, B) | ||||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2384 | class TestHasTraitsNotify(TestCase): | ||
Brian Granger
|
r2175 | |||
def setUp(self): | ||||
self._notify1 = [] | ||||
self._notify2 = [] | ||||
def notify1(self, name, old, new): | ||||
self._notify1.append((name, old, new)) | ||||
def notify2(self, name, old, new): | ||||
self._notify2.append((name, old, new)) | ||||
def test_notify_all(self): | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2175 | a = Int | ||
b = Float | ||||
a = A() | ||||
Dav Clark
|
r2384 | a.on_trait_change(self.notify1) | ||
Brian Granger
|
r2175 | a.a = 0 | ||
self.assertEquals(len(self._notify1),0) | ||||
a.b = 0.0 | ||||
self.assertEquals(len(self._notify1),0) | ||||
a.a = 10 | ||||
self.assert_(('a',0,10) in self._notify1) | ||||
a.b = 10.0 | ||||
self.assert_(('b',0.0,10.0) in self._notify1) | ||||
Dav Clark
|
r2385 | self.assertRaises(TraitError,setattr,a,'a','bad string') | ||
self.assertRaises(TraitError,setattr,a,'b','bad string') | ||||
Brian Granger
|
r2175 | self._notify1 = [] | ||
Dav Clark
|
r2384 | a.on_trait_change(self.notify1,remove=True) | ||
Brian Granger
|
r2175 | a.a = 20 | ||
a.b = 20.0 | ||||
self.assertEquals(len(self._notify1),0) | ||||
def test_notify_one(self): | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2175 | a = Int | ||
b = Float | ||||
a = A() | ||||
Dav Clark
|
r2384 | a.on_trait_change(self.notify1, 'a') | ||
Brian Granger
|
r2175 | a.a = 0 | ||
self.assertEquals(len(self._notify1),0) | ||||
a.a = 10 | ||||
self.assert_(('a',0,10) in self._notify1) | ||||
Dav Clark
|
r2385 | self.assertRaises(TraitError,setattr,a,'a','bad string') | ||
Brian Granger
|
r2175 | |||
def test_subclass(self): | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2175 | a = Int | ||
class B(A): | ||||
b = Float | ||||
b = B() | ||||
self.assertEquals(b.a,0) | ||||
self.assertEquals(b.b,0.0) | ||||
b.a = 100 | ||||
b.b = 100.0 | ||||
self.assertEquals(b.a,100) | ||||
self.assertEquals(b.b,100.0) | ||||
def test_notify_subclass(self): | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2175 | a = Int | ||
class B(A): | ||||
b = Float | ||||
b = B() | ||||
Dav Clark
|
r2384 | b.on_trait_change(self.notify1, 'a') | ||
b.on_trait_change(self.notify2, 'b') | ||||
Brian Granger
|
r2175 | b.a = 0 | ||
b.b = 0.0 | ||||
self.assertEquals(len(self._notify1),0) | ||||
self.assertEquals(len(self._notify2),0) | ||||
b.a = 10 | ||||
b.b = 10.0 | ||||
self.assert_(('a',0,10) in self._notify1) | ||||
self.assert_(('b',0.0,10.0) in self._notify2) | ||||
def test_static_notify(self): | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2175 | a = Int | ||
_notify1 = [] | ||||
def _a_changed(self, name, old, new): | ||||
self._notify1.append((name, old, new)) | ||||
a = A() | ||||
a.a = 0 | ||||
Brian Granger
|
r2182 | # This is broken!!! | ||
Brian Granger
|
r2175 | self.assertEquals(len(a._notify1),0) | ||
a.a = 10 | ||||
self.assert_(('a',0,10) in a._notify1) | ||||
class B(A): | ||||
b = Float | ||||
_notify2 = [] | ||||
def _b_changed(self, name, old, new): | ||||
self._notify2.append((name, old, new)) | ||||
b = B() | ||||
b.a = 10 | ||||
b.b = 10.0 | ||||
self.assert_(('a',0,10) in b._notify1) | ||||
self.assert_(('b',0.0,10.0) in b._notify2) | ||||
def test_notify_args(self): | ||||
def callback0(): | ||||
self.cb = () | ||||
def callback1(name): | ||||
self.cb = (name,) | ||||
def callback2(name, new): | ||||
self.cb = (name, new) | ||||
def callback3(name, old, new): | ||||
self.cb = (name, old, new) | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2175 | a = Int | ||
a = A() | ||||
Dav Clark
|
r2384 | a.on_trait_change(callback0, 'a') | ||
Brian Granger
|
r2175 | a.a = 10 | ||
self.assertEquals(self.cb,()) | ||||
Dav Clark
|
r2384 | a.on_trait_change(callback0, 'a', remove=True) | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2384 | a.on_trait_change(callback1, 'a') | ||
Brian Granger
|
r2175 | a.a = 100 | ||
self.assertEquals(self.cb,('a',)) | ||||
Dav Clark
|
r2384 | a.on_trait_change(callback1, 'a', remove=True) | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2384 | a.on_trait_change(callback2, 'a') | ||
Brian Granger
|
r2175 | a.a = 1000 | ||
self.assertEquals(self.cb,('a',1000)) | ||||
Dav Clark
|
r2384 | a.on_trait_change(callback2, 'a', remove=True) | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2384 | a.on_trait_change(callback3, 'a') | ||
Brian Granger
|
r2175 | a.a = 10000 | ||
self.assertEquals(self.cb,('a',1000,10000)) | ||||
Dav Clark
|
r2384 | a.on_trait_change(callback3, 'a', remove=True) | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2385 | self.assertEquals(len(a._trait_notifiers['a']),0) | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2384 | class TestHasTraits(TestCase): | ||
Brian Granger
|
r2179 | |||
Dav Clark
|
r2384 | def test_trait_names(self): | ||
class A(HasTraits): | ||||
Brian Granger
|
r2184 | i = Int | ||
f = Float | ||||
a = A() | ||||
Dav Clark
|
r2384 | self.assertEquals(a.trait_names(),['i','f']) | ||
Brian Granger
|
r3789 | self.assertEquals(A.class_trait_names(),['i','f']) | ||
Brian Granger
|
r2184 | |||
Dav Clark
|
r2384 | def test_trait_metadata(self): | ||
class A(HasTraits): | ||||
Brian Granger
|
r2184 | i = Int(config_key='MY_VALUE') | ||
Brian Granger
|
r2179 | a = A() | ||
Dav Clark
|
r2384 | self.assertEquals(a.trait_metadata('i','config_key'), 'MY_VALUE') | ||
Brian Granger
|
r2179 | |||
Dav Clark
|
r2384 | def test_traits(self): | ||
class A(HasTraits): | ||||
Brian Granger
|
r2184 | i = Int | ||
f = Float | ||||
a = A() | ||||
Dav Clark
|
r2384 | self.assertEquals(a.traits(), dict(i=A.i, f=A.f)) | ||
Brian Granger
|
r3789 | self.assertEquals(A.class_traits(), dict(i=A.i, f=A.f)) | ||
Brian Granger
|
r2184 | |||
Dav Clark
|
r2384 | def test_traits_metadata(self): | ||
class A(HasTraits): | ||||
Brian Granger
|
r2184 | i = Int(config_key='VALUE1', other_thing='VALUE2') | ||
f = Float(config_key='VALUE3', other_thing='VALUE2') | ||||
Brian Granger
|
r2245 | j = Int(0) | ||
Brian Granger
|
r2184 | a = A() | ||
Dav Clark
|
r2384 | self.assertEquals(a.traits(), dict(i=A.i, f=A.f, j=A.j)) | ||
Dav Clark
|
r2385 | traits = a.traits(config_key='VALUE1', other_thing='VALUE2') | ||
self.assertEquals(traits, dict(i=A.i)) | ||||
Brian Granger
|
r2245 | |||
# This passes, but it shouldn't because I am replicating a bug in | ||||
# traits. | ||||
Dav Clark
|
r2385 | traits = a.traits(config_key=lambda v: True) | ||
self.assertEquals(traits, dict(i=A.i, f=A.f, j=A.j)) | ||||
Brian Granger
|
r2245 | |||
Brian Granger
|
r2740 | def test_init(self): | ||
class A(HasTraits): | ||||
i = Int() | ||||
x = Float() | ||||
a = A(i=1, x=10.0) | ||||
self.assertEquals(a.i, 1) | ||||
self.assertEquals(a.x, 10.0) | ||||
Brian Granger
|
r2175 | |||
#----------------------------------------------------------------------------- | ||||
Dav Clark
|
r2385 | # Tests for specific trait types | ||
Brian Granger
|
r2175 | #----------------------------------------------------------------------------- | ||
Brian Granger
|
r2177 | |||
class TestType(TestCase): | ||||
def test_default(self): | ||||
class B(object): pass | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2177 | klass = Type | ||
a = A() | ||||
self.assertEquals(a.klass, None) | ||||
Brian Granger
|
r2229 | |||
Brian Granger
|
r2177 | a.klass = B | ||
self.assertEquals(a.klass, B) | ||||
Dav Clark
|
r2385 | self.assertRaises(TraitError, setattr, a, 'klass', 10) | ||
Brian Granger
|
r2177 | |||
def test_value(self): | ||||
class B(object): pass | ||||
class C(object): pass | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2177 | klass = Type(B) | ||
a = A() | ||||
self.assertEquals(a.klass, B) | ||||
Dav Clark
|
r2385 | self.assertRaises(TraitError, setattr, a, 'klass', C) | ||
self.assertRaises(TraitError, setattr, a, 'klass', object) | ||||
Brian Granger
|
r2177 | a.klass = B | ||
def test_allow_none(self): | ||||
class B(object): pass | ||||
class C(B): pass | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2177 | klass = Type(B, allow_none=False) | ||
a = A() | ||||
self.assertEquals(a.klass, B) | ||||
Dav Clark
|
r2385 | self.assertRaises(TraitError, setattr, a, 'klass', None) | ||
Brian Granger
|
r2177 | a.klass = C | ||
self.assertEquals(a.klass, C) | ||||
Brian Granger
|
r2182 | def test_validate_klass(self): | ||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2229 | klass = Type('no strings allowed') | ||
self.assertRaises(ImportError, A) | ||||
Brian Granger
|
r2182 | |||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2229 | klass = Type('rub.adub.Duck') | ||
self.assertRaises(ImportError, A) | ||||
Brian Granger
|
r2182 | |||
def test_validate_default(self): | ||||
class B(object): pass | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2182 | klass = Type('bad default', B) | ||
Brian Granger
|
r2229 | self.assertRaises(ImportError, A) | ||
Brian Granger
|
r2182 | |||
Dav Clark
|
r2384 | class C(HasTraits): | ||
Brian Granger
|
r2182 | klass = Type(None, B, allow_none=False) | ||
Dav Clark
|
r2385 | self.assertRaises(TraitError, C) | ||
Brian Granger
|
r2177 | |||
Brian Granger
|
r2229 | def test_str_klass(self): | ||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2229 | klass = Type('IPython.utils.ipstruct.Struct') | ||
from IPython.utils.ipstruct import Struct | ||||
a = A() | ||||
a.klass = Struct | ||||
self.assertEquals(a.klass, Struct) | ||||
Dav Clark
|
r2385 | self.assertRaises(TraitError, setattr, a, 'klass', 10) | ||
Brian Granger
|
r2229 | |||
Brian Granger
|
r2177 | class TestInstance(TestCase): | ||
def test_basic(self): | ||||
class Foo(object): pass | ||||
class Bar(Foo): pass | ||||
class Bah(object): pass | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2177 | inst = Instance(Foo) | ||
a = A() | ||||
Brian Granger
|
r2182 | self.assert_(a.inst is None) | ||
Brian Granger
|
r2177 | a.inst = Foo() | ||
self.assert_(isinstance(a.inst, Foo)) | ||||
a.inst = Bar() | ||||
self.assert_(isinstance(a.inst, Foo)) | ||||
Dav Clark
|
r2385 | self.assertRaises(TraitError, setattr, a, 'inst', Foo) | ||
self.assertRaises(TraitError, setattr, a, 'inst', Bar) | ||||
self.assertRaises(TraitError, setattr, a, 'inst', Bah()) | ||||
Brian Granger
|
r2177 | |||
def test_unique_default_value(self): | ||||
Brian Granger
|
r2229 | class Foo(object): pass | ||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2182 | inst = Instance(Foo,(),{}) | ||
Brian Granger
|
r2177 | |||
a = A() | ||||
b = A() | ||||
self.assert_(a.inst is not b.inst) | ||||
def test_args_kw(self): | ||||
class Foo(object): | ||||
def __init__(self, c): self.c = c | ||||
Brian Granger
|
r2182 | class Bar(object): pass | ||
class Bah(object): | ||||
def __init__(self, c, d): | ||||
self.c = c; self.d = d | ||||
Brian Granger
|
r2177 | |||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2182 | inst = Instance(Foo, (10,)) | ||
Brian Granger
|
r2177 | a = A() | ||
self.assertEquals(a.inst.c, 10) | ||||
Dav Clark
|
r2384 | class B(HasTraits): | ||
Brian Granger
|
r2182 | inst = Instance(Bah, args=(10,), kw=dict(d=20)) | ||
Brian Granger
|
r2177 | b = B() | ||
self.assertEquals(b.inst.c, 10) | ||||
self.assertEquals(b.inst.d, 20) | ||||
Dav Clark
|
r2384 | class C(HasTraits): | ||
Brian Granger
|
r2182 | inst = Instance(Foo) | ||
c = C() | ||||
self.assert_(c.inst is None) | ||||
def test_bad_default(self): | ||||
Brian Granger
|
r2177 | class Foo(object): pass | ||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2182 | inst = Instance(Foo, allow_none=False) | ||
Brian Granger
|
r2177 | |||
Dav Clark
|
r2385 | self.assertRaises(TraitError, A) | ||
Brian Granger
|
r2182 | |||
def test_instance(self): | ||||
class Foo(object): pass | ||||
def inner(): | ||||
Dav Clark
|
r2384 | class A(HasTraits): | ||
Brian Granger
|
r2182 | inst = Instance(Foo()) | ||
Dav Clark
|
r2385 | self.assertRaises(TraitError, inner) | ||
Brian Granger
|
r2177 | |||
Brian Granger
|
r2180 | |||
class TestThis(TestCase): | ||||
def test_this_class(self): | ||||
Dav Clark
|
r2384 | class Foo(HasTraits): | ||
Brian Granger
|
r2180 | this = This | ||
f = Foo() | ||||
self.assertEquals(f.this, None) | ||||
g = Foo() | ||||
f.this = g | ||||
self.assertEquals(f.this, g) | ||||
Dav Clark
|
r2385 | self.assertRaises(TraitError, setattr, f, 'this', 10) | ||
Brian Granger
|
r2180 | |||
def test_this_inst(self): | ||||
Dav Clark
|
r2384 | class Foo(HasTraits): | ||
Brian Granger
|
r2180 | this = This() | ||
f = Foo() | ||||
f.this = Foo() | ||||
self.assert_(isinstance(f.this, Foo)) | ||||
Brian Granger
|
r2183 | def test_subclass(self): | ||
Dav Clark
|
r2384 | class Foo(HasTraits): | ||
Brian Granger
|
r2183 | t = This() | ||
class Bar(Foo): | ||||
pass | ||||
f = Foo() | ||||
b = Bar() | ||||
f.t = b | ||||
b.t = f | ||||
self.assertEquals(f.t, b) | ||||
self.assertEquals(b.t, f) | ||||
def test_subclass_override(self): | ||||
Dav Clark
|
r2384 | class Foo(HasTraits): | ||
Brian Granger
|
r2183 | t = This() | ||
class Bar(Foo): | ||||
t = This() | ||||
f = Foo() | ||||
b = Bar() | ||||
f.t = b | ||||
self.assertEquals(f.t, b) | ||||
Dav Clark
|
r2385 | self.assertRaises(TraitError, setattr, b, 't', f) | ||
Brian Granger
|
r2180 | |||
Dav Clark
|
r2385 | class TraitTestBase(TestCase): | ||
"""A best testing class for basic trait types.""" | ||||
Brian Granger
|
r2175 | |||
def assign(self, value): | ||||
self.obj.value = value | ||||
def coerce(self, value): | ||||
return value | ||||
def test_good_values(self): | ||||
if hasattr(self, '_good_values'): | ||||
for value in self._good_values: | ||||
self.assign(value) | ||||
self.assertEquals(self.obj.value, self.coerce(value)) | ||||
def test_bad_values(self): | ||||
if hasattr(self, '_bad_values'): | ||||
for value in self._bad_values: | ||||
Dav Clark
|
r2385 | self.assertRaises(TraitError, self.assign, value) | ||
Brian Granger
|
r2175 | |||
def test_default_value(self): | ||||
if hasattr(self, '_default_value'): | ||||
self.assertEquals(self._default_value, self.obj.value) | ||||
Dav Clark
|
r2385 | class AnyTrait(HasTraits): | ||
Brian Granger
|
r2175 | |||
value = Any | ||||
Dav Clark
|
r2385 | class AnyTraitTest(TraitTestBase): | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2385 | obj = AnyTrait() | ||
Brian Granger
|
r2175 | |||
_default_value = None | ||||
_good_values = [10.0, 'ten', u'ten', [10], {'ten': 10},(10,), None, 1j] | ||||
_bad_values = [] | ||||
Dav Clark
|
r2385 | class IntTrait(HasTraits): | ||
Brian Granger
|
r2175 | |||
value = Int(99) | ||||
Dav Clark
|
r2385 | class TestInt(TraitTestBase): | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2385 | obj = IntTrait() | ||
Brian Granger
|
r2175 | _default_value = 99 | ||
_good_values = [10, -10] | ||||
_bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, 1j, 10L, | ||||
-10L, 10.1, -10.1, '10L', '-10L', '10.1', '-10.1', u'10L', | ||||
u'-10L', u'10.1', u'-10.1', '10', '-10', u'10', u'-10'] | ||||
Dav Clark
|
r2385 | class LongTrait(HasTraits): | ||
Brian Granger
|
r2175 | |||
value = Long(99L) | ||||
Dav Clark
|
r2385 | class TestLong(TraitTestBase): | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2385 | obj = LongTrait() | ||
Brian Granger
|
r2175 | |||
_default_value = 99L | ||||
_good_values = [10, -10, 10L, -10L] | ||||
_bad_values = ['ten', u'ten', [10], [10l], {'ten': 10},(10,),(10L,), | ||||
None, 1j, 10.1, -10.1, '10', '-10', '10L', '-10L', '10.1', | ||||
'-10.1', u'10', u'-10', u'10L', u'-10L', u'10.1', | ||||
u'-10.1'] | ||||
Dav Clark
|
r2385 | class FloatTrait(HasTraits): | ||
Brian Granger
|
r2175 | |||
value = Float(99.0) | ||||
Dav Clark
|
r2385 | class TestFloat(TraitTestBase): | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2385 | obj = FloatTrait() | ||
Brian Granger
|
r2175 | |||
_default_value = 99.0 | ||||
_good_values = [10, -10, 10.1, -10.1] | ||||
_bad_values = [10L, -10L, 'ten', u'ten', [10], {'ten': 10},(10,), None, | ||||
1j, '10', '-10', '10L', '-10L', '10.1', '-10.1', u'10', | ||||
u'-10', u'10L', u'-10L', u'10.1', u'-10.1'] | ||||
Dav Clark
|
r2385 | class ComplexTrait(HasTraits): | ||
Brian Granger
|
r2175 | |||
value = Complex(99.0-99.0j) | ||||
Dav Clark
|
r2385 | class TestComplex(TraitTestBase): | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2385 | obj = ComplexTrait() | ||
Brian Granger
|
r2175 | |||
_default_value = 99.0-99.0j | ||||
_good_values = [10, -10, 10.1, -10.1, 10j, 10+10j, 10-10j, | ||||
10.1j, 10.1+10.1j, 10.1-10.1j] | ||||
_bad_values = [10L, -10L, u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None] | ||||
Dav Clark
|
r2385 | class StringTrait(HasTraits): | ||
Brian Granger
|
r2175 | |||
value = Str('string') | ||||
Dav Clark
|
r2385 | class TestString(TraitTestBase): | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2385 | obj = StringTrait() | ||
Brian Granger
|
r2175 | |||
_default_value = 'string' | ||||
_good_values = ['10', '-10', '10L', | ||||
'-10L', '10.1', '-10.1', 'string'] | ||||
_bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j, [10], | ||||
['ten'],{'ten': 10},(10,), None, u'string'] | ||||
Dav Clark
|
r2385 | class UnicodeTrait(HasTraits): | ||
Brian Granger
|
r2175 | |||
value = Unicode(u'unicode') | ||||
Dav Clark
|
r2385 | class TestUnicode(TraitTestBase): | ||
Brian Granger
|
r2175 | |||
Dav Clark
|
r2385 | obj = UnicodeTrait() | ||
Brian Granger
|
r2175 | |||
_default_value = u'unicode' | ||||
_good_values = ['10', '-10', '10L', '-10L', '10.1', | ||||
'-10.1', '', u'', 'string', u'string', ] | ||||
_bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j, | ||||
[10], ['ten'], [u'ten'], {'ten': 10},(10,), None] | ||||
Brian Granger
|
r2742 | |||
class TCPAddressTrait(HasTraits): | ||||
value = TCPAddress() | ||||
class TestTCPAddress(TraitTestBase): | ||||
obj = TCPAddressTrait() | ||||
_default_value = ('127.0.0.1',0) | ||||
_good_values = [('localhost',0),('192.168.0.1',1000),('www.google.com',80)] | ||||
_bad_values = [(0,0),('localhost',10.0),('localhost',-1)] | ||||
MinRK
|
r3870 | |||
class ListTrait(HasTraits): | ||||
value = List(Int) | ||||
class TestList(TraitTestBase): | ||||
obj = ListTrait() | ||||
_default_value = [] | ||||
_good_values = [[], [1], range(10)] | ||||
_bad_values = [10, [1,'a'], 'a', (1,2)] | ||||
class LenListTrait(HasTraits): | ||||
value = List(Int, [0], minlen=1, maxlen=2) | ||||
class TestLenList(TraitTestBase): | ||||
obj = LenListTrait() | ||||
_default_value = [0] | ||||
_good_values = [[1], range(2)] | ||||
_bad_values = [10, [1,'a'], 'a', (1,2), [], range(3)] | ||||
class TupleTrait(HasTraits): | ||||
value = Tuple(Int) | ||||
class TestTupleTrait(TraitTestBase): | ||||
obj = TupleTrait() | ||||
_default_value = None | ||||
_good_values = [(1,), None,(0,)] | ||||
_bad_values = [10, (1,2), [1],('a'), ()] | ||||
def test_invalid_args(self): | ||||
self.assertRaises(TypeError, Tuple, 5) | ||||
self.assertRaises(TypeError, Tuple, default_value='hello') | ||||
t = Tuple(Int, CStr, default_value=(1,5)) | ||||
class LooseTupleTrait(HasTraits): | ||||
value = Tuple((1,2,3)) | ||||
class TestLooseTupleTrait(TraitTestBase): | ||||
obj = LooseTupleTrait() | ||||
_default_value = (1,2,3) | ||||
_good_values = [(1,), None, (0,), tuple(range(5)), tuple('hello'), ('a',5), ()] | ||||
_bad_values = [10, 'hello', [1], []] | ||||
def test_invalid_args(self): | ||||
self.assertRaises(TypeError, Tuple, 5) | ||||
self.assertRaises(TypeError, Tuple, default_value='hello') | ||||
t = Tuple(Int, CStr, default_value=(1,5)) | ||||
class MultiTupleTrait(HasTraits): | ||||
value = Tuple(Int, Str, default_value=[99,'bottles']) | ||||
class TestMultiTuple(TraitTestBase): | ||||
obj = MultiTupleTrait() | ||||
_default_value = (99,'bottles') | ||||
_good_values = [(1,'a'), (2,'b')] | ||||
_bad_values = ((),10, 'a', (1,'a',3), ('a',1)) | ||||