2018-06-28 00:36:17 +00:00
|
|
|
import 'dart:math';
|
|
|
|
import 'package:code_buffer/code_buffer.dart';
|
|
|
|
import 'package:jael/jael.dart' as jael;
|
|
|
|
import 'package:symbol_table/symbol_table.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
test('render into div', () {
|
|
|
|
var template = '''
|
2018-06-28 00:46:34 +00:00
|
|
|
<div>
|
|
|
|
<element name="square-root">
|
|
|
|
The square root of {{ n }} is {{ sqrt(n).toInt() }}.
|
|
|
|
</element>
|
2018-06-28 01:23:38 +00:00
|
|
|
<square-root @n=16 />
|
2018-06-28 00:46:34 +00:00
|
|
|
</div>
|
2018-06-28 00:36:17 +00:00
|
|
|
''';
|
|
|
|
|
|
|
|
var html = render(template, {'sqrt': sqrt});
|
2018-06-28 00:46:34 +00:00
|
|
|
print(html);
|
|
|
|
|
2018-11-03 00:16:03 +00:00
|
|
|
expect(
|
|
|
|
html,
|
|
|
|
'''
|
2018-06-28 00:46:34 +00:00
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
The square root of 16 is 4.
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-11-03 00:16:03 +00:00
|
|
|
'''
|
|
|
|
.trim());
|
2018-06-28 00:46:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('render into explicit tag name', () {
|
|
|
|
var template = '''
|
|
|
|
<div>
|
|
|
|
<element name="square-root">
|
|
|
|
The square root of {{ n }} is {{ sqrt(n).toInt() }}.
|
|
|
|
</element>
|
2018-06-28 01:23:38 +00:00
|
|
|
<square-root as="span" @n=16 />
|
2018-06-28 00:46:34 +00:00
|
|
|
</div>
|
|
|
|
''';
|
|
|
|
|
|
|
|
var html = render(template, {'sqrt': sqrt});
|
|
|
|
print(html);
|
|
|
|
|
2018-11-03 00:16:03 +00:00
|
|
|
expect(
|
|
|
|
html,
|
|
|
|
'''
|
2018-06-28 00:46:34 +00:00
|
|
|
<div>
|
|
|
|
<span>
|
|
|
|
The square root of 16 is 4.
|
|
|
|
</span>
|
|
|
|
</div>
|
2018-11-03 00:16:03 +00:00
|
|
|
'''
|
|
|
|
.trim());
|
2018-06-28 00:46:34 +00:00
|
|
|
});
|
|
|
|
|
2018-06-28 01:23:38 +00:00
|
|
|
test('pass attributes', () {
|
|
|
|
var template = '''
|
|
|
|
<div>
|
|
|
|
<element name="square-root">
|
|
|
|
The square root of {{ n }} is {{ sqrt(n).toInt() }}.
|
|
|
|
</element>
|
|
|
|
<square-root foo="bar" baz="quux" @n=16 />
|
|
|
|
</div>
|
|
|
|
''';
|
|
|
|
|
|
|
|
var html = render(template, {'sqrt': sqrt});
|
|
|
|
print(html);
|
|
|
|
|
2018-11-03 00:16:03 +00:00
|
|
|
expect(
|
|
|
|
html,
|
|
|
|
'''
|
2018-06-28 01:23:38 +00:00
|
|
|
<div>
|
|
|
|
<div foo="bar" baz="quux">
|
|
|
|
The square root of 16 is 4.
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-11-03 00:16:03 +00:00
|
|
|
'''
|
|
|
|
.trim());
|
2018-06-28 01:23:38 +00:00
|
|
|
});
|
|
|
|
|
2018-06-28 00:46:34 +00:00
|
|
|
test('render without tag name', () {
|
|
|
|
var template = '''
|
|
|
|
<div>
|
|
|
|
<element name="square-root">
|
|
|
|
The square root of {{ n }} is {{ sqrt(n).toInt() }}.
|
|
|
|
</element>
|
2018-06-28 01:23:38 +00:00
|
|
|
<square-root as=false @n=16 />
|
2018-06-28 00:46:34 +00:00
|
|
|
</div>
|
|
|
|
''';
|
|
|
|
|
|
|
|
var html = render(template, {'sqrt': sqrt});
|
|
|
|
print(html);
|
|
|
|
|
2018-11-03 00:16:03 +00:00
|
|
|
expect(
|
|
|
|
html,
|
|
|
|
'''
|
2018-06-28 00:46:34 +00:00
|
|
|
<div>
|
|
|
|
The square root of 16 is 4.
|
|
|
|
|
|
|
|
</div>
|
2018-11-03 00:16:03 +00:00
|
|
|
'''
|
|
|
|
.trim());
|
2018-06-28 00:36:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-28 00:37:33 +00:00
|
|
|
String render(String template, [Map<String, dynamic> values = const {}]) {
|
|
|
|
var doc = jael.parseDocument(template, onError: (e) => throw e)!;
|
2019-07-29 22:12:52 +00:00
|
|
|
var buffer = CodeBuffer();
|
|
|
|
const jael.Renderer().render(doc, buffer, SymbolTable(values: values));
|
2018-06-28 00:36:17 +00:00
|
|
|
return buffer.toString();
|
2018-06-28 00:46:34 +00:00
|
|
|
}
|