-
Notifications
You must be signed in to change notification settings - Fork 3
/
generator.go
73 lines (62 loc) · 2.87 KB
/
generator.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package gogiven
import (
"github.com/corbym/gogiven/generator"
"github.com/corbym/gogiven/generator/htmlspec"
"io"
"path/filepath"
"strings"
)
// Generator is a global variable that holds the GoGivensOutputGenerator.
// You can replace the generator with your own if you match the interface here
// and set Generator = new(myFooGenerator) in a method (usually TestMain or init).
// Don't forget to add the call to the generator function in a "func TestMain(testing.M)" method
// in your test package.
// One file per test file will be generated containing output.
var Generator generator.GoGivensOutputGenerator = htmlspec.NewHTMLOutputGenerator()
const indexIdentifier = "index"
// OutputListeners holds a list of listeners which can process the output generated by the GoGivensOutputGenerator.
// By default, the generator.FileOutputGenerator is added. but you can append or replace with your own.
var OutputListeners = []generator.OutputListener{new(generator.FileOutputGenerator)}
func transformFileNameToHeader(fileWithPath string) (header string) {
baseFileName := filepath.Base(fileWithPath)
withTrimmedSuffix := strings.TrimSuffix(baseFileName, ".go")
return strings.Title(strings.Replace(withTrimmedSuffix, "_", " ", -1))
}
// GenerateTestOutput generates the test output. Call this method from TestMain.
// The global var Generator is used to generate the content, and the OutputListeners are iterated and Notified
// of the content that has been generated. When the test meta data has been processed, an index is generated
// and the listeners notified with an identifier of "index" instead of a test file path and name.
func GenerateTestOutput() {
var indexData []generator.IndexData
for _, key := range globalTestContextMap.Keys() {
value, _ := globalTestContextMap.Load(key)
currentTestContext := value.(*TestContext)
tests := currentTestContext.SomeTests()
pageData := generator.NewPageData(
transformFileNameToHeader(currentTestContext.FileName()),
tests.asMapOfSome(),
)
output := Generator.Generate(pageData)
contentType := Generator.ContentType()
indexData = generateIndexData(currentTestContext, contentType, indexData, pageData)
notifyListeners(currentTestContext.FileName(), contentType, output)
}
index := Generator.GenerateIndex(indexData)
notifyListeners(indexIdentifier, Generator.ContentType(), index)
}
func generateIndexData(
currentTestContext *TestContext,
contentType string,
indexData []generator.IndexData,
pageData generator.PageData) []generator.IndexData {
for _, listener := range OutputListeners {
ref := listener.Ref(currentTestContext.fileName, contentType)
indexData = append(indexData, generator.NewIndexData(ref, pageData))
}
return indexData
}
func notifyListeners(fileNameWithPath string, contentType string, output io.Reader) {
for _, listener := range OutputListeners {
listener.Notify(fileNameWithPath, contentType, output)
}
}