6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import { compareSegments , offsetSegment , segmentDiff } from '../../src/sourcemaps/segment_marker' ;
9
- import { computeLineLengths } from '../../src/sourcemaps/source_file' ;
9
+ import { computeStartOfLinePositions } from '../../src/sourcemaps/source_file' ;
10
10
11
11
describe ( 'SegmentMarker utils' , ( ) => {
12
12
describe ( 'compareSegments()' , ( ) => {
@@ -34,77 +34,99 @@ describe('SegmentMarker utils', () => {
34
34
35
35
describe ( 'segmentDiff()' , ( ) => {
36
36
it ( 'should return 0 if the segments are the same' , ( ) => {
37
- const lineLengths = computeLineLengths ( 'abcdef\nabcdefghj\nabcdefghijklm\nabcdef' ) ;
38
- expect ( segmentDiff ( lineLengths , { line : 0 , column : 0 } , { line : 0 , column : 0 } ) ) . toEqual ( 0 ) ;
39
- expect ( segmentDiff ( lineLengths , { line : 3 , column : 0 } , { line : 3 , column : 0 } ) ) . toEqual ( 0 ) ;
40
- expect ( segmentDiff ( lineLengths , { line : 0 , column : 5 } , { line : 0 , column : 5 } ) ) . toEqual ( 0 ) ;
41
- expect ( segmentDiff ( lineLengths , { line : 3 , column : 5 } , { line : 3 , column : 5 } ) ) . toEqual ( 0 ) ;
37
+ const startOfLinePositions =
38
+ computeStartOfLinePositions ( 'abcdef\nabcdefghj\nabcdefghijklm\nabcdef' ) ;
39
+ expect ( segmentDiff ( startOfLinePositions , { line : 0 , column : 0 } , { line : 0 , column : 0 } ) )
40
+ . toEqual ( 0 ) ;
41
+ expect ( segmentDiff ( startOfLinePositions , { line : 3 , column : 0 } , { line : 3 , column : 0 } ) )
42
+ . toEqual ( 0 ) ;
43
+ expect ( segmentDiff ( startOfLinePositions , { line : 0 , column : 5 } , { line : 0 , column : 5 } ) )
44
+ . toEqual ( 0 ) ;
45
+ expect ( segmentDiff ( startOfLinePositions , { line : 3 , column : 5 } , { line : 3 , column : 5 } ) )
46
+ . toEqual ( 0 ) ;
42
47
} ) ;
43
48
44
49
it ( 'should return the column difference if the markers are on the same line' , ( ) => {
45
- const lineLengths = computeLineLengths ( 'abcdef\nabcdefghj\nabcdefghijklm\nabcdef' ) ;
46
- expect ( segmentDiff ( lineLengths , { line : 0 , column : 0 } , { line : 0 , column : 3 } ) ) . toEqual ( 3 ) ;
47
- expect ( segmentDiff ( lineLengths , { line : 1 , column : 1 } , { line : 1 , column : 5 } ) ) . toEqual ( 4 ) ;
48
- expect ( segmentDiff ( lineLengths , { line : 2 , column : 5 } , { line : 2 , column : 1 } ) ) . toEqual ( - 4 ) ;
49
- expect ( segmentDiff ( lineLengths , { line : 3 , column : 3 } , { line : 3 , column : 0 } ) ) . toEqual ( - 3 ) ;
50
+ const startOfLinePositions =
51
+ computeStartOfLinePositions ( 'abcdef\nabcdefghj\nabcdefghijklm\nabcdef' ) ;
52
+ expect ( segmentDiff ( startOfLinePositions , { line : 0 , column : 0 } , { line : 0 , column : 3 } ) )
53
+ . toEqual ( 3 ) ;
54
+ expect ( segmentDiff ( startOfLinePositions , { line : 1 , column : 1 } , { line : 1 , column : 5 } ) )
55
+ . toEqual ( 4 ) ;
56
+ expect ( segmentDiff ( startOfLinePositions , { line : 2 , column : 5 } , { line : 2 , column : 1 } ) )
57
+ . toEqual ( - 4 ) ;
58
+ expect ( segmentDiff ( startOfLinePositions , { line : 3 , column : 3 } , { line : 3 , column : 0 } ) )
59
+ . toEqual ( - 3 ) ;
50
60
} ) ;
51
61
52
- it ( 'should return the number of actual characters difference (including newlineLengths ) if not on the same line' ,
62
+ it ( 'should return the number of actual characters difference (including newline markers ) if not on the same line' ,
53
63
( ) => {
54
- let lineLengths : number [ ] ;
64
+ let startOfLinePositions : number [ ] ;
55
65
56
- lineLengths = computeLineLengths ( 'A12345\nB123456789' ) ;
57
- expect ( segmentDiff ( lineLengths , { line : 0 , column : 0 } , { line : 1 , column : 0 } ) )
66
+ startOfLinePositions = computeStartOfLinePositions ( 'A12345\nB123456789' ) ;
67
+ expect ( segmentDiff ( startOfLinePositions , { line : 0 , column : 0 } , { line : 1 , column : 0 } ) )
58
68
. toEqual ( 6 + 1 ) ;
59
69
60
- lineLengths = computeLineLengths ( '012A45\n01234B6789' ) ;
61
- expect ( segmentDiff ( lineLengths , { line : 0 , column : 3 } , { line : 1 , column : 5 } ) )
70
+ startOfLinePositions = computeStartOfLinePositions ( '012A45\n01234B6789' ) ;
71
+ expect ( segmentDiff ( startOfLinePositions , { line : 0 , column : 3 } , { line : 1 , column : 5 } ) )
62
72
. toEqual ( 3 + 1 + 5 ) ;
63
73
64
- lineLengths = computeLineLengths ( '012345\n012345A789\n01234567\nB123456' ) ;
65
- expect ( segmentDiff ( lineLengths , { line : 1 , column : 6 } , { line : 3 , column : 0 } ) )
74
+ startOfLinePositions =
75
+ computeStartOfLinePositions ( '012345\n012345A789\n01234567\nB123456' ) ;
76
+ expect ( segmentDiff ( startOfLinePositions , { line : 1 , column : 6 } , { line : 3 , column : 0 } ) )
66
77
. toEqual ( 4 + 1 + 8 + 1 + 0 ) ;
67
78
68
- lineLengths = computeLineLengths ( '012345\nA123456789\n01234567\n012B456' ) ;
69
- expect ( segmentDiff ( lineLengths , { line : 1 , column : 0 } , { line : 3 , column : 3 } ) )
79
+ startOfLinePositions =
80
+ computeStartOfLinePositions ( '012345\nA123456789\n01234567\n012B456' ) ;
81
+ expect ( segmentDiff ( startOfLinePositions , { line : 1 , column : 0 } , { line : 3 , column : 3 } ) )
70
82
. toEqual ( 10 + 1 + 8 + 1 + 3 ) ;
71
83
72
- lineLengths = computeLineLengths ( '012345\nB123456789\nA1234567\n0123456' ) ;
73
- expect ( segmentDiff ( lineLengths , { line : 2 , column : 0 } , { line : 1 , column : 0 } ) )
84
+ startOfLinePositions =
85
+ computeStartOfLinePositions ( '012345\nB123456789\nA1234567\n0123456' ) ;
86
+ expect ( segmentDiff ( startOfLinePositions , { line : 2 , column : 0 } , { line : 1 , column : 0 } ) )
74
87
. toEqual ( 0 - 1 - 10 + 0 ) ;
75
88
76
- lineLengths = computeLineLengths ( '012345\n0123B56789\n01234567\n012A456' ) ;
77
- expect ( segmentDiff ( lineLengths , { line : 3 , column : 3 } , { line : 1 , column : 4 } ) )
89
+ startOfLinePositions =
90
+ computeStartOfLinePositions ( '012345\n0123B56789\n01234567\n012A456' ) ;
91
+ expect ( segmentDiff ( startOfLinePositions , { line : 3 , column : 3 } , { line : 1 , column : 4 } ) )
78
92
. toEqual ( - 3 - 1 - 8 - 1 - 10 + 4 ) ;
79
93
80
- lineLengths = computeLineLengths ( 'B12345\n0123456789\n0123A567\n0123456' ) ;
81
- expect ( segmentDiff ( lineLengths , { line : 2 , column : 4 } , { line : 0 , column : 0 } ) )
94
+ startOfLinePositions =
95
+ computeStartOfLinePositions ( 'B12345\n0123456789\n0123A567\n0123456' ) ;
96
+ expect ( segmentDiff ( startOfLinePositions , { line : 2 , column : 4 } , { line : 0 , column : 0 } ) )
82
97
. toEqual ( - 4 - 1 - 10 - 1 - 6 + 0 ) ;
83
98
84
- lineLengths = computeLineLengths ( '0123B5\n0123456789\nA1234567\n0123456' ) ;
85
- expect ( segmentDiff ( lineLengths , { line : 2 , column : 0 } , { line : 0 , column : 4 } ) )
99
+ startOfLinePositions =
100
+ computeStartOfLinePositions ( '0123B5\n0123456789\nA1234567\n0123456' ) ;
101
+ expect ( segmentDiff ( startOfLinePositions , { line : 2 , column : 0 } , { line : 0 , column : 4 } ) )
86
102
. toEqual ( 0 - 1 - 10 - 1 - 6 + 4 ) ;
87
103
} ) ;
88
104
} ) ;
89
105
90
106
describe ( 'offsetSegment()' , ( ) => {
91
107
it ( 'should return an identical marker if offset is 0' , ( ) => {
92
- const lineLengths = computeLineLengths ( '012345\n0123456789\n01234567\n0123456' ) ;
108
+ const startOfLinePositions =
109
+ computeStartOfLinePositions ( '012345\n0123456789\r\n01234567\n0123456' ) ;
93
110
const marker = { line : 2 , column : 3 } ;
94
- expect ( offsetSegment ( lineLengths , marker , 0 ) ) . toBe ( marker ) ;
111
+ expect ( offsetSegment ( startOfLinePositions , marker , 0 ) ) . toBe ( marker ) ;
95
112
} ) ;
96
113
97
114
it ( 'should return a new marker offset by the given chars' , ( ) => {
98
- const lineLengths = computeLineLengths ( '012345\n0123456789\n012*4567\n0123456' ) ;
115
+ const startOfLinePositions =
116
+ computeStartOfLinePositions ( '012345\n0123456789\r\n012*4567\n0123456' ) ;
99
117
const marker = { line : 2 , column : 3 } ;
100
- expect ( offsetSegment ( lineLengths , marker , 1 ) ) . toEqual ( { line : 2 , column : 4 } ) ;
101
- expect ( offsetSegment ( lineLengths , marker , 2 ) ) . toEqual ( { line : 2 , column : 5 } ) ;
102
- expect ( offsetSegment ( lineLengths , marker , 4 ) ) . toEqual ( { line : 2 , column : 7 } ) ;
103
- expect ( offsetSegment ( lineLengths , marker , 8 ) ) . toEqual ( { line : 3 , column : 2 } ) ;
104
- expect ( offsetSegment ( lineLengths , marker , - 1 ) ) . toEqual ( { line : 2 , column : 2 } ) ;
105
- expect ( offsetSegment ( lineLengths , marker , - 2 ) ) . toEqual ( { line : 2 , column : 1 } ) ;
106
- expect ( offsetSegment ( lineLengths , marker , - 4 ) ) . toEqual ( { line : 1 , column : 10 } ) ;
107
- expect ( offsetSegment ( lineLengths , marker , - 6 ) ) . toEqual ( { line : 1 , column : 8 } ) ;
118
+ expect ( offsetSegment ( startOfLinePositions , marker , 1 ) ) . toEqual ( { line : 2 , column : 4 } ) ;
119
+ expect ( offsetSegment ( startOfLinePositions , marker , 2 ) ) . toEqual ( { line : 2 , column : 5 } ) ;
120
+ expect ( offsetSegment ( startOfLinePositions , marker , 4 ) ) . toEqual ( { line : 2 , column : 7 } ) ;
121
+ expect ( offsetSegment ( startOfLinePositions , marker , 6 ) ) . toEqual ( { line : 3 , column : 0 } ) ;
122
+ expect ( offsetSegment ( startOfLinePositions , marker , 8 ) ) . toEqual ( { line : 3 , column : 2 } ) ;
123
+ expect ( offsetSegment ( startOfLinePositions , marker , 20 ) ) . toEqual ( { line : 3 , column : 14 } ) ;
124
+ expect ( offsetSegment ( startOfLinePositions , marker , - 1 ) ) . toEqual ( { line : 2 , column : 2 } ) ;
125
+ expect ( offsetSegment ( startOfLinePositions , marker , - 2 ) ) . toEqual ( { line : 2 , column : 1 } ) ;
126
+ expect ( offsetSegment ( startOfLinePositions , marker , - 3 ) ) . toEqual ( { line : 2 , column : 0 } ) ;
127
+ expect ( offsetSegment ( startOfLinePositions , marker , - 4 ) ) . toEqual ( { line : 1 , column : 10 } ) ;
128
+ expect ( offsetSegment ( startOfLinePositions , marker , - 6 ) ) . toEqual ( { line : 1 , column : 8 } ) ;
129
+ expect ( offsetSegment ( startOfLinePositions , marker , - 16 ) ) . toEqual ( { line : 0 , column : 5 } ) ;
108
130
} ) ;
109
131
} ) ;
110
132
} ) ;
0 commit comments