location_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package detect
  2. import (
  3. "testing"
  4. )
  5. // TestGetLocation tests the getLocation function.
  6. func TestGetLocation(t *testing.T) {
  7. tests := []struct {
  8. linePairs [][]int
  9. start int
  10. end int
  11. wantLocation Location
  12. }{
  13. {
  14. linePairs: [][]int{
  15. {0, 39},
  16. {40, 55},
  17. {56, 57},
  18. },
  19. start: 35,
  20. end: 38,
  21. wantLocation: Location{
  22. startLine: 1,
  23. startColumn: 36,
  24. endLine: 1,
  25. endColumn: 38,
  26. startLineIndex: 0,
  27. endLineIndex: 40,
  28. },
  29. },
  30. {
  31. linePairs: [][]int{
  32. {0, 39},
  33. {40, 55},
  34. {56, 57},
  35. },
  36. start: 40,
  37. end: 44,
  38. wantLocation: Location{
  39. startLine: 2,
  40. startColumn: 1,
  41. endLine: 2,
  42. endColumn: 4,
  43. startLineIndex: 40,
  44. endLineIndex: 56,
  45. },
  46. },
  47. }
  48. for _, test := range tests {
  49. loc := location(Fragment{newlineIndices: test.linePairs}, []int{test.start, test.end})
  50. if loc != test.wantLocation {
  51. t.Errorf("\nstartLine %d\nstartColumn: %d\nendLine: %d\nendColumn: %d\nstartLineIndex: %d\nendlineIndex %d",
  52. loc.startLine, loc.startColumn, loc.endLine, loc.endColumn, loc.startLineIndex, loc.endLineIndex)
  53. t.Error("got", loc, "want", test.wantLocation)
  54. }
  55. }
  56. }