mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-30 23:16:52 +00:00
Split up common.go, file path fixes and much more
This commit is contained in:
@@ -29,7 +29,7 @@ import "github.com/thrasher-/gocryptotrader/common"
|
||||
|
||||
testString := "aAaAa"
|
||||
|
||||
upper := common.StringToUpper(testString)
|
||||
upper := strings.ToUpper(testString)
|
||||
|
||||
// upper == "AAAAA"
|
||||
```
|
||||
|
||||
108
common/common.go
108
common/common.go
@@ -14,7 +14,6 @@ import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -81,12 +80,6 @@ func StringSliceDifference(slice1, slice2 []string) []string {
|
||||
return diff
|
||||
}
|
||||
|
||||
// StringContains checks a substring if it contains your input then returns a
|
||||
// bool
|
||||
func StringContains(input, substring string) bool {
|
||||
return strings.Contains(input, substring)
|
||||
}
|
||||
|
||||
// StringDataContains checks the substring array with an input and returns a bool
|
||||
func StringDataContains(haystack []string, needle string) bool {
|
||||
data := strings.Join(haystack, ",")
|
||||
@@ -118,45 +111,13 @@ func StringDataCompareInsensitive(haystack []string, needle string) bool {
|
||||
// a bool irrespective of lower or upper case strings
|
||||
func StringDataContainsInsensitive(haystack []string, needle string) bool {
|
||||
for _, data := range haystack {
|
||||
if strings.Contains(StringToUpper(data), StringToUpper(needle)) {
|
||||
if strings.Contains(strings.ToUpper(data), strings.ToUpper(needle)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// JoinStrings joins an array together with the required separator and returns
|
||||
// it as a string
|
||||
func JoinStrings(input []string, separator string) string {
|
||||
return strings.Join(input, separator)
|
||||
}
|
||||
|
||||
// SplitStrings splits blocks of strings from string into a string array using
|
||||
// a separator ie "," or "_"
|
||||
func SplitStrings(input, separator string) []string {
|
||||
return strings.Split(input, separator)
|
||||
}
|
||||
|
||||
// TrimString trims unwanted prefixes or postfixes
|
||||
func TrimString(input, cutset string) string {
|
||||
return strings.Trim(input, cutset)
|
||||
}
|
||||
|
||||
// ReplaceString replaces a string with another
|
||||
func ReplaceString(input, old, newStr string, n int) string {
|
||||
return strings.Replace(input, old, newStr, n)
|
||||
}
|
||||
|
||||
// StringToUpper changes strings to uppercase
|
||||
func StringToUpper(input string) string {
|
||||
return strings.ToUpper(input)
|
||||
}
|
||||
|
||||
// StringToLower changes strings to lowercase
|
||||
func StringToLower(input string) string {
|
||||
return strings.ToLower(input)
|
||||
}
|
||||
|
||||
// IsEnabled takes in a boolean param and returns a string if it is enabled
|
||||
// or disabled
|
||||
func IsEnabled(isEnabled bool) string {
|
||||
@@ -170,7 +131,7 @@ func IsEnabled(isEnabled bool) string {
|
||||
// regexp package // Validation issues occurring because "3" is contained in
|
||||
// litecoin and Bitcoin addresses - non-fatal
|
||||
func IsValidCryptoAddress(address, crypto string) (bool, error) {
|
||||
switch StringToLower(crypto) {
|
||||
switch strings.ToLower(crypto) {
|
||||
case "btc":
|
||||
return regexp.MatchString("^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$", address)
|
||||
case "ltc":
|
||||
@@ -184,7 +145,7 @@ func IsValidCryptoAddress(address, crypto string) (bool, error) {
|
||||
|
||||
// YesOrNo returns a boolean variable to check if input is "y" or "yes"
|
||||
func YesOrNo(input string) bool {
|
||||
if StringToLower(input) == "y" || StringToLower(input) == "yes" {
|
||||
if strings.ToLower(input) == "y" || strings.ToLower(input) == "yes" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@@ -276,7 +237,7 @@ func JSONEncode(v interface{}) ([]byte, error) {
|
||||
|
||||
// JSONDecode decodes JSON data into a structure
|
||||
func JSONDecode(data []byte, to interface{}) error {
|
||||
if !StringContains(reflect.ValueOf(to).Type().String(), "*") {
|
||||
if !strings.Contains(reflect.ValueOf(to).Type().String(), "*") {
|
||||
return errors.New("json decode error - memory address not supplied")
|
||||
}
|
||||
return json.Unmarshal(data, to)
|
||||
@@ -294,7 +255,7 @@ func EncodeURLValues(urlPath string, values url.Values) string {
|
||||
|
||||
// ExtractHost returns the hostname out of a string
|
||||
func ExtractHost(address string) string {
|
||||
host := SplitStrings(address, ":")[0]
|
||||
host := strings.Split(address, ":")[0]
|
||||
if host == "" {
|
||||
return "localhost"
|
||||
}
|
||||
@@ -303,7 +264,7 @@ func ExtractHost(address string) string {
|
||||
|
||||
// ExtractPort returns the port name out of a string
|
||||
func ExtractPort(host string) int {
|
||||
portStr := SplitStrings(host, ":")[1]
|
||||
portStr := strings.Split(host, ":")[1]
|
||||
port, _ := strconv.Atoi(portStr)
|
||||
return port
|
||||
}
|
||||
@@ -386,15 +347,6 @@ func GetExecutablePath() (string, error) {
|
||||
return filepath.Dir(ex), nil
|
||||
}
|
||||
|
||||
// GetOSPathSlash returns the slash used by the operating systems
|
||||
// file system
|
||||
func GetOSPathSlash() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return "\\"
|
||||
}
|
||||
return "/"
|
||||
}
|
||||
|
||||
// UnixMillis converts a UnixNano timestamp to milliseconds
|
||||
func UnixMillis(t time.Time) int64 {
|
||||
return t.UnixNano() / int64(time.Millisecond)
|
||||
@@ -405,54 +357,6 @@ func RecvWindow(d time.Duration) int64 {
|
||||
return int64(d) / int64(time.Millisecond)
|
||||
}
|
||||
|
||||
// FloatFromString format
|
||||
func FloatFromString(raw interface{}) (float64, error) {
|
||||
str, ok := raw.(string)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
|
||||
}
|
||||
flt, err := strconv.ParseFloat(str, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("could not convert value: %s Error: %s", str, err)
|
||||
}
|
||||
return flt, nil
|
||||
}
|
||||
|
||||
// IntFromString format
|
||||
func IntFromString(raw interface{}) (int, error) {
|
||||
str, ok := raw.(string)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
|
||||
}
|
||||
n, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("unable to parse as int: %T", raw)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Int64FromString format
|
||||
func Int64FromString(raw interface{}) (int64, error) {
|
||||
str, ok := raw.(string)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
|
||||
}
|
||||
n, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("unable to parse as int64: %T", raw)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// TimeFromUnixTimestampFloat format
|
||||
func TimeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
|
||||
ts, ok := raw.(float64)
|
||||
if !ok {
|
||||
return time.Time{}, fmt.Errorf("unable to parse, value not float64: %T", raw)
|
||||
}
|
||||
return time.Unix(0, int64(ts)*int64(time.Millisecond)), nil
|
||||
}
|
||||
|
||||
// GetDefaultDataDir returns the default data directory
|
||||
// Windows - C:\Users\%USER%\AppData\Roaming\GoCryptoTrader
|
||||
// Linux/Unix or OSX - $HOME/.gocryptotrader
|
||||
|
||||
@@ -73,28 +73,6 @@ func TestIsValidCryptoAddress(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringToLower(t *testing.T) {
|
||||
t.Parallel()
|
||||
upperCaseString := "HEY MAN"
|
||||
expectedResult := "hey man"
|
||||
actualResult := StringToLower(upperCaseString)
|
||||
if actualResult != expectedResult {
|
||||
t.Errorf("Test failed. Expected '%s'. Actual '%s'",
|
||||
expectedResult, actualResult)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringToUpper(t *testing.T) {
|
||||
t.Parallel()
|
||||
upperCaseString := "hey man"
|
||||
expectedResult := "HEY MAN"
|
||||
actualResult := StringToUpper(upperCaseString)
|
||||
if actualResult != expectedResult {
|
||||
t.Errorf("Test failed. Expected '%s'. Actual '%s'",
|
||||
expectedResult, actualResult)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringSliceDifference(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalInputOne := []string{"hello"}
|
||||
@@ -107,18 +85,6 @@ func TestStringSliceDifference(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringContains(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalInput := "hello"
|
||||
originalInputSubstring := "he"
|
||||
expectedOutput := true
|
||||
actualResult := StringContains(originalInput, originalInputSubstring)
|
||||
if actualResult != expectedOutput {
|
||||
t.Errorf("Test failed. Expected '%v'. Actual '%v'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringDataContains(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalHaystack := []string{"hello", "world", "USDT", "Contains", "string"}
|
||||
@@ -196,64 +162,6 @@ func TestStringDataContainsUpper(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestJoinStrings(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalInputOne := []string{"hello", "moto"}
|
||||
separator := ","
|
||||
expectedOutput := "hello,moto"
|
||||
actualResult := JoinStrings(originalInputOne, separator)
|
||||
if expectedOutput != actualResult {
|
||||
t.Errorf("Test failed. Expected '%s'. Actual '%s'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitStrings(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalInputOne := "hello,moto"
|
||||
separator := ","
|
||||
expectedOutput := []string{"hello", "moto"}
|
||||
actualResult := SplitStrings(originalInputOne, separator)
|
||||
if !reflect.DeepEqual(expectedOutput, actualResult) {
|
||||
t.Errorf("Test failed. Expected '%s'. Actual '%s'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrimString(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalInput := "abcd"
|
||||
cutset := "ad"
|
||||
expectedOutput := "bc"
|
||||
actualResult := TrimString(originalInput, cutset)
|
||||
if expectedOutput != actualResult {
|
||||
t.Errorf("Test failed. Expected '%s'. Actual '%s'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReplaceString replaces a string with another
|
||||
func TestReplaceString(t *testing.T) {
|
||||
t.Parallel()
|
||||
currency := "BTC-USD"
|
||||
expectedOutput := "BTCUSD"
|
||||
|
||||
actualResult := ReplaceString(currency, "-", "", -1)
|
||||
if expectedOutput != actualResult {
|
||||
t.Errorf(
|
||||
"Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult,
|
||||
)
|
||||
}
|
||||
|
||||
currency = "BTC-USD--"
|
||||
actualResult = ReplaceString(currency, "-", "", 3)
|
||||
if expectedOutput != actualResult {
|
||||
t.Errorf(
|
||||
"Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestYesOrNo(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !YesOrNo("y") {
|
||||
@@ -580,14 +488,6 @@ func TestGetExecutablePath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOSPathSlash(t *testing.T) {
|
||||
output := GetOSPathSlash()
|
||||
if output != "/" && output != "\\" {
|
||||
t.Errorf("Test failed. Common GetOSPathSlash. Returned '%s'", output)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUnixMillis(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTime := time.Date(2014, time.October, 28, 0, 32, 0, 0, time.UTC)
|
||||
@@ -612,96 +512,6 @@ func TestRecvWindow(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloatFromString(t *testing.T) {
|
||||
t.Parallel()
|
||||
testString := "1.41421356237"
|
||||
expectedOutput := float64(1.41421356237)
|
||||
|
||||
actualOutput, err := FloatFromString(testString)
|
||||
if actualOutput != expectedOutput || err != nil {
|
||||
t.Errorf("Test failed. Common FloatFromString. Expected '%v'. Actual '%v'. Error: %s",
|
||||
expectedOutput, actualOutput, err)
|
||||
}
|
||||
|
||||
var testByte []byte
|
||||
_, err = FloatFromString(testByte)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common FloatFromString. Converted non-string.")
|
||||
}
|
||||
|
||||
testString = " something unconvertible "
|
||||
_, err = FloatFromString(testString)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common FloatFromString. Converted invalid syntax.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntFromString(t *testing.T) {
|
||||
t.Parallel()
|
||||
testString := "1337"
|
||||
expectedOutput := 1337
|
||||
|
||||
actualOutput, err := IntFromString(testString)
|
||||
if actualOutput != expectedOutput || err != nil {
|
||||
t.Errorf("Test failed. Common IntFromString. Expected '%v'. Actual '%v'. Error: %s",
|
||||
expectedOutput, actualOutput, err)
|
||||
}
|
||||
|
||||
var testByte []byte
|
||||
_, err = IntFromString(testByte)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common IntFromString. Converted non-string.")
|
||||
}
|
||||
|
||||
testString = "1.41421356237"
|
||||
_, err = IntFromString(testString)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common IntFromString. Converted invalid syntax.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt64FromString(t *testing.T) {
|
||||
t.Parallel()
|
||||
testString := "4398046511104"
|
||||
expectedOutput := int64(1 << 42)
|
||||
|
||||
actualOutput, err := Int64FromString(testString)
|
||||
if actualOutput != expectedOutput || err != nil {
|
||||
t.Errorf("Test failed. Common Int64FromString. Expected '%v'. Actual '%v'. Error: %s",
|
||||
expectedOutput, actualOutput, err)
|
||||
}
|
||||
|
||||
var testByte []byte
|
||||
_, err = Int64FromString(testByte)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common Int64FromString. Converted non-string.")
|
||||
}
|
||||
|
||||
testString = "1.41421356237"
|
||||
_, err = Int64FromString(testString)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common Int64FromString. Converted invalid syntax.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeFromUnixTimestampFloat(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTimestamp := float64(1414456320000)
|
||||
expectedOutput := time.Date(2014, time.October, 28, 0, 32, 0, 0, time.UTC)
|
||||
|
||||
actualOutput, err := TimeFromUnixTimestampFloat(testTimestamp)
|
||||
if actualOutput.UTC().String() != expectedOutput.UTC().String() || err != nil {
|
||||
t.Errorf("Test failed. Common TimeFromUnixTimestampFloat. Expected '%v'. Actual '%v'. Error: %s",
|
||||
expectedOutput, actualOutput, err)
|
||||
}
|
||||
|
||||
testString := "Time"
|
||||
_, err = TimeFromUnixTimestampFloat(testString)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common TimeFromUnixTimestampFloat. Converted invalid syntax.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDefaultDataDir(t *testing.T) {
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
@@ -758,7 +568,7 @@ func TestCreateDir(t *testing.T) {
|
||||
if !ok {
|
||||
t.Fatal("LookupEnv failed. APPDATA is not set")
|
||||
}
|
||||
dir = dir + GetOSPathSlash() + "GoCryptoTrader\\TestFileASDFG"
|
||||
dir = filepath.Join(dir, "GoCryptoTrader", "TestFileASDFG")
|
||||
err = CreateDir(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateDir failed. Err: %v", err)
|
||||
@@ -796,13 +606,14 @@ func TestCreateDir(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestChangePerm(t *testing.T) {
|
||||
testDir := filepath.Join(GetDefaultDataDir(runtime.GOOS), "TestFileASDFGHJ")
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
err := ChangePerm("*")
|
||||
if err == nil {
|
||||
t.Fatal("expected an error on non-existent path")
|
||||
}
|
||||
err = os.Mkdir(GetDefaultDataDir(runtime.GOOS)+GetOSPathSlash()+"TestFileASDFGHJ", 0777)
|
||||
err = os.Mkdir(testDir, 0777)
|
||||
if err != nil {
|
||||
t.Fatalf("Mkdir failed. Err: %v", err)
|
||||
}
|
||||
@@ -810,11 +621,11 @@ func TestChangePerm(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("ChangePerm was unsuccessful. Err: %v", err)
|
||||
}
|
||||
_, err = os.Stat(GetDefaultDataDir(runtime.GOOS) + GetOSPathSlash() + "TestFileASDFGHJ")
|
||||
_, err = os.Stat(testDir)
|
||||
if err != nil {
|
||||
t.Fatalf("os.Stat failed. Err: %v", err)
|
||||
}
|
||||
err = RemoveFile(GetDefaultDataDir(runtime.GOOS) + GetOSPathSlash() + "TestFileASDFGHJ")
|
||||
err = RemoveFile(testDir)
|
||||
if err != nil {
|
||||
t.Fatalf("RemoveFile failed. Err: %v", err)
|
||||
}
|
||||
@@ -823,7 +634,7 @@ func TestChangePerm(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("expected an error on non-existent path")
|
||||
}
|
||||
err = os.Mkdir(GetDefaultDataDir(runtime.GOOS)+GetOSPathSlash()+"TestFileASDFGHJ", 0777)
|
||||
err = os.Mkdir(testDir, 0777)
|
||||
if err != nil {
|
||||
t.Fatalf("Mkdir failed. Err: %v", err)
|
||||
}
|
||||
@@ -832,14 +643,14 @@ func TestChangePerm(t *testing.T) {
|
||||
t.Fatalf("ChangePerm was unsuccessful. Err: %v", err)
|
||||
}
|
||||
var a os.FileInfo
|
||||
a, err = os.Stat(GetDefaultDataDir(runtime.GOOS) + GetOSPathSlash() + "TestFileASDFGHJ")
|
||||
a, err = os.Stat(testDir)
|
||||
if err != nil {
|
||||
t.Fatalf("os.Stat failed. Err: %v", err)
|
||||
}
|
||||
if a.Mode().Perm() != 0770 {
|
||||
t.Fatalf("expected file permissions differ. expecting 0770 got %#o", a.Mode().Perm())
|
||||
}
|
||||
err = RemoveFile(GetDefaultDataDir(runtime.GOOS) + GetOSPathSlash() + "TestFileASDFGHJ")
|
||||
err = RemoveFile(testDir)
|
||||
if err != nil {
|
||||
t.Fatalf("RemoveFile failed. Err: %v", err)
|
||||
}
|
||||
|
||||
55
common/convert/convert.go
Normal file
55
common/convert/convert.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// FloatFromString format
|
||||
func FloatFromString(raw interface{}) (float64, error) {
|
||||
str, ok := raw.(string)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
|
||||
}
|
||||
flt, err := strconv.ParseFloat(str, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("could not convert value: %s Error: %s", str, err)
|
||||
}
|
||||
return flt, nil
|
||||
}
|
||||
|
||||
// IntFromString format
|
||||
func IntFromString(raw interface{}) (int, error) {
|
||||
str, ok := raw.(string)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
|
||||
}
|
||||
n, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("unable to parse as int: %T", raw)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Int64FromString format
|
||||
func Int64FromString(raw interface{}) (int64, error) {
|
||||
str, ok := raw.(string)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
|
||||
}
|
||||
n, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("unable to parse as int64: %T", raw)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// TimeFromUnixTimestampFloat format
|
||||
func TimeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
|
||||
ts, ok := raw.(float64)
|
||||
if !ok {
|
||||
return time.Time{}, fmt.Errorf("unable to parse, value not float64: %T", raw)
|
||||
}
|
||||
return time.Unix(0, int64(ts)*int64(time.Millisecond)), nil
|
||||
}
|
||||
96
common/convert/convert_test.go
Normal file
96
common/convert/convert_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFloatFromString(t *testing.T) {
|
||||
t.Parallel()
|
||||
testString := "1.41421356237"
|
||||
expectedOutput := float64(1.41421356237)
|
||||
|
||||
actualOutput, err := FloatFromString(testString)
|
||||
if actualOutput != expectedOutput || err != nil {
|
||||
t.Errorf("Test failed. Common FloatFromString. Expected '%v'. Actual '%v'. Error: %s",
|
||||
expectedOutput, actualOutput, err)
|
||||
}
|
||||
|
||||
var testByte []byte
|
||||
_, err = FloatFromString(testByte)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common FloatFromString. Converted non-string.")
|
||||
}
|
||||
|
||||
testString = " something unconvertible "
|
||||
_, err = FloatFromString(testString)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common FloatFromString. Converted invalid syntax.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntFromString(t *testing.T) {
|
||||
t.Parallel()
|
||||
testString := "1337"
|
||||
expectedOutput := 1337
|
||||
|
||||
actualOutput, err := IntFromString(testString)
|
||||
if actualOutput != expectedOutput || err != nil {
|
||||
t.Errorf("Test failed. Common IntFromString. Expected '%v'. Actual '%v'. Error: %s",
|
||||
expectedOutput, actualOutput, err)
|
||||
}
|
||||
|
||||
var testByte []byte
|
||||
_, err = IntFromString(testByte)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common IntFromString. Converted non-string.")
|
||||
}
|
||||
|
||||
testString = "1.41421356237"
|
||||
_, err = IntFromString(testString)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common IntFromString. Converted invalid syntax.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt64FromString(t *testing.T) {
|
||||
t.Parallel()
|
||||
testString := "4398046511104"
|
||||
expectedOutput := int64(1 << 42)
|
||||
|
||||
actualOutput, err := Int64FromString(testString)
|
||||
if actualOutput != expectedOutput || err != nil {
|
||||
t.Errorf("Test failed. Common Int64FromString. Expected '%v'. Actual '%v'. Error: %s",
|
||||
expectedOutput, actualOutput, err)
|
||||
}
|
||||
|
||||
var testByte []byte
|
||||
_, err = Int64FromString(testByte)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common Int64FromString. Converted non-string.")
|
||||
}
|
||||
|
||||
testString = "1.41421356237"
|
||||
_, err = Int64FromString(testString)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common Int64FromString. Converted invalid syntax.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeFromUnixTimestampFloat(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTimestamp := float64(1414456320000)
|
||||
expectedOutput := time.Date(2014, time.October, 28, 0, 32, 0, 0, time.UTC)
|
||||
|
||||
actualOutput, err := TimeFromUnixTimestampFloat(testTimestamp)
|
||||
if actualOutput.UTC().String() != expectedOutput.UTC().String() || err != nil {
|
||||
t.Errorf("Test failed. Common TimeFromUnixTimestampFloat. Expected '%v'. Actual '%v'. Error: %s",
|
||||
expectedOutput, actualOutput, err)
|
||||
}
|
||||
|
||||
testString := "Time"
|
||||
_, err = TimeFromUnixTimestampFloat(testString)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common TimeFromUnixTimestampFloat. Converted invalid syntax.")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user