def test_02(self): """TestListIsEmpty_Special: listIsEmpty flag is maintained [02].""" # INC == 1\n myListGen = ListGen.ListAsGenerator([' ', '==', ' ', '1']) myObj = ListGen.ListAsGenerator([ '#', 'if', 'INC', ], next(myListGen)) myGen = next(myObj) #print resultS = [] while not myObj.listIsEmpty: myVal = next(myGen) #print 'aVal: %8s %s' % ('"%s"' % myVal, myObj.listIsEmpty) resultS.append((myVal, myObj.listIsEmpty)) #print resultS expResultS = [ ('#', False), ('if', False), ('INC', True), ] self.assertEqual(resultS, expResultS) #print '----' for aVal in myGen: #print 'aVal: %8s %s' % ('"%s"' % aVal, myObj.listIsEmpty) resultS.append((aVal, myObj.listIsEmpty)) #print resultS expResultS = [('#', False), ('if', False), ('INC', True), (' ', True), ('==', True), (' ', True), ('1', True)] self.assertEqual(resultS, expResultS)
def testContinuationSimpleInit(self): """ListAsGenerator: simple list with list comprehension and continuation.""" myCont = ListGen.ListAsGenerator(list(range(4, 8))) myObj = ListGen.ListAsGenerator(list(range(4)), next(myCont)) myGen = next(myObj) myResult = [x for x in myGen] self.assertRaises(StopIteration, next, myGen) self.assertEqual(myResult, list(range(8)))
def testContinuation(self): """ListAsGenerator: test of using a extra generator on a list of ints.""" myList = list(range(5)) myLetterList = [chr(x + ord('A')) for x in range(6)] myContGen = next(ListGen.ListAsGenerator(myLetterList)) myG = next(ListGen.ListAsGenerator(myList, myContGen)) self.assertEqual([0, 1, 2, 3, 4, 'A', 'B', 'C', 'D', 'E', 'F'], [x for x in myG]) self.assertRaises(StopIteration, next, myG)
def testListIsEmpty_00(self): """ListAsGenerator: listIsEmpty flag is maintained [00].""" myCont = ListGen.ListAsGenerator(list('ABCDEFG')) myObj = ListGen.ListAsGenerator(list(range(4)), next(myCont)) myGen = next(myObj) myResult = [] #print for aVal in myGen: #print '%s %s' % (myObj.listIsEmpty, aVal) myResult.append(aVal) self.assertRaises(StopIteration, next, myGen) self.assertEqual(myResult, list(range(4)) + list('ABCDEFG'))
def testListIsEmpty_03(self): """ListAsGenerator: listIsEmpty flag is maintained [03].""" myCont = ListGen.ListAsGenerator(list('ABCDEFG')) myObj = ListGen.ListAsGenerator(list(range(4)), next(myCont)) myGen = next(myObj) myResult = [x for x in myGen if myObj.listIsEmpty] #print #print myResult self.assertRaises(StopIteration, next, myGen) self.assertEqual(myResult, [ 3, ] + list('ABCDEFG'))
def testListIsEmpty_01(self): """ListAsGenerator: listIsEmpty flag is maintained [01].""" myCont = ListGen.ListAsGenerator(list('ABCDEFG')) myObj = ListGen.ListAsGenerator(list(range(4)), next(myCont)) myGen = next(myObj) myResult = [] if not myObj.listIsEmpty: for aVal in myGen: myResult.append(aVal) if myObj.listIsEmpty: break myRemainderResult = [x for x in myGen] self.assertRaises(StopIteration, next, myGen) self.assertEqual(myResult, list(range(4)))
def testListIsEmpty_02(self): """ListAsGenerator: listIsEmpty flag is maintained [02].""" myCont = ListGen.ListAsGenerator(list('ABCDEFG')) myObj = ListGen.ListAsGenerator(list(range(4)), next(myCont)) myGen = next(myObj) #print #print [x for x in myGen if myObj.listIsEmpty] #myResult = [x for x in myGen if myObj.listIsEmpty] if not myObj.listIsEmpty: for _aVal in myGen: if myObj.listIsEmpty: break myResult = [x for x in myGen] self.assertRaises(StopIteration, next, myGen) self.assertEqual(myResult, list('ABCDEFG'))
def testContinuationSendOnAlternateInGen(self): """ListAsGenerator: Continuation with send on alternate yields in continuation part only.""" myCont = ListGen.ListAsGenerator(list(range(4, 8))) myObj = ListGen.ListAsGenerator(list(range(4)), next(myCont)) myGen = next(myObj) myResult = [] for aVal in myGen: if aVal % 2 == 1 and myObj.listIsEmpty: # Odd numbers myGen.send(aVal) myResult.append(next(myGen)) else: myResult.append(aVal) self.assertRaises(StopIteration, next, myGen) self.assertEqual(myResult, list(range(8)))
def test_00(self): """TestListIsEmpty_Special: listIsEmpty flag is maintained [00].""" # INC == 1\n myGen = ListGen.ListAsGenerator([' ', '==', ' ', '1']) myObj = ListGen.ListAsGenerator([ 'INC', ], next(myGen)) resultS = [] #print for aVal in next(myObj): #print 'aVal: %8s %s' % ('"%s"' % aVal, myObj.listIsEmpty) resultS.append((aVal, myObj.listIsEmpty)) #print resultS expResultS = [('INC', True), (' ', True), ('==', True), (' ', True), ('1', True)] self.assertEqual(resultS, expResultS)
def testIncGenUngetOnEmptyList(self): """ListAsGenerator: inc. gen. and send() where the initialy empty.""" myObj = ListGen.ListAsGenerator([]) myGen = next(myObj) # Try an insert self.assertRaises(TypeError, myGen.send, 127) self.assertRaises(StopIteration, next, myGen)
def testIncGen(self): """ListAsGenerator: with incremental generation (inc. gen.).""" myObj = ListGen.ListAsGenerator(list(range(2))) myGen = next(myObj) # Iterate myVal = next(myGen) self.assertEqual(myVal, 0) myVal = next(myGen) self.assertEqual(myVal, 1) self.assertRaises(StopIteration, next, myGen)
def testIncGenUngetAtStart(self): """ListAsGenerator: inc. gen. and single send() before next().""" myObj = ListGen.ListAsGenerator(list(range(2))) myGen = next(myObj) # Try sending before iterating self.assertRaises(TypeError, myGen.send, 42) myVal = next(myGen) self.assertEqual(myVal, 0) myVal = next(myGen) self.assertEqual(myVal, 1) self.assertRaises(StopIteration, next, myGen)
def testPassingGenerator(self): """ListAsGenerator: Passing a generator around yielding list of ints.""" myList = list(range(6)) myG = next(ListGen.ListAsGenerator(myList)) result = [] try: while 1: result.append(self._takeGenAndConsume(myG)) except StopIteration: pass self.assertRaises(StopIteration, next, myG) self.assertEqual(myList, result)
def testIncGenUngetAtEnd(self): """ListAsGenerator: inc. gen. and single send() after last next().""" myObj = ListGen.ListAsGenerator(list(range(2))) myGen = next(myObj) # Iterate myVal = next(myGen) self.assertEqual(myVal, 0) myVal = next(myGen) self.assertEqual(myVal, 1) myGen.send(42) myVal = next(myGen) self.assertEqual(myVal, 42) self.assertRaises(StopIteration, next, myGen)
def testIncGenUngetMultipleCallsAtStart(self): """ListAsGenerator: inc. gen. where pairs of send() cancel each other.""" myObj = ListGen.ListAsGenerator(list(range(2))) myGen = next(myObj) # Iterate myVal = next(myGen) self.assertEqual(myVal, 0) self.assertEqual(None, myGen.send(42)) self.assertEqual(42, myGen.send(84)) # 84 is thrown away by the external loop of UnitGen.next() myVal = next(myGen) self.assertEqual(myVal, 1) self.assertRaises(StopIteration, next, myGen)
def testTokenGenerator(self): """ListAsGenerator: list of PpToken(token, token_type).""" myList = [ PpToken.PpToken('#', 'preprocessing-op-or-punc'), PpToken.PpToken('define', 'identifier'), PpToken.PpToken(' ', 'whitespace'), PpToken.PpToken('BAR', 'identifier'), PpToken.PpToken('\n', 'whitespace'), ] myG = next(ListGen.ListAsGenerator(myList)) self.assertEqual(PpToken.PpToken('#', 'preprocessing-op-or-punc'), next(myG)) self.assertEqual(PpToken.PpToken('define', 'identifier'), next(myG)) self.assertEqual(PpToken.PpToken(' ', 'whitespace'), next(myG)) self.assertEqual(PpToken.PpToken('BAR', 'identifier'), next(myG)) self.assertEqual(PpToken.PpToken('\n', 'whitespace'), next(myG)) self.assertRaises(StopIteration, next, myG)
def testSimple(self): """ListAsGenerator: simple test of a list of integers.""" myList = list(range(5)) myG = next(ListGen.ListAsGenerator(myList)) self.assertEqual(myList, [x for x in myG]) self.assertRaises(StopIteration, next, myG)