github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/internal/text/text_test.go (about)

     1  package text
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestRemoveExcessiveWhitespace(t *testing.T) {
    11  	tests := []struct {
    12  		name  string
    13  		input string
    14  		want  string
    15  	}{
    16  		{
    17  			name:  "nothing to remove",
    18  			input: "one two three",
    19  			want:  "one two three",
    20  		},
    21  		{
    22  			name:  "whitespace b-gone",
    23  			input: "\n  one\n\t  two  three\r\n  ",
    24  			want:  "one two three",
    25  		},
    26  	}
    27  	for _, tt := range tests {
    28  		t.Run(tt.name, func(t *testing.T) {
    29  			got := RemoveExcessiveWhitespace(tt.input)
    30  			assert.Equal(t, tt.want, got)
    31  		})
    32  	}
    33  }
    34  
    35  func TestFuzzyAgoAbbr(t *testing.T) {
    36  	const form = "2006-Jan-02 15:04:05"
    37  	now, _ := time.Parse(form, "2020-Nov-22 14:00:00")
    38  	cases := map[string]string{
    39  		"2020-Nov-22 14:00:00": "0m",
    40  		"2020-Nov-22 13:59:00": "1m",
    41  		"2020-Nov-22 13:30:00": "30m",
    42  		"2020-Nov-22 13:00:00": "1h",
    43  		"2020-Nov-22 02:00:00": "12h",
    44  		"2020-Nov-21 14:00:00": "1d",
    45  		"2020-Nov-07 14:00:00": "15d",
    46  		"2020-Oct-24 14:00:00": "29d",
    47  		"2020-Oct-23 14:00:00": "Oct 23, 2020",
    48  		"2019-Nov-22 14:00:00": "Nov 22, 2019",
    49  	}
    50  	for createdAt, expected := range cases {
    51  		d, err := time.Parse(form, createdAt)
    52  		assert.NoError(t, err)
    53  		fuzzy := FuzzyAgoAbbr(now, d)
    54  		assert.Equal(t, expected, fuzzy)
    55  	}
    56  }