1
0
mirror of https://github.com/golang/go synced 2024-11-21 13:34:39 -07:00

token/position: provide files iterator

R=golang-dev, r2
CC=golang-dev
https://golang.org/cl/3541044
This commit is contained in:
Robert Griesemer 2010-12-13 17:08:40 -08:00
parent 52c9fb6f3d
commit 6fcae0f044
2 changed files with 40 additions and 0 deletions

View File

@ -385,3 +385,25 @@ func (s *FileSet) AddFile(filename string, base, size int) *File {
s.files = append(s.files, f)
return f
}
// Files returns the files added to the file set.
func (s *FileSet) Files() <-chan *File {
ch := make(chan *File)
go func() {
for i := 0; ; i++ {
var f *File
s.mutex.RLock()
if i < len(s.files) {
f = s.files[i]
}
s.mutex.RUnlock()
if f == nil {
break
}
ch <- f
}
close(ch)
}()
return ch
}

View File

@ -138,3 +138,21 @@ func TestLineInfo(t *testing.T) {
checkPos(t, msg, fset.Position(p), Position{"bar", offs, 42, col})
}
}
func TestFiles(t *testing.T) {
fset := NewFileSet()
for i, test := range tests {
fset.AddFile(test.filename, fset.Base(), test.size)
j := 0
for g := range fset.Files() {
if g.Name() != tests[j].filename {
t.Errorf("expected filename = %s; got %s", tests[j].filename, g.Name())
}
j++
}
if j != i+1 {
t.Errorf("expected %d files; got %d", i+1, j)
}
}
}