diff --git a/.analysis_options.yaml b/.analysis_options.yaml new file mode 100644 index 0000000..8178e30 --- /dev/null +++ b/.analysis_options.yaml @@ -0,0 +1,15 @@ +# ------------------------------------------------------------------------------ +# Config for Analyzer +# More: https://pub.dartlang.org/packages/analyzer +# +# Analyzer options: +# https://www.dartlang.org/guides/language/analysis-options +# + +analyzer: + strong-mode: true + + # Exclude wegen https://github.com/dart-lang/sdk/issues/26420 + # und https://github.com/dart-lang/test/issues/436 + # exclude: + # - test/** diff --git a/example/basic.reflectable.dart b/example/basic.reflectable.dart new file mode 100644 index 0000000..cfba632 --- /dev/null +++ b/example/basic.reflectable.dart @@ -0,0 +1,28 @@ +// This file has been generated by the reflectable package. +// https://github.com/dart-lang/reflectable. + +import "dart:core"; +import 'package:mustache/mustache.dart' as prefix0; + +// ignore:unused_import +import "package:reflectable/mirrors.dart" as m; +// ignore:unused_import +import "package:reflectable/src/reflectable_transformer_based.dart" as r; +// ignore:unused_import +import "package:reflectable/reflectable.dart" show isTransformed; + +final _data = {const prefix0.MustacheMirrorsUsedAnnotation(): new r.ReflectorData([], [], [], [], 0, {}, {}, null, [])}; + + +final _memberSymbolMap = null; + +initializeReflectable() { + if (!isTransformed) { + throw new UnsupportedError( + "The transformed code is running with the untransformed " + "reflectable package. Remember to set your package-root to " + "'build/.../packages'."); + } + r.data = _data; + r.memberSymbolMap = _memberSymbolMap; +} diff --git a/example/browser/reflection/.gitignore b/example/browser/reflection/.gitignore new file mode 100644 index 0000000..8afd37e --- /dev/null +++ b/example/browser/reflection/.gitignore @@ -0,0 +1,8 @@ +.buildlog +.DS_Store +.idea +.packages +.pub/ +build/ +packages +pubspec.lock diff --git a/example/browser/reflection/CHANGELOG.md b/example/browser/reflection/CHANGELOG.md new file mode 100644 index 0000000..791a857 --- /dev/null +++ b/example/browser/reflection/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## 0.0.1 + +- Initial version, created by Stagehand diff --git a/example/browser/reflection/LICENSE b/example/browser/reflection/LICENSE new file mode 100644 index 0000000..d15e112 --- /dev/null +++ b/example/browser/reflection/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2016, . +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/example/browser/reflection/README.md b/example/browser/reflection/README.md new file mode 100644 index 0000000..7d26971 --- /dev/null +++ b/example/browser/reflection/README.md @@ -0,0 +1,5 @@ +# Uber-simple web app + +An absolute bare-bones web app. + +Generated by Stagehand. See LICENSE. diff --git a/example/browser/reflection/pubspec.yaml b/example/browser/reflection/pubspec.yaml new file mode 100644 index 0000000..5bfb005 --- /dev/null +++ b/example/browser/reflection/pubspec.yaml @@ -0,0 +1,22 @@ +name: 'mustache_reflection_test' + +version: 0.0.1 +description: An absolute bare-bones web app. +#author: Your Name +#homepage: https://www.example.com + +environment: + sdk: '>=1.0.0 <2.0.0' + +dependencies: + + mustache: + path: ../../../ + +dev_dependencies: + + build_runner: ^0.8.0 + build_test: ^0.10.0 + build_web_compilers: ^0.4.0 + + diff --git a/example/browser/reflection/web/index.html b/example/browser/reflection/web/index.html new file mode 100644 index 0000000..d876040 --- /dev/null +++ b/example/browser/reflection/web/index.html @@ -0,0 +1,25 @@ + + + + + + + + + + + reflection + + + + + + + +
+ + + diff --git a/example/browser/reflection/web/main.dart b/example/browser/reflection/web/main.dart new file mode 100644 index 0000000..c5ef832 --- /dev/null +++ b/example/browser/reflection/web/main.dart @@ -0,0 +1,43 @@ +// Copyright (c) 2016, . All rights reserved. Use of this source code +// is governed by a BSD-style license that can be found in the LICENSE file. + +import 'dart:html' as dom; +import 'package:mustache/mustache.dart'; + +import 'main.reflectable.dart'; + +@mustache +class Version { + final int major; + final int minor; + + Version(this.major, this.minor); +} + +@mustache +class DartLang { + final String name; + final Version version; + final String message; + + DartLang(this.name, this.version, this.message); +} + +void main() { + initializeReflectable(); + + final Template template = new Template( + """ +
+ Language: {{name}}
+ Version: {{version.major}}.{{version.minor}}
+ Comment: {{message}} +
+ """.trim(), lenient: false,htmlEscapeValues: false); + final DartLang language = new DartLang("Dart",new Version(1,13),"Your Dart app is running."); + + final String content = template.renderString(language); + final dom.Element child = new dom.Element.html(content); + + dom.querySelector('#output').append(child); +} diff --git a/example/browser/reflection/web/main.reflectable.dart b/example/browser/reflection/web/main.reflectable.dart new file mode 100644 index 0000000..ab7ad95 --- /dev/null +++ b/example/browser/reflection/web/main.reflectable.dart @@ -0,0 +1,29 @@ +// This file has been generated by the reflectable package. +// https://github.com/dart-lang/reflectable. + +import "dart:core"; +import 'main.dart' as prefix1; +import 'package:mustache/mustache.dart' as prefix0; + +// ignore:unused_import +import "package:reflectable/mirrors.dart" as m; +// ignore:unused_import +import "package:reflectable/src/reflectable_transformer_based.dart" as r; +// ignore:unused_import +import "package:reflectable/reflectable.dart" show isTransformed; + +final _data = {const prefix0.MustacheMirrorsUsedAnnotation(): new r.ReflectorData([new r.NonGenericClassMirrorImpl(r"Version", r".Version", 7, 0, const prefix0.MustacheMirrorsUsedAnnotation(), const [0, 1, 7], const [8, 9, 10, 11, 12, 5, 6], const [], -1, {}, {}, {r"": (b) => (major, minor) => b ? new prefix1.Version(major, minor) : null}, -1, -1, const [-1], null, null), new r.NonGenericClassMirrorImpl(r"DartLang", r".DartLang", 7, 1, const prefix0.MustacheMirrorsUsedAnnotation(), const [2, 3, 4, 16], const [8, 9, 10, 11, 12, 13, 14, 15], const [], -1, {}, {}, {r"": (b) => (name, version, message) => b ? new prefix1.DartLang(name, version, message) : null}, -1, -1, const [-1], null, null)], [new r.VariableMirrorImpl(r"major", 33797, 0, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 2, 2, null), new r.VariableMirrorImpl(r"minor", 33797, 0, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 2, 2, null), new r.VariableMirrorImpl(r"name", 33797, 1, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 3, 3, null), new r.VariableMirrorImpl(r"version", 33797, 1, const prefix0.MustacheMirrorsUsedAnnotation(), 0, 0, 0, null), new r.VariableMirrorImpl(r"message", 33797, 1, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 3, 3, null), new r.ImplicitGetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 0, 2, 2, 5), new r.ImplicitGetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 1, 2, 2, 6), new r.MethodMirrorImpl(r"", 0, 0, -1, 0, 0, const [0, 1], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"==", 131074, null, -1, 4, 4, const [2], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"toString", 131074, null, -1, 3, 3, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"noSuchMethod", 65538, null, null, null, null, const [3], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"hashCode", 131075, null, -1, 2, 2, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"runtimeType", 131075, null, -1, 5, 5, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.ImplicitGetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 2, 3, 3, 13), new r.ImplicitGetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 3, 0, 0, 14), new r.ImplicitGetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 4, 3, 3, 15), new r.MethodMirrorImpl(r"", 0, 1, -1, 1, 1, const [4, 5, 6], const prefix0.MustacheMirrorsUsedAnnotation(), null)], [new r.ParameterMirrorImpl(r"major", 32774, 7, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 2, 2, null, null, null), new r.ParameterMirrorImpl(r"minor", 32774, 7, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 2, 2, null, null, null), new r.ParameterMirrorImpl(r"other", 16390, 8, const prefix0.MustacheMirrorsUsedAnnotation(), null, null, null, null, null, null), new r.ParameterMirrorImpl(r"invocation", 32774, 10, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 6, 6, null, null, null), new r.ParameterMirrorImpl(r"name", 32774, 16, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 3, 3, null, null, null), new r.ParameterMirrorImpl(r"version", 32774, 16, const prefix0.MustacheMirrorsUsedAnnotation(), 0, 0, 0, null, null, null), new r.ParameterMirrorImpl(r"message", 32774, 16, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 3, 3, null, null, null)], [prefix1.Version, prefix1.DartLang, int, String, bool, Type, Invocation], 2, {r"==": (dynamic instance) => (x) => instance == x, r"toString": (dynamic instance) => instance.toString, r"noSuchMethod": (dynamic instance) => instance.noSuchMethod, r"hashCode": (dynamic instance) => instance.hashCode, r"runtimeType": (dynamic instance) => instance.runtimeType, r"major": (dynamic instance) => instance.major, r"minor": (dynamic instance) => instance.minor, r"name": (dynamic instance) => instance.name, r"version": (dynamic instance) => instance.version, r"message": (dynamic instance) => instance.message}, {}, null, [])}; + + +final _memberSymbolMap = null; + +initializeReflectable() { + if (!isTransformed) { + throw new UnsupportedError( + "The transformed code is running with the untransformed " + "reflectable package. Remember to set your package-root to " + "'build/.../packages'."); + } + r.data = _data; + r.memberSymbolMap = _memberSymbolMap; +} diff --git a/example/lambdas.dart b/example/lambdas.dart index c8ffd92..7493fa3 100644 --- a/example/lambdas.dart +++ b/example/lambdas.dart @@ -2,7 +2,7 @@ import 'package:mustache/mustache.dart'; main() { var t = new Template('{{ foo }}'); - var lambda = (_) => 'bar'; + var lambda = (LambdaContext ctx) => 'bar'; var output = t.renderString({'foo': lambda}); // bar print(output); diff --git a/example/lambdas.reflectable.dart b/example/lambdas.reflectable.dart new file mode 100644 index 0000000..cfba632 --- /dev/null +++ b/example/lambdas.reflectable.dart @@ -0,0 +1,28 @@ +// This file has been generated by the reflectable package. +// https://github.com/dart-lang/reflectable. + +import "dart:core"; +import 'package:mustache/mustache.dart' as prefix0; + +// ignore:unused_import +import "package:reflectable/mirrors.dart" as m; +// ignore:unused_import +import "package:reflectable/src/reflectable_transformer_based.dart" as r; +// ignore:unused_import +import "package:reflectable/reflectable.dart" show isTransformed; + +final _data = {const prefix0.MustacheMirrorsUsedAnnotation(): new r.ReflectorData([], [], [], [], 0, {}, {}, null, [])}; + + +final _memberSymbolMap = null; + +initializeReflectable() { + if (!isTransformed) { + throw new UnsupportedError( + "The transformed code is running with the untransformed " + "reflectable package. Remember to set your package-root to " + "'build/.../packages'."); + } + r.data = _data; + r.memberSymbolMap = _memberSymbolMap; +} diff --git a/example/nested_paths.reflectable.dart b/example/nested_paths.reflectable.dart new file mode 100644 index 0000000..cfba632 --- /dev/null +++ b/example/nested_paths.reflectable.dart @@ -0,0 +1,28 @@ +// This file has been generated by the reflectable package. +// https://github.com/dart-lang/reflectable. + +import "dart:core"; +import 'package:mustache/mustache.dart' as prefix0; + +// ignore:unused_import +import "package:reflectable/mirrors.dart" as m; +// ignore:unused_import +import "package:reflectable/src/reflectable_transformer_based.dart" as r; +// ignore:unused_import +import "package:reflectable/reflectable.dart" show isTransformed; + +final _data = {const prefix0.MustacheMirrorsUsedAnnotation(): new r.ReflectorData([], [], [], [], 0, {}, {}, null, [])}; + + +final _memberSymbolMap = null; + +initializeReflectable() { + if (!isTransformed) { + throw new UnsupportedError( + "The transformed code is running with the untransformed " + "reflectable package. Remember to set your package-root to " + "'build/.../packages'."); + } + r.data = _data; + r.memberSymbolMap = _memberSymbolMap; +} diff --git a/example/partials.reflectable.dart b/example/partials.reflectable.dart new file mode 100644 index 0000000..cfba632 --- /dev/null +++ b/example/partials.reflectable.dart @@ -0,0 +1,28 @@ +// This file has been generated by the reflectable package. +// https://github.com/dart-lang/reflectable. + +import "dart:core"; +import 'package:mustache/mustache.dart' as prefix0; + +// ignore:unused_import +import "package:reflectable/mirrors.dart" as m; +// ignore:unused_import +import "package:reflectable/src/reflectable_transformer_based.dart" as r; +// ignore:unused_import +import "package:reflectable/reflectable.dart" show isTransformed; + +final _data = {const prefix0.MustacheMirrorsUsedAnnotation(): new r.ReflectorData([], [], [], [], 0, {}, {}, null, [])}; + + +final _memberSymbolMap = null; + +initializeReflectable() { + if (!isTransformed) { + throw new UnsupportedError( + "The transformed code is running with the untransformed " + "reflectable package. Remember to set your package-root to " + "'build/.../packages'."); + } + r.data = _data; + r.memberSymbolMap = _memberSymbolMap; +} diff --git a/lib/mustache.dart b/lib/mustache.dart index 945c9d2..95a6484 100644 --- a/lib/mustache.dart +++ b/lib/mustache.dart @@ -2,8 +2,19 @@ library mustache; +import 'package:reflectable/reflectable.dart'; import 'src/template.dart' as t; +class MustacheMirrorsUsedAnnotation extends Reflectable { + const MustacheMirrorsUsedAnnotation() : super( + invokingCapability, + reflectedTypeCapability + ); +} + +const MustacheMirrorsUsedAnnotation mustache = const MustacheMirrorsUsedAnnotation(); + + /// Use new Template(source) instead. @deprecated Template parse(String source, {bool lenient: false}) => @@ -69,13 +80,6 @@ abstract class LambdaContext { Object lookup(String variableName); } -const MustacheMirrorsUsedAnnotation mustache = - const MustacheMirrorsUsedAnnotation(); - -class MustacheMirrorsUsedAnnotation { - const MustacheMirrorsUsedAnnotation(); -} - /// [TemplateException] is used to obtain the line and column numbers /// of the token which caused parse or render to fail. abstract class TemplateException implements Exception { diff --git a/lib/src/lambda_context.dart b/lib/src/lambda_context.dart index 8b00d74..2b2e754 100644 --- a/lib/src/lambda_context.dart +++ b/lib/src/lambda_context.dart @@ -72,7 +72,7 @@ class LambdaContext implements m.LambdaContext { if (nodes.isEmpty) return ''; - if (nodes.length == 1 && nodes.first is TextNode) return nodes.first.text; + if (nodes.length == 1 && nodes.first is TextNode) return (nodes.first as TextNode).text; return _renderer.source.substring(node.contentStart, node.contentEnd); } diff --git a/lib/src/parser.dart b/lib/src/parser.dart index 10f8bd7..d742146 100644 --- a/lib/src/parser.dart +++ b/lib/src/parser.dart @@ -147,8 +147,11 @@ class Parser { children.add(new TextNode(token.value, token.start, token.end)); } else { var last = children.removeLast(); - var node = new TextNode(last.text + token.value, last.start, token.end); - children.add(node); + if(last is! TextNode) { + children.add(new TextNode(token.value, token.start, token.end)); + } else { + children.add(new TextNode((last as TextNode).text + token.value, last.start, token.end)); + } } } diff --git a/lib/src/renderer.dart b/lib/src/renderer.dart index 9735195..627c9f3 100644 --- a/lib/src/renderer.dart +++ b/lib/src/renderer.dart @@ -1,8 +1,11 @@ library mustache.renderer; -@MirrorsUsed(metaTargets: const [m.MustacheMirrorsUsedAnnotation]) -import 'dart:mirrors'; +//@MirrorsUsed(metaTargets: const [m.MustacheMirrorsUsedAnnotation]) +//import 'dart:mirrors'; + import 'package:mustache/mustache.dart' as m; +import 'package:reflectable/reflectable.dart'; + import 'lambda_context.dart'; import 'node.dart'; import 'template.dart'; @@ -91,13 +94,15 @@ class Renderer extends Visitor { if (value is Function) { var context = new LambdaContext(node, this, isSection: false); - value = value(context); + final Function callback = value; + value = callback(context); context.close(); } 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 @@ -231,34 +236,63 @@ class Renderer extends Visitor { // objects, this is object.name or object.name(). If no property // by the given name exists, this method returns noSuchProperty. _getNamedProperty(object, name) { - if (object is Map && object.containsKey(name)) return object[name]; + if (object is Map) { + return (object.containsKey(name) ? object[name] : noSuchProperty); + } + + // Checks if object has 'isNotEmpty' and avoids a mirror for this case + if(name == "isNotEmpty") { + try { + return object.isNotEmpty; + + } on NoSuchMethodError { + return noSuchProperty; + } + } - if (object is List && _integerTag.hasMatch(name)) return object[ - int.parse(name)]; - if (lenient && !_validTag.hasMatch(name)) return noSuchProperty; + if (object is List) { + try { + final int index = int.parse(name); + return (_integerTag.hasMatch(name) ? object[index] : noSuchProperty); + } on FormatException { + return noSuchProperty; + } + } - var instance = reflect(object); - var field = instance.type.instanceMembers[new Symbol(name)]; - if (field == null) return noSuchProperty; + if (lenient && !_validTag.hasMatch(name)) { + return noSuchProperty; + } var invocation = null; - if ((field is VariableMirror) || - ((field is MethodMirror) && (field.isGetter))) { - invocation = instance.getField(field.simpleName); - } else if ((field is MethodMirror) && (field.parameters.length == 0)) { - invocation = instance.invoke(field.simpleName, []); + try { + final InstanceMirror instance = m.mustache.reflect(object); + var field = instance.type.instanceMembers[name]; + + if (field == null) { + return noSuchProperty; + } + + if ((field is VariableMirror) || ((field is MethodMirror) && (field.isGetter))) { + invocation = instance.invokeGetter(field.simpleName); + } else if ((field is MethodMirror) && (field.parameters.length == 0)) { + invocation = instance.invoke(field.simpleName, []); + } + } on Error { + return noSuchProperty; } + if (invocation == null) { return noSuchProperty; } - return invocation.reflectee; + + return invocation; } m.TemplateException error(String message, Node node) => new TemplateException(message, templateName, source, node.start); - static const Map _htmlEscapeMap = const { + static const Map _htmlEscapeMap = const { _AMP: '&', _LT: '<', _GT: '>', diff --git a/pubspec.yaml b/pubspec.yaml index 652bc7e..2e0b86f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,9 +1,19 @@ name: mustache version: 0.2.5 + author: Greg Lowe description: Mustache template library homepage: https://github.com/xxgreg/mustache + environment: - sdk: '>=1.0.0 <2.0.0' + sdk: '>=1.8.0 <2.0.0' + +dependencies: + reflectable: '^2.0.0' + dev_dependencies: - unittest: ">=0.9.0 <0.12.0" + test: any + + build_runner: ^0.8.0 + build_test: ^0.10.0 + build_web_compilers: ^0.4.0 diff --git a/test/all.reflectable.dart b/test/all.reflectable.dart new file mode 100644 index 0000000..742c4ab --- /dev/null +++ b/test/all.reflectable.dart @@ -0,0 +1,29 @@ +// This file has been generated by the reflectable package. +// https://github.com/dart-lang/reflectable. + +import "dart:core"; +import 'mustache_test.dart' as prefix1; +import 'package:mustache/mustache.dart' as prefix0; + +// ignore:unused_import +import "package:reflectable/mirrors.dart" as m; +// ignore:unused_import +import "package:reflectable/src/reflectable_transformer_based.dart" as r; +// ignore:unused_import +import "package:reflectable/reflectable.dart" show isTransformed; + +final _data = {const prefix0.MustacheMirrorsUsedAnnotation(): new r.ReflectorData([new r.NonGenericClassMirrorImpl(r"Foo", r"mustache_test.Foo", 7, 0, const prefix0.MustacheMirrorsUsedAnnotation(), const [0, 1, 2, 7], const [8, 9, 10, 11, 12, 2, 3, 4, 5, 6], const [], -1, {}, {}, {r"": (b) => () => b ? new prefix1.Foo() : null}, -1, -1, const [-1], null, null)], [new r.VariableMirrorImpl(r"bar", 32773, 0, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 1, 1, null), new r.VariableMirrorImpl(r"lambda", 32773, 0, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 2, 2, null), new r.MethodMirrorImpl(r"jim", 65538, 0, null, null, null, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.ImplicitGetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 0, 1, 1, 3), new r.ImplicitSetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 0, 1, 1, 4), new r.ImplicitGetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 1, 2, 2, 5), new r.ImplicitSetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 1, 2, 2, 6), new r.MethodMirrorImpl(r"", 64, 0, -1, 0, 0, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"==", 131074, null, -1, 3, 3, const [2], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"toString", 131074, null, -1, 1, 1, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"noSuchMethod", 65538, null, null, null, null, const [3], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"hashCode", 131075, null, -1, 4, 4, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"runtimeType", 131075, null, -1, 5, 5, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null)], [new r.ParameterMirrorImpl(r"_bar", 32870, 4, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 1, 1, null, null, null), new r.ParameterMirrorImpl(r"_lambda", 32870, 6, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 2, 2, null, null, null), new r.ParameterMirrorImpl(r"other", 16390, 8, const prefix0.MustacheMirrorsUsedAnnotation(), null, null, null, null, null, null), new r.ParameterMirrorImpl(r"invocation", 32774, 10, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 6, 6, null, null, null)], [prefix1.Foo, String, Function, bool, int, Type, Invocation], 1, {r"==": (dynamic instance) => (x) => instance == x, r"toString": (dynamic instance) => instance.toString, r"noSuchMethod": (dynamic instance) => instance.noSuchMethod, r"hashCode": (dynamic instance) => instance.hashCode, r"runtimeType": (dynamic instance) => instance.runtimeType, r"jim": (dynamic instance) => instance.jim, r"bar": (dynamic instance) => instance.bar, r"lambda": (dynamic instance) => instance.lambda}, {r"bar=": (dynamic instance, value) => instance.bar = value, r"lambda=": (dynamic instance, value) => instance.lambda = value}, null, [])}; + + +final _memberSymbolMap = null; + +initializeReflectable() { + if (!isTransformed) { + throw new UnsupportedError( + "The transformed code is running with the untransformed " + "reflectable package. Remember to set your package-root to " + "'build/.../packages'."); + } + r.data = _data; + r.memberSymbolMap = _memberSymbolMap; +} diff --git a/test/mustache_specs.dart b/test/mustache_specs.dart index 01c5144..ad45ee5 100644 --- a/test/mustache_specs.dart +++ b/test/mustache_specs.dart @@ -7,9 +7,12 @@ library mustache_specs; import 'dart:io'; import 'dart:convert'; -import 'package:unittest/unittest.dart'; +import 'package:test/test.dart'; + import 'package:mustache/mustache.dart'; +import 'mustache_specs.reflectable.dart'; + String render(source, values, {partial}) { var resolver = null; resolver = (name) { @@ -22,6 +25,8 @@ String render(source, values, {partial}) { } main() { + initializeReflectable(); + defineTests(); } @@ -46,7 +51,8 @@ _defineGroupFromFile(filename, text) { //Make sure that we reset the state of the Interpolation - Multiple Calls test //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'].reset()); + + // tearDown(() => lambdas['Interpolation - Multiple Calls'].reset()); tests.forEach((t) { var testDescription = new StringBuffer(t['name']); @@ -104,7 +110,7 @@ class _DummyCallableWithState { Function wrapLambda(Function f) => (LambdaContext ctx) => ctx.renderSource(f(ctx.source).toString()); -var lambdas = { +var lambdas = { 'Interpolation': wrapLambda((t) => 'world'), 'Interpolation - Expansion': wrapLambda((t) => '{{planet}}'), 'Interpolation - Alternate Delimiters': diff --git a/test/mustache_specs.reflectable.dart b/test/mustache_specs.reflectable.dart new file mode 100644 index 0000000..cfba632 --- /dev/null +++ b/test/mustache_specs.reflectable.dart @@ -0,0 +1,28 @@ +// This file has been generated by the reflectable package. +// https://github.com/dart-lang/reflectable. + +import "dart:core"; +import 'package:mustache/mustache.dart' as prefix0; + +// ignore:unused_import +import "package:reflectable/mirrors.dart" as m; +// ignore:unused_import +import "package:reflectable/src/reflectable_transformer_based.dart" as r; +// ignore:unused_import +import "package:reflectable/reflectable.dart" show isTransformed; + +final _data = {const prefix0.MustacheMirrorsUsedAnnotation(): new r.ReflectorData([], [], [], [], 0, {}, {}, null, [])}; + + +final _memberSymbolMap = null; + +initializeReflectable() { + if (!isTransformed) { + throw new UnsupportedError( + "The transformed code is running with the untransformed " + "reflectable package. Remember to set your package-root to " + "'build/.../packages'."); + } + r.data = _data; + r.memberSymbolMap = _memberSymbolMap; +} diff --git a/test/mustache_test.dart b/test/mustache_test.dart index 10dd19a..9f6cb11 100644 --- a/test/mustache_test.dart +++ b/test/mustache_test.dart @@ -1,8 +1,10 @@ library mustache_test; -import 'package:unittest/unittest.dart'; +import 'package:test/test.dart'; import 'package:mustache/mustache.dart'; +import 'mustache_test.reflectable.dart'; + const MISMATCHED_TAG = 'Mismatched tag'; const UNEXPECTED_EOF = 'Unexpected end of input'; const BAD_VALUE_SECTION = 'Invalid value type for section'; @@ -16,6 +18,8 @@ Template parse(String source, {bool lenient: false}) => new Template(source, lenient: lenient); main() { + initializeReflectable(); + group('Basic', () { test('Variable', () { var output = parse('_{{var}}_').renderString({"var": "bob"}); @@ -98,12 +102,12 @@ Empty. '''); expect( t.renderString({ - "section": [1, 2, 3] + "section": [5, 6, 7] }), equals('''
    -
  • 1
  • -
  • 2
  • -
  • 3
  • +
  • 5
  • +
  • 6
  • +
  • 7
''')); expect(t.renderString({"section": []}), equals('Empty.\n')); @@ -549,12 +553,12 @@ Empty. }); //FIXME - skip_test('inverted sections truthy', () { + test('inverted sections truthy', () { var template = '<{{^lambda}}{{static}}{{/lambda}}>'; var values = {'lambda': (_) => false, 'static': 'static'}; var output = '<>'; expect(parse(template).renderString(values), equals(output)); - }); + },skip: "???"); test("seth's use case", () { var template = '<{{#markdown}}{{content}}{{/markdown}}>'; @@ -652,7 +656,7 @@ Empty. }); }); - group('Mirrors', () { + group('Reflectable', () { test('Simple field', () { var output = parse('_{{bar}}_').renderString(new Foo()..bar = 'bob'); expect(output, equals('_bob_')); @@ -668,6 +672,24 @@ Empty. parse('_{{lambda}}_').renderString(new Foo()..lambda = (_) => 'yo'); expect(output, equals('_yo_')); }); + + test('Map', () { + var output = parse('{{#section}}_{{var.bar}}_{{/section}}').renderString({ + "section": {"var": new Foo()..bar = 'bob' } + }); + expect(output, equals('_bob_')); + }); + + test('List', () { + var output = parse('{{#section}}_{{var.bar}}_{{/section}}').renderString({ + "section": [ + {"var": new Foo()..bar = 'bob' }, + {"var": new Foo()..bar = 'jim'} + ] + }); + expect(output, equals('_bob__jim_')); + }); + }); group('Delimiters', () { @@ -777,6 +799,7 @@ expectFail(ex, int line, int column, [String msgStartsWith]) { if (msgStartsWith != null) expect(ex.message, startsWith(msgStartsWith)); } +@mustache class Foo { String bar; Function lambda; diff --git a/test/mustache_test.reflectable.dart b/test/mustache_test.reflectable.dart new file mode 100644 index 0000000..742c4ab --- /dev/null +++ b/test/mustache_test.reflectable.dart @@ -0,0 +1,29 @@ +// This file has been generated by the reflectable package. +// https://github.com/dart-lang/reflectable. + +import "dart:core"; +import 'mustache_test.dart' as prefix1; +import 'package:mustache/mustache.dart' as prefix0; + +// ignore:unused_import +import "package:reflectable/mirrors.dart" as m; +// ignore:unused_import +import "package:reflectable/src/reflectable_transformer_based.dart" as r; +// ignore:unused_import +import "package:reflectable/reflectable.dart" show isTransformed; + +final _data = {const prefix0.MustacheMirrorsUsedAnnotation(): new r.ReflectorData([new r.NonGenericClassMirrorImpl(r"Foo", r"mustache_test.Foo", 7, 0, const prefix0.MustacheMirrorsUsedAnnotation(), const [0, 1, 2, 7], const [8, 9, 10, 11, 12, 2, 3, 4, 5, 6], const [], -1, {}, {}, {r"": (b) => () => b ? new prefix1.Foo() : null}, -1, -1, const [-1], null, null)], [new r.VariableMirrorImpl(r"bar", 32773, 0, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 1, 1, null), new r.VariableMirrorImpl(r"lambda", 32773, 0, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 2, 2, null), new r.MethodMirrorImpl(r"jim", 65538, 0, null, null, null, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.ImplicitGetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 0, 1, 1, 3), new r.ImplicitSetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 0, 1, 1, 4), new r.ImplicitGetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 1, 2, 2, 5), new r.ImplicitSetterMirrorImpl(const prefix0.MustacheMirrorsUsedAnnotation(), 1, 2, 2, 6), new r.MethodMirrorImpl(r"", 64, 0, -1, 0, 0, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"==", 131074, null, -1, 3, 3, const [2], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"toString", 131074, null, -1, 1, 1, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"noSuchMethod", 65538, null, null, null, null, const [3], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"hashCode", 131075, null, -1, 4, 4, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null), new r.MethodMirrorImpl(r"runtimeType", 131075, null, -1, 5, 5, const [], const prefix0.MustacheMirrorsUsedAnnotation(), null)], [new r.ParameterMirrorImpl(r"_bar", 32870, 4, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 1, 1, null, null, null), new r.ParameterMirrorImpl(r"_lambda", 32870, 6, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 2, 2, null, null, null), new r.ParameterMirrorImpl(r"other", 16390, 8, const prefix0.MustacheMirrorsUsedAnnotation(), null, null, null, null, null, null), new r.ParameterMirrorImpl(r"invocation", 32774, 10, const prefix0.MustacheMirrorsUsedAnnotation(), -1, 6, 6, null, null, null)], [prefix1.Foo, String, Function, bool, int, Type, Invocation], 1, {r"==": (dynamic instance) => (x) => instance == x, r"toString": (dynamic instance) => instance.toString, r"noSuchMethod": (dynamic instance) => instance.noSuchMethod, r"hashCode": (dynamic instance) => instance.hashCode, r"runtimeType": (dynamic instance) => instance.runtimeType, r"jim": (dynamic instance) => instance.jim, r"bar": (dynamic instance) => instance.bar, r"lambda": (dynamic instance) => instance.lambda}, {r"bar=": (dynamic instance, value) => instance.bar = value, r"lambda=": (dynamic instance, value) => instance.lambda = value}, null, [])}; + + +final _memberSymbolMap = null; + +initializeReflectable() { + if (!isTransformed) { + throw new UnsupportedError( + "The transformed code is running with the untransformed " + "reflectable package. Remember to set your package-root to " + "'build/.../packages'."); + } + r.data = _data; + r.memberSymbolMap = _memberSymbolMap; +} diff --git a/test/parser_test.dart b/test/parser_test.dart index eb1231e..eb917f0 100644 --- a/test/parser_test.dart +++ b/test/parser_test.dart @@ -1,4 +1,4 @@ -import 'package:unittest/unittest.dart'; +import 'package:test/test.dart'; import 'package:mustache/src/node.dart'; import 'package:mustache/src/parser.dart'; @@ -6,7 +6,11 @@ import 'package:mustache/src/scanner.dart'; import 'package:mustache/src/template_exception.dart'; import 'package:mustache/src/token.dart'; +import 'parser_test.reflectable.dart'; + main() { + initializeReflectable(); + group('Scanner', () { test('scan text', () { var source = 'abc'; @@ -155,13 +159,13 @@ main() { test('parse section', () { var source = 'abc{{#foo}}def{{/foo}}ghi'; var parser = new Parser(source, 'foo', '{{ }}', lenient: false); - var nodes = parser.parse(); + final List nodes = parser.parse(); expectNodes(nodes, [ new TextNode('abc', 0, 3), new SectionNode('foo', 3, 11, '{{ }}'), new TextNode('ghi', 22, 25) ]); - expectNodes(nodes[1].children, [new TextNode('def', 11, 14)]); + expectNodes((nodes[1] as SectionNode).children, [new TextNode('def', 11, 14)]); }); test('parse section standalone tag whitespace', () { @@ -173,7 +177,7 @@ main() { new SectionNode('foo', 4, 12, '{{ }}'), new TextNode('ghi', 26, 29) ]); - expectNodes(nodes[1].children, [new TextNode('def\n', 13, 17)]); + expectNodes((nodes[1] as SectionNode).children, [new TextNode('def\n', 13, 17)]); }); test('parse section standalone tag whitespace consecutive', () { @@ -186,7 +190,7 @@ main() { new SectionNode('foo', 26, 34, '{{ }}'), new TextNode('ghi', 48, 51), ]); - expectNodes(nodes[1].children, [new TextNode('def\n', 13, 17)]); + expectNodes((nodes[1] as SectionNode).children, [new TextNode('def\n', 13, 17)]); }); test('parse section standalone tag whitespace on first line', () { @@ -197,7 +201,7 @@ main() { new SectionNode('foo', 2, 10, '{{ }}'), new TextNode('ghi', 26, 29) ]); - expectNodes(nodes[0].children, [new TextNode('def\n', 13, 17)]); + expectNodes((nodes[0] as SectionNode).children, [new TextNode('def\n', 13, 17)]); }); test('parse section standalone tag whitespace on last line', () { @@ -205,7 +209,7 @@ main() { var parser = new Parser(source, 'foo', '{{ }}', lenient: false); var nodes = parser.parse(); expectNodes(nodes, [new SectionNode('foo', 0, 8, '{{ }}')]); - expectNodes(nodes[0].children, [new TextNode('def\n', 8, 12)]); + expectNodes((nodes[0] as SectionNode).children, [new TextNode('def\n', 8, 12)]); }); test('parse variable newline', () { @@ -228,7 +232,7 @@ main() { new SectionNode('foo', 5, 13, '{{ }}'), new TextNode('ghi', 27, 30) ]); - expectNodes(nodes[1].children, [new TextNode('def\n', 14, 18)]); + expectNodes((nodes[1] as SectionNode).children, [new TextNode('def\n', 14, 18)]); }); test('parse whitespace', () { @@ -253,13 +257,13 @@ main() { var source = '{{= | | =}}<|#lambda|-|/lambda|>'; var parser = new Parser(source, 'foo', '{{ }}', lenient: false); var nodes = parser.parse(); - expect(nodes[1].delimiters, equals('| |')); + expect((nodes[1] as SectionNode).delimiters, equals('| |')); expectNodes(nodes, [ new TextNode('<', 11, 12), new SectionNode('lambda', 12, 21, '| |'), new TextNode('>', 31, 32), ]); - expectNodes(nodes[1].children, [new TextNode('-', 21, 22)]); + expectNodes((nodes[1] as SectionNode).children, [new TextNode('-', 21, 22)]); }); test('corner case strict', () { diff --git a/test/parser_test.reflectable.dart b/test/parser_test.reflectable.dart new file mode 100644 index 0000000..cfba632 --- /dev/null +++ b/test/parser_test.reflectable.dart @@ -0,0 +1,28 @@ +// This file has been generated by the reflectable package. +// https://github.com/dart-lang/reflectable. + +import "dart:core"; +import 'package:mustache/mustache.dart' as prefix0; + +// ignore:unused_import +import "package:reflectable/mirrors.dart" as m; +// ignore:unused_import +import "package:reflectable/src/reflectable_transformer_based.dart" as r; +// ignore:unused_import +import "package:reflectable/reflectable.dart" show isTransformed; + +final _data = {const prefix0.MustacheMirrorsUsedAnnotation(): new r.ReflectorData([], [], [], [], 0, {}, {}, null, [])}; + + +final _memberSymbolMap = null; + +initializeReflectable() { + if (!isTransformed) { + throw new UnsupportedError( + "The transformed code is running with the untransformed " + "reflectable package. Remember to set your package-root to " + "'build/.../packages'."); + } + r.data = _data; + r.memberSymbolMap = _memberSymbolMap; +}