summaryrefslogtreecommitdiff
path: root/tests/test_generator.py
blob: f83fb1bac4d34611de498083f4e903f661ddcf82 (plain)
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
from qface.generator import FileSystem, Generator
from unittest.mock import patch
from io import StringIO
import logging
import logging.config
import tempfile
from path import Path

# logging.config.fileConfig('logging.ini')
logging.basicConfig()

log = logging.getLogger(__name__)

inputPath = Path('tests/in')
log.debug('input path folder: {0}'.format(inputPath.abspath()))


def loadSystem():
    path = inputPath / 'com.pelagicore.ivi.tuner.qface'
    return FileSystem.parse_document(path)


def test_gen_module():
    system = loadSystem()
    gen = Generator(search_path='tests/templates')
    template = "{{module}}"
    module = system.lookup('com.pelagicore.ivi.tuner')
    text = gen.apply(template, {"module": module})
    assert text == 'com.pelagicore.ivi.tuner'


def test_gen_interface():
    system = loadSystem()
    gen = Generator(search_path='tests/templates')
    template = """{{module.interfaces|join(',')}}"""
    module = system.lookup('com.pelagicore.ivi.tuner')
    text = gen.apply(template, {"module": module})
    assert text == 'BaseTuner,Tuner,TunerExtension'


def test_parse_document():
    system = FileSystem.parse(inputPath / 'com.pelagicore.ivi.tuner.qface')
    assert system.lookup('com.pelagicore.ivi.tuner')


def test_parse_document_list():
    src = [inputPath / 'com.pelagicore.ivi.tuner.qface',
           inputPath / 'com.pelagicore.ivi.climate.qface']
    system = FileSystem.parse(src)
    assert system.lookup('com.pelagicore.ivi.tuner')
    assert system.lookup('com.pelagicore.ivi.climate')


def test_parse_document_mixed():
    src = [inputPath, inputPath / 'com.pelagicore.ivi.climate.qface']
    system = FileSystem.parse(src)
    assert system.lookup('com.pelagicore.ivi.tuner')
    assert system.lookup('com.pelagicore.ivi.climate')
    assert system.lookup('com.pelagicore.one')


def test_destination_prefix():
    system = FileSystem.parse(inputPath)
    out = Path('tests/out')
    out.rmtree_p()
    out.makedirs_p()
    generator = Generator(search_path='tests/templates')
    for module in system.modules:
        dst_template = '{{out}}/{{module|lower}}.txt'
        ctx = {'out': out.abspath(), 'module': module}
        generator.write(dst_template, 'module.txt', ctx)
        path = generator.apply(dst_template, ctx)
        assert Path(path).exists() == True
    out.rmtree_p()

@patch('sys.stderr', new_callable=StringIO)
def test_error_template_syntax_error(mock_stderr):
    tmpDir = tempfile.TemporaryDirectory()
    src = [inputPath, inputPath / 'com.pelagicore.ivi.climate.qface']
    system = FileSystem.parse(src)
    dst_template = '{{out}}/out.txt'
    ctx = {'out': tmpDir.name}
    generator = Generator(search_path='tests/templates')
    generator.write(dst_template, 'syntaxError.txt', ctx)
    path = generator.apply(dst_template, ctx)
    assert Path(path).exists() == False
    assert mock_stderr.getvalue() == "tests/templates/syntaxError.txt:1: error: Encountered unknown tag 'fooo'.\n"

@patch('sys.stderr', new_callable=StringIO)
def test_error_template_undefined_variable(mock_stderr):
    tmpDir = tempfile.TemporaryDirectory()
    src = [inputPath, inputPath / 'com.pelagicore.ivi.climate.qface']
    system = FileSystem.parse(src)
    dst_template = '{{out}}/out.txt'
    ctx = {'out': tmpDir.name}
    generator = Generator(search_path='tests/templates')
    generator.write(dst_template, 'undefinedVariable.txt', ctx)
    path = generator.apply(dst_template, ctx)
    assert Path(path).exists() == False
    assert mock_stderr.getvalue() == "tests/templates/undefinedVariable.txt:1: error: 'this_is_not_defined' is undefined\n"

@patch('sys.stderr', new_callable=StringIO)
def test_error_template_doesnt_exist(mock_stderr):
    tmpDir = tempfile.TemporaryDirectory()
    src = [inputPath, inputPath / 'com.pelagicore.ivi.climate.qface']
    system = FileSystem.parse(src)
    dst_template = '{{out}}/out.txt'
    ctx = {'out': tmpDir.name}
    generator = Generator(search_path='tests/templates')
    generator.write(dst_template, 'doesnt_exist.txt', ctx)
    path = generator.apply(dst_template, ctx)
    assert Path(path).exists() == False
    assert mock_stderr.getvalue() == "/doesnt_exist.txt: error: Template not found\n"