def testStackLengthOne(self): "Calling on a stack with only one item must result in a StackError" c = Calculator() c.execute('4') error = r"^Cannot run 'cmd' \(stack has only 1 item\)$" self.assertRaisesRegex(StackError, error, c.findStringAndArgs, 'cmd', strToModifiers('r'), None)
def testStackLengthThree(self): "Calling on a stack with three items (count=2) must return correctly" c = Calculator() c.execute('5') c.execute('4') c.execute("'string'") string, args = c.findStringAndArgs('cmd', strToModifiers('r'), 2) self.assertEqual('string', string) self.assertEqual([5, 4], args)
def testStackLengthTwoWithCount(self): """Calling on a stack with two items and a count must return correctly.""" c = Calculator() c.execute('4') c.execute("'string'") string, args = c.findStringAndArgs('cmd', strToModifiers('r'), 1) self.assertEqual('string', string) self.assertEqual([4], args)
def testStackLengthTwoWithCount(self): """Calling on a stack with two items and a count must return correctly.""" c = Calculator() c.execute('4') c.execute('log10 :!') func, args = c.findCallableAndArgs('cmd', strToModifiers('r'), 1) self.assertIs(math.log10, func) self.assertEqual([4], args)
def testStackLengthThree(self): "Calling on a stack with three items (count=2) must return correctly" c = Calculator() c.execute('5') c.execute('4') c.execute('log10 :!') func, args = c.findCallableAndArgs('cmd', strToModifiers('r'), 2) self.assertIs(math.log10, func) self.assertEqual([5, 4], args)
def testStackLengthTwoNoCount(self): """Calling on a stack with two items and no count must return correctly. The number of stack items is obtained from the function signature""" c = Calculator() c.execute('4') c.execute("'string'") string, args = c.findStringAndArgs('cmd', strToModifiers('r'), None) self.assertEqual('string', string) self.assertEqual([4], args)
def testAllStack(self): "Calling on all the stack must return correctly" c = Calculator() c.execute('4') c.execute('5') c.execute('6') c.execute('log10 :!') func, args = c.findCallableAndArgs('cmd', strToModifiers('*r'), None) self.assertIs(math.log10, func) self.assertEqual([4, 5, 6], args)
def testAllStack(self): "Calling on all the stack must return correctly" c = Calculator() c.execute('4') c.execute('5') c.execute('6') c.execute("'string'") string, args = c.findStringAndArgs('cmd', strToModifiers('*r'), None) self.assertEqual('string', string) self.assertEqual([4, 5, 6], args)
def testStackLengthTwoNoCount(self): """Calling on a stack with two items and no count must return correctly. The number of stack items is obtained from the function signature""" c = Calculator() c.execute('4') c.execute('log10 :!') func, args = c.findCallableAndArgs('cmd', strToModifiers('r'), None) self.assertIs(math.log10, func) self.assertEqual([4], args)
def findModifiers(line): """Find the modifiers (if any) in an input line. @param line: A C{str} input line. @return: A 3-C{tuple} with the C{int} offset of the modifier separator in C{line} or -1 if no modifier is found, a C{Modifiers} instance, and an C{int} count (or C{None} if no count is found). """ index = line.rfind(_MODIFIERS_SEPARATOR) if index == -1: return -1, Modifiers(), None line = line[index + 1:] # Look for a number and extract it if present. match = _NUMBER_RE.search(line) if match: count = int(match.group(1)) line = line[:match.start(1)] + line[match.end(1):] else: count = None # Examine all the other characters. # # We only consider this to be a valid set of modifiers if the letters # are all in MODIFIERS. Be slightly lenient and allow repeated letters. seen = set() for letter in line: if letter in MODIFIERS: seen.add(letter) elif not letter.isspace(): break else: return index, strToModifiers(''.join(seen)), count # Even though we found the modifier separator, there are unknown # characters that follow it, so we conclude that this is not actually # an attempt to give modifier. E.g., in {'a': 27, 'b': 77} there is # a colon but it's followed by a '}' so we reject. return -1, Modifiers(), None
def testPrint(self): "Test the strToModifiers function sets print." modifiers = strToModifiers('p') self.assertTrue(modifiers.print)
def testPresevereStack(self): "Test the strToModifiers function sets preserveStack." modifiers = strToModifiers('=') self.assertTrue(modifiers.preserveStack)
def testNoSplit(self): "Test the strToModifiers function sets noSplit." modifiers = strToModifiers('n') self.assertTrue(modifiers.noSplit)
def testForceCommand(self): "Test the strToModifiers function sets forceCommand." modifiers = strToModifiers('c') self.assertTrue(modifiers.forceCommand)
def testIterate(self): "Test the strToModifiers function sets iterate." modifiers = strToModifiers('i') self.assertTrue(modifiers.iterate)
def testSplit(self): "Test the strToModifiers function sets split." modifiers = strToModifiers('s') self.assertTrue(modifiers.split)
def testCommandWithModifiersThenCount(self): "Test a command with modifiers then a count" self.assertEqual((('hey', strToModifiers('=p'), 101),), tuple(findCommands('hey :=p101')))
def testTwoCommandsSecondWithModifiersAfterSpace(self): "Test two commands on one line, the 2nd with modifiers after a space" self.assertEqual( (('hey', Modifiers(), None), ('you', strToModifiers('='), None)), tuple(findCommands('hey you :=')))
def testThreeCommandsSecondWithModifiers(self): "Test three commands on one line, the 2nd with modifiers" self.assertEqual( (('hey', Modifiers(), None), ('you', strToModifiers('='), None), ('there', Modifiers(), None)), tuple(findCommands('hey you:= there')))
def testEmptyStack(self): "Calling on an empty stack must return None, None" c = Calculator() error = r"^Cannot run 'cmd' \(stack has only 0 items\)$" self.assertRaisesRegex(StackError, error, c.findStringAndArgs, 'cmd', strToModifiers('r'), None)
def testTwoCommandsFirstWithModifiers(self): "Test two commands on one line, the 1st with modifiers (and no space)" self.assertEqual( (('hey', strToModifiers('='), None), ('you', Modifiers(), None)), tuple(findCommands('hey:= you')))
def testDictWithModifiers(self): "A dict with modifiers must be processed correctly." self.assertEqual((24, strToModifiers('=p'), 17), findModifiers("{'a':6, 'b':10, 'c':15} :=p17"))
def testCommandWithModifiersSurroundingCountNoSplit(self): "Test a command with modifiers before and after a count" self.assertEqual( (('hey', strToModifiers('=p*'), 16),), tuple(findCommands('hey :=p 16 *', splitLines=False)))
def testCommandWithCountThenSpaceAndModifiers(self): "Test a command with a count then a space then modifiers with space" self.assertEqual( (('hey', strToModifiers('=p'), 101),), tuple(findCommands('hey :101 =p', splitLines=False)))
def testPush(self): "Test the strToModifiers function sets push." modifiers = strToModifiers('!') self.assertTrue(modifiers.push)
def testAll(self): "Test the strToModifiers function sets all." modifiers = strToModifiers('*') self.assertTrue(modifiers.all)
def testReverse(self): "Test the strToModifiers function sets reverse." modifiers = strToModifiers('r') self.assertTrue(modifiers.reverse)
def testDebug(self): "Test the strToModifiers function sets debug." modifiers = strToModifiers('D') self.assertTrue(modifiers.debug)
def testDuplicatedLetter(self): "A string with a duplicated modifier must be processed correctly." self.assertEqual((4, strToModifiers('=p'), None), findModifiers("abs :=pp"))
def testCommandWithCountThenModifiers(self): "Test a command with a count then modifiers" self.assertEqual((('hey', strToModifiers('=p'), 101),), tuple(findCommands('hey :101=p')))