Skip to content

Commit

Permalink
test: test unit table simple for command root, version 🚨
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwelbm committed Oct 16, 2024
1 parent 6ea0f05 commit cfbacc7
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
50 changes: 50 additions & 0 deletions components/mdz/pkg/cmd/root/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package root

import (
"bytes"
"testing"

"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
"github.com/stretchr/testify/assert"
)

func TestNewCmdRoot(t *testing.T) {
tests := []struct {
name string
version string
expectedOut string
expectedErr string
expectedHelp string
}{
{
name: "Standard output",
version: "1.0.0",
expectedOut: "1.0.0",
},
{
name: "Version empty",
version: "",
expectedOut: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)
f := &factory.Factory{
CLIVersion: tt.version,
IOStreams: &iostreams.IOStreams{
Out: outBuf,
Err: errBuf,
},
}

cmd := NewCmdRoot(f)
cmd.SetArgs([]string{"--help"})
err := cmd.Execute()
assert.NoError(t, err)
})
}
}
55 changes: 55 additions & 0 deletions components/mdz/pkg/cmd/version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package version

import (
"bytes"
"testing"

"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestFactoryVersionRunE(t *testing.T) {
tests := []struct {
name string
version string
expected string
}{
{
name: "Standard version output",
version: "1.0.0",
expected: color.New(color.Bold).Sprint("1.0.0") + "\n",
},
{
name: "Empty version",
version: "",
expected: color.New(color.Bold).Sprint("") + "\n",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &factory.Factory{
CLIVersion: tt.version,
IOStreams: &iostreams.IOStreams{
Out: new(bytes.Buffer),
},
}

fVersion := &factoryVersion{
factory: f,
}
cmd := &cobra.Command{
RunE: fVersion.runE,
}

err := cmd.Execute()
assert.NoError(t, err)

output := f.IOStreams.Out.(*bytes.Buffer).String()
assert.Equal(t, tt.expected, output)
})
}
}

0 comments on commit cfbacc7

Please sign in to comment.