def test_equality(self): h1 = 'h1' h2 = 'h2' hc1 = HandleConstraint(h1, HandleConstraint.QEQ, h2) assert hc1 == HandleConstraint(h1, HandleConstraint.QEQ, h2) assert hc1 != HandleConstraint(h2, HandleConstraint.QEQ, h1) assert hc1 != HandleConstraint(h1, HandleConstraint.LHEQ, h2)
def read_hcons(tokens, variables=None): # HCONS:< HANDLE (qeq|lheq|outscopes) HANDLE ... > """Read and return an HCONS list.""" if tokens[0] != _hcons: return None tokens.popleft() # pop "HCONS" if variables is None: variables = {} hcons = [] validate_tokens(tokens, [_colon, _left_angle]) while tokens[0] != _right_angle: hi = read_variable(tokens, sort='h', variables=variables)[0] # rels are case-insensitive and the convention is lower-case rel = tokens.popleft().lower() if rel == _qeq: rel = HandleConstraint.QEQ elif rel == _lheq: rel = HandleConstraint.LHEQ elif rel == _outscopes: rel = HandleConstraint.OUTSCOPES else: invalid_token_error(rel, '('+'|'.join(_valid_hcons)+')') lo = read_variable(tokens, sort='h', variables=variables)[0] hcons.append(HandleConstraint(hi, rel, lo)) tokens.popleft() # we know this is a right angle return hcons
def _decode_hcons(elem): # <!ELEMENT hcons (hi, lo)> # <!ATTLIST hcons # hreln (qeq|lheq|outscopes) #REQUIRED > # <!ELEMENT hi (var)> # <!ELEMENT lo (label|var)> lo = elem.find('lo/') return HandleConstraint( _decode_var(elem.find('hi/var')), elem.get('hreln'), _decode_var(lo) if lo.tag == 'var' else _decode_label(lo))
def test_construct(self): h1 = 'handle1' h2 = 'handle2' with pytest.raises(TypeError): HandleConstraint() with pytest.raises(TypeError): HandleConstraint(h1) with pytest.raises(TypeError): HandleConstraint(h1, HandleConstraint.QEQ) hc = HandleConstraint(h1, HandleConstraint.QEQ, h2) assert hc.hi == h1 assert hc.relation == HandleConstraint.QEQ assert hc.lo == h2 hc = HandleConstraint(h1, HandleConstraint.LHEQ, h2) assert hc.relation == HandleConstraint.LHEQ hc = HandleConstraint('h1', HandleConstraint.QEQ, 'h2') assert hc.hi == 'h1' assert hc.lo == 'h2' repr(hc) # no error
def test_hashable(self): hc1 = HandleConstraint('h1', HandleConstraint.QEQ, 'h2') hc2 = HandleConstraint('h3', HandleConstraint.QEQ, 'h4') d = {hc1:1, hc2:2} assert d[hc1] == 1 assert d[hc2] == 2
def test_qeq(self): # qeq classmethod is just a convenience hc = HandleConstraint.qeq('h0', 'h1') assert hc.hi == 'h0' assert hc.relation == HandleConstraint.QEQ assert hc.lo == 'h1'