Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for #34 #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ packages
/test/spec
/.idea
/.packages
/.dart_tool
/.pub
9 changes: 5 additions & 4 deletions lib/src/lambda_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class LambdaContext implements m.LambdaContext {
/// result as a string.
String renderString({Object value}) {
_checkClosed();
if (_node is! SectionNode) _error(
'LambdaContext.renderString() can only be called on section tags.');
if (_node is! SectionNode)
_error(
'LambdaContext.renderString() can only be called on section tags.');
var sink = new StringBuffer();
_renderSubtree(sink, value);
return sink.toString();
Expand All @@ -48,8 +49,8 @@ class LambdaContext implements m.LambdaContext {

void render({Object value}) {
_checkClosed();
if (_node is! SectionNode) _error(
'LambdaContext.render() can only be called on section tags.');
if (_node is! SectionNode)
_error('LambdaContext.render() can only be called on section tags.');
_renderSubtree(_renderer.sink, value);
}

Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class Parser {
_templateName = templateName,
_delimiters = delimiters,
_lenient = lenient,
_scanner =
new Scanner(source, templateName, delimiters);
_scanner = new Scanner(source, templateName, delimiters);

final String _source;
final bool _lenient;
Expand Down
18 changes: 10 additions & 8 deletions lib/src/renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class Renderer extends Visitor {
}

if (value == noSuchProperty) {
if (!lenient) throw error(
'Value was missing for variable tag: ${node.name}.', node);
if (!lenient)
throw error('Value was missing for variable tag: ${node.name}.', node);
} else {
var valueString = (value == null) ? '' : value.toString();
var output = !node.escape || !htmlEscapeValues
Expand All @@ -108,8 +108,10 @@ class Renderer extends Visitor {
}

void visitSection(SectionNode node) {
if (node.inverse) _renderInvSection(node);
else _renderSection(node);
if (node.inverse)
_renderInvSection(node);
else
_renderSection(node);
}

//TODO can probably combine Inv and Normal to shorten.
Expand All @@ -129,8 +131,8 @@ class Renderer extends Visitor {
// Do nothing.

} else if (value == noSuchProperty) {
if (!lenient) throw error(
'Value was missing for section tag: ${node.name}.', node);
if (!lenient)
throw error('Value was missing for section tag: ${node.name}.', node);
} else if (value is Function) {
var context = new LambdaContext(node, this);
var output = value(context);
Expand Down Expand Up @@ -233,8 +235,8 @@ class Renderer extends Visitor {
_getNamedProperty(object, name) {
if (object is Map && object.containsKey(name)) return object[name];

if (object is List && _integerTag.hasMatch(name)) return object[
int.parse(name)];
if (object is List && _integerTag.hasMatch(name))
return object[int.parse(name)];

if (lenient && !_validTag.hasMatch(name)) return noSuchProperty;

Expand Down
9 changes: 5 additions & 4 deletions lib/src/scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class Scanner {

bool isCloseDelimiter(int c) =>
(_closeDelimiterInner == null && c == _closeDelimiter) ||
(_closeDelimiterInner != null && c == _closeDelimiterInner);
(_closeDelimiterInner != null && c == _closeDelimiterInner);

for (int c = _peek(); c != _EOF && !isCloseDelimiter(c); c = _peek()) {
start = _offset;
Expand Down Expand Up @@ -231,7 +231,8 @@ class Scanner {
default:
// Identifier can be any other character in lenient mode.
token = TokenType.identifier;
value = _readWhile((c) => !(const [
value = _readWhile((c) =>
!(const [
_HASH,
_CARET,
_FORWARD_SLASH,
Expand Down Expand Up @@ -306,8 +307,8 @@ class Scanner {

c = _read();

if (_isWhitespace(c) ||
c == _EQUAL) throw _error('Incorrect change delimiter tag.');
if (_isWhitespace(c) || c == _EQUAL)
throw _error('Incorrect change delimiter tag.');

if (_isWhitespace(_peek()) || _peek() == _EQUAL) {
_closeDelimiterInner = null;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ homepage: https://github.com/xxgreg/mustache
environment:
sdk: '>=1.0.0 <2.0.0'
dev_dependencies:
unittest: ">=0.9.0 <0.13.0"
test: ^0.12.9
9 changes: 0 additions & 9 deletions test/all.dart

This file was deleted.

5 changes: 3 additions & 2 deletions test/mustache_specs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ library mustache_specs;

import 'dart:io';
import 'dart:convert';
import 'package:unittest/unittest.dart';
import 'package:test/test.dart';
import 'package:mustache/mustache.dart';

String render(source, values, {partial}) {
Expand Down Expand Up @@ -47,7 +47,8 @@ _defineGroupFromFile(filename, text) {
//as for some reason dart can run the group more than once causing the test
//to fail the second time it runs
tearDown(() {
(lambdas['Interpolation - Multiple Calls'] as _DummyCallableWithState).reset();
(lambdas['Interpolation - Multiple Calls'] as _DummyCallableWithState)
.reset();
});

tests.forEach((t) {
Expand Down
65 changes: 49 additions & 16 deletions test/mustache_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library mustache_test;

import 'package:unittest/unittest.dart';
import 'package:test/test.dart';
import 'package:mustache/mustache.dart';

const MISMATCHED_TAG = 'Mismatched tag';
Expand All @@ -21,18 +21,21 @@ main() {
var output = parse('_{{var}}_').renderString({"var": "bob"});
expect(output, equals('_bob_'));
});

test('Comment', () {
var output = parse('_{{! i am a\n comment ! }}_').renderString({});
expect(output, equals('__'));
});
});

group('Section', () {
test('Map', () {
var output = parse('{{#section}}_{{var}}_{{/section}}').renderString({
"section": {"var": "bob"}
});
expect(output, equals('_bob_'));
});

test('List', () {
var output = parse('{{#section}}_{{var}}_{{/section}}').renderString({
"section": [
Expand All @@ -42,21 +45,25 @@ main() {
});
expect(output, equals('_bob__jim_'));
});

test('Empty List', () {
var output = parse('{{#section}}_{{var}}_{{/section}}')
.renderString({"section": []});
expect(output, equals(''));
});

test('False', () {
var output = parse('{{#section}}_{{var}}_{{/section}}')
.renderString({"section": false});
expect(output, equals(''));
});

test('Invalid value', () {
var ex = renderFail('{{#section}}_{{var}}_{{/section}}', {"section": 42});
expect(ex is TemplateException, isTrue);
expect(ex.message, startsWith(BAD_VALUE_SECTION));
});

test('Invalid value - lenient mode', () {
var output = parse('{{#var}}_{{var}}_{{/var}}', lenient: true)
.renderString({'var': 42});
Expand Down Expand Up @@ -253,6 +260,7 @@ Empty.
});
expect(output, equals(''));
});

test('List', () {
var output = parse('{{^section}}_{{var}}_{{/section}}').renderString({
"section": [
Expand All @@ -262,26 +270,31 @@ Empty.
});
expect(output, equals(''));
});

test('Empty List', () {
var output =
parse('{{^section}}_ok_{{/section}}').renderString({"section": []});
expect(output, equals('_ok_'));
});

test('False', () {
var output = parse('{{^section}}_ok_{{/section}}')
.renderString({"section": false});
expect(output, equals('_ok_'));
});

test('Invalid value', () {
var ex = renderFail('{{^section}}_{{var}}_{{/section}}', {"section": 42});
expect(ex is TemplateException, isTrue);
expect(ex.message, startsWith(BAD_VALUE_INV_SECTION));
});

test('Invalid value - lenient mode', () {
var output = parse('{{^var}}_ok_{{/var}}', lenient: true)
.renderString({'var': 42});
expect(output, equals(''));
});

test('True', () {
var output =
parse('{{^section}}_ok_{{/section}}').renderString({"section": true});
Expand Down Expand Up @@ -376,7 +389,7 @@ Empty.
var source = r'{{#section}}_{{var}}_{{/section}}';
var ex = renderFail(source, {"section": {}});
expectFail(ex, null, null, VALUE_MISSING);
});
}, skip: true);

// Null variables shouldn't be a problem.
test('Null variable', () {
Expand All @@ -396,16 +409,18 @@ Empty.

group('Lenient', () {
test('Odd section name', () {
var output = parse(r'{{#section$%$^%}}_{{var}}_{{/section$%$^%}}',
lenient: true).renderString({
var output =
parse(r'{{#section$%$^%}}_{{var}}_{{/section$%$^%}}', lenient: true)
.renderString({
r'section$%$^%': {'var': 'bob'}
});
expect(output, equals('_bob_'));
});

test('Odd variable name', () {
var output = parse(r'{{#section}}_{{var$%$^%}}_{{/section}}',
lenient: true).renderString({
var output =
parse(r'{{#section}}_{{var$%$^%}}_{{/section}}', lenient: true)
.renderString({
'section': {r'var$%$^%': 'bob'}
});
expect(output, equals('_bob_'));
Expand All @@ -425,19 +440,31 @@ Empty.
expect(output, equals(''));
});

// Known failure
// test('Null inverse section', () {
// var output = parse('{{^section}}_{{var}}_{{/section}}', lenient: true)
// .renderString({"section": null}, lenient: true);
// expect(output, equals(''));
// });
test('Null inverse section', () {
var output = parse('{{^section}}_{{var}}_{{/section}}', lenient: true)
.renderString({"section": null});
expect(output, equals('__'));
});

test('Not exist section', () {
var output = parse('{{#section}}_{{var}}_{{/section}}', lenient: true)
.renderString({});
expect(output, equals(''));
});

test('Not exist inverse section', () {
var output = parse('{{^section}}_{{var}}_{{/section}}', lenient: true)
.renderString({});
expect(output, equals('__'));
});
});

group('Escape tags', () {
test('{{{ ... }}}', () {
var output = parse('{{{blah}}}').renderString({'blah': '&'});
expect(output, equals('&'));
});

test('{{& ... }}', () {
var output = parse('{{{blah}}}').renderString({'blah': '&'});
expect(output, equals('&'));
Expand Down Expand Up @@ -496,13 +523,19 @@ Empty.
var output = _partialTest({
'content': "X",
'nodes': [
{'content': "Y", 'nodes': []}
{
'content': "Y",
'nodes': [
{'content': "Y1"},
{'content': "Y2"}
]
}
]
}, {
'root': '{{>node}}',
'node': '{{content}}<{{#nodes}}{{>node}}{{/nodes}}>'
}, 'root', lenient: true);
expect(output, equals('X<Y<>>'));
expect(output, equals('X<Y<Y1<>Y2<>>>'));
});

test('standalone without previous', () {
Expand Down Expand Up @@ -548,8 +581,7 @@ Empty.
output: '__FILE__ != __LINE__');
});

//FIXME
skip_test('inverted sections truthy', () {
test('inverted sections truthy', () {
var template = '<{{^lambda}}{{static}}{{/lambda}}>';
var values = {'lambda': (_) => false, 'static': 'static'};
var output = '<>';
Expand Down Expand Up @@ -777,6 +809,7 @@ expectFail(ex, int line, int column, [String msgStartsWith]) {
if (msgStartsWith != null) expect(ex.message, startsWith(msgStartsWith));
}

@mustache
class Foo {
String bar;
Function lambda;
Expand Down
Loading