time_test.go 764 B

1234567891011121314151617181920212223242526272829303132333435
  1. package format
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func assertEqual(t *testing.T, a interface{}, b interface{}) {
  7. if a != b {
  8. t.Errorf("Assert failed, expected %v, got %v", b, a)
  9. }
  10. }
  11. func TestHumanTime(t *testing.T) {
  12. now := time.Now()
  13. t.Run("zero value", func(t *testing.T) {
  14. assertEqual(t, HumanTime(time.Time{}, "never"), "never")
  15. })
  16. t.Run("time in the future", func(t *testing.T) {
  17. v := now.Add(48 * time.Hour)
  18. assertEqual(t, HumanTime(v, ""), "2 days from now")
  19. })
  20. t.Run("time in the past", func(t *testing.T) {
  21. v := now.Add(-48 * time.Hour)
  22. assertEqual(t, HumanTime(v, ""), "2 days ago")
  23. })
  24. t.Run("soon", func(t *testing.T) {
  25. v := now.Add(800*time.Millisecond)
  26. assertEqual(t, HumanTime(v, ""), "Less than a second from now")
  27. })
  28. }