Best Cucumber Common Library code snippet using PickleTag
PickleCompiler.php
Source: PickleCompiler.php
...12use Cucumber\Messages\PickleStepArgument;13use Cucumber\Messages\PickleTable;14use Cucumber\Messages\PickleTableCell;15use Cucumber\Messages\PickleTableRow;16use Cucumber\Messages\PickleTag;17use Cucumber\Messages\Rule;18use Cucumber\Messages\Scenario;19use Cucumber\Messages\Step;20use Cucumber\Messages\TableCell;21use Cucumber\Messages\TableRow;22use Cucumber\Messages\Tag;23use Generator;24final class PickleCompiler25{26 /** @var array<string, PickleStep\Type|null> */27 private array $pickleStepTypeFromKeyword = [];28 private Step\KeywordType $lastKeywordType = Step\KeywordType::UNKNOWN;29 public function __construct(30 private readonly IdGenerator $idGenerator,31 ) {32 $this->pickleStepTypeFromKeyword[Step\KeywordType::UNKNOWN->name] = PickleStep\Type::UNKNOWN;33 $this->pickleStepTypeFromKeyword[Step\KeywordType::CONTEXT->name] = PickleStep\Type::CONTEXT;34 $this->pickleStepTypeFromKeyword[Step\KeywordType::ACTION->name] = PickleStep\Type::ACTION;35 $this->pickleStepTypeFromKeyword[Step\KeywordType::OUTCOME->name] = PickleStep\Type::OUTCOME;36 $this->pickleStepTypeFromKeyword[Step\KeywordType::CONJUNCTION->name] = null;37 }38 /**39 * @return Generator<Pickle>40 */41 public function compile(GherkinDocument $gherkinDocument, string $uri): Generator42 {43 if (!$gherkinDocument->feature) {44 return;45 }46 $feature = $gherkinDocument->feature;47 $language = $feature->language;48 yield from $this->compileFeature($feature, $language, $uri);49 }50 /**51 * @return Generator<Pickle>52 */53 private function compileFeature(Feature $feature, string $language, string $uri): Generator54 {55 $tags = $feature->tags;56 $featureBackgroundSteps = [];57 foreach ($feature->children as $featureChild) {58 if ($featureChild->background) {59 $featureBackgroundSteps = [...$featureBackgroundSteps, ...$featureChild->background->steps];60 } elseif ($featureChild->rule) {61 yield from $this->compileRule($featureChild->rule, $tags, $featureBackgroundSteps, $language, $uri);62 } elseif ($featureChild->scenario) {63 if (!$featureChild->scenario->examples) {64 yield from $this->compileScenario($featureChild->scenario, $tags, $featureBackgroundSteps, $language, $uri);65 } else {66 yield from $this->compileScenarioOutline($featureChild->scenario, $tags, $featureBackgroundSteps, $language, $uri);67 }68 }69 }70 }71 /**72 * @param list<Tag> $parentTags73 * @param list<Step> $featureBackgroundSteps74 *75 * @return Generator<Pickle>76 */77 private function compileRule(Rule $rule, array $parentTags, array $featureBackgroundSteps, string $language, string $uri): Generator78 {79 $ruleBackgroundSteps = $featureBackgroundSteps;80 $ruleTags = [...$parentTags, ...$rule->tags];81 foreach ($rule->children as $ruleChild) {82 if ($ruleChild->background) {83 $ruleBackgroundSteps = [...$ruleBackgroundSteps, ...$ruleChild->background->steps];84 } elseif ($ruleChild->scenario && $ruleChild->scenario->examples) {85 yield from $this->compileScenarioOutline($ruleChild->scenario, $ruleTags, $ruleBackgroundSteps, $language, $uri);86 } elseif ($ruleChild->scenario) {87 yield from $this->compileScenario($ruleChild->scenario, $ruleTags, $ruleBackgroundSteps, $language, $uri);88 }89 }90 }91 /**92 * @param list<Tag> $parentTags93 * @param list<Step> $backgroundSteps94 *95 * @return Generator<Pickle>96 */97 private function compileScenario(Scenario $scenario, array $parentTags, array $backgroundSteps, string $language, string $uri): Generator98 {99 $steps = [100 ...($scenario->steps ? $this->pickleSteps($backgroundSteps) : []),101 ...array_map(fn ($s) => $this->pickleStep($s), $scenario->steps),102 ];103 $tags = [...$parentTags, ...$scenario->tags];104 $this->lastKeywordType = Step\KeywordType::UNKNOWN;105 yield new Pickle(106 id: $this->idGenerator->newId(),107 uri: $uri,108 name: $scenario->name,109 language: $language,110 steps: $steps,111 tags: $this->pickleTags($tags),112 astNodeIds: [$scenario->id],113 );114 }115 /**116 * @param list<Tag> $featureTags117 * @param list<Step> $backgroundSteps118 *119 * @return Generator<Pickle>120 */121 private function compileScenarioOutline(Scenario $scenario, array $featureTags, array $backgroundSteps, string $language, string $uri): Generator122 {123 foreach ($scenario->examples as $examples) {124 if (!$examples->tableHeader) {125 continue;126 }127 $variableCells = $examples->tableHeader->cells;128 foreach ($examples->tableBody as $valuesRow) {129 $valueCells = $valuesRow->cells;130 $steps = [131 ...($scenario->steps ? $this->pickleSteps($backgroundSteps) : []),132 ...array_map(fn ($s) => $this->pickleStep($s, $variableCells, $valuesRow), $scenario->steps),133 ];134 $tags = [...$featureTags, ...$scenario->tags, ...$examples->tags];135 $sourceIds = [$scenario->id, $valuesRow->id];136 yield new Pickle(137 id: $this->idGenerator->newId(),138 uri: $uri,139 name: $this->interpolate($scenario->name, $variableCells, $valueCells),140 language: $language,141 steps: $steps,142 tags: $this->pickleTags($tags),143 astNodeIds: $sourceIds,144 );145 }146 }147 }148 /**149 * @param list<Step> $steps150 * @return list<PickleStep>151 */152 private function pickleSteps(array $steps): array153 {154 return array_map(fn ($s) => $this->pickleStep($s), $steps);155 }156 /**157 * @param list<TableCell> $variableCells158 */159 private function pickleStep(Step $step, array $variableCells = [], ?TableRow $valuesRow = null): PickleStep160 {161 $valueCells = $valuesRow?->cells ?? [];162 $stepText = $this->interpolate($step->text, $variableCells, $valueCells);163 $astNodeIds = $valuesRow ? [$step->id, $valuesRow->id] : [$step->id];164 if ($step->dataTable) {165 $argument = new PickleStepArgument(dataTable: $this->pickleDataTable($step->dataTable, $variableCells, $valueCells));166 } elseif ($step->docString) {167 $argument = new PickleStepArgument(docString: $this->pickleDocString($step->docString, $variableCells, $valueCells));168 } else {169 $argument = null;170 }171 $this->lastKeywordType =172 $step->keywordType === Step\KeywordType::CONJUNCTION173 ? $this->lastKeywordType174 : ($step->keywordType ?? Step\KeywordType::UNKNOWN)175 ;176 return new PickleStep(177 argument: $argument,178 astNodeIds: $astNodeIds,179 id: $this->idGenerator->newId(),180 type: $this->pickleStepTypeFromKeyword[$this->lastKeywordType->name],181 text: $stepText,182 );183 }184 /**185 * @param list<TableCell> $variableCells186 * @param list<TableCell> $valueCells187 */188 private function interpolate(string $name, array $variableCells, array $valueCells): string189 {190 $variables = array_map(fn ($c) => '<'.$c->value.'>', $variableCells);191 $values = array_map(fn ($c) => $c->value, $valueCells);192 $replacements = array_combine($variables, $values);193 return StringUtils::replace($name, $replacements);194 }195 /**196 * @param list<TableCell> $variableCells197 * @param list<TableCell> $valueCells198 */199 private function pickleDataTable(DataTable $dataTable, array $variableCells, array $valueCells): PickleTable200 {201 return new PickleTable(202 rows: array_map(203 fn ($r) => new PickleTableRow(204 cells: array_map(205 fn ($c) => new PickleTableCell(206 $this->interpolate($c->value, $variableCells, $valueCells),207 ),208 $r->cells,209 ),210 ),211 $dataTable->rows,212 ),213 );214 }215 /**216 * @param list<TableCell> $variableCells217 * @param list<TableCell> $valueCells218 */219 private function pickleDocString(DocString $docstring, array $variableCells, array $valueCells): PickleDocString220 {221 return new PickleDocString(222 mediaType: $docstring->mediaType ? $this->interpolate($docstring->mediaType, $variableCells, $valueCells) : null,223 content: $this->interpolate($docstring->content, $variableCells, $valueCells),224 );225 }226 /**227 * @param list<Tag> $tags228 * @return list<PickleTag>229 */230 private function pickleTags(array $tags): array231 {232 return array_map(fn ($t) => $this->pickleTag($t), $tags);233 }234 private function pickleTag(Tag $tag): PickleTag235 {236 return new PickleTag($tag->name, $tag->id);237 }238}...
PickleTag.php
Source: PickleTag.php
...6namespace Cucumber\Messages;7use JsonSerializable;8use Cucumber\Messages\DecodingException\SchemaViolationException;9/**10 * Represents the PickleTag message in Cucumber's message protocol11 * @see https://github.com/cucumber/common/tree/main/messages#readme12 *13 * A tag */14final class PickleTag implements JsonSerializable15{16 use JsonEncodingTrait;17 /**18 * Construct the PickleTag with all properties19 *20 */21 public function __construct(22 public readonly string $name = '',23 /**24 * Points to the AST node this was created from25 */26 public readonly string $astNodeId = '',27 ) {28 }29 /**30 * @throws SchemaViolationException31 *32 * @internal...
PickleTag
Using AI Code Generation
1require_once 'cucumber_common_lib.php';2use CucumberCommonLib\PickleTag;3{4 private $tag;5 public function __construct()6 {7 $this->tag = new PickleTag();8 }9 * @Given /^I have a tag named "([^"]*)"$/10 public function iHaveATagNamed($tagName)11 {12 $this->tag->setTag($tagName);13 }14 * @When /^I add a tag named "([^"]*)"$/15 public function iAddATagNamed($tagName)16 {17 $this->tag->addTag($tagName);18 }19 * @Then /^I should have a tag named "([^"]*)"$/20 public function iShouldHaveATagNamed($tagName)21 {22 assert($this->tag->getTag() == $tagName);23 }24}
Check out the latest blogs from LambdaTest on this topic:
QA testers have a unique role and responsibility to serve the customer. Serving the customer in software testing means protecting customers from application defects, failures, and perceived failures from missing or misunderstood requirements. Testing for known requirements based on documentation or discussion is the core of the testing profession. One unique way QA testers can both differentiate themselves and be innovative occurs when senseshaping is used to improve the application user experience.
When I started writing tests with Cypress, I was always going to use the user interface to interact and change the application’s state when running tests.
Ever since the Internet was invented, web developers have searched for the most efficient ways to display content on web browsers.
JUnit is one of the most popular unit testing frameworks in the Java ecosystem. The JUnit 5 version (also known as Jupiter) contains many exciting innovations, including support for new features in Java 8 and above. However, many developers still prefer to use the JUnit 4 framework since certain features like parallel execution with JUnit 5 are still in the experimental phase.
The web paradigm has changed considerably over the last few years. Web 2.0, a term coined way back in 1999, was one of the pivotal moments in the history of the Internet. UGC (User Generated Content), ease of use, and interoperability for the end-users were the key pillars of Web 2.0. Consumers who were only consuming content up till now started creating different forms of content (e.g., text, audio, video, etc.).
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.
Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.
Test now for FreeGet 100 minutes of automation test minutes FREE!!