platform/packages/jael/jael2/test/text/scan_test.dart

107 lines
3.9 KiB
Dart
Raw Normal View History

2017-09-29 22:39:37 +00:00
import 'package:jael/src/ast/token.dart';
import 'package:jael/src/text/scanner.dart';
import 'package:test/test.dart';
import 'common.dart';
2021-04-29 07:21:31 +00:00
void main() {
2017-09-29 22:39:37 +00:00
test('plain html', () {
2019-07-29 22:01:24 +00:00
var tokens = scan('<img src="foo.png" />', sourceUrl: 'test.jael').tokens;
2017-09-29 22:39:37 +00:00
tokens.forEach(print);
expect(tokens, hasLength(7));
expect(tokens[0], isToken(TokenType.lt));
expect(tokens[1], isToken(TokenType.id, 'img'));
expect(tokens[2], isToken(TokenType.id, 'src'));
expect(tokens[3], isToken(TokenType.equals));
expect(tokens[4], isToken(TokenType.string, '"foo.png"'));
expect(tokens[5], isToken(TokenType.slash));
expect(tokens[6], isToken(TokenType.gt));
});
2017-11-28 23:15:33 +00:00
test('single quotes', () {
2019-07-29 22:01:24 +00:00
var tokens = scan('<p>It\'s lit</p>', sourceUrl: 'test.jael').tokens;
2017-11-28 23:15:33 +00:00
tokens.forEach(print);
expect(tokens, hasLength(8));
expect(tokens[0], isToken(TokenType.lt));
expect(tokens[1], isToken(TokenType.id, 'p'));
expect(tokens[2], isToken(TokenType.gt));
expect(tokens[3], isToken(TokenType.text, 'It\'s lit'));
expect(tokens[4], isToken(TokenType.lt));
expect(tokens[5], isToken(TokenType.slash));
expect(tokens[6], isToken(TokenType.id, 'p'));
expect(tokens[7], isToken(TokenType.gt));
});
2017-09-29 22:39:37 +00:00
test('text node', () {
2019-07-29 22:01:24 +00:00
var tokens = scan('<p>Hello\nworld</p>', sourceUrl: 'test.jael').tokens;
2017-09-29 22:39:37 +00:00
tokens.forEach(print);
expect(tokens, hasLength(8));
expect(tokens[0], isToken(TokenType.lt));
expect(tokens[1], isToken(TokenType.id, 'p'));
expect(tokens[2], isToken(TokenType.gt));
expect(tokens[3], isToken(TokenType.text, 'Hello\nworld'));
expect(tokens[4], isToken(TokenType.lt));
expect(tokens[5], isToken(TokenType.slash));
expect(tokens[6], isToken(TokenType.id, 'p'));
expect(tokens[7], isToken(TokenType.gt));
});
test('mixed', () {
2017-10-02 17:29:02 +00:00
var tokens = scan('<ul number=1 + 2>three{{four > five.six}}</ul>',
2019-07-29 22:01:24 +00:00
sourceUrl: 'test.jael')
2017-10-02 17:29:02 +00:00
.tokens;
2017-09-29 22:39:37 +00:00
tokens.forEach(print);
expect(tokens, hasLength(20));
expect(tokens[0], isToken(TokenType.lt));
expect(tokens[1], isToken(TokenType.id, 'ul'));
expect(tokens[2], isToken(TokenType.id, 'number'));
expect(tokens[3], isToken(TokenType.equals));
expect(tokens[4], isToken(TokenType.number, '1'));
expect(tokens[5], isToken(TokenType.plus));
expect(tokens[6], isToken(TokenType.number, '2'));
expect(tokens[7], isToken(TokenType.gt));
expect(tokens[8], isToken(TokenType.text, 'three'));
2018-06-26 15:29:03 +00:00
expect(tokens[9], isToken(TokenType.lDoubleCurly));
2017-09-29 22:39:37 +00:00
expect(tokens[10], isToken(TokenType.id, 'four'));
expect(tokens[11], isToken(TokenType.gt));
expect(tokens[12], isToken(TokenType.id, 'five'));
expect(tokens[13], isToken(TokenType.dot));
expect(tokens[14], isToken(TokenType.id, 'six'));
2018-06-26 15:29:03 +00:00
expect(tokens[15], isToken(TokenType.rDoubleCurly));
2017-09-29 22:39:37 +00:00
expect(tokens[16], isToken(TokenType.lt));
expect(tokens[17], isToken(TokenType.slash));
expect(tokens[18], isToken(TokenType.id, 'ul'));
expect(tokens[19], isToken(TokenType.gt));
});
2017-10-02 17:29:02 +00:00
test('script tag interpolation', () {
var tokens = scan(
'''
2017-10-16 21:01:02 +00:00
<script aria-label="script">
2017-10-02 17:29:02 +00:00
window.alert('a string');
</script>
2018-11-03 00:16:03 +00:00
'''
.trim(),
2019-07-29 22:01:24 +00:00
sourceUrl: 'test.jael',
2018-04-03 18:07:34 +00:00
).tokens;
2017-10-02 17:29:02 +00:00
tokens.forEach(print);
2017-10-16 21:01:02 +00:00
expect(tokens, hasLength(11));
2017-10-02 17:29:02 +00:00
expect(tokens[0], isToken(TokenType.lt));
expect(tokens[1], isToken(TokenType.id, 'script'));
2017-10-16 21:01:02 +00:00
expect(tokens[2], isToken(TokenType.id, 'aria-label'));
expect(tokens[3], isToken(TokenType.equals));
expect(tokens[4], isToken(TokenType.string));
expect(tokens[5], isToken(TokenType.gt));
2017-10-02 17:29:02 +00:00
expect(
2017-10-16 21:01:02 +00:00
tokens[6], isToken(TokenType.text, "\n window.alert('a string');\n"));
expect(tokens[7], isToken(TokenType.lt));
expect(tokens[8], isToken(TokenType.slash));
expect(tokens[9], isToken(TokenType.id, 'script'));
expect(tokens[10], isToken(TokenType.gt));
2017-10-02 17:29:02 +00:00
});
2017-09-29 22:39:37 +00:00
}