From 5e9668c2e4676be1a47ecaa5871f4c857d57c9fe Mon Sep 17 00:00:00 2001 From: sqshq Date: Sat, 3 Aug 2019 16:50:41 -0400 Subject: [PATCH] fixed the issue with decimal comma parsing, tests added --- component/util/parse.go | 1 + component/util/parse_test.go | 2 ++ metadata/storage_test.go | 65 ++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 metadata/storage_test.go diff --git a/component/util/parse.go b/component/util/parse.go index a3212d3..160dfb0 100644 --- a/component/util/parse.go +++ b/component/util/parse.go @@ -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") diff --git a/component/util/parse_test.go b/component/util/parse_test.go index c5f87da..0200d0e 100644 --- a/component/util/parse_test.go +++ b/component/util/parse_test.go @@ -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}, } diff --git a/metadata/storage_test.go b/metadata/storage_test.go new file mode 100644 index 0000000..b1cdeab --- /dev/null +++ b/metadata/storage_test.go @@ -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") + } +}