File: 1_fixture_collector.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 1,013 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 "go/ast"

type fixtureCollector struct {
	candidates map[string]*fixtureInfo
	fixtures   map[string]*fixtureInfo
}

func newFixtureCollector() *fixtureCollector {
	return &fixtureCollector{
		candidates: make(map[string]*fixtureInfo),
		fixtures:   make(map[string]*fixtureInfo),
	}
}

func (this *fixtureCollector) Collect(file *ast.File) map[string]*fixtureInfo {
	ast.Walk(this, file) // Calls this.Visit(...) recursively which populates this.fixtures
	return this.fixtures
}

func (this *fixtureCollector) Visit(node ast.Node) ast.Visitor {
	switch t := node.(type) {
	case *ast.TypeSpec:
		name := t.Name.Name
		this.candidates[name] = &fixtureInfo{StructName: name}
		return &fixtureValidator{Parent: this, FixtureName: name}
	default:
		return this
	}
}

func (this *fixtureCollector) Validate(fixture string) {
	this.fixtures[fixture] = this.candidates[fixture]
	delete(this.candidates, fixture)
}

func (this *fixtureCollector) Invalidate(fixture string) {
	this.Validate(fixture)
}