File: 0_parser.go

package info (click to toggle)
golang-github-smartystreets-gunit 1.2.0%2Bgit20180314.6f0d627-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 200 kB
  • sloc: makefile: 5
file content (40 lines) | stat: -rwxr-xr-x 978 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package scan

import (
	"bytes"
	"errors"
	"go/ast"
	"go/parser"
	"go/token"
)

//////////////////////////////////////////////////////////////////////////////

func scanForFixtures(code string) ([]*fixtureInfo, error) {
	fileset := token.NewFileSet()
	file, err := parser.ParseFile(fileset, "", code, 0)
	if err != nil {
		return nil, err
	}
	// ast.Print(fileset, file) // helps with debugging...
	return findAndListFixtures(file)
}

func findAndListFixtures(file *ast.File) ([]*fixtureInfo, error) {
	collection := newFixtureCollector().Collect(file)
	collection = newFixtureMethodFinder(collection).Find(file)
	return listFixtures(collection)
}

func listFixtures(collection map[string]*fixtureInfo) ([]*fixtureInfo, error) {
	var fixtures []*fixtureInfo
	errorMessage := new(bytes.Buffer)

	for _, fixture := range collection {
		fixtures = append(fixtures, fixture)
	}
	if errorMessage.Len() > 0 {
		return nil, errors.New(errorMessage.String())
	}
	return fixtures, nil
}