file_test.go 674 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package file
  2. import "testing"
  3. func TestPosition(t *testing.T) {
  4. const SRC = `line1
  5. line2
  6. line3`
  7. f := NewFile("", SRC, 0)
  8. tests := []struct {
  9. offset int
  10. line int
  11. col int
  12. }{
  13. {0, 1, 1},
  14. {2, 1, 3},
  15. {2, 1, 3},
  16. {6, 2, 1},
  17. {7, 2, 2},
  18. {12, 3, 1},
  19. {12, 3, 1},
  20. {13, 3, 2},
  21. {13, 3, 2},
  22. {16, 3, 5},
  23. {17, 3, 6},
  24. }
  25. for i, test := range tests {
  26. if p := f.Position(test.offset); p.Line != test.line || p.Column != test.col {
  27. t.Fatalf("%d. Line: %d, col: %d", i, p.Line, p.Column)
  28. }
  29. }
  30. }
  31. func TestFileConcurrency(t *testing.T) {
  32. const SRC = `line1
  33. line2
  34. line3`
  35. f := NewFile("", SRC, 0)
  36. go func() {
  37. f.Position(12)
  38. }()
  39. f.Position(2)
  40. }