2019-10-29 22:05:18 +01:00
|
|
|
// Copyright 2019 Google Inc. Use of this source code is governed by an
|
|
|
|
// MIT-style license that can be found in the LICENSE file or at
|
|
|
|
// https://opensource.org/licenses/MIT.
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
import 'package:sass_embedded/src/util/length_delimited_transformer.dart';
|
|
|
|
|
|
|
|
import 'package:async/async.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
group("encoder", () {
|
2021-05-18 05:57:37 +02:00
|
|
|
late Sink<List<int>> sink;
|
|
|
|
late Stream<List<int>> stream;
|
2019-10-29 22:05:18 +01:00
|
|
|
setUp(() {
|
|
|
|
var controller = StreamController<List<int>>();
|
|
|
|
sink = controller.sink;
|
2019-10-30 20:10:20 +01:00
|
|
|
stream = controller.stream
|
|
|
|
.map((chunk) => Uint8List.fromList(chunk))
|
|
|
|
.transform(lengthDelimitedEncoder);
|
2019-10-29 22:05:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("encodes an empty message", () {
|
|
|
|
sink.add([]);
|
|
|
|
sink.close();
|
2020-12-23 01:06:16 +01:00
|
|
|
expect(collectBytes(stream), completion(equals([0])));
|
2019-10-29 22:05:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("encodes a message of length 1", () {
|
|
|
|
sink.add([123]);
|
|
|
|
sink.close();
|
2020-12-23 01:06:16 +01:00
|
|
|
expect(collectBytes(stream), completion(equals([1, 123])));
|
2019-10-29 22:05:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("encodes a message of length greater than 256", () {
|
|
|
|
sink.add(List.filled(300, 1));
|
|
|
|
sink.close();
|
|
|
|
expect(collectBytes(stream),
|
2020-12-23 01:06:16 +01:00
|
|
|
completion(equals([172, 2, ...List.filled(300, 1)])));
|
2019-10-29 22:05:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("encodes multiple messages", () {
|
|
|
|
sink.add([10]);
|
|
|
|
sink.add([20, 30]);
|
|
|
|
sink.add([40, 50, 60]);
|
|
|
|
sink.close();
|
2020-12-23 01:06:16 +01:00
|
|
|
expect(collectBytes(stream),
|
|
|
|
completion(equals([1, 10, 2, 20, 30, 3, 40, 50, 60])));
|
2019-10-29 22:05:18 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("decoder", () {
|
2021-05-18 05:57:37 +02:00
|
|
|
late Sink<List<int>> sink;
|
|
|
|
late StreamQueue<Uint8List> queue;
|
2019-10-29 22:05:18 +01:00
|
|
|
setUp(() {
|
|
|
|
var controller = StreamController<List<int>>();
|
|
|
|
sink = controller.sink;
|
|
|
|
queue = StreamQueue(controller.stream.transform(lengthDelimitedDecoder));
|
|
|
|
});
|
|
|
|
|
|
|
|
group("decodes an empty message", () {
|
|
|
|
test("from a single chunk", () {
|
2020-12-23 01:06:16 +01:00
|
|
|
sink.add([0]);
|
2019-10-29 22:05:18 +01:00
|
|
|
expect(queue, emits(isEmpty));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("from a chunk that contains more data", () {
|
2020-12-23 01:06:16 +01:00
|
|
|
sink.add([0, 1, 100]);
|
2019-10-29 22:05:18 +01:00
|
|
|
expect(queue, emits(isEmpty));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("decodes a longer message", () {
|
|
|
|
test("from a single chunk", () {
|
2020-12-23 01:06:16 +01:00
|
|
|
sink.add([172, 2, ...List.filled(300, 1)]);
|
|
|
|
expect(queue, emits(List.filled(300, 1)));
|
2019-10-29 22:05:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("from multiple chunks", () {
|
2021-09-29 02:17:11 +02:00
|
|
|
sink
|
|
|
|
..add([172])
|
|
|
|
..add([2, 1])
|
|
|
|
..add(List.filled(299, 1));
|
2020-12-23 01:06:16 +01:00
|
|
|
expect(queue, emits(List.filled(300, 1)));
|
2019-10-29 22:05:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("from one chunk per byte", () {
|
2020-12-23 01:06:16 +01:00
|
|
|
for (var byte in [172, 2, ...List.filled(300, 1)]) {
|
2019-10-29 22:05:18 +01:00
|
|
|
sink.add([byte]);
|
|
|
|
}
|
2020-12-23 01:06:16 +01:00
|
|
|
expect(queue, emits(List.filled(300, 1)));
|
2019-10-29 22:05:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("from a chunk that contains more data", () {
|
2020-12-23 01:06:16 +01:00
|
|
|
sink.add([172, 2, ...List.filled(300, 1), 1, 10]);
|
2019-10-29 22:05:18 +01:00
|
|
|
expect(queue, emits(List.filled(300, 1)));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("decodes multiple messages", () {
|
|
|
|
test("from single chunk", () {
|
2020-12-23 01:06:16 +01:00
|
|
|
sink.add([4, 1, 2, 3, 4, 2, 101, 102]);
|
2019-10-29 22:05:18 +01:00
|
|
|
expect(queue, emits([1, 2, 3, 4]));
|
|
|
|
expect(queue, emits([101, 102]));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("from multiple chunks", () {
|
2020-12-23 01:06:16 +01:00
|
|
|
sink
|
|
|
|
..add([4])
|
|
|
|
..add([1, 2, 3, 4, 172])
|
|
|
|
..add([2, ...List.filled(300, 1)]);
|
2019-10-29 22:05:18 +01:00
|
|
|
expect(queue, emits([1, 2, 3, 4]));
|
2020-12-23 01:06:16 +01:00
|
|
|
expect(queue, emits(List.filled(300, 1)));
|
2019-10-29 22:05:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("from one chunk per byte", () {
|
2020-12-23 01:06:16 +01:00
|
|
|
for (var byte in [4, 1, 2, 3, 4, 172, 2, ...List.filled(300, 1)]) {
|
2019-10-29 22:05:18 +01:00
|
|
|
sink.add([byte]);
|
|
|
|
}
|
|
|
|
expect(queue, emits([1, 2, 3, 4]));
|
2020-12-23 01:06:16 +01:00
|
|
|
expect(queue, emits(List.filled(300, 1)));
|
2019-10-29 22:05:18 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|