def resolve(self, argument: Word): """ Resolve an argument, meaning expand variables, escaped characters... :param argument: The argument to resolve :return: The resolved argument """ resolver = RunnerResolver(self) argument.apply(resolver) return resolver.result
def eat_once(self): if self.token == TOKEN.END: return self.token while self.pos < len(self.line) and is_whitespace(self.line[self.pos]): self.pos += 1 if self.pos == len(self.line): if self.token.type == TOKEN.SEPARATOR or self.token.type == TOKEN.NONE: self.pos = 0 self.line = self.file.readline() if self.line == '': self.token = Token.end() return self.token if self.line[-1] == '\n': self.line = self.line[:-1] return self.eat_once() else: self.token = Token.separator() return self.token if self.line[self.pos] == '#': self.pos = len(self.line) return self.eat_once() elif self.line[self.pos] == ';': self.pos += 1 self.token = Token.separator() return self.token else: start = self.pos self.pos, word = Word.parse(self.line, start) self.token = Token.word(word) return self.token
from io import StringIO from typing import List import pytest from helpers.parsing.lexer import Lexer, Token from helpers.parsing.lexer_error import LexerError from helpers.parsing.word import Word, Raw, Variable, Quoted @pytest.mark.parametrize(['input', 'expected'], [ pytest.param(';', [Token.separator()], id="semicolon"), pytest.param('a', [Token.word(Word([Raw('a')])), Token.separator()], id="one letter"), pytest.param('test', [Token.word(Word([Raw('test')])), Token.separator()], id="several letters"), pytest.param('test123_:=&@', [Token.word(Word([Raw('test123_:=&@')])), Token.separator()], id="many regular characters"), pytest.param('first_word second_word', [ Token.word(Word([Raw('first_word')])), Token.word(Word([Raw('second_word')])), Token.separator() ], id="two words"), pytest.param('first_word second_word', [ Token.word(Word([Raw('first_word')])), Token.word(Word([Raw('second_word')])),
def test_word_unsuccessful(input: str, error: str): with pytest.raises(LexerError, match=error): Word.parse(input, 0)
def test_word_successful_stop(input: str, expected: Word, stop: int): i, result = Word.parse(input, 0) assert i == stop assert result == expected
def test_word_escaped(raw: str, escaped: str): i, result = Word.parse(f'\\{raw}', 0) assert i == 2 assert result == Word([Raw(escaped)])
def test_word_successful(input: str, expected: Word): i, result = Word.parse(input, 0) assert i == len(input) assert result == expected
import pytest from helpers.parsing.lexer_error import LexerError from helpers.parsing.word import Word, Raw, Quoted, Variable @pytest.mark.parametrize(['input', 'expected'], [ pytest.param('test', Word([Raw('test')]), id="simple"), pytest.param('"test"', Word([Quoted([Raw('test')])]), id="simple quotes"), pytest.param('${test}', Word([Variable('test')]), id="simple variable"), pytest.param('"${test}"', Word([Quoted([Variable('test')])]), id="simple quoted variable"), pytest.param('first"second"', Word([Raw('first'), Quoted([Raw('second')])]), id="mixed not quoted and quoted"), pytest.param('"first"second', Word([Quoted([Raw('first')]), Raw('second')]), id="mixed quoted and not quoted"), pytest.param('first${second}', Word([Raw('first'), Variable('second')]), id="mixed not quoted and variable"), pytest.param('${first}second', Word([Variable('first'), Raw('second')]), id="mixed variable and not quoted"), ]) def test_word_successful(input: str, expected: Word): i, result = Word.parse(input, 0) assert i == len(input) assert result == expected