def testSplitKeepingQuotes_withMatchingPattern(self): self.assertListEqual( QuoteParser.split_keeping_quotes("aCaba'AAc'", self.LOWERCASE), ['', 'C', '', '', "'AAc'"]) self.assertListEqual( QuoteParser.split_keeping_quotes('aCaba"AAc"', self.LOWERCASE), ['', 'C', '', '', '"AAc"'])
def testFindUnquotedSymbols_matchesForSimpleSymbols(self): self.assertSetEqual( QuoteParser.find_unquoted_symbols("abaCaba'ABAc'", self.LOWERCASE), {0, 1, 2, 4, 5, 6}) self.assertSetEqual( QuoteParser.find_unquoted_symbols('abaCaba"ABAc"', self.LOWERCASE), {0, 1, 2, 4, 5, 6})
def testNextState_afterNonQuote(self): self.assertEqual(QuoteParser._next_state(_State.UNQUOTED, '`'), _State.UNQUOTED) self.assertEqual(QuoteParser._next_state(_State.IN_SINGLE, '`'), _State.IN_SINGLE) self.assertEqual(QuoteParser._next_state(_State.IN_DOUBLE, '`'), _State.IN_DOUBLE)
def testNextState_afterSingleQuote(self): self.assertEqual(QuoteParser._next_state(_State.UNQUOTED, "'"), _State.IN_SINGLE) self.assertEqual(QuoteParser._next_state(_State.IN_SINGLE, "'"), _State.UNQUOTED) self.assertEqual(QuoteParser._next_state(_State.IN_DOUBLE, "'"), _State.IN_DOUBLE)
def testFindExpandableVariables_withExpandableVariables(self): self.assertSetEqual(QuoteParser.find_expandable_variables('echo $5'), {5}) self.assertSetEqual(QuoteParser.find_expandable_variables('$variable'), {0}) self.assertSetEqual( QuoteParser.find_expandable_variables('echo "$5 $6" | $7'), {6, 9, 15})
def testSplitKeepingQuotes_noMatchingPattern(self): self.assertListEqual( QuoteParser.split_keeping_quotes('', self.UPPERCASE), ['']) self.assertListEqual( QuoteParser.split_keeping_quotes("aba'ABAcABA'", self.UPPERCASE), ["aba'ABAcABA'"]) self.assertListEqual( QuoteParser.split_keeping_quotes('aba"ABAcABA"', self.UPPERCASE), ['aba"ABAcABA"'])
def testFindUnquotedSymbols_matchingQuotes(self): self.assertSetEqual( QuoteParser.find_unquoted_symbols(r'''""''', self.QUOTES), {0, 1}) self.assertSetEqual( QuoteParser.find_unquoted_symbols(r'''"'"''"'"''', self.QUOTES), {0, 2, 3, 4, 5, 7}) self.assertSetEqual( QuoteParser.find_unquoted_symbols(r'''a"b"'c'd''', self.QUOTES), {1, 3, 4, 6})
def testFindUnquotedSymbols_noMatchesSimpleSymbols(self): self.assertSetEqual( QuoteParser.find_unquoted_symbols('', self.UPPERCASE), set()) self.assertSetEqual( QuoteParser.find_unquoted_symbols("abacaba'ABAcABA'", self.UPPERCASE), set()) self.assertSetEqual( QuoteParser.find_unquoted_symbols('abacaba"ABAcABA"', self.UPPERCASE), set())
def read(): """ Reads lines from the input. New lines continue to be queried while the quotes do not match, or while the input ends with a pipe. """ result = input('$ ') while not QuoteParser.quotes_match(result) or result.rstrip().endswith( '|'): result += os.linesep + input('> ') return result
def testRemoveQuotes_quotesInsideQuotes(self): self.assertEqual(QuoteParser.remove_quotes(r'''"''"'''), "''") self.assertEqual(QuoteParser.remove_quotes(r"""'""'"""), '""') self.assertEqual( QuoteParser.remove_quotes(r'''"'quote'"'"removal"'"'test'"'''), r"""'quote'"removal"'test'""")
def testRemoveQuotes_simpleQuotes(self): self.assertEqual(QuoteParser.remove_quotes('""'), '') self.assertEqual(QuoteParser.remove_quotes("''"), '') self.assertEqual( QuoteParser.remove_quotes(r'''"quote"'removal'"test"'''), 'quoteremovaltest')
def testRemoveQuotes_noQuotes(self): self.assertEqual(QuoteParser.remove_quotes(''), '') self.assertEqual( QuoteParser.remove_quotes('expression without quotes'), 'expression without quotes')
def testFindExpandableVariables_noExpandableVariables(self): self.assertSetEqual(QuoteParser.find_expandable_variables('echo 5'), set()) self.assertSetEqual(QuoteParser.find_expandable_variables(''), set()) self.assertSetEqual(QuoteParser.find_expandable_variables("echo '$5"), set())
def testQuotesMatch_trailingDoubleQuote(self): self.assertFalse(QuoteParser.quotes_match('hello "world')) self.assertFalse(QuoteParser.quotes_match('"'))
def testQuotesMatch_trailingSingleQuote(self): self.assertFalse(QuoteParser.quotes_match("hello 'world")) self.assertFalse(QuoteParser.quotes_match("'"))
def testQuotesMatch_quotesMatch(self): self.assertTrue(QuoteParser.quotes_match('')) self.assertTrue(QuoteParser.quotes_match(r''''hello' "world"'''))