File: scan.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 (49 lines) | stat: -rw-r--r-- 1,186 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
41
42
43
44
45
46
47
48
49
package scan

import (
	"fmt"
	"io/ioutil"
	"strings"
)

type TestCasePositions map[string]string

func LocateTestCases(filename string) TestCasePositions {
	return gatherTestCaseLineNumbers(parseFixtures(filename))
}

func gatherTestCaseLineNumbers(fixtures []*fixtureInfo) TestCasePositions {
	positions := make(TestCasePositions)
	for _, fixture := range fixtures {
		for _, test := range fixture.TestCases {
			key := fmt.Sprintf("Test%s/%s", fixture.StructName, test.Name)
			value := fmt.Sprintf("%s:%d", fixture.Filename, test.LineNumber)
			positions[key] = value
		}
	}
	return positions
}

func parseFixtures(filename string) (fixtures []*fixtureInfo) {
	source, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil
	}
	batch, err := scanForFixtures(string(source))
	if err != nil {
		return nil
	}
	for _, fixture := range batch {
		fixture.Filename = filename
		fixtures = append(fixtures, fixture)
		for _, test := range fixture.TestCases {
			test.LineNumber = lineNumber(string(source), test.CharacterPosition)
		}
	}
	return fixtures
}

func lineNumber(source string, position int) int {
	source = source[:position+1]
	return strings.Count(source, "\n") + 1
}