Skip to content

Commit

Permalink
refactor: add language tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed May 14, 2021
1 parent 6e23cdd commit d96cb79
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 10 deletions.
31 changes: 31 additions & 0 deletions src/segment_crystal_test.go
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"testing"

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

func TestCrystal(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
Version string
}{
{Case: "Crystal 1.0.0", ExpectedString: "1.0.0", Version: "Crystal 1.0.0 (2021-03-22)"},
}
for _, tc := range cases {
params := &mockedLanguageParams{
cmd: "crystal",
versionParam: "--version",
versionOutput: tc.Version,
extension: "*.cr",
}
env, props := getMockedLanguageEnv(params)
c := &crystal{}
c.init(props, env)
assert.True(t, c.enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
assert.Equal(t, tc.ExpectedString, c.string(), fmt.Sprintf("Failed in case: %s", tc.Case))
}
}
38 changes: 28 additions & 10 deletions src/segment_golang_test.go
Expand Up @@ -7,6 +7,28 @@ import (
"github.com/stretchr/testify/assert"
)

type mockedLanguageParams struct {
cmd string
versionParam string
versionOutput string
extension string
}

func getMockedLanguageEnv(params *mockedLanguageParams) (*MockedEnvironment, *properties) {
env := new(MockedEnvironment)
env.On("hasCommand", params.cmd).Return(true)
env.On("runCommand", params.cmd, []string{params.versionParam}).Return(params.versionOutput, nil)
env.On("hasFiles", params.extension).Return(true)
env.On("getcwd", nil).Return("/usr/home/project")
env.On("homeDir", nil).Return("/usr/home")
props := &properties{
values: map[Property]interface{}{
DisplayVersion: true,
},
}
return env, props
}

func TestGolang(t *testing.T) {
cases := []struct {
Case string
Expand All @@ -17,17 +39,13 @@ func TestGolang(t *testing.T) {
{Case: "Go 1.16", ExpectedString: "1.16", Version: "go version go1.16 darwin/amd64"},
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("hasCommand", "go").Return(true)
env.On("runCommand", "go", []string{"version"}).Return(tc.Version, nil)
env.On("hasFiles", "*.go").Return(true)
env.On("getcwd", nil).Return("/usr/home/project")
env.On("homeDir", nil).Return("/usr/home")
props := &properties{
values: map[Property]interface{}{
DisplayVersion: true,
},
params := &mockedLanguageParams{
cmd: "go",
versionParam: "version",
versionOutput: tc.Version,
extension: "*.go",
}
env, props := getMockedLanguageEnv(params)
g := &golang{}
g.init(props, env)
assert.True(t, g.enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
Expand Down
32 changes: 32 additions & 0 deletions src/segment_julia_test.go
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"testing"

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

func TestJulia(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
Version string
}{
{Case: "Julia 1.6.0", ExpectedString: "1.6.0", Version: "julia version 1.6.0"},
{Case: "Julia 1.6.1", ExpectedString: "1.6.1", Version: "julia version 1.6.1"},
}
for _, tc := range cases {
params := &mockedLanguageParams{
cmd: "julia",
versionParam: "--version",
versionOutput: tc.Version,
extension: "*.jl",
}
env, props := getMockedLanguageEnv(params)
j := &julia{}
j.init(props, env)
assert.True(t, j.enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
assert.Equal(t, tc.ExpectedString, j.string(), fmt.Sprintf("Failed in case: %s", tc.Case))
}
}

0 comments on commit d96cb79

Please sign in to comment.