Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1512)

Unified Diff: tests/lib/convert/latin1_test.dart

Issue 23224014: Add Latin-1 converters to convert library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« sdk/lib/convert/latin1.dart ('K') | « sdk/lib/convert/latin1.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/lib/convert/latin1_test.dart
diff --git a/tests/lib/convert/latin1_test.dart b/tests/lib/convert/latin1_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..a03f5044b077762ea297131d2544bc63ec5c1e12
--- /dev/null
+++ b/tests/lib/convert/latin1_test.dart
@@ -0,0 +1,121 @@
+// Copyright (c) 2012, 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:convert';
+
+var latin1Strings = [
floitsch 2013/08/21 14:07:33 It would be good to create additional tests from t
Lasse Reichstein Nielsen 2013/08/22 08:28:37 Wouldn't it be great if String had an operator*, s
+ "pure ascii",
+ "blåbærgrød",
+ "\x00 edge cases \xff"
+];
+
+var nonLatin1Strings = [
+ "Edge case \u{100}",
+ "Edge case super-BMP \u{10000}"
+];
+
+void main() {
+ testDirectConversions();
+ testChunkedConversions();
+}
+
+void testDirectConversions() {
+ for (var codec in [LATIN1, new Latin1Codec()]) {
+ for (var latin1String in latin1Strings) {
+ List bytes = codec.encoder.convert(latin1String);
+ Expect.listEquals(latin1String.codeUnits.toList(), bytes, latin1String);
+ String roundTripString = codec.decoder.convert(bytes);
+ Expect.equals(latin1String, roundTripString);
+ roundTripString = codec.decode(bytes);
+ Expect.equals(latin1String, roundTripString);
+ }
+
+ for (var nonLatin1String in nonLatin1Strings) {
+ Expect.throws(() {
+ print(codec.encoder.convert(nonLatin1String));
+ }, null, nonLatin1String);
+ }
+ }
+
+ var allowInvalidCodec = new Latin1Codec(allowInvalid: true);
+ var invalidBytes = [0, 1, 0xff, 0xdead, 0];
+ String decoded = allowInvalidCodec.decode(invalidBytes);
+ Expect.equals("\x00\x01\xFF\uFFFD\x00", decoded);
+ decoded = allowInvalidCodec.decoder.convert(invalidBytes);
+ Expect.equals("\x00\x01\xFF\uFFFD\x00", decoded);
+ decoded = LATIN1.decode(invalidBytes, allowInvalid: true);
+ Expect.equals("\x00\x01\xFF\uFFFD\x00", decoded);
+}
+
+List<int> encode(String str, int chunkSize,
+ Converter<String, List<int>> converter) {
+ List<int> bytes = <int>[];
+ ChunkedConversionSink byteSink =
+ new ByteConversionSink.withCallback(bytes.addAll);
+ var stringConversionSink = converter.startChunkedConversion(byteSink);
+ for (int i = 0; i < str.length; i += chunkSize) {
+ if (i + chunkSize <= str.length) {
+ stringConversionSink.add(str.substring(i, i + chunkSize));
+ } else {
+ stringConversionSink.add(str.substring(i));
+ }
+ }
+ stringConversionSink.close();
+ return bytes;
+}
+
+String decode(List<int> bytes, int chunkSize,
+ Converter<List<int>, String> converter) {
+ StringBuffer buf = new StringBuffer();
+ var stringSink =
+ new StringConversionSink.fromStringSink(buf);
+ var byteConversionSink = converter.startChunkedConversion(stringSink);
+ for (int i = 0; i < bytes.length; i += chunkSize) {
+ if (i + chunkSize <= bytes.length) {
+ byteConversionSink.add(bytes.sublist(i, i + chunkSize));
+ } else {
+ byteConversionSink.add(bytes.sublist(i));
+ }
+ }
+ byteConversionSink.close();
+ return buf.toString();
+}
+
+void testChunkedConversions() {
+ // Check encoding.
+ for (var converter in [LATIN1.encoder,
+ new Latin1Codec().encoder,
+ new Latin1Encoder()]) {
+ for (int chunkSize in [1, 2, 5, 50]) {
+ for (var latin1String in latin1Strings) {
+ var units = latin1String.codeUnits.toList();
+ List bytes = encode(latin1String, chunkSize, converter);
+ Expect.listEquals(units, bytes);
+ }
+ for (var nonLatin1String in nonLatin1Strings) {
+ Expect.throws(() {
+ encode(nonLatin1Strings, chunkSize, converter);
+ });
+ }
+ }
+ }
+ // Check decoding.
+ for (var converter in [LATIN1.decoder,
+ new Latin1Codec().decoder,
+ new Latin1Decoder()]) {
+ for (int chunkSize in [1, 2, 5, 50]) {
+ for (var latin1String in latin1Strings) {
+ var units = latin1String.codeUnits.toList();
+ Expect.equals(latin1String, decode(units, chunkSize, converter));
+ }
+ for (var nonLatin1String in nonLatin1Strings) {
+ var units = nonLatin1String.codeUnits.toList();
+ Expect.throws(() {
+ decode(units, chunkSize, converter);
+ });
+ }
+ }
+ }
+}
« sdk/lib/convert/latin1.dart ('K') | « sdk/lib/convert/latin1.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698