-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathload_concat_dill_keeps_source_test.dart
99 lines (85 loc) · 3.42 KB
/
load_concat_dill_keeps_source_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2019, 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.
// Test that 79cc54e51924cd5a6bdc2bd1771f2d0ee7af8899 works as intended.
import 'dart:convert';
import 'dart:typed_data';
import 'package:kernel/binary/ast_from_binary.dart';
import 'package:kernel/binary/ast_to_binary.dart';
import 'package:kernel/kernel.dart';
Uri uri1 = Uri.parse("foo://lib1.dart");
Uri uri2 = Uri.parse("foo://lib2.dart");
void main() {
Library library1 = new Library(uri1, fileUri: uri1);
Library library2 = new Library(uri2, fileUri: uri2);
Procedure p1 = new Procedure(new Name("p1"), ProcedureKind.Method,
new FunctionNode(new ReturnStatement()),
fileUri: uri2);
Procedure p2 = new Procedure(new Name("p2"), ProcedureKind.Method,
new FunctionNode(new ReturnStatement()),
fileUri: uri1);
library1.addProcedure(p1);
library2.addProcedure(p2);
Component component = new Component(libraries: [library1, library2])
..setMainMethodAndMode(null, false);
component.uriToSource[uri1] =
new Source([42, 2 * 42], utf8.encode("source #1"), uri1, uri1);
component.uriToSource[uri2] =
new Source([43, 3 * 43], utf8.encode("source #2"), uri1, uri1);
expectSource(serialize(component), true, true);
Component cPartial1 = new Component(nameRoot: component.root)
..setMainMethodAndMode(null, false)
..libraries.add(library1);
cPartial1.uriToSource[uri1] =
new Source([42, 2 * 42], utf8.encode("source #1"), uri1, uri1);
cPartial1.uriToSource[uri2] =
new Source.emptySource([43, 3 * 43], uri1, uri1);
Uint8List partial1Serialized = serialize(cPartial1);
expectSource(partial1Serialized, true, false);
Component cPartial2 = new Component(nameRoot: component.root)
..setMainMethodAndMode(null, false)
..libraries.add(library2);
cPartial2.uriToSource[uri1] =
new Source.emptySource([42, 2 * 42], uri1, uri1);
cPartial2.uriToSource[uri2] =
new Source([43, 3 * 43], utf8.encode("source #2"), uri1, uri1);
Uint8List partial2Serialized = serialize(cPartial2);
expectSource(partial2Serialized, false, true);
Uint8List combined =
new Uint8List.fromList([...partial1Serialized, ...partial2Serialized]);
expectSource(combined, true, true);
}
void expectSource(Uint8List data, bool expect1, bool expect2) {
BinaryBuilder builder = new BinaryBuilder(data);
Component tmp = new Component();
builder.readComponent(tmp);
if (tmp.uriToSource[uri1] == null) throw "No data for $uri1";
if (tmp.uriToSource[uri2] == null) throw "No data for $uri2";
if (expect1 && tmp.uriToSource[uri1]!.source.isEmpty) {
throw "No data for $uri1";
}
if (!expect1 && tmp.uriToSource[uri1]!.source.isNotEmpty) {
throw "Unexpected data for $uri1";
}
if (expect2 && tmp.uriToSource[uri2]!.source.isEmpty) {
throw "No data for $uri2";
}
if (!expect2 && tmp.uriToSource[uri2]!.source.isNotEmpty) {
throw "Unexpected data for $uri2";
}
}
Uint8List serialize(Component c) {
SimpleSink sink = new SimpleSink();
BinaryPrinter printerWhole = new BinaryPrinter(sink);
printerWhole.writeComponentFile(c);
return sink.builder.takeBytes();
}
class SimpleSink implements Sink<List<int>> {
final BytesBuilder builder = new BytesBuilder();
@override
void add(List<int> chunk) {
builder.add(chunk);
}
@override
void close() {}
}