| 1 |
// Copyright 2011 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 |
|
| 5 |
package token |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"encoding/gob" |
| 10 |
"fmt" |
| 11 |
"slices" |
| 12 |
"testing" |
| 13 |
) |
| 14 |
|
| 15 |
// equal returns nil if p and q describe the same file set; |
| 16 |
// otherwise it returns an error describing the discrepancy. |
| 17 |
func equal(p, q *FileSet) error { |
| 18 |
if p == q { |
| 19 |
// avoid deadlock if p == q |
| 20 |
return nil |
| 21 |
} |
| 22 |
|
| 23 |
// not strictly needed for the test |
| 24 |
p.mutex.Lock() |
| 25 |
q.mutex.Lock() |
| 26 |
defer q.mutex.Unlock() |
| 27 |
defer p.mutex.Unlock() |
| 28 |
|
| 29 |
if p.base != q.base { |
| 30 |
return fmt.Errorf("different bases: %d != %d", p.base, q.base) |
| 31 |
} |
| 32 |
|
| 33 |
pfiles := slices.Collect(p.tree.all()) |
| 34 |
qfiles := slices.Collect(q.tree.all()) |
| 35 |
if len(pfiles) != len(qfiles) { |
| 36 |
return fmt.Errorf("different number of files: %d != %d", len(pfiles), len(qfiles)) |
| 37 |
} |
| 38 |
|
| 39 |
for i, f := range pfiles { |
| 40 |
g := qfiles[i] |
| 41 |
if f.name != g.name { |
| 42 |
return fmt.Errorf("different filenames: %q != %q", f.name, g.name) |
| 43 |
} |
| 44 |
if f.base != g.base { |
| 45 |
return fmt.Errorf("different base for %q: %d != %d", f.name, f.base, g.base) |
| 46 |
} |
| 47 |
if f.size != g.size { |
| 48 |
return fmt.Errorf("different size for %q: %d != %d", f.name, f.size, g.size) |
| 49 |
} |
| 50 |
for j, l := range f.lines { |
| 51 |
m := g.lines[j] |
| 52 |
if l != m { |
| 53 |
return fmt.Errorf("different offsets for %q", f.name) |
| 54 |
} |
| 55 |
} |
| 56 |
for j, l := range f.infos { |
| 57 |
m := g.infos[j] |
| 58 |
if l.Offset != m.Offset || l.Filename != m.Filename || l.Line != m.Line { |
| 59 |
return fmt.Errorf("different infos for %q", f.name) |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// we don't care about .last - it's just a cache |
| 65 |
return nil |
| 66 |
} |
| 67 |
|
| 68 |
func checkSerialize(t *testing.T, p *FileSet) { |
| 69 |
var buf bytes.Buffer |
| 70 |
encode := func(x any) error { |
| 71 |
return gob.NewEncoder(&buf).Encode(x) |
| 72 |
} |
| 73 |
if err := p.Write(encode); err != nil { |
| 74 |
t.Errorf("writing fileset failed: %s", err) |
| 75 |
return |
| 76 |
} |
| 77 |
q := NewFileSet() |
| 78 |
decode := func(x any) error { |
| 79 |
return gob.NewDecoder(&buf).Decode(x) |
| 80 |
} |
| 81 |
if err := q.Read(decode); err != nil { |
| 82 |
t.Errorf("reading fileset failed: %s", err) |
| 83 |
return |
| 84 |
} |
| 85 |
if err := equal(p, q); err != nil { |
| 86 |
t.Errorf("filesets not identical: %s", err) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func TestSerialization(t *testing.T) { |
| 91 |
p := NewFileSet() |
| 92 |
checkSerialize(t, p) |
| 93 |
// add some files |
| 94 |
for i := range 10 { |
| 95 |
f := p.AddFile(fmt.Sprintf("file%d", i), p.Base()+i, i*100) |
| 96 |
checkSerialize(t, p) |
| 97 |
// add some lines and alternative file infos |
| 98 |
line := 1000 |
| 99 |
for offs := 0; offs < f.Size(); offs += 40 + i { |
| 100 |
f.AddLine(offs) |
| 101 |
if offs%7 == 0 { |
| 102 |
f.AddLineInfo(offs, fmt.Sprintf("file%d", offs), line) |
| 103 |
line += 33 |
| 104 |
} |
| 105 |
} |
| 106 |
checkSerialize(t, p) |
| 107 |
} |
| 108 |
} |