test_slots.py :  » Game-2D-3D » CGKit » cgkit-2.0.0alpha9 » unittests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » CGKit 
CGKit » cgkit 2.0.0alpha9 » unittests » test_slots.py
# Test slots

import unittest
from cgkit import _core
from cgkit.all import *

class TestSlot(unittest.TestCase):
    
    def testConstructor(self):

        # Standard constructor
        asl = _core.DoubleSlot()
        self.assertEqual(asl.getValue(), 0)
        self.assertEqual(asl.getController(), None)

        asl = _core.IntSlot()
        self.assertEqual(asl.getValue(), 0)

        asl = _core.Vec3Slot()
        self.assertEqual(asl.getValue(), vec3(0))

        asl = _core.StrSlot()
        self.assertEqual(asl.getValue(), "")        

        # Constructor (initialvalue, flags)
        asl = _core.DoubleSlot(12.7, 0)
        self.assertEqual(asl.getValue(), 12.7)
        self.assertEqual(asl.getController(), None)

        asl = _core.IntSlot(5, 0)
        self.assertEqual(asl.getValue(), 5)

        asl = _core.Vec3Slot(vec3(1,2,3), 0)
        self.assertEqual(asl.getValue(), vec3(1,2,3))

        asl = _core.StrSlot("spam", 0)
        self.assertEqual(asl.getValue(), "spam")

    def testNoneConnection(self):
        """Check that connecting to None doesn't crash."""
        asl = DoubleSlot()
        asl.connect(None)

    def testNoneDisconnection(self):
        """Check that disconnecting from None doesn't crash."""
        asl = DoubleSlot()
        asl.disconnect(None)

    def testDisconnect(self):
        """Test the disconnect method."""
        ctrl = DoubleSlot(12)
        asl = DoubleSlot(5)

        # Check that disconnecing a non-existent connection raises an error
        self.assertRaises(ValueError, ctrl.disconnect, asl)

        # Connect and disconnect and check that the controller doesn't
        # influence asl anymore
        ctrl.connect(asl)
        self.assertEqual(asl.getValue(), 12.0)
        ctrl.disconnect(asl)
        ctrl.setValue(4)
        self.assertEqual(ctrl.getValue(), 4.0)
        self.assertEqual(asl.getValue(), 12.0)


##class TestArraySlot(unittest.TestCase):
    
##    def testConstructor(self):

##        # Standard constructor (check if items are 0)
##        asl = _core.IntArraySlot()
##        asl.resize(100)
##        self.assertEqual(list(as), 100*[0])

##        asl = _core.DoubleArraySlot()
##        asl.resize(100)
##        self.assertEqual(list(as), 100*[0.0])
        

class TestProceduralSlot(unittest.TestCase):

    def doubleProcedure(self):
        """Dummy procedure to compute a slot value."""
        return 42.0

    def testGetValue(self):
        """Check that the procedural value is actually obtained."""
        ps = ProceduralDoubleSlot(self.doubleProcedure)
        self.assertEqual(ps.getValue(), 42.0)
    
    def testSetValue(self):
        """Check that setting a value has no effect."""
        ps = ProceduralDoubleSlot(self.doubleProcedure)
        ps.setValue(15.5)
        self.assertEqual(ps.getValue(), 42.0)

    def testInConnection(self):
        """Check that a procedural slot cannot take controllers."""
        ctrl = DoubleSlot(15.0)
        ps = ProceduralDoubleSlot(self.doubleProcedure)

        # The following should not throw an exception
        ps.setController(None)
        
#        ctrl.connect(None)


class TestTransformSlots(unittest.TestCase):

    def testConstructor(self):
        w = WorldObject()
        self.assertEqual(w.transform, mat4(1))
        self.assertEqual(w.pos, vec3(0))
        self.assertEqual(w.rot, mat3(1))
        self.assertEqual(w.scale, vec3(1))

    def testSetValueWithConnections(self):
        """Check setValue() in the presence of connections."""

        # Create an expression (which has a *fixed* output)
        e = Expression("(1,2,3)")
        w = WorldObject()
        w.pos = vec3(-1,5,0.5)
        self.assertEqual(w.pos, vec3(-1,5,0.5))
        self.assertEqual(w.transform, mat4(1).translation(vec3(-1,5,0.5)))

        # Connect the pos slot -> the new position must show up
        e.output_slot.connect(w.pos_slot)
        self.assertEqual(w.pos, vec3(1,2,3))
        self.assertEqual(w.transform, mat4(1).translation(vec3(1,2,3)))

        # Set a new pos -> should have no effect as the output slot of the
        # expression cannot be changed
        w.pos = vec3(0,0.2,0.5)
        self.assertEqual(w.pos, vec3(1,2,3))
        self.assertEqual(w.transform, mat4(1).translation(vec3(1,2,3)))

        # Set a new position via the transform slot -> should also have no
        # effect on the position
        w.transform = mat4([2,0,0,-1, 0,2,0,0.2, 0,0,2,1.7, 0,0,0,1])
        self.assertEqual(w.pos, vec3(1,2,3))
        self.assertEqual(w.transform, mat4([2,0,0,1, 0,2,0,2, 0,0,2,3, 0,0,0,1]))

######################################################################

if __name__=="__main__":
    unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.