mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 19:10:05 +00:00
b05a57d5f3
Change-Id: I04e64359acb95c63d1b5dc5c7fdfaadcb6c55fd9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/130965 Commit-Queue: Bob Nystrom <rnystrom@google.com> Auto-Submit: Bob Nystrom <rnystrom@google.com> Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
90 lines
2.6 KiB
Dart
90 lines
2.6 KiB
Dart
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import "package:expect/expect.dart";
|
|
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
const _NOOP = 'Nothing_to_escape';
|
|
|
|
const _TEST_INPUT = """<A </test> of \xA0 "double" & 'single' values>""";
|
|
|
|
const _OUTPUT_UNKNOWN = '<A </test> of \xA0 "double" '
|
|
'& 'single' values>';
|
|
|
|
const _OUTPUT_ATTRIBUTE =
|
|
"<A </test> of \xA0 "double" & 'single' values>";
|
|
|
|
const _OUTPUT_SQ_ATTRIBUTE =
|
|
'<A </test> of \xA0 "double" & 'single' values>';
|
|
|
|
const _OUTPUT_ELEMENT =
|
|
"""<A </test> of \xA0 "double" & 'single' values>""";
|
|
|
|
void _testMode(HtmlEscape escape, String input, String expected) {
|
|
_testConvert(escape, input, expected);
|
|
_testTransform(escape, input, expected);
|
|
_testChunked(escape, input, expected);
|
|
}
|
|
|
|
void _testChunked(HtmlEscape escape, String input, String expected) {
|
|
var buffer = new StringBuffer();
|
|
|
|
var rootSink = new StringConversionSink.fromStringSink(buffer);
|
|
var sink = escape.startChunkedConversion(rootSink);
|
|
|
|
sink.addSlice("1" + input + "2", 1, input.length + 1, false);
|
|
sink.close();
|
|
|
|
Expect.equals(expected, buffer.toString());
|
|
}
|
|
|
|
void _testConvert(HtmlEscape escape, String input, String expected) {
|
|
var output = escape.convert(input);
|
|
Expect.equals(expected, output);
|
|
}
|
|
|
|
void _testTransform(HtmlEscape escape, String input, String expected) {
|
|
var controller = new StreamController<String>(sync: true);
|
|
|
|
var stream = controller.stream.transform(escape);
|
|
|
|
var done = false;
|
|
int count = 0;
|
|
|
|
void stringData(value) {
|
|
Expect.equals(expected, value);
|
|
count++;
|
|
}
|
|
|
|
void streamClosed() {
|
|
done = true;
|
|
}
|
|
|
|
stream.listen(stringData, onDone: streamClosed);
|
|
|
|
for (var i = 0; i < _COUNT; i++) {
|
|
controller.add(input);
|
|
}
|
|
controller.close();
|
|
Expect.isTrue(done);
|
|
Expect.equals(_COUNT, count);
|
|
}
|
|
|
|
const _COUNT = 3;
|
|
|
|
void main() {
|
|
_testMode(htmlEscape, _TEST_INPUT, _OUTPUT_UNKNOWN);
|
|
_testMode(const HtmlEscape(), _TEST_INPUT, _OUTPUT_UNKNOWN);
|
|
_testMode(
|
|
const HtmlEscape(HtmlEscapeMode.unknown), _TEST_INPUT, _OUTPUT_UNKNOWN);
|
|
_testMode(const HtmlEscape(HtmlEscapeMode.attribute), _TEST_INPUT,
|
|
_OUTPUT_ATTRIBUTE);
|
|
_testMode(const HtmlEscape(HtmlEscapeMode.sqAttribute), _TEST_INPUT,
|
|
_OUTPUT_SQ_ATTRIBUTE);
|
|
_testMode(
|
|
const HtmlEscape(HtmlEscapeMode.element), _TEST_INPUT, _OUTPUT_ELEMENT);
|
|
_testMode(htmlEscape, _NOOP, _NOOP);
|
|
}
|