Skip to content

Commit

Permalink
Removing testify
Browse files Browse the repository at this point in the history
  • Loading branch information
drewstinnett committed Feb 23, 2024
1 parent 75cafee commit beff581
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 262 deletions.
183 changes: 55 additions & 128 deletions formats/gotemplate/gotemplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,144 +2,71 @@ package gotemplate

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestGTOFormatterFormat(t *testing.T) {
f := Formatter{
Template: `{{ printf "%+v" . }}`,
}
got, err := f.Format(struct {
Title string
Year int
}{
Title: "Ghostbusters",
Year: 1985,
})
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, "{Title:Ghostbusters Year:1985}", string(got))
}

func TestEmptyTemplate(t *testing.T) {
f := Formatter{}
got, err := f.Format(struct {
Title string
Year int
}{
Title: "Ghostbusters",
Year: 1985,
})
require.EqualError(t, err, "no Template set for gotemplate")
require.Nil(t, got)
}

func TestGTOFormatter(t *testing.T) {
v := struct {
type movie struct {
Title string
Year int
}{
Title: "Ghostbusters",
Year: 1985,
}
f := Formatter{
Template: "{{ .Title }}",
}
got, err := f.Format(v)
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, "Ghostbusters", string(got))
}

func TestGTOFormatterTemplateError(t *testing.T) {
v := struct {
Title string
Year int
}{
Title: "Ghostbusters",
Year: 1985,
}
f := Formatter{
Template: "{{ .NotExistingField }}",
}
got, err := f.Format(v)
require.Error(t, err)
require.Nil(t, got)
}

func TestGTOFormatterMultiVal(t *testing.T) {
v := []struct {
Title string
Year int
tests := map[string]struct {
given []movie
givenT string
expect string
expectErr string
}{
{
Title: "Ghostbusters",
Year: 1985,
"custom-template": {
given: []movie{
{Title: "Ghostbusters", Year: 1985},
},
givenT: `{{ printf "%+v" . }}`,
expect: "{Title:Ghostbusters Year:1985}",
},
{
Title: "Halloween",
Year: 1978,
"missing-template": {
given: []movie{
{Title: "Ghostbusters"},
},
expectErr: "no Template set for gotemplate",
},
"bad-template": {
given: []movie{
{Title: "Ghostbusters"},
},
givenT: "{{ .NotExistingField }}",
expectErr: `template: item:1:3: executing "item" at <.NotExistingField>: can't evaluate field NotExistingField in type gotemplate.movie`,
},
"multiple-items": {
given: []movie{
{Title: "Ghostbusters"},
{Title: "Halloween"},
},
givenT: "{{ range . }}{{ .Title }}\n{{ end }}",
expect: "Ghostbusters\nHalloween\n",
},
}
f := Formatter{
Template: "{{ range . }}{{ .Title }}\n{{ end }}",
}
got, err := f.Format(v)
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, "Ghostbusters\nHalloween\n", string(got))
}

func TestGTOWithOptsFormatter(t *testing.T) {
v := struct {
Title string
Year int
}{
Title: "Ghostbusters",
Year: 1985,
}
f := Formatter{
Template: "{{ .Title }}",
}
got, err := f.Format(v)
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, "Ghostbusters", string(got))
}

/*
func TestGTOWithOptsFormatterMissingTemplate(t *testing.T) {
f := Formatter{
Template: "",
}
v := struct {
Title string
Year int
}{
Title: "Ghostbusters",
Year: 1985,
}
// No 'template' option
got, err := f.Format(v)
require.Error(t, err)
require.Nil(t, got)
}
*/

func TestFormatWithContext(t *testing.T) {
v := struct {
Title string
Year int
}{
Title: "Ghostbusters",
Year: 1985,
}
f := Formatter{
Template: "{{ .Title }}",
for desc, tt := range tests {
// If not multiple 'givens', only pass in a single non-iteralbe interface
var realGiven any
if len(tt.given) == 1 {
realGiven = tt.given[0]
} else {
realGiven = tt.given
}
got, err := Formatter{
Template: tt.givenT,
}.Format(realGiven)
if tt.expectErr != "" {
if err.Error() != tt.expectErr {
t.Fatalf("%v: expected an error of:\n'%v'\nbut got\n'%v", desc, tt.expectErr, err.Error())
}
} else {
if err != nil {
t.Fatalf("%v: got an error when none was expected: %v", desc, err)
}
if tt.expect != string(got) {
t.Fatalf("%v: expected %v but got %v", desc, tt.expect, string(got))
}
}
}
got, err := f.Format(v)
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, "Ghostbusters", string(got))
}
13 changes: 8 additions & 5 deletions formats/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package json

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestJSONFormatter(t *testing.T) {
Expand All @@ -13,7 +11,12 @@ func TestJSONFormatter(t *testing.T) {
}{
Foo: "bar",
})
require.NoError(t, err)
require.IsType(t, []byte{}, got)
require.Equal(t, string(`{"Foo":"bar"}`), string(got))
if err != nil {
t.Fatalf("got an unexpected error: %v", err)
}
expect := `{"Foo":"bar"}`
gotS := string(got)
if expect != gotS {
t.Fatalf("expected: %v, but got: %v", expect, got)
}
}
16 changes: 9 additions & 7 deletions formats/plain/plain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package plain

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestPlainFormatter(t *testing.T) {
f := Formatter{}
got, err := f.Format(struct {
got, err := Formatter{}.Format(struct {
Foo string
}{
Foo: "bar",
})
require.NoError(t, err)
require.IsType(t, []byte{}, got)
require.Equal(t, string("{Foo:bar}\n"), string(got))
if err != nil {
t.Fatalf("got an unexpected error: %v", err)
}
expect := "{Foo:bar}\n"
gotS := string(got)
if gotS != expect {
t.Fatalf("expected: %v but got %v", expect, got)
}
}
17 changes: 11 additions & 6 deletions formats/registry_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package formats_test

import (
"slices"
"testing"

"github.com/drewstinnett/gout/v2/formats"
_ "github.com/drewstinnett/gout/v2/formats/builtin"
"github.com/stretchr/testify/require"
)

func TestBuiltinRegistry(t *testing.T) {
require.Equal(
t,
[]string{"gotemplate", "json", "plain", "xml", "yaml"},
formats.Names(),
)
got := formats.Names()
formats := []string{"gotemplate", "json", "plain", "xml", "yaml"}
for _, expect := range formats {
if len(got) != len(formats) {
t.Fatalf("expected len to be %v but got %v", len(formats), len(got))
}
if !slices.Contains(got, expect) {
t.Fatalf("expected %v to be in %v", expect, got)
}
}
}
16 changes: 9 additions & 7 deletions formats/xml/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ package xml
import (
"encoding/xml"
"testing"

"github.com/stretchr/testify/require"
)

func TestXMLFormatter(t *testing.T) {
f := Formatter{}
got, err := f.Format(struct {
got, err := Formatter{}.Format(struct {
XMLName xml.Name `xml:"test"`
Foo string `xml:"foo"`
}{
Foo: "bar",
})
require.NoError(t, err)
require.IsType(t, []byte{}, got)
require.Equal(t, string("<test><foo>bar</foo></test>"), string(got))
if err != nil {
t.Fatalf("got unexpected error: %v", err)
}
expect := "<test><foo>bar</foo></test>"
gotS := string(got)
if expect != gotS {
t.Fatalf("expected: %v but got %v", expect, gotS)
}
}
63 changes: 7 additions & 56 deletions formats/yaml/yaml_test.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,22 @@
package yaml

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

type Opts struct {
items map[string]interface{}
}

func (o *Opts) Set(s string, v interface{}) error {
o.items[s] = v
return nil
}

func (o *Opts) Get(s string) (interface{}, error) {
if _, ok := o.items[s]; ok {
return nil, fmt.Errorf("Could not find config item '%s'", s)
}
return o.items[s], nil
}

func TestYAMLFormatter(t *testing.T) {
f := Formatter{}
got, err := f.Format(struct {
Foo string
}{
Foo: "bar",
})
require.NoError(t, err)
require.IsType(t, []byte{}, got)
require.Equal(t, string("foo: bar\n"), string(got))
}

/*
func TestYAMLFormatterWithOpts(t *testing.T) {
f := Formatter{}
got, err := f.FormatWithContext(context.Background(), []struct {
Foo string
}{
{Foo: "bar"},
{Foo: "baz"},
})
require.NoError(t, err)
require.IsType(t, []byte{}, got)
require.Equal(t, string("- foo: bar\n- foo: baz\n"), string(got))
}
*/

/*
func TestYAMLFormatterWithOpts(t *testing.T) {
f := Formatter{}
opts := config.FormatterOpts{
"indent": 10, // TODO: This does not work yet...y??
"header": true,
if err != nil {
t.Fatalf("got unexpected error: %v", err)
}
expect := "foo: bar\n"
gotS := string(got)
if expect != gotS {
t.Fatalf("expected %v but got %v", expect, got)
}
got, err := f.FormatWithOpts([]struct {
Foo string
}{
{Foo: "bar"},
{Foo: "baz"},
}, opts)
require.NoError(t, err)
require.IsType(t, []byte{}, got)
require.Equal(t, string("---\n- foo: bar\n- foo: baz\n"), string(got))
}
*/
Loading

0 comments on commit beff581

Please sign in to comment.