How to use GherkinDialect class

Best Gherkin-php code snippet using .GherkinDialect

Run Gherkin-php automation tests on LambdaTest cloud grid

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

GherkinDialectProvider.php

Source: GherkinDialectProvider.php Github

copy
1<?php
2
3declare(strict_types=1);
4
5namespace Cucumber\Gherkin;
6
7use Cucumber\Gherkin\ParserException\NoSuchLanguageException;
8use JsonException;
9use RuntimeException;
10
11/**
12 * @psalm-import-type Dialect from GherkinDialect
13 */
14final class GherkinDialectProvider
15{
16    /**
17     * @var non-empty-array<non-empty-string, Dialect>
18     */
19    private readonly array $DIALECTS;
20
21    public const JSON_PATH = __DIR__ . '/../resources/gherkin-languages.json';
22
23    /** @param non-empty-string $defaultDialectName */
24    public function __construct(
25        private readonly string $defaultDialectName = 'en',
26    ) {
27        try {
28            /**
29             * Here we force the type checker to assume the decoded JSON has the correct
30             * structure, rather than validating it. This is safe because it's not dynamic
31             *
32             * @var non-empty-array<non-empty-string, Dialect> $data
33             */
34            $data = json_decode(file_get_contents(self::JSON_PATH), true, flags: JSON_THROW_ON_ERROR);
35            $this->DIALECTS = $data;
36        } catch (JsonException $e) {
37            throw new RuntimeException("Unable to parse " . self::JSON_PATH, previous: $e);
38        }
39    }
40
41    /**
42     * @return non-empty-list<non-empty-string>
43     */
44    public function getLanguages(): array
45    {
46        return array_keys($this->DIALECTS);
47    }
48
49    /**
50     * @param non-empty-string $language
51     */
52    public function getDialect(string $language, ?Location $location): GherkinDialect
53    {
54        if (!isset($this->DIALECTS[$language])) {
55            throw new NoSuchLanguageException($language, $location);
56        }
57
58        return new GherkinDialect($language, $this->DIALECTS[$language]);
59    }
60
61    public function getDefaultDialect(): GherkinDialect
62    {
63        return $this->getDialect($this->defaultDialectName, null);
64    }
65}
66
Full Screen

Token.php

Source: Token.php Github

copy
1<?php
2
3declare(strict_types=1);
4
5namespace Cucumber\Gherkin;
6
7use Cucumber\Gherkin\Parser\TokenType;
8
9final class Token
10{
11    private const EOF_VALUE = 'EOF';
12    public ?TokenMatch $match = null;
13
14    public function __construct(
15        public readonly ?GherkinLine $line,
16        public readonly Location $location,
17    ) {
18    }
19
20    /**
21     * @psalm-assert-if-true null $this->line
22     */
23    public function isEof(): bool
24    {
25        return null === $this->line;
26    }
27
28    public function getLocation(): Location
29    {
30        return $this->match ? $this->match->location : $this->location;
31    }
32
33    public function getTokenValue(): string
34    {
35        return $this->isEof() ? self::EOF_VALUE : $this->line->getLineText(-1);
36    }
37
38    /**
39     * @param list<GherkinLineSpan> $items
40     */
41    public function match(
42        TokenType $matchedType,
43        GherkinDialect $gherkinDialect,
44        int $indent,
45        string $keyword,
46        string $text,
47        array $items,
48    ): void {
49        $this->match = new TokenMatch(
50            $matchedType,
51            $gherkinDialect,
52            $indent,
53            $keyword,
54            $text,
55            $items,
56            new Location($this->location->line, $indent + 1),
57        );
58    }
59}
60
Full Screen

TokenMatch.php

Source: TokenMatch.php Github

copy
1<?php
2
3declare(strict_types=1);
4
5namespace Cucumber\Gherkin;
6
7use Cucumber\Gherkin\Parser\TokenType;
8
9final class TokenMatch
10{
11    /**
12     * @param list<GherkinLineSpan> $items
13     */
14    public function __construct(
15        public readonly TokenType $tokenType,
16        public readonly GherkinDialect $gherkinDialect,
17        public readonly int $indent,
18        public readonly string $keyword,
19        public readonly string $text,
20        public readonly array $items,
21        public readonly Location $location,
22    ) {
23    }
24}
25
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)