ce4c83ff
wxy
初始提交
|
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
"use strict"
const TransformableString = require("../TransformableString")
it("should be a function", () => {
expect(typeof TransformableString).toBe("function")
})
describe("toString", () => {
it("should return the original string if no transform are made", () => {
const ts = new TransformableString("foo")
expect(ts.toString()).toBe("foo")
})
})
describe("replace", () => {
it("should replace a slice", () => {
const ts = new TransformableString("foo")
ts.replace(1, 2, "OO")
expect(ts.toString()).toBe("fOOo")
})
it("should throw if trying to replace the same thing twice", () => {
const ts = new TransformableString("abcd")
ts.replace(1, 3, "OO")
expect(() => ts.replace(2, 4, "OO")).toThrow()
expect(() => ts.replace(0, 2, "OO")).toThrow()
})
it("should replace adjacent slices", () => {
const ts = new TransformableString("abcde")
ts.replace(2, 3, "OO")
ts.replace(3, 4, "MM")
ts.replace(1, 2, "NN")
expect(ts.toString()).toBe("aNNOOMMe")
})
})
describe("originalIndex", () => {
it("should return the same index if nothing changed", () => {
const ts = new TransformableString("abcde")
expect(ts.originalIndex(0)).toBe(0)
expect(ts.originalIndex(1)).toBe(1)
expect(ts.originalIndex(4)).toBe(4)
})
it("should throw if the index is invalid", () => {
const ts = new TransformableString("abcde")
expect(() => ts.originalIndex(-1)).toThrow()
expect(() => ts.originalIndex(6)).toThrow()
})
it("should return the original index of a string with removed parts", () => {
const ts = new TransformableString("abcde")
ts.replace(1, 2, "")
ts.replace(3, 4, "")
expect(ts.toString()).toBe("ace")
expect(ts.originalIndex(0)).toBe(0) // a
expect(ts.originalIndex(1)).toBe(2) // c
expect(ts.originalIndex(2)).toBe(4) // e
expect(ts.originalIndex(3)).toBe(5) // index directly after the end is allowed
expect(() => ts.originalIndex(4)).toThrow()
})
it("should return the original index of a string with added parts", () => {
const ts = new TransformableString("ace")
ts.replace(1, 1, "b")
ts.replace(2, 2, "d")
expect(ts.toString()).toBe("abcde")
expect(ts.originalIndex(0)).toBe(0) // a
expect(ts.originalIndex(1)).toBe(undefined)
expect(ts.originalIndex(2)).toBe(1) // c
expect(ts.originalIndex(3)).toBe(undefined)
expect(ts.originalIndex(4)).toBe(2) // e
expect(ts.originalIndex(5)).toBe(3) // index directly after the end is allowed
expect(() => ts.originalIndex(6)).toThrow()
})
it("should return the original index of a string with added parts (2)", () => {
const ts = new TransformableString("ab")
ts.replace(1, 1, "XX")
expect(ts.toString()).toBe("aXXb")
expect(ts.originalIndex(0)).toBe(0) // a
expect(ts.originalIndex(1)).toBe(undefined)
expect(ts.originalIndex(2)).toBe(undefined)
expect(ts.originalIndex(3)).toBe(1) // b
})
it("should return the last index of the last block if the index is after the end", () => {
const ts = new TransformableString("abcd")
ts.replace(2, 4, "X")
expect(ts.toString()).toBe("abX")
expect(ts.originalIndex(0)).toBe(0) // a
expect(ts.originalIndex(1)).toBe(1) // b
expect(ts.originalIndex(2)).toBe(undefined)
expect(ts.originalIndex(3)).toBe(3) // c
})
})
describe("originalLocation", () => {
it("should return the same location if nothing changed", () => {
const ts = new TransformableString("aaaa\nbbbb\ncccc")
expect(ts.originalLocation({ line: 1, column: 1 })).toEqual({
line: 1,
column: 1,
})
expect(ts.originalLocation({ line: 1, column: 3 })).toEqual({
line: 1,
column: 3,
})
expect(ts.originalLocation({ line: 2, column: 1 })).toEqual({
line: 2,
column: 1,
})
})
it("should return the original location of a string with removed parts", () => {
const ts = new TransformableString("aaaa\nbbbb\ncccc")
ts.replace(3, 6, "")
expect(ts.toString()).toBe("aaabbb\ncccc")
expect(ts.originalLocation({ line: 1, column: 1 })).toEqual({
line: 1,
column: 1,
})
expect(ts.originalLocation({ line: 1, column: 4 })).toEqual({
line: 2,
column: 2,
})
expect(ts.originalLocation({ line: 2, column: 1 })).toEqual({
line: 3,
column: 1,
})
})
it("should return the original location of a string with added parts", () => {
const ts = new TransformableString("aaaa\nbbbbcccc")
ts.replace(9, 9, "X\nX")
expect(ts.toString()).toBe("aaaa\nbbbbX\nXcccc")
expect(ts.originalLocation({ line: 1, column: 1 })).toEqual({
line: 1,
column: 1,
})
expect(ts.originalLocation({ line: 1, column: 4 })).toEqual({
line: 1,
column: 4,
})
expect(ts.originalLocation({ line: 2, column: 1 })).toEqual({
line: 2,
column: 1,
})
expect(ts.originalLocation({ line: 2, column: 5 })).toEqual(undefined)
})
})
describe("getOriginalLine", () => {
it("returns original lines", () => {
const ts = new TransformableString("aa\nbb\r\ncc")
expect(() => ts.getOriginalLine(0)).toThrow()
expect(ts.getOriginalLine(1)).toEqual("aa")
expect(ts.getOriginalLine(2)).toEqual("bb")
expect(ts.getOriginalLine(3)).toEqual("cc")
expect(() => ts.getOriginalLine(4)).toThrow()
})
})
|