Changeset 2869

Show
Ignore:
Timestamp:
03/02/07 16:28:35 (2 years ago)
Author:
nate
Message:

All 15 unit tests are running. We are in the green. Had to modify the sanity test because it was a slow test smell. There are several code smells and refactoring jobs I can see. No test smells though. Will continue with the rest of dColors now.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/dColors.py

    r2868 r2869  
    1 import re 
     1import re, types 
    22 
    33class HexError(Exception): pass 
     
    165165 
    166166def hexToDec(hx): 
     167    if not isinstance(hx, types.StringTypes): 
     168        raise TypeError, "Input must be a string" 
    167169    # Define a dict of char-value pairs 
    168170    hex = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8,  
     
    173175    pos = 1 
    174176    for c in rev: 
     177        if hex.get(c) == None: 
     178            raise InvalidCharError, "%s is an invalid hex character" % (c, ) 
    175179        ret += (hex[c] * pos) 
    176180        pos = pos * 16 
     
    180184def tupleToHex(t, includeHash=True): 
    181185    """Convert a color tuple into an HTML hex format.""" 
     186    if not len(t) == 3: 
     187        raise LengthError, "Color tuple needs to contain 3 elements" 
     188    for rgb in t: 
     189        if not isinstance(rgb, types.IntType): 
     190            raise IntegerTypeError, "Tuple elements should be all integers." 
     191        if not 0 <= rgb <= 255: 
     192            raise RgbValueError, "Rgb Value must be in the range 0-255" 
    182193    rx, gx, bx = hex(t[0]), hex(t[1]), hex(t[2]) 
    183194    # Each is in the format '0x00'. 
     
    185196    g = gx[2:].upper() 
    186197    b= bx[2:].upper() 
     198    if len(r) == 1: r = '0' + r 
     199    if len(g) == 1: g = '0' + g 
     200    if len(b) == 1: b = '0' + b 
    187201    ret = "" 
    188202    if includeHash: 
     
    195209    # Strip the pound sign, if any 
    196210    hx = hx.replace("#", "") 
     211    hx = hx.lstrip('0') 
     212    if len(hx) < 6: hx = '0'*(6-len(hx)) + hx 
    197213    red = hexToDec(hx[:2]) 
    198214    green = hexToDec(hx[2:4]) 
  • trunk/tests/unitTests/Test_dColors.py

    r2868 r2869  
    1414import unittest 
    1515from dabo import dColors 
     16import random 
    1617 
    1718 
     
    8788    knownValues = (("#000000",(0,0,0)), ("#000001",(0,0,1)), ("#000101",(0,1,1)), ("#010101",(1,1,1)), 
    8889                  ("#898989",(137,137,137)), ("#FFFFFF",(255,255,255)), ("#AAAAAA",(170,170,170)),  
    89                   ("#A9A9A9",(169,169,169)), ("#A0A0A0",(10,10,10)), ("#090909",(9,9,9)), 
     90                  ("#A9A9A9",(169,169,169)), ("#0A0A0A",(10,10,10)), ("#090909",(9,9,9)), 
    9091                  ("#1A2BF3",(26,43,243)), ("#3F029E",(63,02,158)), ("#707070",(112,112,112)), 
    9192                  ("#6F6F6F",(111,111,111)), ("#8A8A8A",(138,138,138)), ("#00FF80",(0,255,128)), 
     
    100101     
    101102    def testKnownValuesHexToTuple(self): 
    102         """tupleToHex should give known result with known input""" 
     103        """colorTupleFromHex should give known result with known input""" 
    103104        for hex, tuple in self.knownValues: 
    104105            result = dColors.colorTupleFromHex(hex) 
     
    106107     
    107108    def testSanity(self): 
    108         """tuple = tupleToHex(colorTupleFromHex(tuple)) for all valid input values""" 
    109         for a in range(256): 
    110            for b in range(256): 
    111                for c in range(256): 
    112                    self.assertEqual((a,b,c), dColors.colorTupleFromHex(dColors.tupleToHex((a,b,c)))) 
     109        """tuple = tupleToHex(colorTupleFromHex(tuple)) for range of valid input values""" 
     110        #All inputs was way too many 
     111        for run in range(1000): 
     112            a, b, c = random.choice(range(256)), random.choice(range(256)), random.choice(range(256)) 
     113            self.assertEqual((a,b,c), dColors.colorTupleFromHex(dColors.tupleToHex((a,b,c)))) 
    113114     
    114115    #Test Errors 
     
    142143    def testHexToTupleLeadingZeros(self): 
    143144        """testing colorTupleFromHex(number) = colorTupleFromHex('00'+number)""" 
    144         number = "FF" 
     145        number = "0A0CFF" 
    145146        result = dColors.colorTupleFromHex(number) 
    146147        zeroResult = dColors.colorTupleFromHex("000" + number)