Commit 6fcae0f0 authored by Robert Griesemer's avatar Robert Griesemer

token/position: provide files iterator

R=golang-dev, r2
CC=golang-dev
https://golang.org/cl/3541044
parent 52c9fb6f
......@@ -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
}
......@@ -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)
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment