mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
tests: Convert from testify to quicktest
This commit is contained in:
@@ -16,12 +16,14 @@ package transform
|
||||
import (
|
||||
"testing"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/deps"
|
||||
"github.com/gohugoio/hugo/htesting/hqt"
|
||||
"github.com/gohugoio/hugo/tpl/internal"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
var found bool
|
||||
var ns *internal.TemplateFuncsNamespace
|
||||
|
||||
@@ -33,6 +35,6 @@ func TestInit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
require.True(t, found)
|
||||
require.IsType(t, &Namespace{}, ns.Context())
|
||||
c.Assert(found, qt.Equals, true)
|
||||
c.Assert(ns.Context(), hqt.IsSameType, &Namespace{})
|
||||
}
|
||||
|
@@ -14,12 +14,11 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/helpers"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRemarshal(t *testing.T) {
|
||||
@@ -28,7 +27,7 @@ func TestRemarshal(t *testing.T) {
|
||||
v := viper.New()
|
||||
v.Set("contentDir", "content")
|
||||
ns := New(newDeps(v))
|
||||
assert := require.New(t)
|
||||
c := qt.New(t)
|
||||
|
||||
tomlExample := `title = "Test Metadata"
|
||||
|
||||
@@ -96,10 +95,10 @@ title: Test Metadata
|
||||
for _, v1 := range variants {
|
||||
for _, v2 := range variants {
|
||||
// Both from and to may be the same here, but that is fine.
|
||||
fromTo := fmt.Sprintf("%s => %s", v2.format, v1.format)
|
||||
fromTo := qt.Commentf("%s => %s", v2.format, v1.format)
|
||||
|
||||
converted, err := ns.Remarshal(v1.format, v2.data)
|
||||
assert.NoError(err, fromTo)
|
||||
c.Assert(err, qt.IsNil, fromTo)
|
||||
diff := helpers.DiffStrings(v1.data, converted)
|
||||
if len(diff) > 0 {
|
||||
t.Errorf("[%s] Expected \n%v\ngot\n%v\ndiff:\n%v", fromTo, v1.data, converted, diff)
|
||||
@@ -117,7 +116,7 @@ func TestRemarshalComments(t *testing.T) {
|
||||
v.Set("contentDir", "content")
|
||||
ns := New(newDeps(v))
|
||||
|
||||
assert := require.New(t)
|
||||
c := qt.New(t)
|
||||
|
||||
input := `
|
||||
Hugo = "Rules"
|
||||
@@ -138,14 +137,14 @@ Hugo = "Rules"
|
||||
`
|
||||
|
||||
for _, format := range []string{"json", "yaml", "toml"} {
|
||||
fromTo := fmt.Sprintf("%s => %s", "toml", format)
|
||||
fromTo := qt.Commentf("%s => %s", "toml", format)
|
||||
|
||||
converted := input
|
||||
var err error
|
||||
// Do a round-trip conversion
|
||||
for _, toFormat := range []string{format, "toml"} {
|
||||
converted, err = ns.Remarshal(toFormat, converted)
|
||||
assert.NoError(err, fromTo)
|
||||
c.Assert(err, qt.IsNil, fromTo)
|
||||
}
|
||||
|
||||
diff := helpers.DiffStrings(expected, converted)
|
||||
@@ -161,12 +160,12 @@ func TestTestRemarshalError(t *testing.T) {
|
||||
v := viper.New()
|
||||
v.Set("contentDir", "content")
|
||||
ns := New(newDeps(v))
|
||||
assert := require.New(t)
|
||||
c := qt.New(t)
|
||||
|
||||
_, err := ns.Remarshal("asdf", "asdf")
|
||||
assert.Error(err)
|
||||
c.Assert(err, qt.Not(qt.IsNil))
|
||||
|
||||
_, err = ns.Remarshal("json", "asdf")
|
||||
assert.Error(err)
|
||||
c.Assert(err, qt.Not(qt.IsNil))
|
||||
|
||||
}
|
||||
|
@@ -14,29 +14,28 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"testing"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/config"
|
||||
"github.com/gohugoio/hugo/deps"
|
||||
"github.com/gohugoio/hugo/helpers"
|
||||
"github.com/gohugoio/hugo/hugofs"
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type tstNoStringer struct{}
|
||||
|
||||
func TestEmojify(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
v := viper.New()
|
||||
ns := New(newDeps(v))
|
||||
|
||||
for i, test := range []struct {
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
expect interface{}
|
||||
}{
|
||||
@@ -45,28 +44,28 @@ func TestEmojify(t *testing.T) {
|
||||
// errors
|
||||
{tstNoStringer{}, false},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d] %s", i, test.s)
|
||||
|
||||
result, err := ns.Emojify(test.s)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
require.Error(t, err, errMsg)
|
||||
c.Assert(err, qt.Not(qt.IsNil))
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err, errMsg)
|
||||
assert.Equal(t, test.expect, result, errMsg)
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(result, qt.Equals, test.expect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHighlight(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
v := viper.New()
|
||||
v.Set("contentDir", "content")
|
||||
ns := New(newDeps(v))
|
||||
|
||||
for i, test := range []struct {
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
lang string
|
||||
opts string
|
||||
@@ -77,28 +76,28 @@ func TestHighlight(t *testing.T) {
|
||||
{`<Foo attr=" < "></Foo>`, "xml", "", `&lt;`},
|
||||
{tstNoStringer{}, "go", "", false},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d]", i)
|
||||
|
||||
result, err := ns.Highlight(test.s, test.lang, test.opts)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
require.Error(t, err, errMsg)
|
||||
c.Assert(err, qt.Not(qt.IsNil))
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err, errMsg)
|
||||
assert.Contains(t, result, test.expect.(string), errMsg)
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(string(result), qt.Contains, test.expect.(string))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTMLEscape(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
v := viper.New()
|
||||
v.Set("contentDir", "content")
|
||||
ns := New(newDeps(v))
|
||||
|
||||
for i, test := range []struct {
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
expect interface{}
|
||||
}{
|
||||
@@ -107,28 +106,28 @@ func TestHTMLEscape(t *testing.T) {
|
||||
// errors
|
||||
{tstNoStringer{}, false},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d] %s", i, test.s)
|
||||
|
||||
result, err := ns.HTMLEscape(test.s)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
require.Error(t, err, errMsg)
|
||||
c.Assert(err, qt.Not(qt.IsNil))
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err, errMsg)
|
||||
assert.Equal(t, test.expect, result, errMsg)
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(result, qt.Equals, test.expect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTMLUnescape(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
v := viper.New()
|
||||
v.Set("contentDir", "content")
|
||||
ns := New(newDeps(v))
|
||||
|
||||
for i, test := range []struct {
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
expect interface{}
|
||||
}{
|
||||
@@ -137,28 +136,28 @@ func TestHTMLUnescape(t *testing.T) {
|
||||
// errors
|
||||
{tstNoStringer{}, false},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d] %s", i, test.s)
|
||||
|
||||
result, err := ns.HTMLUnescape(test.s)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
require.Error(t, err, errMsg)
|
||||
c.Assert(err, qt.Not(qt.IsNil))
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err, errMsg)
|
||||
assert.Equal(t, test.expect, result, errMsg)
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(result, qt.Equals, test.expect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkdownify(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
v := viper.New()
|
||||
v.Set("contentDir", "content")
|
||||
ns := New(newDeps(v))
|
||||
|
||||
for i, test := range []struct {
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
expect interface{}
|
||||
}{
|
||||
@@ -166,26 +165,23 @@ func TestMarkdownify(t *testing.T) {
|
||||
{[]byte("Hello Bytes **World!**"), template.HTML("Hello Bytes <strong>World!</strong>")},
|
||||
{tstNoStringer{}, false},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d] %s", i, test.s)
|
||||
|
||||
result, err := ns.Markdownify(test.s)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
require.Error(t, err, errMsg)
|
||||
c.Assert(err, qt.Not(qt.IsNil))
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err, errMsg)
|
||||
assert.Equal(t, test.expect, result, errMsg)
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(result, qt.Equals, test.expect)
|
||||
}
|
||||
}
|
||||
|
||||
// Issue #3040
|
||||
func TestMarkdownifyBlocksOfText(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := require.New(t)
|
||||
|
||||
c := qt.New(t)
|
||||
v := viper.New()
|
||||
v.Set("contentDir", "content")
|
||||
ns := New(newDeps(v))
|
||||
@@ -203,20 +199,20 @@ And then some.
|
||||
`
|
||||
|
||||
result, err := ns.Markdownify(text)
|
||||
assert.NoError(err)
|
||||
assert.Equal(template.HTML(
|
||||
"<p>#First</p>\n\n<p>This is some <em>bold</em> text.</p>\n\n<h2 id=\"second\">Second</h2>\n\n<p>This is some more text.</p>\n\n<p>And then some.</p>\n"),
|
||||
result)
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(result, qt.Equals, template.HTML(
|
||||
"<p>#First</p>\n\n<p>This is some <em>bold</em> text.</p>\n\n<h2 id=\"second\">Second</h2>\n\n<p>This is some more text.</p>\n\n<p>And then some.</p>\n"))
|
||||
|
||||
}
|
||||
|
||||
func TestPlainify(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
v := viper.New()
|
||||
ns := New(newDeps(v))
|
||||
|
||||
for i, test := range []struct {
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
expect interface{}
|
||||
}{
|
||||
@@ -224,17 +220,16 @@ func TestPlainify(t *testing.T) {
|
||||
// errors
|
||||
{tstNoStringer{}, false},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d] %s", i, test.s)
|
||||
|
||||
result, err := ns.Plainify(test.s)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
require.Error(t, err, errMsg)
|
||||
c.Assert(err, qt.Not(qt.IsNil))
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err, errMsg)
|
||||
assert.Equal(t, test.expect, result, errMsg)
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(result, qt.Equals, test.expect)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -23,9 +23,9 @@ import (
|
||||
|
||||
"github.com/gohugoio/hugo/media"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/resources/resource"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -82,13 +82,13 @@ func TestUnmarshal(t *testing.T) {
|
||||
|
||||
v := viper.New()
|
||||
ns := New(newDeps(v))
|
||||
assert := require.New(t)
|
||||
c := qt.New(t)
|
||||
|
||||
assertSlogan := func(m map[string]interface{}) {
|
||||
assert.Equal("Hugo Rocks!", m["slogan"])
|
||||
c.Assert(m["slogan"], qt.Equals, "Hugo Rocks!")
|
||||
}
|
||||
|
||||
for i, test := range []struct {
|
||||
for _, test := range []struct {
|
||||
data interface{}
|
||||
options interface{}
|
||||
expect interface{}
|
||||
@@ -113,27 +113,27 @@ func TestUnmarshal(t *testing.T) {
|
||||
}},
|
||||
{testContentResource{key: "r1", content: `1997,Ford,E350,"ac, abs, moon",3000.00
|
||||
1999,Chevy,"Venture ""Extended Edition""","",4900.00`, mime: media.CSVType}, nil, func(r [][]string) {
|
||||
assert.Equal(2, len(r))
|
||||
c.Assert(len(r), qt.Equals, 2)
|
||||
first := r[0]
|
||||
assert.Equal(5, len(first))
|
||||
assert.Equal("Ford", first[1])
|
||||
c.Assert(len(first), qt.Equals, 5)
|
||||
c.Assert(first[1], qt.Equals, "Ford")
|
||||
}},
|
||||
{testContentResource{key: "r1", content: `a;b;c`, mime: media.CSVType}, map[string]interface{}{"delimiter": ";"}, func(r [][]string) {
|
||||
assert.Equal(r, [][]string{{"a", "b", "c"}})
|
||||
c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
||||
|
||||
}},
|
||||
{"a,b,c", nil, func(r [][]string) {
|
||||
assert.Equal(r, [][]string{{"a", "b", "c"}})
|
||||
c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
||||
|
||||
}},
|
||||
{"a;b;c", map[string]interface{}{"delimiter": ";"}, func(r [][]string) {
|
||||
assert.Equal(r, [][]string{{"a", "b", "c"}})
|
||||
c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
||||
|
||||
}},
|
||||
{testContentResource{key: "r1", content: `
|
||||
% This is a comment
|
||||
a;b;c`, mime: media.CSVType}, map[string]interface{}{"DElimiter": ";", "Comment": "%"}, func(r [][]string) {
|
||||
assert.Equal(r, [][]string{{"a", "b", "c"}})
|
||||
c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
||||
|
||||
}},
|
||||
// errors
|
||||
@@ -144,7 +144,6 @@ a;b;c`, mime: media.CSVType}, map[string]interface{}{"DElimiter": ";", "Comment"
|
||||
{`{ notjson }`, nil, false},
|
||||
{tstNoStringer{}, nil, false},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d]", i)
|
||||
|
||||
ns.cache.Clear()
|
||||
|
||||
@@ -159,20 +158,20 @@ a;b;c`, mime: media.CSVType}, map[string]interface{}{"DElimiter": ";", "Comment"
|
||||
result, err := ns.Unmarshal(args...)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
assert.Error(err, errMsg)
|
||||
c.Assert(err, qt.Not(qt.IsNil))
|
||||
} else if fn, ok := test.expect.(func(m map[string]interface{})); ok {
|
||||
assert.NoError(err, errMsg)
|
||||
c.Assert(err, qt.IsNil)
|
||||
m, ok := result.(map[string]interface{})
|
||||
assert.True(ok, errMsg)
|
||||
c.Assert(ok, qt.Equals, true)
|
||||
fn(m)
|
||||
} else if fn, ok := test.expect.(func(r [][]string)); ok {
|
||||
assert.NoError(err, errMsg)
|
||||
c.Assert(err, qt.IsNil)
|
||||
r, ok := result.([][]string)
|
||||
assert.True(ok, errMsg)
|
||||
c.Assert(ok, qt.Equals, true)
|
||||
fn(r)
|
||||
} else {
|
||||
assert.NoError(err, errMsg)
|
||||
assert.Equal(test.expect, result, errMsg)
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(result, qt.Equals, test.expect)
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user