-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencode.test.ts
More file actions
142 lines (112 loc) · 4.06 KB
/
encode.test.ts
File metadata and controls
142 lines (112 loc) · 4.06 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2025 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import { beforeEach, describe, it } from "@std/testing/bdd";
import type { ScopeInfo, SourceMapJson } from "../scopes.ts";
import { assertStrictEquals, assertThrows } from "@std/assert";
import { encode } from "./encode.ts";
import { ScopeInfoBuilder } from "../builder/builder.ts";
describe("encode", () => {
let builder: ScopeInfoBuilder;
beforeEach(() => {
builder = new ScopeInfoBuilder();
});
it("throws when sources.length and scopes.length don't match", () => {
const info: ScopeInfo = {
scopes: [null, null],
ranges: [],
};
const map: SourceMapJson = {
version: 3,
sources: ["foo.ts"],
mappings: "",
};
assertThrows(() => encode(info, map));
});
it("returns the provided SourceMapJson object", () => {
const info: ScopeInfo = {
scopes: [null],
ranges: [],
};
const map: SourceMapJson = {
version: 3,
sources: ["foo.ts"],
mappings: "",
};
assertStrictEquals(encode(info, map), map);
});
it("encodes null OriginalScopes correctly", () => {
const info = builder.addNullScope().addNullScope().addNullScope().build();
assertStrictEquals(encode(info).scopes, "A,A,A");
});
it("throws when a child scope' start is not nested properly within its parent", () => {
const info = builder.startScope(10, 0).startScope(0, 0).endScope(20, 0)
.endScope(30, 0).build();
assertThrows(() => encode(info));
});
it("throws when a scopes' end precedes the scopes' start", () => {
const info = builder.startScope(10, 0).endScope(0, 0).build();
assertThrows(() => encode(info));
});
it("throws when a child range' start is not nested properly within its parent", () => {
const info = builder.startRange(10, 0).startRange(0, 0).endRange(20, 0)
.endRange(30, 0).build();
assertThrows(() => encode(info));
});
it("throws when a ranges' end precedes the ranges' start", () => {
const info = builder.startRange(10, 0).endRange(0, 0).build();
assertThrows(() => encode(info));
});
it("throws when a ranges' definition scope is not known to the encoder", () => {
const scope = builder.startScope(0, 0).endScope(10, 0).lastScope()!;
const info = builder.startRange(0, 10).endRange(0, 20).build();
// Set the range's definition as a copy of `scope`.
info.ranges[0].originalScope = { ...scope };
assertThrows(() => encode(info));
});
it("throws when a range has bindings but no definition scope", () => {
const info = builder.startRange(0, 0, { values: ["a", null] }).endRange(
0,
10,
).build();
assertThrows(() => encode(info));
});
it("throws when range bindings don't match with scope variables", () => {
const info = builder.startScope(0, 0, {
key: "key",
variables: ["foo", "bar"],
}).endScope(10, 0).startRange(0, 0, {
scopeKey: "key",
values: ["a", null, "b"],
}).endRange(0, 10).build();
assertThrows(() => encode(info));
});
it("throws when sub-range bindings are not sorted", () => {
const info = builder.startScope(0, 0, { key: "key", variables: ["a"] })
.endScope(10, 0).startRange(0, 0, {
scopeKey: "key",
values: [[{
from: { line: 5, column: 0 },
to: { line: 10, column: 0 },
}, {
from: { line: 0, column: 0 },
to: { line: 5, column: 0 },
}]],
}).endRange(10, 0).build();
assertThrows(() => encode(info));
});
it("throws when sub-range bindings have a gap", () => {
const info = builder.startScope(0, 0, { key: "key", variables: ["a"] })
.endScope(10, 0).startRange(0, 0, {
scopeKey: "key",
values: [[{
from: { line: 0, column: 0 },
to: { line: 4, column: 0 },
}, {
from: { line: 5, column: 0 },
to: { line: 10, column: 0 },
}]],
}).endRange(10, 0).build();
assertThrows(() => encode(info));
});
});