Merge pull request #10 from sqshq/float_parse_for_decimal_comma

Fixed the issue with decimal comma parsing
This commit is contained in:
Alexander Lukyanchikov 2019-08-03 17:24:01 -04:00 committed by GitHub
commit 929dbdc848
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
func ParseFloat(input string) (float64, error) {
clean := strings.TrimSpace(input)
clean = strings.Replace(clean, ",", ".", -1) // replace decimal comma with decimal point
if strings.Contains(clean, "\n") {
lastIndex := strings.LastIndex(clean, "\n")

View File

@ -13,6 +13,8 @@ func TestParseFloat(t *testing.T) {
wantErr bool
}{
{"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 last line in the given string", args{"123\n456"}, 456, false},
}

78
metadata/storage_test.go Normal file
View File

@ -0,0 +1,78 @@
package metadata
import (
"bytes"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"testing"
)
type f struct {
a int
}
func Test_fileExists(t *testing.T) {
initStorage()
_, err := os.Create(getPlatformStoragePath("exists"))
if err != nil {
panic(err)
}
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) {
initStorage()
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) {
initStorage()
file, _ := yaml.Marshal(f{a: 1})
name := "test"
err := ioutil.WriteFile(getPlatformStoragePath(name), file, os.ModePerm)
if err != nil {
panic(err)
}
read := readStorageFile(getPlatformStoragePath(name))
if !bytes.Equal(file, read) {
t.Errorf("read file != saved file")
}
}