| 1 |
import pytest |
| 2 |
|
| 3 |
from pygments.lexers.templates import JavascriptDjangoLexer, MasonLexer, \ |
| 4 |
SqlJinjaLexer, VelocityLexer |
| 5 |
|
| 6 |
from pygments.token import Comment |
| 7 |
|
| 8 |
|
| 9 |
@pytest.fixture(scope="module") |
| 10 |
def lexer(): |
| 11 |
yield JavascriptDjangoLexer() |
| 12 |
|
| 13 |
@pytest.fixture(scope='module') |
| 14 |
def lexerMason(): |
| 15 |
yield MasonLexer() |
| 16 |
|
| 17 |
@pytest.fixture(scope='module') |
| 18 |
def lexerVelocity(): |
| 19 |
yield VelocityLexer() |
| 20 |
|
| 21 |
@pytest.fixture(scope='module') |
| 22 |
def lexerSqlJinja(): |
| 23 |
yield SqlJinjaLexer() |
| 24 |
|
| 25 |
def test_do_not_mistake_JSDoc_for_django_comment(lexer): |
| 26 |
""" |
| 27 |
Test to make sure the lexer doesn't mistake |
| 28 |
{* ... *} to be a django comment |
| 29 |
""" |
| 30 |
text = """/** |
| 31 |
* @param {*} cool |
| 32 |
*/ |
| 33 |
func = function(cool) { |
| 34 |
}; |
| 35 |
|
| 36 |
/** |
| 37 |
* @param {*} stuff |
| 38 |
*/ |
| 39 |
fun = function(stuff) { |
| 40 |
};""" |
| 41 |
tokens = lexer.get_tokens(text) |
| 42 |
assert not any(t[0] == Comment for t in tokens) |
| 43 |
|
| 44 |
def test_mason_unnamed_block(lexerMason): |
| 45 |
text = """ |
| 46 |
<%class> |
| 47 |
has 'foo'; |
| 48 |
has 'bar' => (required => 1); |
| 49 |
has 'baz' => (isa => 'Int', default => 17); |
| 50 |
</%class> |
| 51 |
""" |
| 52 |
res = lexerMason.analyse_text(text) |
| 53 |
assert res == 1.0 |
| 54 |
|
| 55 |
def test_velocity_macro(lexerVelocity): |
| 56 |
text = """ |
| 57 |
#macro(getBookListLink, $readingTrackerResult) |
| 58 |
$readingTrackerResult.getBookListLink() |
| 59 |
#end |
| 60 |
""" |
| 61 |
res = lexerVelocity.analyse_text(text) |
| 62 |
assert res == 0.26 |
| 63 |
|
| 64 |
def test_velocity_foreach(lexerVelocity): |
| 65 |
text = """ |
| 66 |
<ul> |
| 67 |
#foreach( $product in $allProducts ) |
| 68 |
<li>$product</li> |
| 69 |
#end |
| 70 |
</ul> |
| 71 |
""" |
| 72 |
res = lexerVelocity.analyse_text(text) |
| 73 |
assert res == 0.16 |
| 74 |
|
| 75 |
def test_velocity_if(lexerVelocity): |
| 76 |
text = """ |
| 77 |
#if( $display ) |
| 78 |
<strong>Velocity!</strong> |
| 79 |
#end |
| 80 |
""" |
| 81 |
res = lexerVelocity.analyse_text(text) |
| 82 |
assert res == 0.16 |
| 83 |
|
| 84 |
def test_velocity_reference(lexerVelocity): |
| 85 |
text = """ |
| 86 |
Hello $name! Welcome to Velocity! |
| 87 |
""" |
| 88 |
res = lexerVelocity.analyse_text(text) |
| 89 |
assert res == 0.01 |
| 90 |
|
| 91 |
def test_sql_jinja_dbt_ref(lexerSqlJinja): |
| 92 |
text = """ |
| 93 |
{%- set payment_methods = ["bank_transfer", "credit_card", "gift_card"] -%} |
| 94 |
|
| 95 |
select |
| 96 |
order_id, |
| 97 |
{%- for payment_method in payment_methods %} |
| 98 |
sum(case when payment_method = '{{payment_method}}' then amount end) as {{payment_method}}_amount |
| 99 |
{%- if not loop.last %},{% endif -%} |
| 100 |
{% endfor %} |
| 101 |
from {{ ref('raw_payments') }} |
| 102 |
group by 1 |
| 103 |
""" |
| 104 |
res = lexerSqlJinja.analyse_text(text) |
| 105 |
assert res == 0.4 |
| 106 |
|
| 107 |
def test_sql_jinja_dbt_source(lexerSqlJinja): |
| 108 |
text = """ |
| 109 |
{%- set payment_methods = ["bank_transfer", "credit_card", "gift_card"] -%} |
| 110 |
|
| 111 |
select |
| 112 |
order_id, |
| 113 |
{%- for payment_method in payment_methods %} |
| 114 |
sum(case when payment_method = '{{payment_method}}' then amount end) as {{payment_method}}_amount |
| 115 |
{%- if not loop.last %},{% endif -%} |
| 116 |
{% endfor %} |
| 117 |
from {{ source('payments_db', 'payments') }} |
| 118 |
group by 1 |
| 119 |
""" |
| 120 |
res = lexerSqlJinja.analyse_text(text) |
| 121 |
assert res == 0.25 |
| 122 |
|
| 123 |
def test_sql_jinja_dbt_macro(lexerSqlJinja): |
| 124 |
text = """ |
| 125 |
{% macro cents_to_dollars(column_name, precision=2) %} |
| 126 |
({{ column_name }} / 100)::numeric(16, {{ precision }}) |
| 127 |
{% endmacro %} |
| 128 |
""" |
| 129 |
res = lexerSqlJinja.analyse_text(text) |
| 130 |
assert res == 0.15 |