Skip to content

Commit

Permalink
Add tests directory for tuple tests (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
barweiss authored Dec 30, 2021
1 parent f2228ef commit c16834e
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 191 deletions.
2 changes: 1 addition & 1 deletion scripts/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func main() {
tpl: codeTpl,
},
{
fullPath: path.Join(outputDir, fmt.Sprintf("tuple%d_test.go", tupleLength)),
fullPath: path.Join(outputDir, "tests", fmt.Sprintf("tuple%d_test.go", tupleLength)),
tpl: testTpl,
},
}
Expand Down
39 changes: 20 additions & 19 deletions scripts/gen/tuple_test.tpl
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
package tuple
package tuple_test

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/barweiss/go-tuple"
)

{{/* These variables can be used when the context of dot changes. */}}
{{$indexes := .Indexes}}
{{$len := .Len}}

func TestT{{.Len}}_New(t *testing.T) {
tup := New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
require.Equal(t, T{{.Len}}[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}]{
tup := tuple.New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
require.Equal(t, tuple.T{{.Len}}[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}]{
{{range .Indexes -}}
V{{.}}: {{. | quote}},
{{end}}
}, tup)
}

func TestT{{.Len}}_Len(t *testing.T) {
tup := New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
tup := tuple.New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
require.Equal(t, {{.Len}}, tup.Len())
}

func TestT{{.Len}}_Values(t *testing.T) {
tup := New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
tup := tuple.New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}v{{$index}}{{end}} := tup.Values()
{{range .Indexes -}}
require.Equal(t, {{. | quote}}, v{{.}})
{{end -}}
}

func TestT{{.Len}}_String(t *testing.T) {
tup := New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
tup := tuple.New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
require.Equal(t, `[{{range $i, $index := .Indexes}}{{if gt $i 0}} {{end}}{{. | quote}}{{end}}]`, tup.String())
}

func TestT{{.Len}}_GoString(t *testing.T) {
tup := New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
tup := tuple.New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
require.Equal(t, `tuple.T{{.Len}}[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}]{
{{- range $i, $index := .Indexes -}}
{{- if gt $i 0}}, {{end -}}
Expand All @@ -48,14 +49,14 @@ func TestT{{.Len}}_GoString(t *testing.T) {
}

func TestT{{.Len}}_ToArray(t *testing.T) {
tup := New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
tup := tuple.New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
require.Equal(t, [{{.Len}}]any{
{{range .Indexes -}}{{. | quote}},{{end}}
}, tup.Array())
}

func TestT{{.Len}}_ToSlice(t *testing.T) {
tup := New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
tup := tuple.New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}})
require.Equal(t, []any{
{{range .Indexes -}}{{. | quote}},{{end}}
}, tup.Slice())
Expand Down Expand Up @@ -93,8 +94,8 @@ func TestT{{.Len}}_FromArrayX(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
do := func () T{{.Len}}[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}] {
return FromArray{{.Len}}X[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}](tt.array)
do := func () tuple.T{{.Len}}[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}] {
return tuple.FromArray{{.Len}}X[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}](tt.array)
}

if tt.wantPanic {
Expand All @@ -104,7 +105,7 @@ func TestT{{.Len}}_FromArrayX(t *testing.T) {
return
}

require.Equal(t, New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}}), do())
require.Equal(t, tuple.New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}}), do())
})
}
}
Expand Down Expand Up @@ -141,14 +142,14 @@ func TestT{{.Len}}_FromArray(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tup, err := FromArray{{.Len}}[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}](tt.array)
tup, err := tuple.FromArray{{.Len}}[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}](tt.array)
if tt.wantErr {
require.Error(t, err)
return
}

require.NoError(t, err)
require.Equal(t, New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}}), tup)
require.Equal(t, tuple.New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}}), tup)
})
}
}
Expand Down Expand Up @@ -205,8 +206,8 @@ func TestT{{.Len}}_FromSliceX(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
do := func () T{{.Len}}[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}] {
return FromSlice{{.Len}}X[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}](tt.slice)
do := func () tuple.T{{.Len}}[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}] {
return tuple.FromSlice{{.Len}}X[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}](tt.slice)
}

if tt.wantPanic {
Expand All @@ -216,7 +217,7 @@ func TestT{{.Len}}_FromSliceX(t *testing.T) {
return
}

require.Equal(t, New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}}), do())
require.Equal(t, tuple.New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}}), do())
})
}
}
Expand Down Expand Up @@ -273,14 +274,14 @@ func TestT{{.Len}}_FromSlice(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tup, err := FromSlice{{.Len}}[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}](tt.slice)
tup, err := tuple.FromSlice{{.Len}}[{{range $i, $index := .Indexes}}{{if gt $i 0}}, {{end}}string{{end}}](tt.slice)
if tt.wantErr {
require.Error(t, err)
return
}

require.NoError(t, err)
require.Equal(t, New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}}), tup)
require.Equal(t, tuple.New{{len .Indexes}}({{range .Indexes}}{{. | quote}},{{end}}), tup)
})
}
}
39 changes: 20 additions & 19 deletions tuple1_test.go → tests/tuple1_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
package tuple
package tuple_test

import (
"testing"

"github.com/barweiss/go-tuple"
"github.com/stretchr/testify/require"
)

func TestT1_New(t *testing.T) {
tup := New1("1")
require.Equal(t, T1[string]{
tup := tuple.New1("1")
require.Equal(t, tuple.T1[string]{
V1: "1",
}, tup)
}

func TestT1_Len(t *testing.T) {
tup := New1("1")
tup := tuple.New1("1")
require.Equal(t, 1, tup.Len())
}

func TestT1_Values(t *testing.T) {
tup := New1("1")
tup := tuple.New1("1")
v1 := tup.Values()
require.Equal(t, "1", v1)
}

func TestT1_String(t *testing.T) {
tup := New1("1")
tup := tuple.New1("1")
require.Equal(t, `["1"]`, tup.String())
}

func TestT1_GoString(t *testing.T) {
tup := New1("1")
tup := tuple.New1("1")
require.Equal(t, `tuple.T1[string]{V1: "1"}`, tup.GoString())
}

func TestT1_ToArray(t *testing.T) {
tup := New1("1")
tup := tuple.New1("1")
require.Equal(t, [1]any{
"1",
}, tup.Array())
}

func TestT1_ToSlice(t *testing.T) {
tup := New1("1")
tup := tuple.New1("1")
require.Equal(t, []any{
"1",
}, tup.Slice())
Expand Down Expand Up @@ -71,8 +72,8 @@ func TestT1_FromArrayX(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
do := func() T1[string] {
return FromArray1X[string](tt.array)
do := func() tuple.T1[string] {
return tuple.FromArray1X[string](tt.array)
}

if tt.wantPanic {
Expand All @@ -82,7 +83,7 @@ func TestT1_FromArrayX(t *testing.T) {
return
}

require.Equal(t, New1("1"), do())
require.Equal(t, tuple.New1("1"), do())
})
}
}
Expand Down Expand Up @@ -110,14 +111,14 @@ func TestT1_FromArray(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tup, err := FromArray1[string](tt.array)
tup, err := tuple.FromArray1[string](tt.array)
if tt.wantErr {
require.Error(t, err)
return
}

require.NoError(t, err)
require.Equal(t, New1("1"), tup)
require.Equal(t, tuple.New1("1"), tup)
})
}
}
Expand Down Expand Up @@ -163,8 +164,8 @@ func TestT1_FromSliceX(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
do := func() T1[string] {
return FromSlice1X[string](tt.slice)
do := func() tuple.T1[string] {
return tuple.FromSlice1X[string](tt.slice)
}

if tt.wantPanic {
Expand All @@ -174,7 +175,7 @@ func TestT1_FromSliceX(t *testing.T) {
return
}

require.Equal(t, New1("1"), do())
require.Equal(t, tuple.New1("1"), do())
})
}
}
Expand Down Expand Up @@ -220,14 +221,14 @@ func TestT1_FromSlice(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tup, err := FromSlice1[string](tt.slice)
tup, err := tuple.FromSlice1[string](tt.slice)
if tt.wantErr {
require.Error(t, err)
return
}

require.NoError(t, err)
require.Equal(t, New1("1"), tup)
require.Equal(t, tuple.New1("1"), tup)
})
}
}
Loading

0 comments on commit c16834e

Please sign in to comment.