github.com/nuvolaris/goja@v0.0.0-20230825100449-967811910c6d/file/file_test.go (about)

     1  package file
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestPosition(t *testing.T) {
     8  	const SRC = `line1
     9  line2
    10  line3`
    11  	f := NewFile("", SRC, 0)
    12  
    13  	tests := []struct {
    14  		offset int
    15  		line   int
    16  		col    int
    17  	}{
    18  		{0, 1, 1},
    19  		{2, 1, 3},
    20  		{2, 1, 3},
    21  		{6, 2, 1},
    22  		{7, 2, 2},
    23  		{12, 3, 1},
    24  		{12, 3, 1},
    25  		{13, 3, 2},
    26  		{13, 3, 2},
    27  		{16, 3, 5},
    28  		{17, 3, 6},
    29  	}
    30  
    31  	for i, test := range tests {
    32  		if p := f.Position(test.offset); p.Line != test.line || p.Column != test.col {
    33  			t.Fatalf("%d. Line: %d, col: %d", i, p.Line, p.Column)
    34  		}
    35  	}
    36  }
    37  
    38  func TestFileConcurrency(t *testing.T) {
    39  	const SRC = `line1
    40  line2
    41  line3`
    42  	f := NewFile("", SRC, 0)
    43  	go func() {
    44  		f.Position(12)
    45  	}()
    46  	f.Position(2)
    47  }
    48  
    49  func TestGetSourceFilename(t *testing.T) {
    50  	tests := []struct {
    51  		source, basename, result string
    52  	}{
    53  		{"test.js", "base.js", "test.js"},
    54  		{"test.js", "../base.js", "../test.js"},
    55  		{"test.js", "/somewhere/base.js", "/somewhere/test.js"},
    56  		{"/test.js", "/somewhere/base.js", "/test.js"},
    57  		{"/test.js", "file:///somewhere/base.js", "file:///test.js"},
    58  		{"file:///test.js", "base.js", "file:///test.js"},
    59  		{"file:///test.js", "/somwehere/base.js", "file:///test.js"},
    60  		{"file:///test.js", "file:///somewhere/base.js", "file:///test.js"},
    61  		{"../test.js", "/somewhere/else/base.js", "/somewhere/test.js"},
    62  		{"../test.js", "file:///somewhere/else/base.js", "file:///somewhere/test.js"},
    63  		{"../test.js", "https://example.com/somewhere/else/base.js", "https://example.com/somewhere/test.js"},
    64  		{"\ntest.js", "base123.js", "test.js"},
    65  		{"\rtest2.js\t\n  ", "base123.js", "test2.js"},
    66  		// TODO find something that won't parse
    67  	}
    68  	for _, test := range tests {
    69  		resultURL := ResolveSourcemapURL(test.basename, test.source)
    70  		result := resultURL.String()
    71  		if result != test.result {
    72  			t.Fatalf("source: %q, basename %q produced %q instead of %q", test.source, test.basename, result, test.result)
    73  		}
    74  	}
    75  }