fixed the issue with decimal comma parsing, tests added
This commit is contained in:
parent
0a88bd3727
commit
5e9668c2e4
|
@ -8,6 +8,7 @@ import (
|
||||||
func ParseFloat(input string) (float64, error) {
|
func ParseFloat(input string) (float64, error) {
|
||||||
|
|
||||||
clean := strings.TrimSpace(input)
|
clean := strings.TrimSpace(input)
|
||||||
|
clean = strings.Replace(clean, ",", ".", -1) // replace decimal comma with decimal point
|
||||||
|
|
||||||
if strings.Contains(clean, "\n") {
|
if strings.Contains(clean, "\n") {
|
||||||
lastIndex := strings.LastIndex(clean, "\n")
|
lastIndex := strings.LastIndex(clean, "\n")
|
||||||
|
|
|
@ -13,6 +13,8 @@ func TestParseFloat(t *testing.T) {
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{"should parse a regular number", args{"123"}, 123, false},
|
{"should parse a regular number", args{"123"}, 123, false},
|
||||||
|
{"should parse a float with decimal point", args{"123.456"}, 123.456, false},
|
||||||
|
{"should parse a float with decimal comma", args{"123,456"}, 123.456, false},
|
||||||
{"should parse a regular number with spaces, tabs and line breaks", args{" \t 123 \t \n "}, 123, false},
|
{"should parse a regular number with spaces, tabs and line breaks", args{" \t 123 \t \n "}, 123, false},
|
||||||
{"should parse a last line in the given string", args{"123\n456"}, 456, false},
|
{"should parse a last line in the given string", args{"123\n456"}, 456, false},
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
package metadata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type f struct {
|
||||||
|
a int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_fileExists(t *testing.T) {
|
||||||
|
|
||||||
|
_, _ = os.Create(getPlatformStoragePath("exists"))
|
||||||
|
defer os.Remove(getPlatformStoragePath("exists"))
|
||||||
|
|
||||||
|
type args struct {
|
||||||
|
filename string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"should verify that file does not exist", args{"does-not-exist"}, false},
|
||||||
|
{"should verify that file exists", args{"exists"}, true},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := fileExists(tt.args.filename); got != tt.want {
|
||||||
|
t.Errorf("fileExists() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_saveStorageFile(t *testing.T) {
|
||||||
|
|
||||||
|
file, _ := yaml.Marshal(f{a: 1})
|
||||||
|
name := "test"
|
||||||
|
|
||||||
|
saveStorageFile(file, name)
|
||||||
|
|
||||||
|
read, _ := ioutil.ReadFile(getPlatformStoragePath(name))
|
||||||
|
|
||||||
|
if !bytes.Equal(file, read) {
|
||||||
|
t.Errorf("read file != saved file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_readStorageFile(t *testing.T) {
|
||||||
|
|
||||||
|
file, _ := yaml.Marshal(f{a: 1})
|
||||||
|
name := "test"
|
||||||
|
|
||||||
|
_ = ioutil.WriteFile(getPlatformStoragePath(name), file, os.ModePerm)
|
||||||
|
read := readStorageFile(getPlatformStoragePath(name))
|
||||||
|
|
||||||
|
if !bytes.Equal(file, read) {
|
||||||
|
t.Errorf("read file != saved file")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue