Repository navigation
Expand file tree
/
Copy pathtest_app.py
More file actions
32 lines (29 loc) · 2.34 KB
/
Copy pathtest_app.py
File metadata and controls
32 lines (29 loc) · 2.34 KB
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
"""Run with: python test_app.py (no test framework required)."""
from main import SAMPLE, app, convert_cpp
result = convert_cpp(SAMPLE)
assert r'\For{' in result['latex'] and r'\EndFor' in result['latex']
assert result['name'] == 'sumArray' and result['caption'] == 'Sum array'
assert result['preview'][1]['kind'] == 'Comment'
nested = convert_cpp('int f(int n) { if(n > 0) { if(n > 2) return 2; else return 1; } else if(n == 0) return 0; else return -1; }')['latex']
assert nested.count(r'\If{') == nested.count(r'\EndIf') == 2
assert nested.index(r'\EndIf') < nested.index(r'\ElsIf')
assert r'\Until{not (n \(>\) 0)}' in convert_cpp('void f(int n) { do { n--; } while(n > 0); }')['latex']
escaped = convert_cpp('int foo_bar(int value_x) { // 50% & # $ { } _ \\input\n return value_x; }', caption=r'A_{title} % \input{x}')['latex']
for text in [r'foo\_bar', r'value\_x', r'50\%', r'\textbackslash{}input', r'A\_\{title\}']:
assert text in escaped, text
strings = convert_cpp('void f() { cout << "braces { }; and \\"quotes\\""; }')['latex']
assert r'\{ \}' in strings
assert '"keep ( spaces )"' in convert_cpp('void f(){cout << "keep ( spaces )";}')['latex']
snippet = convert_cpp(SAMPLE, line_numbers=False, full_document=False)['latex']
assert r'\documentclass' not in snippet and r'\begin{algorithmic}[1]' not in snippet
client = app.test_client()
assert client.get('/').status_code == client.get('/display').status_code == 200
assert client.post('/display', data={'code': SAMPLE}).status_code == 200
assert client.post('/api/convert', json={'code': SAMPLE}).json['latex'] == result['latex']
for code in ['', None, [], 'hello', 'int f(){return 1}', 'int f(){if(true)}', 'int f(){while(true)}', 'int f(){x=(2];}', 'int f(){} int g(){}', 'int f(){switch(x){case 1: return 0;}}', 'int f(){' + 'if(true){' * 65 + ';' + '}' * 66, 'x' * 50001]:
response = client.post('/api/convert', json={'code': code})
assert response.status_code == 400 and response.json['error'], (code, response.status_code)
for data in [[], {}, {'code': SAMPLE, 'caption': []}, {'code': SAMPLE, 'caption': 'x' * 121}, {'code': SAMPLE, 'line_numbers': 'false'}]:
assert client.post('/api/convert', json=data).status_code == 400
assert client.post('/api/convert', data='x' * (256 * 1024 + 1), content_type='application/json').status_code == 413
print('All converter and HTTP regression checks passed.')