-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceMapTest.cs
110 lines (96 loc) · 4.45 KB
/
SourceMapTest.cs
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
using System.Linq;
using System.Collections.Generic;
using Xunit;
using NSourceMap;
namespace NSourceMap.Tests
{
public class SourceMapTest
{
const string SimpleSourceMap = @"
{
""version"":3,
""file"":""compiled.js"",
""sources"":[""<stdin>""],
""mappings"":""AAAA;AAAC,GAAC;AAAC,GAAC;AAAC,GAAC;AAAC,MAAG,IAAI,EAAC;OAAG;AAAC,SAAK;QAAG,KAAK,EAAC;SAAG;AAAC,WAAK;SAAG;AAAC;;CAAC"",
""sourcesContent"": [""{1;2;3;if(true)100;else if(false)200;else 300;}""]
}";
static readonly SourceMapObject SimpleSourceMapObject =Newtonsoft.Json.JsonConvert.DeserializeObject<SourceMapObject>(SimpleSourceMap);
/* <stdin>:
{1;2;3;if(true)100;else if(false)200;else 300;}
/* compiled.js:
{
1;
2;
3;
if (true) {
100;
} else {
if (false) {
200;
} else {
300;
}
}
}
/* mappings (RAW, interpreted):
0) [0,0,0,0] ([0,0](#0)=>[0,0])
1) [0,0,0,1], [3,0,0,1] ([0,1](#0)=>[1,0]) | ([0,2](#0)=>[1,3])
2) [0,0,0,1], [3,0,0,1] ([0,3](#0)=>[2,0]) | ([0,4](#0)=>[2,3])
3) [0,0,0,1], [3,0,0,1] ([0,5](#0)=>[3,0]) | ([0,6](#0)=>[3,3])
4) [0,0,0,1], [6,0,0,3], [4,0,0,4], [2,0,0,1] ([0,7](#0)=>[4,0]) | ([0,10](#0)=>[4,6]) | ([0,14](#0)=>[4,10]) | ([0,15](#0)=>[4,12])
5) [7,0,0,3] ([0,18](#0)=>[5,7])
6) [0,0,0,1], [9,0,0,5] ([0,19](#0)=>[6,0]) | ([0,24](#0)=>[6,9])
7) [8,0,0,3], [5,0,0,5], [2,0,0,1] ([0,27](#0)=>[7,8]) | ([0,32](#0)=>[7,13]) | ([0,33](#0)=>[7,15])
8) [9,0,0,3] ([0,36](#0)=>[8,9])
9) [0,0,0,1], [11,0,0,5] ([0,37](#0)=>[9,0]) | ([0,42](#0)=>[9,11])
10) [9,0,0,3] ([0,45](#0)=>[10,9])
11) [0,0,0,1] ([0,46](#0)=>[11,0])
13) [1,0,0,1] ([0,47](#0)=>[13,1])
*/
[Fact]
public void SimpleSampleTest()
{
// from http://evanw.github.io/source-map-visualization/
var consumer = new SourceMapConsumer();
consumer.Parse(SimpleSourceMapObject);
Mapping mapping;
mapping = consumer.GetMappingForLine(new FilePosition(1, 3)); // semicolon on line 2
Assert.Equal(0, mapping.Original.Line);
Assert.Equal(2, mapping.Original.Column);
mapping = consumer.GetMappingForLine(new FilePosition(4, 8)); // the true in if(true)
Assert.Equal(0, mapping.Original.Line);
Assert.Equal(10, mapping.Original.Column);
mapping = consumer.GetMappingForLine(new FilePosition(7, 8)); // the false in if(false)
Assert.Equal(0, mapping.Original.Line);
Assert.Equal(27, mapping.Original.Column);
mapping = consumer.GetMappingForLine(new FilePosition(13, 1)); // the last semicolon on line 14
Assert.Equal(0, mapping.Original.Line);
Assert.Equal(47, mapping.Original.Column);
}
[Fact]
public void SimpleGeneratorTest()
{
var generator = new SourceMapGenerator();
var stdin = "<stdin>";
generator.AddMapping(new Mapping(stdin, new FilePosition(0, 0), new FilePosition(0, 0)));
generator.AddMapping(new Mapping(stdin, new FilePosition(0, 1), new FilePosition(1, 0)));
generator.AddMapping(new Mapping(stdin, new FilePosition(0, 2), new FilePosition(1, 3)));
generator.AddMapping(new Mapping(stdin, new FilePosition(0, 3), new FilePosition(2, 0)));
var map = generator.Generate();
Assert.StartsWith("AAAA;AAAC,GAAC;AAAC", map.mappings);
}
[Fact]
public void DecodeEncodeTest()
{
var consumer = new SourceMapConsumer();
consumer.Parse(SimpleSourceMapObject);
var generator = new SourceMapGenerator();
foreach(var m in consumer.Mappings)
{
generator.AddMapping(m);
}
var map = generator.Generate();
Assert.Contains("AAAA;AAAC,GAAC;AAAC,GAAC;AAAC,GAAC;AAAC,MAAG,IAAI,EAAC;OAAG;AAAC,SAAK;QAAG,KAAK,EAAC;SAAG;AAAC,WAAK;SAAG;AAAC;;CAAC", map.mappings);
}
}
}