github.com/wtfutil/wtf@v0.43.0/checklist/checklist_test.go (about)

     1  package checklist
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func Test_NewCheckist(t *testing.T) {
    10  	cl := NewChecklist("o", "-")
    11  
    12  	assert.IsType(t, Checklist{}, cl)
    13  	assert.Equal(t, "o", cl.checkedIcon)
    14  	assert.Equal(t, -1, cl.selected)
    15  	assert.Equal(t, "-", cl.uncheckedIcon)
    16  	assert.Equal(t, 0, len(cl.Items))
    17  }
    18  
    19  func Test_Add(t *testing.T) {
    20  	cl := NewChecklist("o", "-")
    21  	cl.Add(true, nil, make([]string, 0), "test item")
    22  
    23  	assert.Equal(t, 1, len(cl.Items))
    24  }
    25  
    26  func Test_CheckedItems(t *testing.T) {
    27  	tests := []struct {
    28  		name        string
    29  		expectedLen int
    30  		checkedLen  int
    31  		before      func(cl *Checklist)
    32  	}{
    33  		{
    34  			name:        "with no items",
    35  			expectedLen: 0,
    36  			checkedLen:  0,
    37  			before:      func(cl *Checklist) {},
    38  		},
    39  		{
    40  			name:        "with no checked items",
    41  			expectedLen: 1,
    42  			checkedLen:  0,
    43  			before: func(cl *Checklist) {
    44  				cl.Add(false, nil, make([]string, 0), "unchecked item")
    45  			},
    46  		},
    47  		{
    48  			name:        "with one checked item",
    49  			expectedLen: 2,
    50  			checkedLen:  1,
    51  			before: func(cl *Checklist) {
    52  				cl.Add(false, nil, make([]string, 0), "unchecked item")
    53  				cl.Add(true, nil, make([]string, 0), "checked item")
    54  			},
    55  		},
    56  		{
    57  			name:        "with multiple checked items",
    58  			expectedLen: 3,
    59  			checkedLen:  2,
    60  			before: func(cl *Checklist) {
    61  				cl.Add(false, nil, make([]string, 0), "unchecked item")
    62  				cl.Add(true, nil, make([]string, 0), "checked item 11")
    63  				cl.Add(true, nil, make([]string, 0), "checked item 2")
    64  			},
    65  		},
    66  	}
    67  
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			cl := NewChecklist("o", "-")
    71  			tt.before(&cl)
    72  
    73  			assert.Equal(t, tt.expectedLen, len(cl.Items))
    74  			assert.Equal(t, tt.checkedLen, len(cl.CheckedItems()))
    75  		})
    76  	}
    77  }
    78  
    79  func Test_Delete(t *testing.T) {
    80  	tests := []struct {
    81  		name        string
    82  		idx         int
    83  		expectedLen int
    84  	}{
    85  		{
    86  			name:        "with valid index",
    87  			idx:         0,
    88  			expectedLen: 0,
    89  		},
    90  		{
    91  			name:        "with invalid index",
    92  			idx:         2,
    93  			expectedLen: 1,
    94  		},
    95  	}
    96  
    97  	for _, tt := range tests {
    98  		t.Run(tt.name, func(t *testing.T) {
    99  			cl := NewChecklist("o", "-")
   100  
   101  			cl.Add(true, nil, make([]string, 0), "test item")
   102  			cl.Delete(tt.idx)
   103  
   104  			assert.Equal(t, tt.expectedLen, len(cl.Items))
   105  		})
   106  	}
   107  }
   108  
   109  func Test_IsSelectable(t *testing.T) {
   110  	tests := []struct {
   111  		name     string
   112  		selected int
   113  		expected bool
   114  	}{
   115  		{
   116  			name:     "nothing selected",
   117  			selected: -1,
   118  			expected: false,
   119  		},
   120  		{
   121  			name:     "valid selection",
   122  			selected: 1,
   123  			expected: true,
   124  		},
   125  		{
   126  			name:     "invalid selection",
   127  			selected: 3,
   128  			expected: false,
   129  		},
   130  	}
   131  
   132  	for _, tt := range tests {
   133  		t.Run(tt.name, func(t *testing.T) {
   134  			cl := NewChecklist("o", "-")
   135  			cl.Add(true, nil, make([]string, 0), "test item 1")
   136  			cl.Add(false, nil, make([]string, 0), "test item 2")
   137  
   138  			cl.selected = tt.selected
   139  
   140  			assert.Equal(t, tt.expected, cl.IsSelectable())
   141  		})
   142  	}
   143  }
   144  
   145  func Test_IsUnselectable(t *testing.T) {
   146  	tests := []struct {
   147  		name     string
   148  		selected int
   149  		expected bool
   150  	}{
   151  		{
   152  			name:     "nothing selected",
   153  			selected: -1,
   154  			expected: true,
   155  		},
   156  		{
   157  			name:     "valid selection",
   158  			selected: 1,
   159  			expected: false,
   160  		},
   161  		{
   162  			name:     "invalid selection",
   163  			selected: 3,
   164  			expected: true,
   165  		},
   166  	}
   167  
   168  	for _, tt := range tests {
   169  		t.Run(tt.name, func(t *testing.T) {
   170  			cl := NewChecklist("o", "-")
   171  			cl.Add(true, nil, make([]string, 0), "test item 1")
   172  			cl.Add(false, nil, make([]string, 0), "test item 2")
   173  
   174  			cl.selected = tt.selected
   175  
   176  			assert.Equal(t, tt.expected, cl.IsUnselectable())
   177  		})
   178  	}
   179  }
   180  
   181  func Test_LongestLine(t *testing.T) {
   182  	tests := []struct {
   183  		name        string
   184  		expectedLen int
   185  		before      func(cl *Checklist)
   186  	}{
   187  		{
   188  			name:        "with no items",
   189  			expectedLen: 0,
   190  			before:      func(cl *Checklist) {},
   191  		},
   192  		{
   193  			name:        "with different-length items",
   194  			expectedLen: 12,
   195  			before: func(cl *Checklist) {
   196  				cl.Add(true, nil, make([]string, 0), "test item 1")
   197  				cl.Add(false, nil, make([]string, 0), "test item 22")
   198  			},
   199  		},
   200  		{
   201  			name:        "with same-length items",
   202  			expectedLen: 11,
   203  			before: func(cl *Checklist) {
   204  				cl.Add(true, nil, make([]string, 0), "test item 1")
   205  				cl.Add(false, nil, make([]string, 0), "test item 2")
   206  			},
   207  		},
   208  	}
   209  
   210  	for _, tt := range tests {
   211  		t.Run(tt.name, func(t *testing.T) {
   212  			cl := NewChecklist("o", "-")
   213  			tt.before(&cl)
   214  
   215  			assert.Equal(t, tt.expectedLen, cl.LongestLine())
   216  		})
   217  	}
   218  }
   219  
   220  func Test_IndexByItem(t *testing.T) {
   221  	cl := NewChecklist("o", "-")
   222  	cl.Add(false, nil, make([]string, 0), "unchecked item")
   223  	cl.Add(true, nil, make([]string, 0), "checked item")
   224  
   225  	tests := []struct {
   226  		name        string
   227  		item        *ChecklistItem
   228  		expectedIdx int
   229  		expectedOk  bool
   230  	}{
   231  		{
   232  			name:        "with nil",
   233  			item:        nil,
   234  			expectedIdx: 0,
   235  			expectedOk:  false,
   236  		},
   237  		{
   238  			name:        "with valid item",
   239  			item:        cl.Items[1],
   240  			expectedIdx: 1,
   241  			expectedOk:  true,
   242  		},
   243  		{
   244  			name:        "with valid item",
   245  			item:        NewChecklistItem(false, nil, make([]string, 0), "invalid", "x", " "),
   246  			expectedIdx: 0,
   247  			expectedOk:  false,
   248  		},
   249  	}
   250  
   251  	for _, tt := range tests {
   252  		t.Run(tt.name, func(t *testing.T) {
   253  
   254  			idx, ok := cl.IndexByItem(tt.item)
   255  
   256  			assert.Equal(t, tt.expectedIdx, idx)
   257  			assert.Equal(t, tt.expectedOk, ok)
   258  		})
   259  	}
   260  }
   261  
   262  func Test_UncheckedItems(t *testing.T) {
   263  	tests := []struct {
   264  		name        string
   265  		expectedLen int
   266  		checkedLen  int
   267  		before      func(cl *Checklist)
   268  	}{
   269  		{
   270  			name:        "with no items",
   271  			expectedLen: 0,
   272  			checkedLen:  0,
   273  			before:      func(cl *Checklist) {},
   274  		},
   275  		{
   276  			name:        "with no unchecked items",
   277  			expectedLen: 1,
   278  			checkedLen:  0,
   279  			before: func(cl *Checklist) {
   280  				cl.Add(true, nil, make([]string, 0), "unchecked item")
   281  			},
   282  		},
   283  		{
   284  			name:        "with one unchecked item",
   285  			expectedLen: 2,
   286  			checkedLen:  1,
   287  			before: func(cl *Checklist) {
   288  				cl.Add(false, nil, make([]string, 0), "unchecked item")
   289  				cl.Add(true, nil, make([]string, 0), "checked item")
   290  			},
   291  		},
   292  		{
   293  			name:        "with multiple unchecked items",
   294  			expectedLen: 3,
   295  			checkedLen:  2,
   296  			before: func(cl *Checklist) {
   297  				cl.Add(false, nil, make([]string, 0), "unchecked item")
   298  				cl.Add(true, nil, make([]string, 0), "checked item 11")
   299  				cl.Add(false, nil, make([]string, 0), "checked item 2")
   300  			},
   301  		},
   302  	}
   303  
   304  	for _, tt := range tests {
   305  		t.Run(tt.name, func(t *testing.T) {
   306  			cl := NewChecklist("o", "-")
   307  			tt.before(&cl)
   308  
   309  			assert.Equal(t, tt.expectedLen, len(cl.Items))
   310  			assert.Equal(t, tt.checkedLen, len(cl.UncheckedItems()))
   311  		})
   312  	}
   313  }
   314  
   315  func Test_Unselect(t *testing.T) {
   316  	cl := NewChecklist("o", "-")
   317  	cl.Add(false, nil, make([]string, 0), "unchecked item")
   318  
   319  	cl.selected = 0
   320  	assert.Equal(t, 0, cl.selected)
   321  
   322  	cl.Unselect()
   323  	assert.Equal(t, -1, cl.selected)
   324  }
   325  
   326  /* -------------------- Sort Interface -------------------- */
   327  
   328  func Test_Len(t *testing.T) {
   329  	tests := []struct {
   330  		name        string
   331  		expectedLen int
   332  		before      func(cl *Checklist)
   333  	}{
   334  		{
   335  			name:        "with no items",
   336  			expectedLen: 0,
   337  			before:      func(cl *Checklist) {},
   338  		},
   339  		{
   340  			name:        "with one item",
   341  			expectedLen: 1,
   342  			before: func(cl *Checklist) {
   343  				cl.Add(false, nil, make([]string, 0), "unchecked item")
   344  			},
   345  		},
   346  		{
   347  			name:        "with multiple items",
   348  			expectedLen: 3,
   349  			before: func(cl *Checklist) {
   350  				cl.Add(false, nil, make([]string, 0), "unchecked item")
   351  				cl.Add(true, nil, make([]string, 0), "checked item 1")
   352  				cl.Add(false, nil, make([]string, 0), "checked item 2")
   353  			},
   354  		},
   355  	}
   356  
   357  	for _, tt := range tests {
   358  		t.Run(tt.name, func(t *testing.T) {
   359  			cl := NewChecklist("o", "-")
   360  			tt.before(&cl)
   361  
   362  			assert.Equal(t, tt.expectedLen, cl.Len())
   363  		})
   364  	}
   365  }
   366  
   367  func Test_Less(t *testing.T) {
   368  	tests := []struct {
   369  		name     string
   370  		first    string
   371  		second   string
   372  		expected bool
   373  	}{
   374  		{
   375  			name:     "same",
   376  			first:    "",
   377  			second:   "",
   378  			expected: false,
   379  		},
   380  		{
   381  			name:     "last less",
   382  			first:    "beta",
   383  			second:   "alpha",
   384  			expected: true,
   385  		},
   386  		{
   387  			name:     "first less",
   388  			first:    "alpha",
   389  			second:   "beta",
   390  			expected: false,
   391  		},
   392  	}
   393  
   394  	for _, tt := range tests {
   395  		t.Run(tt.name, func(t *testing.T) {
   396  			cl := NewChecklist("o", "-")
   397  			cl.Add(false, nil, make([]string, 0), tt.first)
   398  			cl.Add(false, nil, make([]string, 0), tt.second)
   399  
   400  			assert.Equal(t, tt.expected, cl.Less(0, 1))
   401  		})
   402  	}
   403  }
   404  
   405  func Test_Swap(t *testing.T) {
   406  	tests := []struct {
   407  		name     string
   408  		first    string
   409  		second   string
   410  		expected bool
   411  	}{
   412  		{
   413  			name:   "same",
   414  			first:  "",
   415  			second: "",
   416  		},
   417  		{
   418  			name:   "last less",
   419  			first:  "alpha",
   420  			second: "beta",
   421  		},
   422  	}
   423  
   424  	for _, tt := range tests {
   425  		t.Run(tt.name, func(t *testing.T) {
   426  			cl := NewChecklist("o", "-")
   427  			cl.Add(false, nil, make([]string, 0), tt.first)
   428  			cl.Add(false, nil, make([]string, 0), tt.second)
   429  
   430  			cl.Swap(0, 1)
   431  
   432  			assert.Equal(t, tt.expected, cl.Items[0].Text == "beta")
   433  			assert.Equal(t, tt.expected, cl.Items[1].Text == "alpha")
   434  		})
   435  	}
   436  }