remap_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2017 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package remap
  32. import (
  33. "go/format"
  34. "testing"
  35. )
  36. func TestErrors(t *testing.T) {
  37. tests := []struct {
  38. in, out string
  39. }{
  40. {"", "x"},
  41. {"x", ""},
  42. {"var x int = 5\n", "var x = 5\n"},
  43. {"these are \"one\" thing", "those are 'another' thing"},
  44. }
  45. for _, test := range tests {
  46. m, err := Compute([]byte(test.in), []byte(test.out))
  47. if err != nil {
  48. t.Logf("Got expected error: %v", err)
  49. continue
  50. }
  51. t.Errorf("Compute(%q, %q): got %+v, wanted error", test.in, test.out, m)
  52. }
  53. }
  54. func TestMatching(t *testing.T) {
  55. // The input is a source text that will be rearranged by the formatter.
  56. const input = `package foo
  57. var s int
  58. func main(){}
  59. `
  60. output, err := format.Source([]byte(input))
  61. if err != nil {
  62. t.Fatalf("Formatting failed: %v", err)
  63. }
  64. m, err := Compute([]byte(input), output)
  65. if err != nil {
  66. t.Fatalf("Unexpected error: %v", err)
  67. }
  68. // Verify that the mapped locations have the same text.
  69. for key, val := range m {
  70. want := input[key.Pos:key.End]
  71. got := string(output[val.Pos:val.End])
  72. if got != want {
  73. t.Errorf("Token at %d:%d: got %q, want %q", key.Pos, key.End, got, want)
  74. }
  75. }
  76. }