Files
undent/undent_test.go
Jim Myhrberg 5dbdbbf341 fix(bytes): change Bytes function to accept string input but return a byte slice
The old method signature was just nonsensical, as you would always be
providing indented values via a string literal. So it makes much more
sense to have all methods accept a string argument, and then return
different types.

This also allows use of a `Bytesf` method.

This is technically a breaking change, but I'm classifying it as a
bugfix cause the old method signature was basically useless.
2020-12-14 14:52:32 +00:00

465 lines
6.1 KiB
Go

package undent
import (
"testing"
"github.com/stretchr/testify/assert"
)
var stringTestCases = []struct {
name string
s string
want string
}{
{
name: "empty",
s: "",
want: "",
},
{
name: "single-line",
s: "hello world",
want: "hello world",
},
{
name: "single-line indented",
s: " hello world",
want: "hello world",
},
{
name: "multi-line",
s: `
{
"hello": "world",
"foo": [
"bar"
]
}`,
want: `{
"hello": "world",
"foo": [
"bar"
]
}`,
},
{
name: "multi-line space indented",
s: `
{
"hello": "world",
"foo": [
"bar"
]
}`,
want: `{
"hello": "world",
"foo": [
"bar"
]
}`,
},
{
name: "multi-line space indented with leading line-breaks",
s: `
{
"hello": "world",
"foo": [
"bar"
]
}`,
want: `
{
"hello": "world",
"foo": [
"bar"
]
}`,
},
{
name: "multi-line tab indented",
s: `
{
"hello": "world",
"foo": [
"bar"
]
}`,
want: `{
"hello": "world",
"foo": [
"bar"
]
}`,
},
{
name: "multi-line tab indented with leading line breaks",
s: `
{
"hello": "world",
"foo": [
"bar"
]
}`,
want: `
{
"hello": "world",
"foo": [
"bar"
]
}`,
},
{
name: "multi-line tab indented with tabs and spaces after indent",
s: `
{
"hello": "world",
"foo": [
"bar"
]
}`,
want: `{
"hello": "world",
"foo": [
"bar"
]
}`,
},
{
name: "multi-line space indented with blank lines",
s: `
{
"hello": "world",
"foo": [
"bar"
]
}`,
want: `{
"hello": "world",
"foo": [
"bar"
]
}`,
},
{
name: "multi-line tab indented with blank lines",
s: `
{
"hello": "world",
"foo": [
"bar"
]
}`,
want: `{
"hello": "world",
"foo": [
"bar"
]
}`,
},
{
name: "multi-line space indented with random indentation",
s: `
hello
world
foo
bar`,
want: ` hello
world
foo
bar`,
},
{
name: "multi-line tab indented with random indentation",
s: `
hello
world
foo
bar`,
want: ` hello
world
foo
bar`,
},
}
var stringfTestCases = []struct {
name string
s string
a []interface{}
want string
}{
{
name: "empty",
s: "",
want: "",
},
{
name: "single-line",
s: "hello %s, %d",
a: []interface{}{"world", 42},
want: "hello world, 42",
},
{
name: "single-line indented",
s: " hello %s, %d",
a: []interface{}{"world", 42},
want: "hello world, 42",
},
{
name: "multi-line",
s: `
{
"hello": "%s",
"foo": [
%d
]
}`,
a: []interface{}{"world", 42},
want: `{
"hello": "world",
"foo": [
42
]
}`,
},
{
name: "multi-line space indented",
s: `
{
"hello": "%s",
"foo": [
%d
]
}`,
a: []interface{}{"world", 42},
want: `{
"hello": "world",
"foo": [
42
]
}`,
},
{
name: "multi-line space indented with leading line-breaks",
s: `
{
"hello": "%s",
"foo": [
%d
]
}`,
a: []interface{}{"world", 42},
want: `
{
"hello": "world",
"foo": [
42
]
}`,
},
{
name: "multi-line tab indented",
s: `
{
"hello": "%s",
"foo": [
%d
]
}`,
a: []interface{}{"world", 42},
want: `{
"hello": "world",
"foo": [
42
]
}`,
},
{
name: "multi-line tab indented with leading line-breaks",
s: `
{
"hello": "%s",
"foo": [
%d
]
}`,
a: []interface{}{"world", 42},
want: `
{
"hello": "world",
"foo": [
42
]
}`,
},
{
name: "multi-line tab indented with tabs and spaces after indent",
s: `
{
"hello": "%s",
"foo": [
%d
]
}`,
a: []interface{}{"world", 42},
want: `{
"hello": "world",
"foo": [
42
]
}`,
},
{
name: "multi-line space indented with blank lines",
s: `
{
"hello": "%s",
"foo": [
%d
]
}`,
a: []interface{}{"world", 42},
want: `{
"hello": "world",
"foo": [
42
]
}`,
},
{
name: "multi-line tab indented with blank lines",
s: `
{
"hello": "%s",
"foo": [
%d
]
}`,
a: []interface{}{"world", 42},
want: `{
"hello": "world",
"foo": [
42
]
}`,
},
{
name: "multi-line space indented with random indentation",
s: `
hello
%s
foo
%d`,
a: []interface{}{"world", 42},
want: ` hello
world
foo
42`,
},
{
name: "multi-line tab indented with random indentation",
s: `
hello
%s
foo
%d`,
a: []interface{}{"world", 42},
want: ` hello
world
foo
42`,
},
}
func TestBytes(t *testing.T) {
for _, tt := range stringTestCases {
t.Run(tt.name, func(t *testing.T) {
got := Bytes(tt.s)
assert.IsType(t, []byte{}, got)
assert.Equal(t, tt.want, string(got))
})
}
}
func TestBytesf(t *testing.T) {
for _, tt := range stringfTestCases {
t.Run(tt.name, func(t *testing.T) {
got := Bytesf(tt.s, tt.a...)
assert.IsType(t, []byte{}, got)
assert.Equal(t, tt.want, string(got))
})
}
}
func TestString(t *testing.T) {
for _, tt := range stringTestCases {
t.Run(tt.name, func(t *testing.T) {
got := String(tt.s)
assert.IsType(t, "", got)
assert.Equal(t, tt.want, got)
})
}
}
func TestStringf(t *testing.T) {
for _, tt := range stringfTestCases {
t.Run(tt.name, func(t *testing.T) {
got := Stringf(tt.s, tt.a...)
assert.IsType(t, "", got)
assert.Equal(t, tt.want, got)
})
}
}
func BenchmarkBytes(b *testing.B) {
for _, tt := range stringTestCases {
b.Run(tt.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
Bytes(tt.s)
}
})
}
}
func BenchmarkString(b *testing.B) {
for _, tt := range stringTestCases {
b.Run(tt.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
String(tt.s)
}
})
}
}