file_test.go 1.8 KB

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