platform/jael/test/render/custom_element_test.dart

114 lines
2.2 KiB
Dart
Raw Normal View History

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>
<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>
<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
});
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,
'''
<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 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>
<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
});
}
String render(String template, [Map<String, dynamic> values]) {
var doc = jael.parseDocument(template, onError: (e) => throw e);
var buffer = new CodeBuffer();
const jael.Renderer().render(doc, buffer, new SymbolTable(values: values));
return buffer.toString();
2018-06-28 00:46:34 +00:00
}