File: test_api.py

package info (click to toggle)
qtsass 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 344 kB
  • sloc: python: 1,337; sh: 7; makefile: 2
file content (182 lines) | stat: -rw-r--r-- 4,264 bytes parent folder | download
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2015 Yann Lanthony
# Copyright (c) 2017-2018 Spyder Project Contributors
#
# Licensed under the terms of the MIT License
# (See LICENSE.txt for details)
# -----------------------------------------------------------------------------
"""Test qtsass api."""

from __future__ import absolute_import

# Standard library imports
from os.path import exists
import logging

# Third party imports
import pytest
import sass

# Local imports
import qtsass

# Local imports
from . import EXAMPLES_DIR, PROJECT_DIR, example


COLORS_STR = """
QWidget {
    background: rgba(127, 127, 127, 100%);
    color: rgb(255, 255, 255);
}
"""
QLINEARGRADIENTS_STR = """
QWidget {
    background: qlineargradient(
        x1: 0,
        y1: 0,
        x2: 0,
        y2: 1,
        stop: 0.1 blue,
        stop: 0.8 green
    );
}
"""
QRADIANTGRADIENTS_STR = """
QWidget {
    background: qradialgradient(
        spread: repeat,
        cx: 0,
        cy: 0,
        fx: 0,
        fy: 1,
        stop: 0.1 blue,
        stop: 0.8 green
    );
}
"""
QNOT_STR = """
QLineEdit:!editable {
    background: white;
}
"""
IMPORT_STR = """
@import 'dummy';
"""
CUSTOM_BORDER_STR = """
QWidget {
    border: custom_border();
}
"""


def setup_module():
    qtsass.enable_logging(level=logging.DEBUG)


def teardown_module():
    qtsass.enable_logging(level=logging.WARNING)


def test_compile_strings():
    """compile various strings."""

    qtsass.compile(COLORS_STR)
    qtsass.compile(QLINEARGRADIENTS_STR)
    qtsass.compile(QRADIANTGRADIENTS_STR)
    qtsass.compile(QNOT_STR)


def test_compile_import_raises():
    """compile string with import raises."""

    with pytest.raises(sass.CompileError):
        qtsass.compile(IMPORT_STR)


def test_compile_import_with_include_paths():
    """compile string with include_paths"""

    qtsass.compile(IMPORT_STR, include_paths=[EXAMPLES_DIR])


def test_compile_raises_ValueError():
    """compile raises ValueError with invalid arguments"""

    # Pass invalid type to importers - must be sequence
    with pytest.raises(ValueError):
        qtsass.compile(COLORS_STR, importers=lambda x: None)

    # Pass invalid type to custom_functions
    with pytest.raises(ValueError):
        qtsass.compile(COLORS_STR, custom_functions=lambda x: None)


def test_compile_custom_function():
    """compile string with custom_functions"""

    custom_str = (
        'QWidget {\n'
        '    border: custom_border();\n'
        '}'
    )

    def custom_border():
        return '1px solid'

    css = qtsass.compile(custom_str, custom_functions=[custom_border])
    assert '1px solid' in css
    assert 'custom_border()' not in css


def test_compile_filename(tmpdir):
    """compile_filename simple."""

    output = tmpdir.join('dummy.css')
    qtsass.compile_filename(example('dummy.scss'), output.strpath)
    assert exists(output.strpath)


def test_compile_filename_no_save():
    """compile_filename simple."""

    qss = qtsass.compile_filename(example('dummy.scss'))
    assert isinstance(qss, str)


def test_compile_filename_imports(tmpdir):
    """compile_filename with imports."""

    output = tmpdir.join('dark.css')
    qtsass.compile_filename(example('complex', 'dark.scss'), output.strpath)
    assert exists(output.strpath)


def test_compile_filename_imports_no_save():
    """compile_filename with imports."""

    qss = qtsass.compile_filename(example('complex', 'dark.scss'))
    assert isinstance(qss, str)


def test_compile_dirname(tmpdir):
    """compile_dirname complex."""

    output = tmpdir.join('complex')
    qtsass.compile_dirname(example('complex'), output.strpath)
    assert exists(output.join('dark.css').strpath)
    assert exists(output.join('light.css').strpath)


def test_watch_raises_ValueError(tmpdir):
    """watch raises ValueError when source does not exist."""

    # Watch file does not raise
    _ = qtsass.watch(example('dummy.scss'), tmpdir.join('dummy.scss').strpath)

    # Watch dir does not raise
    _ = qtsass.watch(example('complex'), tmpdir.join('complex').strpath)

    with pytest.raises(ValueError):
        _ = qtsass.watch('does_not_exist', 'does_not_exist')