Skip to content

Commit d68106f

Browse files
committed
Merge branch 'yaml'
* yaml: Add 'yaml' build tag for conditionally using gopkg.in/yaml.v2 Add YAML() for generating YAML blocks
2 parents 130f1eb + 42b56ca commit d68106f

File tree

7 files changed

+113
-4
lines changed

7 files changed

+113
-4
lines changed

util/tap/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
TESTS = auto check diagnostic failing known skip todo writer
2-
GOPATH = $(CURDIR)/gopath
1+
TESTS = auto check diagnostic failing known skip todo writer yaml
2+
GOPATH ?= $(CURDIR)/gopath
33

44
.PHONY: $(TESTS)
55

@@ -9,8 +9,8 @@ all: $(foreach t,$(TESTS),test/$(t)/test)
99
clean:
1010
rm -f test/*/test
1111

12-
test/%/test: test/%/main.go tap.go
13-
go build -o $@ $<
12+
test/%/test: test/%/*.go tap.go yaml_json.go yaml_yaml.go
13+
go build -o $@ -tags yaml ./test/$*
1414

1515
$(TESTS): %: test/%/test
1616
prove -v -e '' test/$@/test

util/tap/tap.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,15 @@ func (t *T) Diagnostic(message string) {
151151
func (t *T) Diagnosticf(format string, a ...interface{}) {
152152
t.printf("# "+escapeNewlines(format)+"\n", a...)
153153
}
154+
155+
// YAML generates a YAML block from the message.
156+
func (t *T) YAML(message interface{}) error {
157+
bytes, err := yaml(message, " ")
158+
if err != nil {
159+
return err
160+
}
161+
t.printf(" ---\n ")
162+
t.printf(string(bytes))
163+
t.printf(" ...\n")
164+
return nil
165+
}

util/tap/test/yaml/json.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// +build !yaml
2+
3+
package main
4+
5+
const expected = `TAP version 13
6+
1..2
7+
ok 1 - test for anchoring the YAML block
8+
---
9+
{
10+
"code": 3,
11+
"message": "testing YAML blocks"
12+
}
13+
...
14+
`

util/tap/test/yaml/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
8+
tap "github.com/mndrix/tap-go"
9+
)
10+
11+
func main() {
12+
// collect output for comparison later
13+
buf := new(bytes.Buffer)
14+
t := tap.New()
15+
t.Writer = io.MultiWriter(os.Stdout, buf)
16+
17+
t.Header(2)
18+
t.Pass("test for anchoring the YAML block")
19+
message := map[string]interface{}{
20+
"message": "testing YAML blocks",
21+
"code": 3,
22+
}
23+
t.YAML(message)
24+
got := buf.String()
25+
t.Ok(got == expected, "diagnostics gave expected output")
26+
}

util/tap/test/yaml/yaml.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// +build yaml
2+
3+
package main
4+
5+
const expected = `TAP version 13
6+
1..2
7+
ok 1 - test for anchoring the YAML block
8+
---
9+
code: 3
10+
message: testing YAML blocks
11+
...
12+
`

util/tap/yaml_json.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// +build !yaml
2+
3+
package tap
4+
5+
import (
6+
"encoding/json"
7+
)
8+
9+
// yaml serializes a message to YAML. This implementation uses JSON,
10+
// which is a subset of YAML [1] and is implemented by Go's standard
11+
// library.
12+
//
13+
// [1]: http://www.yaml.org/spec/1.2/spec.html#id2759572
14+
func yaml(message interface{}, prefix string) (marshaled []byte, err error) {
15+
marshaled, err = json.MarshalIndent(message, prefix, " ")
16+
if err != nil {
17+
return marshaled, err
18+
}
19+
20+
marshaled = append(marshaled, []byte("\n")...)
21+
return marshaled, err
22+
}

util/tap/yaml_yaml.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// +build yaml
2+
3+
package tap
4+
5+
import (
6+
"bytes"
7+
8+
goyaml "gopkg.in/yaml.v2"
9+
)
10+
11+
// yaml serializes a message to YAML. This implementation uses
12+
// non-JSON YAML, which has better prove support [1].
13+
//
14+
// [1]: https://rt.cpan.org/Public/Bug/Display.html?id=121606
15+
func yaml(message interface{}, prefix string) (marshaled []byte, err error) {
16+
marshaled, err = goyaml.Marshal(message)
17+
if err != nil {
18+
return marshaled, err
19+
}
20+
21+
marshaled = bytes.Replace(marshaled, []byte("\n"), []byte("\n"+prefix), -1)
22+
return marshaled[:len(marshaled)-len(prefix)], err
23+
}

0 commit comments

Comments
 (0)