def test_combine(): assert color.Color(0x12, 0x34, 0x56).combine(r=0x78) == (0x78, 0x34, 0x56, 0xff) assert color.Color(0x12, 0x34, 0x56).combine(g=0x78) == (0x12, 0x78, 0x56, 0xff) assert color.Color(0x12, 0x34, 0x56).combine(b=0x78) == (0x12, 0x34, 0x78, 0xff) assert color.Color(0x12, 0x34, 0x56).combine(a=0x78) == (0x12, 0x34, 0x56, 0x78)
def test_mod(): assert color.Color(0x80, 0x80, 0x80) % color.Color( 0xff, 0xff, 0xff) == color.Color(0x80, 0x80, 0x80) assert color.Color(0x80, 0x80, 0x80, 0x00) % color.Color( 0xff, 0xff, 0xff) == color.Color(0xff, 0xff, 0xff) assert color.Color(0x80, 0x80, 0x80, 0x80) % color.Color( 0xff, 0xff, 0xff) == color.Color(0xbf, 0xbf, 0xbf)
def _load(self, typecode): from ll import misc if typecode is None: typecode = self._nextchar() if typecode == "^": position = self._readint() return self._objects[position] elif typecode in "nN": if typecode == "N": self._loading(None) return None elif typecode in "bB": value = self.stream.read(1) if value == "T": value = True elif value == "F": value = False else: raise ValueError( "broken UL4ON stream at position {}: expected 'T' or 'F' for bool; got {!r}" .format(self.stream.tell(), value)) if typecode == "B": self._loading(value) return value elif typecode in "iI": value = self._readint() if typecode == "I": self._loading(value) return value elif typecode in "fF": chars = [] while True: c = self.stream.read(1) if c and not c.isspace(): chars.append(c) else: value = float("".join(chars)) break if typecode == "F": self._loading(value) return value elif typecode in "sS": delimiter = self.stream.read(1) if not delimiter: raise EOFError() buffer = [delimiter] while True: c = self.stream.read(1) if not c: raise EOFError() buffer.append(c) if c == delimiter: value = ast.literal_eval("".join(buffer)) break elif c == "\\": c2 = self.stream.read(1) if not c2: raise EOFError() buffer.append(c2) if typecode == "S": self._loading(value) return value elif typecode in "cC": from ll import color if typecode == "C": oldpos = self._beginfakeloading() r = self._load(None) g = self._load(None) b = self._load(None) a = self._load(None) value = color.Color(r, g, b, a) if typecode == "C": self._endfakeloading(oldpos, value) return value elif typecode in "zZ": if typecode == "Z": oldpos = self._beginfakeloading() year = self._load(None) month = self._load(None) day = self._load(None) hour = self._load(None) minute = self._load(None) second = self._load(None) microsecond = self._load(None) value = datetime.datetime(year, month, day, hour, minute, second, microsecond) if typecode == "Z": self._endfakeloading(oldpos, value) return value elif typecode in "rR": if typecode == "R": oldpos = self._beginfakeloading() start = self._load(None) stop = self._load(None) value = slice(start, stop) if typecode == "R": self._endfakeloading(oldpos, value) return value elif typecode in "tT": if typecode == "T": oldpos = self._beginfakeloading() days = self._load(None) seconds = self._load(None) microseconds = self._load(None) value = datetime.timedelta(days, seconds, microseconds) if typecode == "T": self._endfakeloading(oldpos, value) return value elif typecode in "mM": from ll import misc if typecode == "M": oldpos = self._beginfakeloading() months = self._load(None) value = misc.monthdelta(months) if typecode == "M": self._endfakeloading(oldpos, value) return value elif typecode in "lL": value = [] if typecode == "L": self._loading(value) while True: typecode = self._nextchar() if typecode == "]": return value else: item = self._load(typecode) value.append(item) elif typecode in "dDeE": value = {} if typecode in "dD" else ordereddict() if typecode in "DE": self._loading(value) while True: typecode = self._nextchar() if typecode == "}": return value else: key = self._load(typecode) if isinstance(key, str): if key in self._keycache: key = self._keycache[key] else: self._keycache[key] = key item = self._load(None) value[key] = item elif typecode in "yY": value = set() if typecode == "Y": self._loading(value) while True: typecode = self._nextchar() if typecode == "}": return value else: item = self._load(typecode) value.add(item) elif typecode in "oO": if typecode == "O": oldpos = self._beginfakeloading() name = self._load(None) cls = None if self.registry is not None: cls = self.registry.get(name) if cls is None: cls = _registry.get(name) if cls is None: raise TypeError("can't decode object of type {}".format(name)) value = cls() if typecode == "O": self._endfakeloading(oldpos, value) value.ul4onload(self) typecode = self._nextchar() if typecode != ")": raise ValueError( "broken UL4ON stream at position {}: object terminator ')' expected, got {!r}" .format(self.stream.tell(), typecode)) return value else: raise ValueError( "broken UL4ON stream at position {}: unknown typecode {!r}". format(self.stream.tell(), typecode))
def test_oracle_color(oracle): if oracle: expected = color.Color(0x66, 0x99, 0xcc, 0xff) got = oracle(f"{oracle.pkg}.color(c_out, 102, 153, 204, 255);") assert expected == got
def test_color(t): c = color.Color(0x66, 0x99, 0xcc, 0xff) assert c == t(c)
def load(self): """ Deserialize the next object in the stream and return it. """ from ll import misc typecode = self._nextchar() if typecode == "^": position = self._readint() return self._objects[position] elif typecode in "nN": if typecode == "N": self._loading(None) return None elif typecode in "bB": value = self.stream.read(1) if value == "T": value = True elif value == "F": value = False else: raise ValueError( f"broken UL4ON stream at position {self.stream.tell():,}: expected 'T' or 'F' for bool; got {value!r}" ) if typecode == "B": self._loading(value) return value elif typecode in "iI": value = self._readint() if typecode == "I": self._loading(value) return value elif typecode in "fF": chars = [] while True: c = self.stream.read(1) if c and not c.isspace(): chars.append(c) else: value = float("".join(chars)) break if typecode == "F": self._loading(value) return value elif typecode in "sS": delimiter = self.stream.read(1) if not delimiter: raise EOFError() buffer = [] while True: c = self.stream.read(1) if not c: raise EOFError() if c == delimiter: value = "".join(buffer).encode( "ascii", "backslashreplace").decode("unicode_escape") break buffer.append(c) if c == "\\": c2 = self.stream.read(1) if not c2: raise EOFError() buffer.append(c2) if typecode == "S": self._loading(value) return value elif typecode in "cC": from ll import color if typecode == "C": oldpos = self._beginfakeloading() r = self.load() g = self.load() b = self.load() a = self.load() value = color.Color(r, g, b, a) if typecode == "C": self._endfakeloading(oldpos, value) return value elif typecode in "zZ": if typecode == "Z": oldpos = self._beginfakeloading() year = self.load() month = self.load() day = self.load() hour = self.load() minute = self.load() second = self.load() microsecond = self.load() value = datetime.datetime(year, month, day, hour, minute, second, microsecond) if typecode == "Z": self._endfakeloading(oldpos, value) return value elif typecode in "xX": if typecode == "X": oldpos = self._beginfakeloading() year = self.load() month = self.load() day = self.load() value = datetime.date(year, month, day) if typecode == "X": self._endfakeloading(oldpos, value) return value elif typecode in "rR": if typecode == "R": oldpos = self._beginfakeloading() start = self.load() stop = self.load() value = slice(start, stop) if typecode == "R": self._endfakeloading(oldpos, value) return value elif typecode in "tT": if typecode == "T": oldpos = self._beginfakeloading() days = self.load() seconds = self.load() microseconds = self.load() value = datetime.timedelta(days, seconds, microseconds) if typecode == "T": self._endfakeloading(oldpos, value) return value elif typecode in "mM": from ll import misc if typecode == "M": oldpos = self._beginfakeloading() months = self.load() value = misc.monthdelta(months) if typecode == "M": self._endfakeloading(oldpos, value) return value elif typecode in "lL": self._stack.append("list") value = [] if typecode == "L": self._loading(value) while True: typecode = self._nextchar() if typecode == "]": self._stack.pop() return value else: self._bufferedchar = typecode item = self.load() value.append(item) elif typecode in "dDeE": self._stack.append("dict" if typecode in "dD" else "odict") value = {} # Load all dicts as a standard Python 3.6 ordered dict if typecode in "DE": self._loading(value) while True: typecode = self._nextchar() if typecode == "}": self._stack.pop() return value else: self._bufferedchar = typecode key = self.load() if isinstance(key, str): if key in self._keycache: key = self._keycache[key] else: self._keycache[key] = key item = self.load() value[key] = item elif typecode in "yY": self._stack.append("set") value = set() if typecode == "Y": self._loading(value) while True: typecode = self._nextchar() if typecode == "}": self._stack.pop() return value else: self._bufferedchar = typecode item = self.load() value.add(item) elif typecode in "oO": if typecode == "O": oldpos = self._beginfakeloading() name = self.load() self._stack.append(name) cls = None if self.registry is not None: cls = self.registry.get(name) if cls is None: cls = _registry.get(name) if cls is None: raise TypeError( f"broken UL4ON stream at position {self.stream.tell():,} (path {self._path()}): can't decode object of type {name!r}" ) value = cls() if typecode == "O": self._endfakeloading(oldpos, value) value.ul4onload(self) typecode = self._nextchar() if typecode != ")": raise ValueError( f"broken UL4ON stream at position {self.stream.tell():,} (path {self._path()}): object terminator ')' expected, got {typecode!r}" ) self._stack.pop() return value else: raise ValueError( f"broken UL4ON stream at position {self.stream.tell():,} (path {self._path()}): unknown typecode {typecode!r}" )
def test_oracle_color(oracle): if oracle: assert color.Color( 0x66, 0x99, 0xcc, 0xff) == oracle("ul4on_pkg.color(c_out, 102, 153, 204, 255);")
def test_floordiv(): assert color.Color(0x25, 0x69, 0xad)//2 == color.Color(0x12, 0x34, 0x56)
def test_truediv(): assert color.Color(0x24, 0x68, 0xac)/2 == color.Color(0x12, 0x34, 0x56)
def test_mul(): assert 2*color.Color(0x12, 0x34, 0x56) == color.Color(0x24, 0x68, 0xac) assert color.Color(0x12, 0x34, 0x56)*2 == color.Color(0x24, 0x68, 0xac)
def test_rgba(): assert color.Color(0x33, 0x66, 0x99, 0xcc).rgba() == (0.2, 0.4, 0.6, 0.8)
def test_r_g_b(): c = color.Color(0x12, 0x34, 0x56, 0x78) assert c.r() == 0x12 assert c.g() == 0x34 assert c.b() == 0x56 assert c.a() == 0x78
def test_str(): assert str(color.Color(0x12, 0x34, 0x56)) == "#123456" assert str(color.Color(0x12, 0x34, 0x56, 0x78)) == "rgba(18,52,86,0.471)"
def test_repr(): assert repr(color.red) == "Color(0xff, 0x00, 0x00)" assert repr(color.Color(0x12, 0x34, 0x56, 0x78)) == "Color(0x12, 0x34, 0x56, 0x78)"
def test_fromrgb(): assert color.Color.fromrgb(0.2, 0.4, 0.6, 0.8) == color.Color(0x33, 0x66, 0x99, 0xcc)
def test_constructor(): assert color.Color(10, 20, 30) == (10, 20, 30, 255) assert color.Color(40, 50, 60, 70) == (40, 50, 60, 70)