diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 11:33:32 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 11:33:32 +0000 |
commit | 1f403ad2197fc7442409f434ee574f3e6b46fb73 (patch) | |
tree | 0299c6dd11d5edfa918a29b6456bc1875f1d288c /tests/test_templates.py | |
parent | Initial commit. (diff) | |
download | pygments-1f403ad2197fc7442409f434ee574f3e6b46fb73.tar.xz pygments-1f403ad2197fc7442409f434ee574f3e6b46fb73.zip |
Adding upstream version 2.14.0+dfsg.upstream/2.14.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/test_templates.py')
-rw-r--r-- | tests/test_templates.py | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/tests/test_templates.py b/tests/test_templates.py new file mode 100644 index 0000000..9ed816f --- /dev/null +++ b/tests/test_templates.py @@ -0,0 +1,130 @@ +import pytest + +from pygments.lexers.templates import JavascriptDjangoLexer, MasonLexer, \ + SqlJinjaLexer, VelocityLexer + +from pygments.token import Comment + + +@pytest.fixture(scope="module") +def lexer(): + yield JavascriptDjangoLexer() + +@pytest.fixture(scope='module') +def lexerMason(): + yield MasonLexer() + +@pytest.fixture(scope='module') +def lexerVelocity(): + yield VelocityLexer() + +@pytest.fixture(scope='module') +def lexerSqlJinja(): + yield SqlJinjaLexer() + +def test_do_not_mistake_JSDoc_for_django_comment(lexer): + """ + Test to make sure the lexer doesn't mistake + {* ... *} to be a django comment + """ + text = """/** + * @param {*} cool + */ + func = function(cool) { + }; + + /** + * @param {*} stuff + */ + fun = function(stuff) { + };""" + tokens = lexer.get_tokens(text) + assert not any(t[0] == Comment for t in tokens) + +def test_mason_unnamed_block(lexerMason): + text = """ + <%class> + has 'foo'; + has 'bar' => (required => 1); + has 'baz' => (isa => 'Int', default => 17); + </%class> + """ + res = lexerMason.analyse_text(text) + assert res == 1.0 + +def test_velocity_macro(lexerVelocity): + text = """ + #macro(getBookListLink, $readingTrackerResult) + $readingTrackerResult.getBookListLink() + #end + """ + res = lexerVelocity.analyse_text(text) + assert res == 0.26 + +def test_velocity_foreach(lexerVelocity): + text = """ + <ul> + #foreach( $product in $allProducts ) + <li>$product</li> + #end + </ul> + """ + res = lexerVelocity.analyse_text(text) + assert res == 0.16 + +def test_velocity_if(lexerVelocity): + text = """ + #if( $display ) + <strong>Velocity!</strong> + #end + """ + res = lexerVelocity.analyse_text(text) + assert res == 0.16 + +def test_velocity_reference(lexerVelocity): + text = """ + Hello $name! Welcome to Velocity! + """ + res = lexerVelocity.analyse_text(text) + assert res == 0.01 + +def test_sql_jinja_dbt_ref(lexerSqlJinja): + text = """ + {%- set payment_methods = ["bank_transfer", "credit_card", "gift_card"] -%} + + select + order_id, + {%- for payment_method in payment_methods %} + sum(case when payment_method = '{{payment_method}}' then amount end) as {{payment_method}}_amount + {%- if not loop.last %},{% endif -%} + {% endfor %} + from {{ ref('raw_payments') }} + group by 1 + """ + res = lexerSqlJinja.analyse_text(text) + assert res == 0.4 + +def test_sql_jinja_dbt_source(lexerSqlJinja): + text = """ + {%- set payment_methods = ["bank_transfer", "credit_card", "gift_card"] -%} + + select + order_id, + {%- for payment_method in payment_methods %} + sum(case when payment_method = '{{payment_method}}' then amount end) as {{payment_method}}_amount + {%- if not loop.last %},{% endif -%} + {% endfor %} + from {{ source('payments_db', 'payments') }} + group by 1 + """ + res = lexerSqlJinja.analyse_text(text) + assert res == 0.25 + +def test_sql_jinja_dbt_macro(lexerSqlJinja): + text = """ + {% macro cents_to_dollars(column_name, precision=2) %} + ({{ column_name }} / 100)::numeric(16, {{ precision }}) + {% endmacro %} + """ + res = lexerSqlJinja.analyse_text(text) + assert res == 0.15 |