Best Cucumber Common Library code snippet using AstNode.getTokenMatch
GherkinDocumentBuilder.php
Source:GherkinDocumentBuilder.php
...116 private function getTableRows(AstNode $node): array117 {118 $rows = array_map(119 fn ($token) => new TableRow($this->getLocation($token, 0), $this->getCells($token), $this->idGenerator->newId()),120 $node->getTokenMatches(TokenType::TableRow),121 );122 $this->ensureCellCount($rows);123 return $rows;124 }125 /** @param list<TableRow> $rows */126 private function ensureCellCount(array $rows): void127 {128 if (!count($rows)) {129 return;130 }131 $cellCount = count($rows[0]->cells);132 foreach ($rows as $row) {133 if (count($row->cells) !== $cellCount) {134 $location = new Location($row->location->line, $row->location->column ?? 0);135 throw new AstBuilderException('inconsistent cell count within the table', $location);136 }137 }138 }139 /**140 * @return list<TableCell>141 */142 private function getCells(TokenMatch $token): array143 {144 return array_map(145 fn ($cellItem) => new TableCell($this->getLocation($token, $cellItem->column), $cellItem->text),146 $token->items,147 );148 }149 /**150 * @return list<Tag>151 */152 private function getTags(AstNode $node): array153 {154 $tagsNode = $node->getSingle(AstNode::class, RuleType::Tags, new AstNode(RuleType::None));155 $tokens = $tagsNode->getTokenMatches(TokenType::TagLine);156 $tags = [];157 foreach ($tokens as $token) {158 foreach ($token->items as $tagItem) {159 $tags[] = new Tag(160 location: $this->getLocation($token, $tagItem->column),161 name: $tagItem->text,162 id: $this->idGenerator->newId(),163 );164 }165 }166 return $tags;167 }168 /**169 * @param array<TokenMatch> $lineTokens170 */171 private function joinMatchedTextWithLinebreaks(array $lineTokens): string172 {173 return join("\n", array_map(fn ($t) => $t->text, $lineTokens));174 }175 private function transformStepNode(AstNode $node): Step176 {177 $stepLine = $node->getTokenMatch(TokenType::StepLine);178 return new Step(179 location: $this->getLocation($stepLine, 0),180 keyword: $stepLine->keyword,181 keywordType: $stepLine->keywordType,182 text: $stepLine->text,183 docString: $node->getSingle(DocString::class, RuleType::DocString),184 dataTable: $node->getSingle(DataTable::class, RuleType::DataTable),185 id: $this->idGenerator->newId(),186 );187 }188 private function transformDocStringNode(AstNode $node): DocString189 {190 $separatorToken = $node->getTokenMatches(TokenType::DocStringSeparator)[0];191 $mediaType = $separatorToken->text;192 $lineTokens = $node->getTokenMatches(TokenType::Other);193 $content = $this->joinMatchedTextWithLinebreaks($lineTokens);194 return new DocString(195 location: $this->getLocation($separatorToken, 0),196 mediaType: $mediaType ?: null, // special case turns '' into null197 content: $content,198 delimiter: $separatorToken->keyword,199 );200 }201 private function transformScenarioDefinitionNode(AstNode $node): ?Scenario202 {203 $scenarioNode = $node->getSingle(AstNode::class, RuleType::Scenario);204 if (null === $scenarioNode) {205 return null;206 }207 $scenarioLine = $scenarioNode->getTokenMatch(TokenType::ScenarioLine);208 return new Scenario(209 location: $this->getLocation($scenarioLine, 0),210 tags: $this->getTags($node),211 keyword: $scenarioLine->keyword,212 name: $scenarioLine->text,213 description: $this->getDescription($scenarioNode),214 steps: $this->getSteps($scenarioNode),215 examples: $scenarioNode->getItems(Examples::class, RuleType::ExamplesDefinition),216 id: $this->idGenerator->newId(),217 );218 }219 private function transformExamplesDefinitionNode(AstNode $node): ?Examples220 {221 $examplesNode = $node->getSingle(AstNode::class, RuleType::Examples);222 if (null === $examplesNode) {223 return null;224 }225 $examplesLine = $examplesNode->getTokenMatch(TokenType::ExamplesLine);226 /** @var list<TableRow>|null $rows */227 $rows = $examplesNode->getSingleUntyped(RuleType::ExamplesTable);228 $tableHeader = is_array($rows) && count($rows) ? $rows[0] : null;229 $tableBody = (is_array($rows) && count($rows) > 0) ? array_slice($rows, 1) : [];230 return new Examples(231 location: $this->getLocation($examplesLine, 0),232 tags: $this->getTags($node),233 keyword: $examplesLine->keyword,234 name: $examplesLine->text,235 description: $this->getDescription($examplesNode),236 tableHeader: $tableHeader,237 tableBody: $tableBody,238 id: $this->idGenerator->newId(),239 );240 }241 private function transformDataTableNode(AstNode $node): DataTable242 {243 $rows = $this->getTableRows($node);244 return new DataTable($rows[0]->location, $rows);245 }246 /** @return list<TableRow> */247 private function transformExamplesTableNode(AstNode $node): array248 {249 return $this->getTableRows($node);250 }251 private function transformBackgroundNode(AstNode $node): Background252 {253 $backgroundLine = $node->getTokenMatch(TokenType::BackgroundLine);254 return new Background(255 location: $this->getLocation($backgroundLine, 0),256 keyword: $backgroundLine->keyword,257 name: $backgroundLine->text,258 description: $this->getDescription($node),259 steps: $this->getSteps($node),260 id: $this->idGenerator->newId(),261 );262 }263 private function transformDescriptionNode(AstNode $node): string264 {265 $lineTokens = $node->getTokenMatches(TokenType::Other);266 $lineText = preg_replace(267 '/(\\n\\s*)*$/u',268 '',269 $this->joinMatchedTextWithLinebreaks($lineTokens),270 );271 return $lineText;272 }273 private function transformFeatureNode(AstNode $node): ?Feature274 {275 $header = $node->getSingle(AstNode::class, RuleType::FeatureHeader, new AstNode(RuleType::FeatureHeader));276 if (!$header instanceof AstNode) {277 return null;278 }279 $tags = $this->getTags($header);280 $featureLine = $header->getTokenMatch(TokenType::FeatureLine);281 $children = [];282 $background = $node->getSingle(Background::class, RuleType::Background);283 if ($background instanceof Background) {284 $children[] = new FeatureChild(background: $background);285 }286 foreach ($node->getItems(Scenario::class, RuleType::ScenarioDefinition) as $scenario) {287 $children[] = new FeatureChild(scenario: $scenario);288 }289 foreach ($node->getItems(Rule::class, RuleType::Rule) as $rule) {290 $children[] = new FeatureChild($rule, null, null);291 }292 $language = $featureLine->gherkinDialect->getLanguage();293 return new Feature(294 location: $this->getLocation($featureLine, 0),295 tags: $tags,296 language: $language,297 keyword: $featureLine->keyword,298 name: $featureLine->text,299 description: $this->getDescription($header),300 children: $children,301 );302 }303 private function transformRuleNode(AstNode $node): Rule304 {305 $header = $node->getSingle(AstNode::class, RuleType::RuleHeader, new AstNode(RuleType::RuleHeader));306 $ruleLine = $header->getTokenMatch(TokenType::RuleLine);307 $children = [];308 $tags = $this->getTags($header);309 $background = $node->getSingle(Background::class, RuleType::Background);310 if ($background) {311 $children[] = new RuleChild(background: $background);312 }313 $scenarios = $node->getItems(Scenario::class, RuleType::ScenarioDefinition);314 foreach ($scenarios as $scenario) {315 $children[] = new RuleChild(scenario: $scenario);316 }317 return new Rule(318 location: $this->getLocation($ruleLine, 0),319 tags: $tags,320 keyword: $ruleLine->keyword,...
AstNodeTest.php
Source:AstNodeTest.php
...41 self::assertSame($obj1, $item);42 }43 public function testItGetsNoTokensWhenNoneAreAdded(): void44 {45 $tokens = $this->astNode->getTokenMatches(TokenType::Empty);46 self::assertSame([], $tokens);47 }48 public function testItGetsTokensWhenTheyAreAddedByRuletype(): void49 {50 $this->astNode->add(RuleType::_Empty, $token1 = $this->getTokenMatch());51 $this->astNode->add(RuleType::_Empty, $token2 = $this->getTokenMatch());52 $tokens = $this->astNode->getTokenMatches(TokenType::Empty);53 self::assertSame([$token1, $token2], $tokens);54 }55 public function testItThrowsWhenGettingATokenThatIsNotAdded(): void56 {57 $this->expectException(\LogicException::class);58 $this->astNode->getTokenMatch(TokenType::Empty);59 }60 public function testItGetsTheFirstTokenWhenSomeAreAdded(): void61 {62 $this->astNode->add(RuleType::_Empty, $token1 = $this->getTokenMatch());63 $this->astNode->add(RuleType::_Empty, $this->getTokenMatch());64 $token = $this->astNode->getTokenMatch(TokenType::Empty);65 self::assertSame($token1, $token);66 }67 private function getTokenMatch(): TokenMatch68 {69 return new TokenMatch(TokenType::Other, (new GherkinDialectProvider())->getDefaultDialect(), 100, 'keyword', KeywordType::UNKNOWN, 'text', [], new Location(1, 1));70 }71}...
getTokenMatch
Using AI Code Generation
1$ast = new AstNode();2$tokens = $ast->getTokenMatch("2.php", "T_STRING");3print_r($tokens);4$ast = new AstNode();5$functions = $ast->getFunctionMatch("3.php", "T_STRING");6print_r($functions);7$ast = new AstNode();8$functions = $ast->getFunctionMatch("4.php", "T_STRING");9print_r($functions);10$ast = new AstNode();11$functions = $ast->getFunctionMatch("5.php", "T_STRING");12print_r($functions);13$ast = new AstNode();14$functions = $ast->getFunctionMatch("6.php", "T_STRING");15print_r($functions);16$ast = new AstNode();17$functions = $ast->getFunctionMatch("7.php", "T_STRING");18print_r($functions);19$ast = new AstNode();20$functions = $ast->getFunctionMatch("8.php", "T_STRING");21print_r($functions);22$ast = new AstNode();23$functions = $ast->getFunctionMatch("9.php", "T_STRING");24print_r($functions);25$ast = new AstNode();26$functions = $ast->getFunctionMatch("10.php", "T_STRING");27print_r($functions);28$ast = new AstNode();29$functions = $ast->getFunctionMatch("11.php", "T_STRING");30print_r($functions);31$ast = new AstNode();
getTokenMatch
Using AI Code Generation
1require_once 'php-ast-0.1.6/lib/bootstrap.php';2if (true) {3 echo "hello";4}5EOC;6$ast = ast\parse_code($code, 30);7$node = new ast\Node($ast);8$node = $node->children[0]->children[0]->children[0]->children[0]->children[0]->children[0];9print_r($node->getTokenMatch(ast\AST_IF, ast\AST_IF_ELEM, ast\AST_STMT_LIST));
getTokenMatch
Using AI Code Generation
1require_once 'PHPParser.php';2$parser = new PHPParser();3$ast = $parser->parseFile('1.php');4$ast->dump();5$ast->getTokenMatch('T_STRING', 'foo');6$ast->getTokenMatch('T_STRING', 'foo1');7$ast->getTokenMatch('T_STRING', 'foo2');8$ast->getTokenMatch('T_STRING', 'foo3');9$ast->getTokenMatch('T_STRING', 'foo4');10$ast->getTokenMatch('T_STRING', 'foo5');11$ast->getTokenMatch('T_STRING', 'foo6');12$ast->getTokenMatch('T_STRING', 'foo7');
getTokenMatch
Using AI Code Generation
1$root = new AstNode();2$root->parse($code);3$token = $root->getTokenMatch('T_VARIABLE', '$_GET');4print_r($token);5 (6 (7 (
getTokenMatch
Using AI Code Generation
1include("astnode.php");2$ast = new AstNode();3$ast->getTokenMatch();4Line 3: include("astnode.php");5Line 4: $ast = new AstNode();6Line 5: $ast->getTokenMatch();
getTokenMatch
Using AI Code Generation
1$root = $ast->getRoot();2$node = $root->getChildOfType(0, ast\AST_FUNC_DECL);3$tokenMatch = $node->getTokenMatch();4print_r($tokenMatch);5 (6 (7 (8 (9 (10 (11 (12 (13 (14 (15 (16 (17 (18 (19 (20 (
getTokenMatch
Using AI Code Generation
1include 'ast.php';2$ast = new AstNode();3$ast->getTokenMatch('T_VARIABLE');4include 'ast.php';5$ast = new AstNode();6$ast->getTokens('T_VARIABLE');7include 'ast.php';8$ast = new AstNode();9$ast->getTokens('T_VARIABLE');10include 'ast.php';11$ast = new AstNode();12$ast->getTokens('T_VARIABLE');13include 'ast.php';14$ast = new AstNode();15$ast->getTokens('T_VARIABLE');16include 'ast.php';17$ast = new AstNode();18$ast->getTokens('T_VARIABLE');
getTokenMatch
Using AI Code Generation
1require_once("phpanalysis.class.php");2$pa = new PhpAnalysis();3$pa->setSource("<?php4if (\$a == \$b) {5\$a++;6}7?>");8$pa->parse();9$ast = $pa->getAst();10$token = $ast->getToken();11while ($token != null) {12if ($token->getToken() == T_IF) {13$match = $token->getTokenMatch();14echo substr($pa->getSource(), $token->getTokenPos(), $match->getTokenPos() - $token->getTokenPos());15}16$token = $token->getNextToken();17}18if ($a == $b) {19$a++;20}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with getTokenMatch on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!