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('attribute binding', () {
const template = '''
Hello
''';
var buf = CodeBuffer();
jael.Document? document;
late SymbolTable scope;
try {
document = jael.parseDocument(template, sourceUrl: 'test.jael');
scope = SymbolTable(values: {
'csrf_token': 'foo',
'profile': {
'avatar': 'thosakwe.png',
}
});
} on jael.JaelError catch (e) {
print(e);
print(e.stackTrace);
}
expect(document, isNotNull);
const jael.Renderer().render(document!, buf, scope);
print(buf);
expect(
buf.toString(),
'''
Hello
'''
.trim());
});
test('interpolation', () {
const template = '''
Pokémon
{{ pokemon.name }} - {{ pokemon.type }}
''';
var buf = CodeBuffer();
//jael.scan(template, sourceUrl: 'test.jael').tokens.forEach(print);
var document = jael.parseDocument(template, sourceUrl: 'test.jael')!;
var scope = SymbolTable(values: {
'pokemon': const _Pokemon('Darkrai', 'Dark'),
});
const jael.Renderer().render(document, buf, scope);
print(buf);
expect(
buf.toString().replaceAll('\n', '').replaceAll(' ', '').trim(),
'''
Pokémon
Darkrai - Dark
'''
.replaceAll('\n', '')
.replaceAll(' ', '')
.trim());
});
test('for loop', () {
const template = '''
Pokémon
- #{{ idx }} {{ starter.name }} - {{ starter.type }}
''';
var buf = CodeBuffer();
var document = jael.parseDocument(template, sourceUrl: 'test.jael')!;
var scope = SymbolTable(values: {
'starters': starters,
});
const jael.Renderer().render(document, buf, scope);
print(buf);
expect(
buf.toString(),
'''
Pokémon
-
#0 Bulbasaur - Grass
-
#1 Charmander - Fire
-
#2 Squirtle - Water
'''
.trim());
});
test('conditional', () {
const template = '''
Conditional
Empty
Not empty
''';
var buf = CodeBuffer();
var document = jael.parseDocument(template, sourceUrl: 'test.jael')!;
var scope = SymbolTable(values: {
'starters': starters,
});
const jael.Renderer().render(document, buf, scope);
print(buf);
expect(
buf.toString(),
'''
Conditional
Not empty
'''
.trim());
});
test('declare', () {
const template = '''
- {{one}}
- {{two}}
- {{three}}
- {{one}}
- {{two}}
- {{three}}
''';
var buf = CodeBuffer();
var document = jael.parseDocument(template, sourceUrl: 'test.jael')!;
var scope = SymbolTable();
const jael.Renderer().render(document, buf, scope);
print(buf);
expect(
buf.toString(),
'''
'''
.trim());
});
test('unescaped attr/interp', () {
const template = '''
" />
{{- "
" }}
''';
var buf = CodeBuffer();
var document = jael.parseDocument(template, sourceUrl: 'test.jael')!;
var scope = SymbolTable();
const jael.Renderer().render(document, buf, scope);
print(buf);
expect(
buf.toString().replaceAll('\n', '').replaceAll(' ', '').trim(),
'''
'''
.replaceAll('\n', '')
.replaceAll(' ', '')
.trim());
});
test('quoted attribute name', () {
const template = '''
''';
var buf = CodeBuffer();
var document = jael.parseDocument(template, sourceUrl: 'test.jael')!;
var scope = SymbolTable();
const jael.Renderer().render(document, buf, scope);
print(buf);
expect(
buf.toString(),
'''
'''
.trim());
});
test('switch', () {
const template = '''
BAN HAMMER LOLOL
You are in good standing.
Weird...
''';
var buf = CodeBuffer();
var document = jael.parseDocument(template, sourceUrl: 'test.jael')!;
var scope = SymbolTable(values: {
'account': _Account(isDisabled: true),
});
const jael.Renderer().render(document, buf, scope);
print(buf);
expect(buf.toString().trim(), 'BAN HAMMER LOLOL');
});
test('default', () {
const template = '''
BAN HAMMER LOLOL
You are in good standing.
Weird...
''';
var buf = CodeBuffer();
var document = jael.parseDocument(template, sourceUrl: 'test.jael')!;
var scope = SymbolTable(values: {
'account': _Account(isDisabled: null),
});
const jael.Renderer().render(document, buf, scope);
print(buf);
expect(buf.toString().trim(), 'Weird...');
});
}
const List<_Pokemon> starters = [
_Pokemon('Bulbasaur', 'Grass'),
_Pokemon('Charmander', 'Fire'),
_Pokemon('Squirtle', 'Water'),
];
class _Pokemon {
final String name, type;
const _Pokemon(this.name, this.type);
}
class _Account {
final bool? isDisabled;
_Account({this.isDisabled});
}