remap_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package remap
  5. import (
  6. "go/format"
  7. "testing"
  8. )
  9. func TestErrors(t *testing.T) {
  10. tests := []struct {
  11. in, out string
  12. }{
  13. {"", "x"},
  14. {"x", ""},
  15. {"var x int = 5\n", "var x = 5\n"},
  16. {"these are \"one\" thing", "those are 'another' thing"},
  17. }
  18. for _, test := range tests {
  19. m, err := Compute([]byte(test.in), []byte(test.out))
  20. if err != nil {
  21. t.Logf("Got expected error: %v", err)
  22. continue
  23. }
  24. t.Errorf("Compute(%q, %q): got %+v, wanted error", test.in, test.out, m)
  25. }
  26. }
  27. func TestMatching(t *testing.T) {
  28. // The input is a source text that will be rearranged by the formatter.
  29. const input = `package foo
  30. var s int
  31. func main(){}
  32. `
  33. output, err := format.Source([]byte(input))
  34. if err != nil {
  35. t.Fatalf("Formatting failed: %v", err)
  36. }
  37. m, err := Compute([]byte(input), output)
  38. if err != nil {
  39. t.Fatalf("Unexpected error: %v", err)
  40. }
  41. // Verify that the mapped locations have the same text.
  42. for key, val := range m {
  43. want := input[key.Pos:key.End]
  44. got := string(output[val.Pos:val.End])
  45. if got != want {
  46. t.Errorf("Token at %d:%d: got %q, want %q", key.Pos, key.End, got, want)
  47. }
  48. }
  49. }