Skip to content

Commit

Permalink
Merge branch 'yaml'
Browse files Browse the repository at this point in the history
* yaml:
  Add 'yaml' build tag for conditionally using gopkg.in/yaml.v2
  Add YAML() for generating YAML blocks
  • Loading branch information
wking committed Dec 3, 2017
2 parents 56cca45 + 4656fa9 commit 629fa40
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TESTS = auto check diagnostic failing known skip todo writer
GOPATH = $(CURDIR)/gopath
TESTS = auto check diagnostic failing known skip todo writer yaml
GOPATH ?= $(CURDIR)/gopath

.PHONY: $(TESTS)

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

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

$(TESTS): %: test/%/test
prove -v -e '' test/$@/test
12 changes: 12 additions & 0 deletions tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,15 @@ func (t *T) Diagnostic(message string) {
func (t *T) Diagnosticf(format string, a ...interface{}) {
t.printf("# "+escapeNewlines(format)+"\n", a...)
}

// YAML generates a YAML block from the message.
func (t *T) YAML(message interface{}) error {
bytes, err := yaml(message, " ")
if err != nil {
return err
}
t.printf(" ---\n ")
t.printf(string(bytes))
t.printf(" ...\n")
return nil
}
14 changes: 14 additions & 0 deletions test/yaml/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build !yaml

package main

const expected = `TAP version 13
1..2
ok 1 - test for anchoring the YAML block
---
{
"code": 3,
"message": "testing YAML blocks"
}
...
`
26 changes: 26 additions & 0 deletions test/yaml/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"bytes"
"io"
"os"

tap "github.com/mndrix/tap-go"
)

func main() {
// collect output for comparison later
buf := new(bytes.Buffer)
t := tap.New()
t.Writer = io.MultiWriter(os.Stdout, buf)

t.Header(2)
t.Pass("test for anchoring the YAML block")
message := map[string]interface{}{
"message": "testing YAML blocks",
"code": 3,
}
t.YAML(message)
got := buf.String()
t.Ok(got == expected, "diagnostics gave expected output")
}
12 changes: 12 additions & 0 deletions test/yaml/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build yaml

package main

const expected = `TAP version 13
1..2
ok 1 - test for anchoring the YAML block
---
code: 3
message: testing YAML blocks
...
`
22 changes: 22 additions & 0 deletions yaml_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// +build !yaml

package tap

import (
"encoding/json"
)

// yaml serializes a message to YAML. This implementation uses JSON,
// which is a subset of YAML [1] and is implemented by Go's standard
// library.
//
// [1]: http://www.yaml.org/spec/1.2/spec.html#id2759572
func yaml(message interface{}, prefix string) (marshaled []byte, err error) {
marshaled, err = json.MarshalIndent(message, prefix, " ")
if err != nil {
return marshaled, err
}

marshaled = append(marshaled, []byte("\n")...)
return marshaled, err
}
23 changes: 23 additions & 0 deletions yaml_yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// +build yaml

package tap

import (
"bytes"

goyaml "gopkg.in/yaml.v2"
)

// yaml serializes a message to YAML. This implementation uses
// non-JSON YAML, which has better prove support [1].
//
// [1]: https://rt.cpan.org/Public/Bug/Display.html?id=121606
func yaml(message interface{}, prefix string) (marshaled []byte, err error) {
marshaled, err = goyaml.Marshal(message)
if err != nil {
return marshaled, err
}

marshaled = bytes.Replace(marshaled, []byte("\n"), []byte("\n"+prefix), -1)
return marshaled[:len(marshaled)-len(prefix)], err
}

0 comments on commit 629fa40

Please sign in to comment.