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 5package remap 6 7import ( 8 "go/format" 9 "testing" 10) 11 12func TestErrors(t *testing.T) { 13 tests := []struct { 14 in, out string 15 }{ 16 {"", "x"}, 17 {"x", ""}, 18 {"var x int = 5\n", "var x = 5\n"}, 19 {"these are \"one\" thing", "those are 'another' thing"}, 20 } 21 for _, test := range tests { 22 m, err := Compute([]byte(test.in), []byte(test.out)) 23 if err != nil { 24 t.Logf("Got expected error: %v", err) 25 continue 26 } 27 t.Errorf("Compute(%q, %q): got %+v, wanted error", test.in, test.out, m) 28 } 29} 30 31func TestMatching(t *testing.T) { 32 // The input is a source text that will be rearranged by the formatter. 33 const input = `package foo 34var s int 35func main(){} 36` 37 38 output, err := format.Source([]byte(input)) 39 if err != nil { 40 t.Fatalf("Formatting failed: %v", err) 41 } 42 m, err := Compute([]byte(input), output) 43 if err != nil { 44 t.Fatalf("Unexpected error: %v", err) 45 } 46 47 // Verify that the mapped locations have the same text. 48 for key, val := range m { 49 want := input[key.Pos:key.End] 50 got := string(output[val.Pos:val.End]) 51 if got != want { 52 t.Errorf("Token at %d:%d: got %q, want %q", key.Pos, key.End, got, want) 53 } 54 } 55} 56