chore: update tests and move convert to utils

This commit is contained in:
MedzikUser 2021-08-13 22:28:15 +02:00
parent c87a921cb5
commit 56db0875df
8 changed files with 94 additions and 80 deletions

View File

@ -8,16 +8,16 @@ import (
/*
Info:
common.Log.Info("Info")
Log.Info("Info")
Debug:
common.Log.Debug("Debug")
Log.Debug("Debug")
Error:
common.Log.Error("Error")
Log.Error("Error")
Warn:
common.Log.Warn("Warn")
Log.Warn("Warn")
*/
var Log = &logrus.Logger{
Out: os.Stdout,

View File

@ -3,9 +3,9 @@ package common
import (
"time"
"github.com/MedzikUser/go-utils/convert"
"github.com/MedzikUser/go-utils/utils"
)
func Uptime(start time.Time) string {
return convert.Seconds(time.Since(start))
return utils.FormatSeconds(time.Since(start))
}

View File

@ -1,31 +0,0 @@
package convert
import "testing"
func TestRound1(t *testing.T) {
r := Round(0.1)
if r != 0 {
t.Fatal(r)
}
}
func TestRound2(t *testing.T) {
r := Round(0.9)
if r != 0 {
t.Fatal(r)
}
}
func TestRound3(t *testing.T) {
r := Round(25.2)
if r != 25 {
t.Fatal(r)
}
}
func TestRound4(t *testing.T) {
r := Round(46.9998655)
if r != 46 {
t.Fatal(r)
}
}

View File

@ -1,39 +0,0 @@
package convert
import (
"testing"
"time"
)
func TestSeconds1(t *testing.T) {
d, err := time.ParseDuration("48h1m2s")
if err != nil {
t.Fatal(err)
}
out := Seconds(d)
if out != "2d 1m 2s" {
t.Fatal(out)
}
}
func TestSeconds2(t *testing.T) {
d, err := time.ParseDuration("48h5h1m2s")
if err != nil {
t.Fatal(err)
}
out := Seconds(d)
if out != "2d 5h 1m 2s" {
t.Fatal(out)
}
}
func TestSeconds3(t *testing.T) {
d, err := time.ParseDuration("48h5h60s")
if err != nil {
t.Fatal(err)
}
out := Seconds(d)
if out != "2d 5h 1m" {
t.Fatal(out)
}
}

View File

@ -1,4 +1,4 @@
package convert
package utils
func Round(val float64) int {
if val < 0 {

29
utils/round_test.go Normal file
View File

@ -0,0 +1,29 @@
package utils
import "testing"
func TestRound(t *testing.T) {
// 1
r := Round(0.1)
if r != 0 {
t.Fatal(r)
}
// 2
r = Round(0.9)
if r != 0 {
t.Fatal(r)
}
// 3
r = Round(25.2)
if r != 25 {
t.Fatal(r)
}
// 4
r = Round(46.9998655)
if r != 46 {
t.Fatal(r)
}
}

View File

@ -1,12 +1,11 @@
package convert
package utils
import (
"strconv"
"time"
)
// FIXME:
func Seconds(s time.Duration) string {
func FormatSeconds(s time.Duration) string {
hours := Round(s.Hours())
minutes := Round(s.Minutes())
seconds := Round(s.Seconds())
@ -55,5 +54,9 @@ func Seconds(s time.Duration) string {
format += strconv.Itoa(seconds) + "s"
}
if len(format) == 0 {
format = "0s"
}
return format
}

52
utils/seconds_test.go Normal file
View File

@ -0,0 +1,52 @@
package utils
import (
"testing"
"time"
)
func TestFormatSeconds(t *testing.T) {
// 1
d, err := time.ParseDuration("48h1m2s")
if err != nil {
t.Fatal(err)
}
out := FormatSeconds(d)
if out != "2d 1m 2s" {
t.Fatal(out)
}
// 2
d, err = time.ParseDuration("48h5h1m2s")
if err != nil {
t.Fatal(err)
}
out = FormatSeconds(d)
if out != "2d 5h 1m 2s" {
t.Fatal(out)
}
// 3
d, err = time.ParseDuration("48h5h60s")
if err != nil {
t.Fatal(err)
}
out = FormatSeconds(d)
if out != "2d 5h 1m" {
t.Fatal(out)
}
// 4
d, err = time.ParseDuration("0s")
if err != nil {
t.Fatal(err)
}
out = FormatSeconds(d)
if out != "0s" {
t.Fatal(out)
}
}