1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/pkg/piperutils/templateUtils_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

50 lines
1.6 KiB
Go

//go:build unit
// +build unit
package piperutils
import (
"github.com/stretchr/testify/assert"
"testing"
"text/template"
)
type SomeDescriptor struct {
GroupID string
ArtifactID string
Version string
}
func TestExecuteTemplate(t *testing.T) {
t.Run("test success", func(t *testing.T) {
context := SomeDescriptor{GroupID: "com.sap.cp.jenkins", ArtifactID: "piper", Version: "1.2.3"}
result, err := ExecuteTemplate("{{ .GroupID }}-{{ .ArtifactID }}:{{ .Version}}", context)
assert.NoError(t, err, "Didn't expect error but got one")
assert.Equal(t, "com.sap.cp.jenkins-piper:1.2.3", result, "Expected different result")
})
t.Run("test template error", func(t *testing.T) {
context := SomeDescriptor{GroupID: "com.sap.cp.jenkins", ArtifactID: "piper", Version: "1.2.3"}
_, err := ExecuteTemplate("{{ $+++.+++GroupID }}-{{ .ArtifactID }}:{{ .Version}}", context)
assert.Error(t, err, "Expected error but got none")
})
t.Run("test functions", func(t *testing.T) {
functions := template.FuncMap{
"testFunc": reverse,
}
context := SomeDescriptor{GroupID: "com.sap.cp.jenkins", ArtifactID: "piper", Version: "1.2.3"}
result, err := ExecuteTemplateFunctions("{{ testFunc .GroupID }}-{{ .ArtifactID }}:{{ .Version}}", functions, context)
assert.NoError(t, err, "Didn't expect error but got one")
assert.Equal(t, "sniknej.pc.pas.moc-piper:1.2.3", result, "Expected different result")
})
}
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}