mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-07-20 10:04:08 +00:00
Tests
This commit is contained in:
parent
0080ea5a20
commit
a160da3ad9
4 changed files with 100 additions and 83 deletions
32
util/util.go
32
util/util.go
|
@ -121,38 +121,6 @@ func ValidRandomString(s string, length int) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
// DurationToHuman converts a duration to a human-readable format
|
||||
func DurationToHuman(d time.Duration) (str string) {
|
||||
if d == 0 {
|
||||
return "0"
|
||||
}
|
||||
|
||||
d = d.Round(time.Second)
|
||||
days := d / time.Hour / 24
|
||||
if days > 0 {
|
||||
str += fmt.Sprintf("%dd", days)
|
||||
}
|
||||
d -= days * time.Hour * 24
|
||||
|
||||
hours := d / time.Hour
|
||||
if hours > 0 {
|
||||
str += fmt.Sprintf("%dh", hours)
|
||||
}
|
||||
d -= hours * time.Hour
|
||||
|
||||
minutes := d / time.Minute
|
||||
if minutes > 0 {
|
||||
str += fmt.Sprintf("%dm", minutes)
|
||||
}
|
||||
d -= minutes * time.Minute
|
||||
|
||||
seconds := d / time.Second
|
||||
if seconds > 0 {
|
||||
str += fmt.Sprintf("%ds", seconds)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ParsePriority parses a priority string into its equivalent integer value
|
||||
func ParsePriority(priority string) (int, error) {
|
||||
switch strings.TrimSpace(strings.ToLower(priority)) {
|
||||
|
|
|
@ -5,33 +5,8 @@ import (
|
|||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDurationToHuman_SevenDays(t *testing.T) {
|
||||
d := 7 * 24 * time.Hour
|
||||
require.Equal(t, "7d", DurationToHuman(d))
|
||||
}
|
||||
|
||||
func TestDurationToHuman_MoreThanOneDay(t *testing.T) {
|
||||
d := 49 * time.Hour
|
||||
require.Equal(t, "2d1h", DurationToHuman(d))
|
||||
}
|
||||
|
||||
func TestDurationToHuman_LessThanOneDay(t *testing.T) {
|
||||
d := 17*time.Hour + 15*time.Minute
|
||||
require.Equal(t, "17h15m", DurationToHuman(d))
|
||||
}
|
||||
|
||||
func TestDurationToHuman_TenOfThings(t *testing.T) {
|
||||
d := 10*time.Hour + 10*time.Minute + 10*time.Second
|
||||
require.Equal(t, "10h10m10s", DurationToHuman(d))
|
||||
}
|
||||
|
||||
func TestDurationToHuman_Zero(t *testing.T) {
|
||||
require.Equal(t, "0", DurationToHuman(0))
|
||||
}
|
||||
|
||||
func TestRandomString(t *testing.T) {
|
||||
s1 := RandomString(10)
|
||||
s2 := RandomString(10)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue