dart-sdk/tests/lib/convert/utf83_test.dart
Robert Nystrom b05a57d5f3 Migrate tests/lib_2/convert -> tests/lib/convert.
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>
2020-01-16 17:46:02 +00:00

74 lines
2.7 KiB
Dart

// 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.
library utf8_test;
import "package:expect/expect.dart";
import 'dart:convert';
import 'dart:typed_data' show Uint8List;
/// [bytes] transforms the input into some representation (eg. plain list,
/// Uint8List).
test(List<int> bytes(List<int> input)) {
// Test that UTF8-decoder removes leading BOM.
Expect.equals("a", utf8.decode(bytes([0xEF, 0xBB, 0xBF, 0x61])));
Expect.equals("a", utf8.decoder.convert(bytes([0xEF, 0xBB, 0xBF, 0x61])));
Expect.equals(
"a", new Utf8Decoder().convert(bytes([0xEF, 0xBB, 0xBF, 0x61])));
Expect.equals(
"a", utf8.decode(bytes([0xEF, 0xBB, 0xBF, 0x61]), allowMalformed: true));
Expect.equals(
"a",
new Utf8Codec(allowMalformed: true)
.decode(bytes([0xEF, 0xBB, 0xBF, 0x61])));
Expect.equals(
"a",
new Utf8Codec(allowMalformed: true)
.decoder
.convert(bytes([0xEF, 0xBB, 0xBF, 0x61])));
Expect.equals(
"a",
new Utf8Decoder(allowMalformed: true)
.convert(bytes([0xEF, 0xBB, 0xBF, 0x61])));
Expect.equals("", utf8.decode(bytes([0xEF, 0xBB, 0xBF])));
Expect.equals("", utf8.decoder.convert(bytes([0xEF, 0xBB, 0xBF])));
Expect.equals("", new Utf8Decoder().convert(bytes([0xEF, 0xBB, 0xBF])));
Expect.equals(
"", utf8.decode(bytes([0xEF, 0xBB, 0xBF]), allowMalformed: true));
Expect.equals("",
new Utf8Codec(allowMalformed: true).decode(bytes([0xEF, 0xBB, 0xBF])));
Expect.equals(
"",
new Utf8Codec(allowMalformed: true)
.decoder
.convert(bytes([0xEF, 0xBB, 0xBF])));
Expect.equals("",
new Utf8Decoder(allowMalformed: true).convert(bytes([0xEF, 0xBB, 0xBF])));
Expect.equals("a\u{FEFF}", utf8.decode(bytes([0x61, 0xEF, 0xBB, 0xBF])));
Expect.equals(
"a\u{FEFF}", utf8.decoder.convert(bytes([0x61, 0xEF, 0xBB, 0xBF])));
Expect.equals(
"a\u{FEFF}", new Utf8Decoder().convert(bytes([0x61, 0xEF, 0xBB, 0xBF])));
Expect.equals("a\u{FEFF}",
utf8.decode(bytes([0x61, 0xEF, 0xBB, 0xBF]), allowMalformed: true));
Expect.equals(
"a\u{FEFF}",
new Utf8Codec(allowMalformed: true)
.decode(bytes([0x61, 0xEF, 0xBB, 0xBF])));
Expect.equals(
"a\u{FEFF}",
new Utf8Codec(allowMalformed: true)
.decoder
.convert(bytes([0x61, 0xEF, 0xBB, 0xBF])));
Expect.equals(
"a\u{FEFF}",
new Utf8Decoder(allowMalformed: true)
.convert(bytes([0x61, 0xEF, 0xBB, 0xBF])));
}
main() {
test((list) => list);
test((list) => new Uint8List.fromList(list));
}