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 32 package remap 33 34 import ( 35 "go/format" 36 "testing" 37 ) 38 39 func TestErrors(t *testing.T) { 40 tests := []struct { 41 in, out string 42 }{ 43 {"", "x"}, 44 {"x", ""}, 45 {"var x int = 5\n", "var x = 5\n"}, 46 {"these are \"one\" thing", "those are 'another' thing"}, 47 } 48 for _, test := range tests { 49 m, err := Compute([]byte(test.in), []byte(test.out)) 50 if err != nil { 51 t.Logf("Got expected error: %v", err) 52 continue 53 } 54 t.Errorf("Compute(%q, %q): got %+v, wanted error", test.in, test.out, m) 55 } 56 } 57 58 func TestMatching(t *testing.T) { 59 // The input is a source text that will be rearranged by the formatter. 60 const input = `package foo 61 var s int 62 func main(){} 63 ` 64 65 output, err := format.Source([]byte(input)) 66 if err != nil { 67 t.Fatalf("Formatting failed: %v", err) 68 } 69 m, err := Compute([]byte(input), output) 70 if err != nil { 71 t.Fatalf("Unexpected error: %v", err) 72 } 73 74 // Verify that the mapped locations have the same text. 75 for key, val := range m { 76 want := input[key.Pos:key.End] 77 got := string(output[val.Pos:val.End]) 78 if got != want { 79 t.Errorf("Token at %d:%d: got %q, want %q", key.Pos, key.End, got, want) 80 } 81 } 82 } 83