| 1 |
""" |
| 2 |
Pygments basic API tests |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
:copyright: Copyright 2006-present by the Pygments team, see AUTHORS. |
| 6 |
:license: BSD, see LICENSE for details. |
| 7 |
""" |
| 8 |
|
| 9 |
from pathlib import Path |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from pygments.lexers import ( |
| 14 |
_fn_matches, |
| 15 |
find_lexer_class_by_name, |
| 16 |
get_lexer_by_name, |
| 17 |
guess_lexer, |
| 18 |
) |
| 19 |
from pygments.lexers.basic import CbmBasicV2Lexer |
| 20 |
from pygments.lexers.ecl import ECLLexer |
| 21 |
|
| 22 |
TESTDIR = Path(__file__).resolve().parent |
| 23 |
|
| 24 |
|
| 25 |
def get_input(lexer, filename): |
| 26 |
return Path(TESTDIR, 'examplefiles', lexer, filename).read_text(encoding='utf-8') |
| 27 |
|
| 28 |
|
| 29 |
@pytest.mark.skip(reason="This is identified as T-SQL") |
| 30 |
def test_guess_lexer_fsharp(): |
| 31 |
lx = guess_lexer(get_input('fsharp', 'Deflate.fs')) |
| 32 |
assert lx.__class__.__name__ == 'FSharpLexer' |
| 33 |
|
| 34 |
|
| 35 |
def test_guess_lexer_brainfuck(): |
| 36 |
lx = guess_lexer('>>[-]<<[->>+<<]') |
| 37 |
assert lx.__class__.__name__ == 'BrainfuckLexer' |
| 38 |
|
| 39 |
|
| 40 |
def test_guess_lexer_singularity(): |
| 41 |
lx = guess_lexer(get_input('singularity', 'Singularity')) |
| 42 |
assert lx.__class__.__name__ == 'SingularityLexer' |
| 43 |
|
| 44 |
|
| 45 |
@pytest.mark.skip(reason="This is identified as MIME") |
| 46 |
def test_guess_lexer_matlab(): |
| 47 |
lx = guess_lexer(r'A \ B') |
| 48 |
assert lx.__class__.__name__ == 'OctaveLexer' |
| 49 |
|
| 50 |
|
| 51 |
@pytest.mark.skip(reason="This is identified as Python") |
| 52 |
def test_guess_lexer_hybris(): |
| 53 |
lx = guess_lexer(get_input('hybris', 'hybris_File.hy')) |
| 54 |
assert lx.__class__.__name__ == 'HybrisLexer' |
| 55 |
|
| 56 |
|
| 57 |
def test_guess_lexer_forth(): |
| 58 |
lx = guess_lexer(get_input('forth', 'demo.frt')) |
| 59 |
assert lx.__class__.__name__ == 'ForthLexer' |
| 60 |
|
| 61 |
|
| 62 |
def test_guess_lexer_modula2(): |
| 63 |
lx = guess_lexer(get_input('modula2', 'modula2_test_cases.def')) |
| 64 |
assert lx.__class__.__name__ == 'Modula2Lexer' |
| 65 |
|
| 66 |
|
| 67 |
def test_guess_lexer_unicon(): |
| 68 |
lx = guess_lexer(get_input('unicon', 'example.icn')) |
| 69 |
assert lx.__class__.__name__ == 'UcodeLexer' |
| 70 |
|
| 71 |
|
| 72 |
def test_guess_lexer_ezhil(): |
| 73 |
lx = guess_lexer(get_input('ezhil', 'ezhil_primefactors.n')) |
| 74 |
assert lx.__class__.__name__ == 'EzhilLexer' |
| 75 |
|
| 76 |
|
| 77 |
def test_guess_lexer_gdscript(): |
| 78 |
lx = guess_lexer(get_input('gdscript', 'gdscript_example.gd')) |
| 79 |
assert lx.__class__.__name__ == 'GDScriptLexer' |
| 80 |
|
| 81 |
|
| 82 |
def test_guess_lexer_gap(): |
| 83 |
lx = guess_lexer(get_input('gap', 'example.gd')) |
| 84 |
assert lx.__class__.__name__ == 'GAPLexer' |
| 85 |
lx = guess_lexer(get_input('gap', 'example.gi')) |
| 86 |
assert lx.__class__.__name__ == 'GAPLexer' |
| 87 |
|
| 88 |
|
| 89 |
def test_guess_lexer_easytrieve(): |
| 90 |
lx = guess_lexer(get_input('easytrieve', 'example.ezt')) |
| 91 |
assert lx.__class__.__name__ == 'EasytrieveLexer' |
| 92 |
lx = guess_lexer(get_input('easytrieve', 'example.mac')) |
| 93 |
assert lx.__class__.__name__ == 'EasytrieveLexer' |
| 94 |
|
| 95 |
|
| 96 |
def test_guess_lexer_jcl(): |
| 97 |
lx = guess_lexer(get_input('jcl', 'example.jcl')) |
| 98 |
assert lx.__class__.__name__ == 'JclLexer' |
| 99 |
|
| 100 |
|
| 101 |
def test_guess_lexer_rexx(): |
| 102 |
lx = guess_lexer(get_input('rexx', 'example.rexx')) |
| 103 |
assert lx.__class__.__name__ == 'RexxLexer' |
| 104 |
|
| 105 |
|
| 106 |
def test_easytrieve_can_guess_from_text(): |
| 107 |
lx = get_lexer_by_name('easytrieve') |
| 108 |
assert lx.analyse_text('MACRO') |
| 109 |
assert lx.analyse_text('\nMACRO') |
| 110 |
assert lx.analyse_text(' \nMACRO') |
| 111 |
assert lx.analyse_text(' \n MACRO') |
| 112 |
assert lx.analyse_text('*\nMACRO') |
| 113 |
assert lx.analyse_text('*\n *\n\n \n*\n MACRO') |
| 114 |
|
| 115 |
|
| 116 |
def test_rexx_can_guess_from_text(): |
| 117 |
lx = get_lexer_by_name('rexx') |
| 118 |
assert lx.analyse_text('/* */') == pytest.approx(0.01) |
| 119 |
assert lx.analyse_text('''/* Rexx */ |
| 120 |
say "hello world"''') == pytest.approx(1.0) |
| 121 |
val = lx.analyse_text('/* */\n' |
| 122 |
'hello:pRoceduRe\n' |
| 123 |
' say "hello world"') |
| 124 |
assert val > 0.5 |
| 125 |
val = lx.analyse_text('''/* */ |
| 126 |
if 1 > 0 then do |
| 127 |
say "ok" |
| 128 |
end |
| 129 |
else do |
| 130 |
say "huh?" |
| 131 |
end''') |
| 132 |
assert val > 0.2 |
| 133 |
val = lx.analyse_text('''/* */ |
| 134 |
greeting = "hello world!" |
| 135 |
parse value greeting "hello" name "!" |
| 136 |
say name''') |
| 137 |
assert val > 0.2 |
| 138 |
|
| 139 |
|
| 140 |
def test_guess_cmake_lexer_from_header(): |
| 141 |
headers = [ |
| 142 |
"CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR)", |
| 143 |
"cmake_minimum_required(version 3.13) # CMake version check", |
| 144 |
" CMAKE_MINIMUM_REQUIRED\t( VERSION 2.6 FATAL_ERROR ) ", |
| 145 |
] |
| 146 |
for header in headers: |
| 147 |
code = '\n'.join([ |
| 148 |
header, |
| 149 |
'project(example)', |
| 150 |
'set(CMAKE_CXX_STANDARD 14)', |
| 151 |
'set(SOURCE_FILES main.cpp)', |
| 152 |
'add_executable(example ${SOURCE_FILES})', |
| 153 |
]) |
| 154 |
lexer = guess_lexer(code) |
| 155 |
assert lexer.__class__.__name__ == 'CMakeLexer', \ |
| 156 |
f"header must be detected as CMake: {header!r}" |
| 157 |
|
| 158 |
|
| 159 |
def test_guess_c_lexer(): |
| 160 |
code = ''' |
| 161 |
#include <stdio.h> |
| 162 |
#include <stdlib.h> |
| 163 |
|
| 164 |
int main(void); |
| 165 |
|
| 166 |
int main(void) { |
| 167 |
uint8_t x = 42; |
| 168 |
uint8_t y = x + 1; |
| 169 |
|
| 170 |
/* exit 1 for success! */ |
| 171 |
return 1; |
| 172 |
} |
| 173 |
''' |
| 174 |
lexer = guess_lexer(code) |
| 175 |
assert lexer.__class__.__name__ == 'CLexer' |
| 176 |
|
| 177 |
def test_guess_carbon_lexer(): |
| 178 |
code = ''' |
| 179 |
package Sorting api; |
| 180 |
|
| 181 |
abstract class C { |
| 182 |
var a: i32; |
| 183 |
} |
| 184 |
|
| 185 |
base class B { |
| 186 |
var value_b: i32; |
| 187 |
} |
| 188 |
|
| 189 |
impl JustX as X {} |
| 190 |
''' |
| 191 |
lexer = guess_lexer(code) |
| 192 |
assert lexer.__class__.__name__ == 'CarbonLexer' |
| 193 |
|
| 194 |
def test_cbmbasicv2_analyse_text(): |
| 195 |
text = "10 PRINT \"PART 1\"" |
| 196 |
res = CbmBasicV2Lexer.analyse_text(text) |
| 197 |
assert res == 0.2 |
| 198 |
|
| 199 |
|
| 200 |
def test_ecl_analyze_text(): |
| 201 |
text = r""" |
| 202 |
STRING ABC -> size32_t lenAbc, const char * abc; |
| 203 |
""" |
| 204 |
res = ECLLexer.analyse_text(text) |
| 205 |
assert res == 0.01 |
| 206 |
|
| 207 |
|
| 208 |
def iter_fixture_dir(sub_dir): |
| 209 |
"""Returns the sub-directories in any of the examplefiles/"snippets" fixture directory.""" |
| 210 |
for dir in Path(TESTDIR, sub_dir).iterdir(): |
| 211 |
# Only keep directories that are not special. |
| 212 |
if ( |
| 213 |
dir.is_dir() |
| 214 |
and not dir.name.startswith("__") |
| 215 |
and not dir.name.startswith(".") |
| 216 |
): |
| 217 |
yield dir |
| 218 |
|
| 219 |
|
| 220 |
@pytest.mark.parametrize("sub_dir", ["examplefiles", "snippets"]) |
| 221 |
def test_lexer_fixtures(sub_dir): |
| 222 |
"""Check that example files and snippets fixtures are in a directory named after a lexer alias.""" |
| 223 |
for dir in iter_fixture_dir(sub_dir): |
| 224 |
assert find_lexer_class_by_name( |
| 225 |
dir.name |
| 226 |
), f"Alias {dir.name} not found in pygments" |
| 227 |
|
| 228 |
|
| 229 |
def all_example_files(): |
| 230 |
example_files = [] |
| 231 |
for dir in iter_fixture_dir("examplefiles"): |
| 232 |
for f in dir.iterdir(): |
| 233 |
if f.is_file() and f.suffix != ".output": |
| 234 |
# XXX Skip the srcinfo directory, whose example files are not strictly named ".SRCINFO". |
| 235 |
marks = [] |
| 236 |
if dir.name == "srcinfo": |
| 237 |
marks = [ |
| 238 |
pytest.mark.skip( |
| 239 |
reason="Example file not strictly named '.SRCINFO'" |
| 240 |
) |
| 241 |
] |
| 242 |
|
| 243 |
example_files.append( |
| 244 |
pytest.param(f, id=f"{f.parent.name}/{f.name}", marks=marks) |
| 245 |
) |
| 246 |
|
| 247 |
return example_files |
| 248 |
|
| 249 |
|
| 250 |
@pytest.mark.parametrize("example_file", all_example_files()) |
| 251 |
def test_filename_matching(example_file): |
| 252 |
"""Check that each example file is correctly identified by its lexer from its filename pattern.""" |
| 253 |
# Example files are stored in subdirectories named after the lexer alias. |
| 254 |
alias = example_file.parent.name |
| 255 |
klass = find_lexer_class_by_name(alias) |
| 256 |
|
| 257 |
# If the class defines no filename patterns, we can't check anything. |
| 258 |
if not klass.filenames: |
| 259 |
return |
| 260 |
|
| 261 |
matches = [] |
| 262 |
for pattern in klass.filenames: |
| 263 |
if _fn_matches(example_file.name, pattern): |
| 264 |
matches.append(pattern) |
| 265 |
|
| 266 |
assert matches, f"No {klass.filenames} patterns matches {example_file.name!r}" |