Skip to content

Commit

Permalink
Add unit test for props
Browse files Browse the repository at this point in the history
  • Loading branch information
ybkuroki committed Feb 12, 2023
1 parent 4943f84 commit 885935a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (c *container) GetConfig() *config.Config {
return c.config
}

// GetMessages returns the map has key and message.
func (c *container) GetMessages() map[string]string {
return c.messages
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ybkuroki/go-webapp-sample

go 1.19
go 1.20

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var propsFile embed.FS
func main() {
e := echo.New()

messages := util.ReadPropertiesFile(propsFile)
messages := util.ReadPropertiesFile(propsFile, "messages.properties")
conf, env := config.Load(yamlFile)
logger := logger.InitLogger(env, zapYamlFile)
logger.GetZapLogger().Infof("Loaded this configuration : application." + env + ".yml")
Expand Down
4 changes: 2 additions & 2 deletions util/props.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const (
)

// ReadPropertiesFile reads a properties file and it returns a map has the keys and values in the file.
func ReadPropertiesFile(fs embed.FS) map[string]string {
func ReadPropertiesFile(fs embed.FS, fileName string) map[string]string {
config := make(map[string]string)

file, err := fs.Open("messages.properties")
file, err := fs.Open(fileName)
if err != nil {
return nil
}
Expand Down
22 changes: 22 additions & 0 deletions util/props_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package util

import (
"embed"
"testing"

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

//go:embed test.properties
var testPropsFile embed.FS

func TestReadPropertiesFile(t *testing.T) {
messages := ReadPropertiesFile(testPropsFile, "test.properties")

assert.Equal(t, "testtest", messages["props1"])
assert.Equal(t, "test test", messages["props2"])
assert.Equal(t, "", messages["props3"])
_, ok := messages["props4"]
assert.Equal(t, false, ok)
assert.Equal(t, "test #test", messages["props5"])
}
7 changes: 7 additions & 0 deletions util/test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# comment line
props1=testtest
props2 = test test
props3=
# comment line
props4
props5= test #test

0 comments on commit 885935a

Please sign in to comment.