コード例 #1
0
    def testUnitsStr(self):
        "Testing conversion to strings"
        a1 = A(sq_m=100)
        a2 = A(sq_km=3.5)

        self.assertEqual(str(a1), '100.0 sq_m')
        self.assertEqual(str(a2), '3.5 sq_km')
        self.assertEqual(repr(a1), 'Area(sq_m=100.0)')
        self.assertEqual(repr(a2), 'Area(sq_km=3.5)')
コード例 #2
0
    def testComparisons(self):
        "Testing comparisons"
        a1 = A(sq_m=100)
        a2 = A(sq_km=1)
        a3 = A(sq_km=0)

        self.assertTrue(a2 > a1)
        self.assertTrue(a1 == a1)
        self.assertTrue(a1 < a2)
        self.failIf(a3)
コード例 #3
0
    def testInit(self):
        "Testing initialisation from valid units"
        a = Area(sq_m=100)
        self.assertEqual(a.sq_m, 100)

        a = A(sq_m=100)
        self.assertEqual(a.sq_m, 100)

        a = A(sq_mi=100)
        self.assertEqual(a.sq_m, 258998811.0336)
コード例 #4
0
    def testUnitConversions(self):
        "Testing default units during maths"
        a1 = A(sq_m=100)
        a2 = A(sq_km=1)

        a3 = a1 + a2
        self.assertEqual(a3._default_unit, 'sq_m')
        a4 = a2 + a1
        self.assertEqual(a4._default_unit, 'sq_km')
        a5 = a1 * 2
        self.assertEqual(a5._default_unit, 'sq_m')
        a6 = a1 / 2
        self.assertEqual(a6._default_unit, 'sq_m')
コード例 #5
0
    def testAddition(self):
        "Test addition & subtraction"
        a1 = A(sq_m=100)
        a2 = A(sq_m=200)

        a3 = a1 + a2
        self.assertEqual(a3.sq_m, 300)
        a3 += a1
        self.assertEqual(a3.sq_m, 400)

        a4 = a1 - a2
        self.assertEqual(a4.sq_m, -100)
        a4 -= a1
        self.assertEqual(a4.sq_m, -200)

        try:
            a5 = a1 + 1
        except TypeError, e:
            pass
コード例 #6
0
    def testMultiplication(self):
        "Test multiplication & division"
        a1 = A(sq_m=100)

        a3 = a1 * 2
        self.assertEqual(a3.sq_m, 200)
        a3 = 2 * a1
        self.assertEqual(a3.sq_m, 200)
        a3 *= 5
        self.assertEqual(a3.sq_m, 1000)

        a4 = a1 / 2
        self.assertEqual(a4.sq_m, 50)
        a4 /= 5
        self.assertEqual(a4.sq_m, 10)

        try:
            a5 = a1 * A(sq_m=1)
        except TypeError, e:
            pass
コード例 #7
0
 def testAccessInvaliA(self):
     "Testing access in invalid units"
     a = A(sq_m=100)
     self.failIf(hasattr(a, 'banana'))
コード例 #8
0
 def testAccess(self):
     "Testing access in different units"
     a = A(sq_m=100)
     self.assertEqual(a.sq_km, 0.0001)
     self.assertAlmostEqual(a.sq_ft, 1076.391, 3)
コード例 #9
0
        self.assertEqual(a3.sq_m, 1000)

        a4 = a1 / 2
        self.assertEqual(a4.sq_m, 50)
        a4 /= 5
        self.assertEqual(a4.sq_m, 10)

        try:
            a5 = a1 * A(sq_m=1)
        except TypeError, e:
            pass
        else:
            self.fail('Area * Area should raise TypeError')

        try:
            a1 *= A(sq_m=1)
        except TypeError, e:
            pass
        else:
            self.fail('Area *= Area should raise TypeError')

        try:
            a5 = a1 / A(sq_m=1)
        except TypeError, e:
            pass
        else:
            self.fail('Area / Area should raise TypeError')

        try:
            a1 /= A(sq_m=1)
        except TypeError, e: