File: file_test.go

package info (click to toggle)
golang-github-dop251-goja 0.0~git20250630.0.58d95d8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,264 kB
  • sloc: javascript: 454; perl: 184; makefile: 6; sh: 1
file content (76 lines) | stat: -rw-r--r-- 1,931 bytes parent folder | download
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
package file

import (
	"testing"
)

func TestPosition(t *testing.T) {
	const SRC = `line1
line2
line3`
	f := NewFile("", SRC, 0)

	tests := []struct {
		offset int
		line   int
		col    int
	}{
		{0, 1, 1},
		{2, 1, 3},
		{2, 1, 3},
		{6, 2, 1},
		{7, 2, 2},
		{12, 3, 1},
		{12, 3, 1},
		{13, 3, 2},
		{13, 3, 2},
		{16, 3, 5},
		{17, 3, 6},
	}

	for i, test := range tests {
		if p := f.Position(test.offset); p.Line != test.line || p.Column != test.col {
			t.Fatalf("%d. Line: %d, col: %d", i, p.Line, p.Column)
		}
	}
}

func TestFileConcurrency(t *testing.T) {
	const SRC = `line1
line2
line3`
	f := NewFile("", SRC, 0)
	go func() {
		f.Position(12)
	}()
	f.Position(2)
}

func TestGetSourceFilename(t *testing.T) {
	tests := []struct {
		source, basename, result string
	}{
		{"test.js", "base.js", "test.js"},
		{"test.js", "../base.js", "../test.js"},
		{"test.js", "/somewhere/base.js", "/somewhere/test.js"},
		{"/test.js", "/somewhere/base.js", "/test.js"},
		{"/test.js", "file:///somewhere/base.js", "file:///test.js"},
		{"file:///test.js", "base.js", "file:///test.js"},
		{"file:///test.js", "/somwehere/base.js", "file:///test.js"},
		{"file:///test.js", "file:///somewhere/base.js", "file:///test.js"},
		{"../test.js", "/somewhere/else/base.js", "/somewhere/test.js"},
		{"../test.js", "file:///somewhere/else/base.js", "file:///somewhere/test.js"},
		{"../test.js", "https://example.com/somewhere/else/base.js", "https://example.com/somewhere/test.js"},
		{"\ntest.js", "base123.js", "test.js"},
		{"\rtest2.js\t\n  ", "base123.js", "test2.js"},
		{"z:/file.map", "a.js", "z:/file.map"},
		// TODO find something that won't parse
	}
	for _, test := range tests {
		resultURL := ResolveSourcemapURL(test.basename, test.source)
		result := resultURL.String()
		if result != test.result {
			t.Fatalf("source: %q, basename %q produced %q instead of %q", test.source, test.basename, result, test.result)
		}
	}
}