File: segments_common_test.py

package info (click to toggle)
sqlfluff 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,000 kB
  • sloc: python: 106,131; sql: 34,188; makefile: 52; sh: 8
file content (34 lines) | stat: -rw-r--r-- 1,153 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""Test the KeywordSegment class."""

from sqlfluff.core.parser import KeywordSegment, StringParser
from sqlfluff.core.parser.context import ParseContext
from sqlfluff.core.parser.match_result import MatchResult


def test__parser__core_keyword(raw_segments):
    """Test the Mystical KeywordSegment."""
    # First make a keyword
    FooKeyword = StringParser("foobar", KeywordSegment, type="bar")
    # Check it looks as expected
    assert FooKeyword.template.upper() == "FOOBAR"
    ctx = ParseContext(dialect=None)
    # Match it against a list and check it doesn't match
    assert not FooKeyword.match(raw_segments, 1, parse_context=ctx)
    # Match it against the final element (returns tuple)
    m = FooKeyword.match(raw_segments, 0, parse_context=ctx)
    assert m
    assert m == MatchResult(
        matched_slice=slice(0, 1),
        matched_class=KeywordSegment,
        segment_kwargs={"instance_types": ("bar",)},
    )
    segments = m.apply(raw_segments)
    assert len(segments) == 1
    segment = segments[0]
    assert segment.class_types == {
        "base",
        "word",
        "keyword",
        "raw",
        "bar",
    }