How to use t_whitespace method in avocado

Best Python code snippet using avocado_python

__init__.py

Source:__init__.py Github

copy

Full Screen

1from muzak.drivers.errors import MQLSyntaxError2from muzak.drivers.parser.tokens import T_EOF, TOKENS, C_List, C_Target, MQLParserToken, T_And, T_Char, T_CloseCurl, T_Equal, T_Int, T_OpenCurl, T_QueryTerminator, T_Whitespace, T_CloseBrac, T_CloseParen, T_Comma, T_Null, T_OpenBrac, T_OpenParen, T_Wildcard, W_Command, W_Condition, W_Generic, W_Int, token_type, word_type3class QueryLexer:4 def __init__(self, query_str: str):5 self._raw = query_str6 self.pos = 07 self.query_str = query_str8 self.strlen = len(query_str)9 def peek(self):10 # Peek at the next character in the input or return EOF if it doesn't exist11 if len(self.query_str) > 0:12 return self.query_str[0]13 else:14 return T_EOF15 def peek_type(self):16 # Peek at the type of the next character in the input, or return EOF if it doesn't exist17 if len(self.query_str) > 0:18 return token_type(self.query_str[0])19 else:20 return T_EOF21 def next(self):22 # Remove and return the next character from the input23 n = self.query_str[0]24 self.pos += 125 self.query_str = self.query_str[1:]26 return n27 def parse_word(self):28 word_chars = []29 # Continue only while the next character is of the T_Char type 30 while self.peek_type() == T_Char:31 # Get the next character in the input and append it to the list of characters in this word32 c = self.next()33 word_chars.append(c)34 if self.peek() == " ":35 self.next()36 # Join all word characters together to form word37 return "".join(word_chars)38 def parse_int(self):39 i_chars = []40 # Continue only while the next character is of the T_Int type41 while self.peek_type() == T_Int:42 # Get the next character in the input and validate that it is also numeric43 # as far as python is concerned before appending to the list of characters 44 # in this int, raise an error otherwise45 c = self.next()46 if not c.isnumeric():47 raise MQLSyntaxError("Expected type 'int' got type '%s' near: %s" % (repr(type(c)), self.query_str))48 i_chars.append(c)49 # Join int characters and cast to int python type before returning50 return int("".join(i_chars))51 def parse_kv_pair(self):52 # Remove leading whitespace if any53 if self.peek_type() == T_Whitespace:54 self.next()55 key_tokens = []56 # Continue until we hit an equal token57 while self.peek_type() != T_Equal:58 # If the next character is not one of T_Int, T_Char, or T_Whitespace throw a SyntaxError since it is not valid for a key59 if self.peek_type() not in (T_Int, T_Char, T_Whitespace):60 raise MQLSyntaxError("Syntax error while parsing list: Expected one of %s, but got '%s' near: %s" % (", ".join(["'%s'" % x.__name__ for x in (T_Int, T_Char, T_Whitespace)]), self.peek(), self.query_str))61 # Append the next character to the key_tokens list since we have made it past any possible exceptions62 key_tokens.append(self.next())63 # This should technically always be true, but better safe than sorry64 if self.peek_type() == T_Equal:65 # Remove leading equal since we don't care about it66 self.next()67 # Join our resulting tokens into the key68 key = "".join(key_tokens)69 value_tokens = []70 # Continue until we hit a closing curly brace or comma, indicating another item71 while self.peek_type() not in (T_Comma, T_CloseCurl):72 # If the next character is not one of T_Int, T_Char, or T_Whitespace throw a SyntaxError since it is not valid for a key73 if self.peek_type() not in (T_Int, T_Char, T_Whitespace):74 raise MQLSyntaxError("Syntax error while parsing list: Expected one of %s, but got '%s' near: %s" % (", ".join(["'%s'" % x.__name__ for x in (T_Int, T_Char, T_Whitespace)]), self.peek(), self.query_str))75 # Append the next character to the value_tokens list since we have made it past any possible exceptions76 value_tokens.append(self.next())77 # Join our resulting tokens into the value78 value = "".join(value_tokens)79 # Remove leading comma if it exists80 if self.peek_type() == T_Comma:81 self.next()82 # Return our key, value pair83 return key, value84 def parse_list_item(self):85 # Remove leading whitespace if any86 if self.peek_type() == T_Whitespace:87 self.next()88 word_chars = []89 while self.peek_type() not in (T_CloseBrac, T_CloseParen, T_Comma, T_Null):90 word_chars.append(self.next())91 if self.peek_type() == T_Comma:92 self.next()93 return "".join(word_chars)94 def parse_list(self, list_type: MQLParserToken):95 # Lists can be defined with either parenthesis or square brackets,96 # so to account for this we match the beginning list token with97 # the proper end token. The default expected type is parenthesis98 close_paren = T_CloseParen99 if list_type == T_OpenBrac:100 close_paren = T_CloseBrac101 items = []102 # Again, this should always be true, but better to be safe than sorry103 if self.peek_type() != list_type:104 raise MQLSyntaxError("Syntax Error while parsing list: expected: '%s' but got '%s' near: %s" % (list_type.__name__, self.peek(), self.query_str))105 else:106 self.next()107 # Continue until we hit a closing token108 while self.peek_type() != close_paren:109 if self.peek_type() not in (T_Char, T_Whitespace):110 raise MQLSyntaxError("Syntax error while parsing list: Expected one of %s, but got '%s' near: %s" % (", ".join(["'%s'" % x.__name__ for x in (T_Char, T_Whitespace)]), self.peek(), self.query_str))111 items.append(self.parse_list_item())112 # Remove extra whitespace and closing tokens.113 while self.peek_type() in (T_Whitespace, close_paren):114 self.next()115 return items116 def parse_target(self):117 eager = True118 definition = {}119 # If the target begins with the & symbol, this target is strict instead of eager120 if self.peek_type() == T_And:121 eager = False122 # Remove token after setting eager to false123 self.next()124 # Ensure the target starts with an opening curly brace125 if self.peek_type() != T_OpenCurl:126 raise MQLSyntaxError("Expected 'T_OpenCurl', but got '%s' near: %s" % (self.peek(), self.query_str))127 if self.peek_type() == T_OpenCurl:128 self.next()129 # Continue until closing curly brace130 while self.peek_type() != T_CloseCurl:131 # If the next character is not one of T_Char or T_Whitespace throw a SyntaxError since it is not valid for a key132 if self.peek_type() not in (T_Char, T_Whitespace):133 raise MQLSyntaxError("Syntax error while parsing list: Expected one of %s, but got '%s' near: %s" % (", ".join(["'%s'" % x.__name__ for x in (T_Char, T_Whitespace)]), self.peek(), self.query_str))134 # Get Key, value pair from query string135 key, value = self.parse_kv_pair()136 # Recognize a value of "\Null" as python's NoneType137 if value == "\\Null":138 value = None139 # Add key, value pair target definition140 if eager:141 # If Eager we use lists since the key could be any of the given values142 if key in definition:143 definition[key].append(value)144 else:145 definition[key] = [value]146 else:147 # When strict we use a string value since 148 # the key needs to be equal to the value strictly149 definition[key] = value150 # Remove any closing curly braces151 if self.peek_type() == T_CloseCurl:152 self.next()153 # Return our target definition and the eager value 154 return definition, eager155 def run(self):156 ast = []157 while self.peek_type() != T_EOF:158 # Ignore whitespace159 if self.peek_type() == T_Whitespace:160 self.next()161 # Parse a word if we detect a regular character162 elif self.peek_type() == T_Char:163 position = self.pos164 w = self.parse_word()165 ast.append({"type": word_type(w), "data": w, "position": position})166 # Parse a number if we detect an int character167 elif self.peek_type() == T_Int:168 position = self.pos169 i = self.parse_int()170 ast.append({"type": W_Int, "data": i, "position": position})171 # Parse a list if we detect an opening token for lists172 elif self.peek_type() in (T_OpenBrac, T_OpenParen):173 position = self.pos174 c = self.parse_list(self.peek_type())175 ast.append({"type": C_List, "data": c, "position": position})176 # Parse a target if we detect an & token or the curly brace token177 elif self.peek_type() in (T_And, T_OpenCurl):178 position = self.pos179 d = self.parse_target()180 ast.append({"type": C_Target, "data": d, "position": position})181 # If we see a query terminator, remove it from the input and add to AST, indicating to the parser that this query is over. 182 elif self.peek_type() == T_QueryTerminator:183 position = self.pos184 self.next()185 ast.append({"type": T_QueryTerminator, "data": None, "position": position})186 # If we see a wildcard, remove it from the input and add it to the AST187 elif self.peek_type() == T_Wildcard:188 position = self.pos189 self.next()190 ast.append({"type": T_Wildcard, "data": "*", "position": position})191 # Raise an error for an unexpected token192 else:193 raise MQLSyntaxError("Unexpected token: '%s' near: %s" % (self.peek(), self.query_str))194 return ast195# This parser essentially pulls the data from the AST that we need to interperet in order to run the query. see muzak.drivers.MuzakQL 196class QueryParser:197 def __init__(self, query_str: str):198 self.query_str = "%s " % query_str199 lexer = QueryLexer(query_str)200 self.ast = lexer.run()201 def peek(self):202 # Peek at the next value without removing and returning it from the AST203 if len(self.query_str) > 0:204 return self.ast[0]205 else:206 return {"type": T_EOF}207 208 def peek_type(self):209 # Peek at the next value without removing and returning it from the AST210 if len(self.query_str) > 0:211 return self.ast[0]["type"]212 else:213 return {"type": T_EOF}214 def peek_pos(self):215 # peek at the next item's position in the input.216 if len(self.query_str) > 0:217 return self.ast[0]["position"]218 else:219 return 0220 def next(self):221 # Remove and return the next value from the AST, or return an EOF token if the AST is empty222 if len(self.ast) > 0:223 n = self.ast.pop(0)224 return n225 else:226 return {"type": T_EOF}227 def expect(self, expect_type):228 # Expect that the next value in the AST is a certain type, or raise an error if it is not.229 i = self.next()230 position = i.get("position", 0)231 if i["type"] == expect_type:232 return i["data"]233 else:234 raise MQLSyntaxError("Expected type: '%s' but got '%s' near: %s" % (expect_type.__name__, i["type"].__name__, self.query_str[position:]))235 def parse_query(self):236 # Start by expecting at least a command from the query237 command = self.expect(W_Command)238 command = command.lower()239 # If the command is "show" we can expect one more word and return240 if command == "show":241 labels = [self.expect(W_Generic)]242 return command, labels, {}, 0, False243 if command == "select":244 # If we arrive here, we should expect a list of labels to query, or a wildcard to return all labels245 if self.peek_type() == C_List:246 labels = self.expect(C_List)247 if len(labels) == 0:248 labels = None249 elif self.peek_type() == T_Wildcard:250 self.expect(T_Wildcard)251 labels = None252 else:253 raise MQLSyntaxError("Expected C_List or T_Wildcard but got '%s' near %s" % (self.peek_type().__name__, self.query_str[self.peek_pos():]))254 if command == "update":255 # If we arrive here, we should expect a target, since we are updating and require a key->value pairing.256 labels = self.expect(C_Target)257 # Set default values in case there are no restriction conditions on this query258 target = {}259 eager = True260 limit = 0261 # If we have more information in our AST, continue on by expecting a condition262 if len(self.ast) > 0 and self.peek_type() != T_QueryTerminator:263 condition = self.expect(W_Condition)264 # If the condition is a "where" condition, we will expect a target 265 # which will contain the target data, and the eager value of this target266 if condition.lower() == "where":267 target, eager = self.expect(C_Target)268 # we will also expect another condition word after our target if the AST is not empty269 if len(self.ast) > 0 and self.peek_type() != T_QueryTerminator:270 condition = self.expect(W_Condition)271 # If our condition is a "limit" condition, we will expect an integer word.272 if condition.lower() == "limit":273 limit = self.expect(W_Int)274 # Return all of these values to whatever program will interpret them, in this case just another python function.275 return command, labels, target, limit, eager276 def parse_all(self):277 queries = []278 # While there are still items in the AST, continue279 while len(self.ast) > 0:280 # If the next item is the query terminator we can remove it281 if self.peek_type() == T_QueryTerminator:282 self.next()283 else:284 # Parse the next query, append to final list, and continue285 q = self.parse_query()286 queries.append(q)...

Full Screen

Full Screen

test.py

Source:test.py Github

copy

Full Screen

1# coding: utf-82"""Tests PhpArrayConverter functions.3"""4import sys5import sublime6import textwrap7from unittest import TestCase8class TestPhpArrayConverterConvertCommand(TestCase):9 """Tests the converter command.10 """11 def setUp(self):12 self.view = sublime.active_window().new_file()13 syntax = self.view.settings().set("syntax", "PHP.sublime-syntax")14 def tearDown(self):15 if self.view:16 self.view.set_scratch(True)17 self.view.window().focus_view(self.view)18 self.view.window().run_command("close_file")19 def test_simple_array(self):20 text = '<?php $a = array();'21 expected = '<?php $a = [];'22 self.setText(text)23 self.view.run_command('php_array_converter_convert')24 self.assertEqual(self.getText(), expected)25 def setText(self, string):26 self.view.run_command("insert", {"characters": string})27 def getText(self):28 content = self.view.substr(sublime.Region(0, self.view.size()))29 return content30PhpArrayConverter = sys.modules["PhpArrayConverter.PhpArrayConverter"]31class TestPhpTokenizer(TestCase):32 """Tests the PhpTokenizer.33 """34 def setUp(self):35 settings = {}36 self.tokenizer = PhpArrayConverter.PhpTokenizer(settings)37 def test_simple_array_tokenization(self):38 text = '<?php $a = array(); '39 expected_beginning = '{"tokens":[["T_OPEN_TAG","<?php ",1]'40 self.tokenizer.run(text)41 result = self.tokenizer.output42 self.assertTrue(result.startswith(expected_beginning))43class TestConvertedCoderGenerator(TestCase):44 """Tests the ConvertedCoderGenerator.45 """46 def setUp(self):47 self.code_generator = PhpArrayConverter.ConvertedCoderGenerator()48 def test_run(self):49 tokenizer_stdout = """{"tokens":[["T_OPEN_TAG","<?php\\n",1],["T_WHITESPACE","\\n",2],[312,"$a",3],["T_WHITESPACE"," ",3],"=",["T_WHITESPACE"," ",3],["T_ARRAY","array",3],"(",["T_WHITESPACE","\\n ",3],[318,"'foo'",4],["T_WHITESPACE"," ",4],[364,"=>",4],["T_WHITESPACE"," ",4],["T_ARRAY","array",4],"(",[295,"(string)",4],["T_WHITESPACE"," ",4],[308,"5",4],",",["T_WHITESPACE"," ",4],[297,"(int)",4],["T_WHITESPACE"," ",4],[309,"10.5",4],")",",",["T_WHITESPACE","\\n ",4],[318,"'bar'",5],["T_WHITESPACE"," ",5],[364,"=>",5],["T_WHITESPACE"," ",5],[308,"300",5],",",["T_WHITESPACE","\\n",5],")",";",["T_WHITESPACE","\\n",6]]}"""50 expected = """<?php\n\n$a = [\n 'foo' => [(string) 5, (int) 10.5],\n 'bar' => 300,\n];\n"""51 self.code_generator.run(tokenizer_stdout)52 actual = self.code_generator.output...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful