コード例 #1
0
 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)
コード例 #2
0
 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)
コード例 #3
0
 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)
コード例 #4
0
 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)
コード例 #5
0
 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)
コード例 #6
0
 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)
コード例 #7
0
 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)
コード例 #8
0
 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)
コード例 #9
0
 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)
コード例 #10
0
ファイル: io.py プロジェクト: terrycojones/rpnpy
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
コード例 #11
0
 def testPrint(self):
     "Test the strToModifiers function sets print."
     modifiers = strToModifiers('p')
     self.assertTrue(modifiers.print)
コード例 #12
0
 def testPresevereStack(self):
     "Test the strToModifiers function sets preserveStack."
     modifiers = strToModifiers('=')
     self.assertTrue(modifiers.preserveStack)
コード例 #13
0
 def testNoSplit(self):
     "Test the strToModifiers function sets noSplit."
     modifiers = strToModifiers('n')
     self.assertTrue(modifiers.noSplit)
コード例 #14
0
 def testForceCommand(self):
     "Test the strToModifiers function sets forceCommand."
     modifiers = strToModifiers('c')
     self.assertTrue(modifiers.forceCommand)
コード例 #15
0
 def testIterate(self):
     "Test the strToModifiers function sets iterate."
     modifiers = strToModifiers('i')
     self.assertTrue(modifiers.iterate)
コード例 #16
0
 def testSplit(self):
     "Test the strToModifiers function sets split."
     modifiers = strToModifiers('s')
     self.assertTrue(modifiers.split)
コード例 #17
0
ファイル: test_io.py プロジェクト: terrycojones/rpnpy
 def testCommandWithModifiersThenCount(self):
     "Test a command with modifiers then a count"
     self.assertEqual((('hey', strToModifiers('=p'), 101),),
                      tuple(findCommands('hey :=p101')))
コード例 #18
0
ファイル: test_io.py プロジェクト: terrycojones/rpnpy
 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 :=')))
コード例 #19
0
ファイル: test_io.py プロジェクト: terrycojones/rpnpy
 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')))
コード例 #20
0
 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)
コード例 #21
0
ファイル: test_io.py プロジェクト: terrycojones/rpnpy
 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')))
コード例 #22
0
ファイル: test_io.py プロジェクト: terrycojones/rpnpy
 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"))
コード例 #23
0
ファイル: test_io.py プロジェクト: terrycojones/rpnpy
 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)))
コード例 #24
0
ファイル: test_io.py プロジェクト: terrycojones/rpnpy
 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)))
コード例 #25
0
 def testPush(self):
     "Test the strToModifiers function sets push."
     modifiers = strToModifiers('!')
     self.assertTrue(modifiers.push)
コード例 #26
0
 def testAll(self):
     "Test the strToModifiers function sets all."
     modifiers = strToModifiers('*')
     self.assertTrue(modifiers.all)
コード例 #27
0
 def testReverse(self):
     "Test the strToModifiers function sets reverse."
     modifiers = strToModifiers('r')
     self.assertTrue(modifiers.reverse)
コード例 #28
0
 def testDebug(self):
     "Test the strToModifiers function sets debug."
     modifiers = strToModifiers('D')
     self.assertTrue(modifiers.debug)
コード例 #29
0
ファイル: test_io.py プロジェクト: terrycojones/rpnpy
 def testDuplicatedLetter(self):
     "A string with a duplicated modifier must be processed correctly."
     self.assertEqual((4, strToModifiers('=p'), None),
                      findModifiers("abs :=pp"))
コード例 #30
0
ファイル: test_io.py プロジェクト: terrycojones/rpnpy
 def testCommandWithCountThenModifiers(self):
     "Test a command with a count then modifiers"
     self.assertEqual((('hey', strToModifiers('=p'), 101),),
                      tuple(findCommands('hey :101=p')))