def testAssertRaises(): # test correct error with assertRaises(NotImplementedError) as e: raise NotImplementedError() assert e.exceptionType == NotImplementedError, (e.type, e.e) # test correct error with assertRaises(NotImplementedError) as e: raise NotImplementedError assert e.exceptionType == NotImplementedError, (e.type, e.e) # test no error try: with assertRaises(NotImplementedError) as e: pass except AssertionError: assert e.exceptionType == None, (e.type, e.e) except Exception as e: assert False, e # test wrong error class Fred(Exception): pass try: with assertRaises(NotImplementedError) as e: raise Fred except AssertionError: assert e.exceptionType == Fred, (e.exceptionType, e.e) except Exception as e: assert False, e
def testNullary(): str(nullary1(1)) >> check >> equal >> 'nullary1(1)' str(nullary2(1, _)) >> check >> equal >> 'nullary2(1, TBC{})' str(nullary2(1, _)(2)) >> check >> equal >> 'nullary2(1, 2)' with assertRaises(SyntaxError) as e: nullary1(_)(1, 2) e.exceptionValue.args[ 0] >> check >> equal >> 'nullary1 - too many args - got 2 needed 1' with assertRaises(SyntaxError) as e: 2 >> nullary2(1, _) e.exceptionValue.args[0] >> check >> equal >> 'syntax not of form nullary()'
def testHolder(): fred = Holder(pystr + int) assert fred._st == pystr + int with assertRaises(ProgrammerError): assert fred._tv == Missing with assertRaises(TypeError): fred._tv = 'joe' with assertRaises(AttributeError): fred._st = str fred._tv = ('hello' >> box | pystr) assert fred._tv._t == pystr fred._tv = 1 assert fred._tv == 1 assert isinstance(fred._tv, int) assert isinstance(fred._tv, int + pystr)
def test_hasT(): assert isT(T) assert isT(T1) matrix = BTAtom.ensure('matrix2') inout = BTAtom.ensure('inout') out = BTAtom.ensure('out') assert matrix[inout, T1].hasT ccy = BType('ccy') fx = BType('fx') GBP = ccy['GBP'] USD = ccy['USD'] assert fx[S(domestic=GBP, foreign=T1)].hasT N = BType('N') assert (T**N).hasT assert ((T * num) ^ num).hasT assert ((num * num) ^ T).hasT assert S(name=T, age=num).hasT assert (T**num).hasT assert (num**T).hasT assert (num**T).hasT with assertRaises(TypeError): 1 | T
def testUnary(): str(unary1(1)) >> check >> equal >> 'unary1(1)' str(unary2(1, _)) >> check >> equal >> 'unary2(1, TBC{})' str(2 >> unary2(1, _)) >> check >> equal >> 'unary2(1, 2)' with assertRaises(SyntaxError) as e: unary1(_)(1, 2) e.exceptionValue.args[ 0] >> check >> equal >> 'unary1 - too many args - got 2 needed 1' with assertRaises(SyntaxError) as e: 2 >> unary3(1, _, _) e.exceptionValue.args[ 0] >> check >> equal >> 'unary3 needs 2 args but 1 will be piped' str(nullary1(1) >> unary1) >> check >> equal >> 'unary1(nullary1(1))'
def testExclusive(): with assertRaises(TypeError) as ex: lol & pylist (pystr & square) >> check >> fitsWithin >> pystr tv(matrix & tdd, [[]]) >> cov >> check >> equal >> 'a:matrix' tv(matrix & lol & tdd, [[]]) >> cov >> check >> equal >> 'a:matrix' tv(matrix & tvarray & tdd, [[]]) >> cov >> check >> equal >> 'a:matrix'
def test_scope(): _ = Scope() with assertRaises(AttributeError): _.fred _.fred = [] with assertRaises(IndexError): _.fred[0] _.joe = {} with assertRaises(KeyError): _.joe['a'] class Sally(object): pass _.sally = Sally() with assertRaises(AttributeError): _.sally.a
def testRau(): str(rau1(1)) >> check >> equal >> 'rau1(1)' str(rau2(1, _)) >> check >> equal >> 'rau2(1, TBC{})' str(rau2(1, _) >> 2) >> check >> equal >> 'rau2(1, 2)' with assertRaises(SyntaxError) as e: rau1(_)(1, 2) e.exceptionValue.args[ 0] >> check >> equal >> 'rau1 - too many args - got 2 needed 1' with assertRaises(SyntaxError) as e: rau3(1, _, _) >> 2 e.exceptionValue.args[ 0] >> check >> equal >> 'needs 2 args but 1 will be piped' str(rau1 >> 1) >> check >> equal >> 'rau1(1)' str(rau1 >> nullary1(1)) >> check >> equal >> 'rau1(nullary1(1))' with assertRaises(TypeError) as e: rau1 >> unary1 with assertRaises(TypeError) as e: rau1 >> binary2 with assertRaises(TypeError) as e: rau1 >> ternary3
def testOrthogonal(): ( pystr & ISIN ) >> check >> doesNotFitWithin >> pystr # because ISIN is left in the residual (pystr & ISIN) >> check >> doesNotFitWithin >> ( pystr & CUSIP) # conflict between ISIN and CUSIP (pystr & ISIN) >> check >> fitsWithin >> (pystr & ISIN) (pystr & ISIN & square) >> check >> fitsWithin >> (pystr & ISIN) (pystr & ISIN) >> check >> fitsWithin >> (pystr & T1) (pystr & ISIN) >> check >> doesNotFitWithin >> (pystr & ISIN & T) (pystr)('DE') >> _join >> ( pystr)('0008402215') >> check >> equal >> join_psps (CUSIP & pystr)('DE') >> _join >> ( CUSIP & pystr)('0008402215') >> check >> equal >> join_pstpst (ISIN & pystr)('DE') >> _join >> ( ISIN & pystr)('0008402215') >> check >> equal >> join_ipsips with assertRaises(TypeError) as ex: (ISIN & pystr)('DE') >> _join >> ('0008402215')
def test_updatingAddOne(): # now we want to use it with strings @coppertop def addOne(x: pystr) -> pystr: return x + 'One' # and floats @coppertop def addOne(x: pyfloat) -> pyfloat: return x + 1.0 # check our new implementation 1 >> addOne >> check >> equal >> 2 'Two' >> addOne >> check >> equal >> 'TwoOne' 1.0 >> addOne >> check >> equal >> 2.0 # but with assertRaises(Exception) as ex: a = ['Two'] >> eachAddOne >> check >> equal >> ['TwoOne'] # which is to be expected since the above are local (defined in a function) b = ['One'] >> eachAddTwo >> check >> equal >> ['OneTwo']
def test(): A = [[1, 2], [3, 5]] >> to(_, matrix[tvarray]) b = [1, 2] >> to(_, matrix[tvarray]) A @ b >> shape >> check >> equal >> (2, 1) with assertRaises(Exception) as e: b @ A
def test_cow(): _ = Scope() "\n_.pad = map(a=map({1:'a1'})" >> PP _.pad = {'a': {1: 'a1'}} f"_.pad => {_.pad}" >> PP f" _.pad -> {numRefs(_.pad)}" >> PP f" _.pad -> {numRefs(_.pad)}" >> PP f" _.pad[\'a\'] -> {numRefs(_.pad['a'])}" >> PP f" _.pad[\'a\'][1] -> {numRefs(_.pad['a'][1])}" >> PP "\n_.fred = _.pad['a']" >> PP _.fred = _.pad['a'] f"_.pad => {_.pad}" >> PP f"_.fred => {_.fred}" >> PP f" _.pad -> {numRefs(_.pad)}" >> PP f" _.pad[\'a\'] -> {numRefs(_.pad['a'])}" >> PP f" _.pad[\'a\'][1] -> {numRefs(_.pad['a'][1])}" >> PP f" _.fred -> {numRefs(_.fred)}" >> PP "\n_.pad[\'b\'] = {1:'b1'}" >> PP _.pad['b'] = {1: 'b1'} f"_.pad => {_.pad}" >> PP f"_.fred => {_.fred}" >> PP f" _.pad -> {numRefs(_.pad)}" >> PP f" _.pad[\'a\'] -> {numRefs(_.pad['a'])}" >> PP f" _.pad[\'a\'][1] -> {numRefs(_.pad['a'][1])}" >> PP f" _.pad[\'b\'] -> {numRefs(_.pad['b'])}" >> PP f" _.fred -> {numRefs(_.fred)}" >> PP "\n_.fred = 'fred'" >> PP _.fred = 'fred' f"_.pad => {_.pad}" >> PP f"_.fred => {_.fred}" >> PP f" _.pad -> {numRefs(_.pad)}" >> PP f" _.pad[\'a\'] -> {numRefs(_.pad['a'])}" >> PP f" _.pad[\'a\'][1] -> {numRefs(_.pad['a'][1])}" >> PP f" _.pad[\'b\'] -> {numRefs(_.pad['b'])}" >> PP f" _.fred -> {numRefs(_.fred)}" >> PP "\n_.fred = _.pad['a']" >> PP # fred = _.pad['a'] _.fred = _.pad['a'] f"_.pad => {_.pad}" >> PP f"_.fred => {_.fred}" >> PP f" _.pad -> {numRefs(_.pad)}" >> PP f" _.pad[\'a\'] -> {numRefs(_.pad['a'])}" >> PP f" _.pad[\'a\'][1] -> {numRefs(_.pad['a'][1])}" >> PP f" _.pad[\'b\'] -> {numRefs(_.pad['b'])}" >> PP f" _.fred -> {numRefs(_.fred)}" >> PP "\n_.pad['a'][2] = 'a2'" >> PP _.pad['a'][2] = 'a2' f"_.pad => {_.pad}" >> PP f"_.fred => {_.fred}" >> PP f" _.pad -> {numRefs(_.pad)}" >> PP f" _.pad[\'a\'] -> {numRefs(_.pad['a'])}" >> PP f" _.pad[\'a\'][1] -> {numRefs(_.pad['a'][1])}" >> PP f" _.pad[\'b\'] -> {numRefs(_.pad['b'])}" >> PP f" _.fred -> {numRefs(_.fred)}" >> PP "\ndiamond" >> PP x = {'child': 1} _.diamond = dict(a=dict(x=x), b=dict(x=x)) x = None _.diamond >> PP f" _.diamond -> {numRefs(_.diamond)}" >> PP f" _.diamond['a'] -> {numRefs(_.diamond['a'])}" >> PP f" _.diamond['a']['x'] -> {numRefs(_.diamond['a']['x'])}" >> PP f" _.diamond['b'] -> {numRefs(_.diamond['b'])}" >> PP f" _.diamond['b']['x'] -> {numRefs(_.diamond['b']['x'])}" >> PP with assertRaises(ProgrammerError): # fix this "\n_.diamond['a']['x'].update(dict(child=2))" >> PP _.diamond['a']['x'].update(dict(child=2)) _.diamond >> PP f" _.diamond -> {numRefs(_.diamond)}" >> PP f" _.diamond['a'] -> {numRefs(_.diamond['a'])}" >> PP f" _.diamond['a']['x'] -> {numRefs(_.diamond['a']['x'])}" >> PP f" _.diamond['b'] -> {numRefs(_.diamond['b'])}" >> PP f" _.diamond['b']['x'] -> {numRefs(_.diamond['b']['x'])}" >> PP "" >> PP len(_.pad) >> check >> equal >> 2 _.pad.items() >> PP dir(_.pad) >> PP type(_.pad) >> PP with assertRaises(ProgrammerError): # fix this with context(halt=True): _.pad.update({'c': 3}) _.pad >> PP
def test_detect_parent_cycle(): _ = Scope() _.pad = {'a': {1: 'a1'}} with assertRaises(ValueError): _.pad['a'][3] = _.pad
def test_anon(): f = anon(pyint^pyint, lambda x: x + 1) fxs = tvseq((N ** pyint)[tvseq], [1, 2, 3]) >> each >> f fxs >> check >> typeOf >> (N ** pyint)[tvseq] with assertRaises(TypeError): fxs = tvseq((N ** pyint)[tvseq], [1, 2, 3]) >> each >> anon(pystr ^ pystr, lambda x: x + 1)