#!/www/python/bin/python

"""$URL: svn://svn/repos/trunk/dulcinea/lib/test/test_specified.py $
$Id: test_specified.py 21278 2003-04-03 21:44:15Z dbinger $

Test for dulcinea.specified.
"""

from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.base import DulcineaPersistent
from dulcinea.spec import *
from dulcinea.specified import *

tested_modules = [ "dulcinea.specified" ]

class SpecifiedTest (TestScenario):

    def check_specified (self):
        class A(Specified):
            flavor = spec(any('sweet', 'sour'), 'the flavor')
            texture = spec(int, 'roughness')
        class B(A):
            color = spec(str)
            texture = nspec(str)
        self.a = A()
        self.test_stmt("self.a.set(flavor='sweet', texture=2)")
        self.test_exc("self.a.set(flavor='sweet', texture='hello')",
                      TypeError)
        self.test_stmt("self.b = B()")
        self.test_stmt("self.b.set(flavor='sweet', texture='hello')")
        self.test_bool("self.b.texture == 'hello'")
        self.test_bool("self.b.get_specification_problems() != []")
        self.test_stmt("self.b.set(color='blue')")
        self.test_bool("self.b.get_specification_problems() == []")
        self.test_stmt("self.b.nonsense = 34")
        self.test_bool("len(self.b.get_specification_problems()) == 1")
        self.test_stmt("del self.b.nonsense")
        self.test_bool("len(self.b.get_specification_problems()) == 0    ")
        self.test_stmt("self.b.color = 42")
        self.test_bool("len(self.b.get_specification_problems()) == 1")
        self.test_stmt("self.b.color = 'red'")
        self.test_bool("len(self.b.get_specification_problems()) == 0        ")
        self.test_bool("len(self.b.get_specifications().values()) == 3")
        self.test_bool("len(self.a.get_specifications().values()) == 2")
        self.test_bool("not self.b.valid(texture=3)")
        self.test_bool("self.b.valid(texture='ouch')")
        self.test_bool("not self.b.valid(flavor='ouch')")
        self.test_bool("self.b.valid(flavor='sweet')")
        self.test_bool("A.texture.format()")
        self.test_bool("B.texture.format()")
        self.test_bool("B.texture.get_spec()")
        self.test_bool("self.b.get_spec('texture')")        
        class C(DulcineaPersistent, Specified):
            color = nspec(str)
        self.test_bool("get_specifications(C)")
        self.test_bool("get_specification_doc(C)")
        self.test_stmt("update_docstring(C)")
        self.test_stmt("update_docstring(C)")
        C.__doc__ = None
        self.test_stmt("update_docstring(C)")
        self.test_stmt("self.b.get_specification_doc()")
        self.test_stmt("self.b.valid_specification('texture', int)")
        self.test_stmt("nspec(None)")
        self.test_stmt("nspec(int)")
        self.test_stmt("nspec(any(int, str))")                

if __name__ == "__main__":
    (scenarios, options) = parse_args()
    options.show_coverage_lines = 1
    options.show_coverage = 1
    run_scenarios (scenarios, options)
