github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/utils/utils_test.go (about) 1 package utils 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestFuzzyAgo(t *testing.T) { 9 cases := map[string]string{ 10 "1s": "less than a minute ago", 11 "30s": "less than a minute ago", 12 "1m08s": "about 1 minute ago", 13 "15m0s": "about 15 minutes ago", 14 "59m10s": "about 59 minutes ago", 15 "1h10m02s": "about 1 hour ago", 16 "15h0m01s": "about 15 hours ago", 17 "30h10m": "about 1 day ago", 18 "50h": "about 2 days ago", 19 "720h05m": "about 1 month ago", 20 "3000h10m": "about 4 months ago", 21 "8760h59m": "about 1 year ago", 22 "17601h59m": "about 2 years ago", 23 "262800h19m": "about 30 years ago", 24 } 25 26 for duration, expected := range cases { 27 d, e := time.ParseDuration(duration) 28 if e != nil { 29 t.Errorf("failed to create a duration: %s", e) 30 } 31 32 fuzzy := FuzzyAgo(d) 33 if fuzzy != expected { 34 t.Errorf("unexpected fuzzy duration value: %s for %s", fuzzy, duration) 35 } 36 } 37 } 38 39 func TestFuzzyAgoAbbr(t *testing.T) { 40 const form = "2006-Jan-02 15:04:05" 41 now, _ := time.Parse(form, "2020-Nov-22 14:00:00") 42 43 cases := map[string]string{ 44 "2020-Nov-22 14:00:00": "0m", 45 "2020-Nov-22 13:59:00": "1m", 46 "2020-Nov-22 13:30:00": "30m", 47 "2020-Nov-22 13:00:00": "1h", 48 "2020-Nov-22 02:00:00": "12h", 49 "2020-Nov-21 14:00:00": "1d", 50 "2020-Nov-07 14:00:00": "15d", 51 "2020-Oct-24 14:00:00": "29d", 52 "2020-Oct-23 14:00:00": "Oct 23, 2020", 53 "2019-Nov-22 14:00:00": "Nov 22, 2019", 54 } 55 56 for createdAt, expected := range cases { 57 d, _ := time.Parse(form, createdAt) 58 fuzzy := FuzzyAgoAbbr(now, d) 59 if fuzzy != expected { 60 t.Errorf("unexpected fuzzy duration abbr value: %s for %s", fuzzy, createdAt) 61 } 62 } 63 }