github.com/wtfutil/wtf@v0.43.0/cfg/common_settings_test.go (about)

     1  package cfg
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/olebedev/config"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  var (
    12  	testYaml = `
    13  wtf:
    14    colors:
    15  `
    16  
    17  	moduleConfig, _   = config.ParseYaml(testYaml)
    18  	globalSettings, _ = config.ParseYaml(testYaml)
    19  
    20  	testCfg = NewCommonSettingsFromModule(
    21  		"test",
    22  		"Test Config",
    23  		true,
    24  		moduleConfig,
    25  		globalSettings,
    26  	)
    27  )
    28  
    29  func Test_NewCommonSettingsFromModule(t *testing.T) {
    30  	assert.Equal(t, true, testCfg.Bordered)
    31  	assert.Equal(t, false, testCfg.Enabled)
    32  	assert.Equal(t, true, testCfg.Focusable)
    33  	assert.Equal(t, "test", testCfg.Module.Name)
    34  	assert.Equal(t, "test", testCfg.Module.Type)
    35  	assert.Equal(t, "", testCfg.FocusChar())
    36  	assert.Equal(t, 300*time.Second, testCfg.RefreshInterval)
    37  	assert.Equal(t, "Test Config", testCfg.Title)
    38  }
    39  
    40  func Test_DefaultFocusedRowColor(t *testing.T) {
    41  	assert.Equal(t, "black:green", testCfg.DefaultFocusedRowColor())
    42  }
    43  
    44  func Test_DefaultRowColor(t *testing.T) {
    45  	assert.Equal(t, "white:transparent", testCfg.DefaultRowColor())
    46  }
    47  
    48  func Test_FocusChar(t *testing.T) {
    49  	tests := []struct {
    50  		name         string
    51  		before       func(testCfg *Common)
    52  		expectedChar string
    53  	}{
    54  		{
    55  			name: "with negative focus char",
    56  			before: func(testCfg *Common) {
    57  				testCfg.focusChar = -1
    58  			},
    59  			expectedChar: "",
    60  		},
    61  		{
    62  			name: "with positive focus char",
    63  			before: func(testCfg *Common) {
    64  				testCfg.focusChar = 3
    65  			},
    66  			expectedChar: "3",
    67  		},
    68  		{
    69  			name: "with zero focus char",
    70  			before: func(testCfg *Common) {
    71  				testCfg.focusChar = 0
    72  			},
    73  			expectedChar: "",
    74  		},
    75  		{
    76  			name: "with large focus char",
    77  			before: func(testCfg *Common) {
    78  				testCfg.focusChar = 10
    79  			},
    80  			expectedChar: "",
    81  		},
    82  	}
    83  
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			tt.before(testCfg)
    87  
    88  			assert.Equal(t, tt.expectedChar, testCfg.FocusChar())
    89  		})
    90  	}
    91  }
    92  
    93  func Test_RowColor(t *testing.T) {
    94  	tests := []struct {
    95  		name          string
    96  		idx           int
    97  		expectedColor string
    98  	}{
    99  		{
   100  			name:          "odd rows, default",
   101  			idx:           3,
   102  			expectedColor: "lightblue:transparent",
   103  		},
   104  		{
   105  			name:          "even rows, default",
   106  			idx:           8,
   107  			expectedColor: "white:transparent",
   108  		},
   109  	}
   110  
   111  	for _, tt := range tests {
   112  		t.Run(tt.name, func(t *testing.T) {
   113  			assert.Equal(t, tt.expectedColor, testCfg.RowColor(tt.idx))
   114  		})
   115  	}
   116  }
   117  
   118  func Test_RightAlignFormat(t *testing.T) {
   119  	tests := []struct {
   120  		name     string
   121  		width    int
   122  		expected string
   123  	}{
   124  		{
   125  			name:     "with zero",
   126  			width:    0,
   127  			expected: "%-2s",
   128  		},
   129  		{
   130  			name:     "with positive integer",
   131  			width:    3,
   132  			expected: "%1s",
   133  		},
   134  		{
   135  			name:     "with negative integer",
   136  			width:    -3,
   137  			expected: "%-5s",
   138  		},
   139  	}
   140  
   141  	for _, tt := range tests {
   142  		t.Run(tt.name, func(t *testing.T) {
   143  			assert.Equal(t, tt.expected, testCfg.RightAlignFormat(tt.width))
   144  		})
   145  	}
   146  }
   147  
   148  func Test_PaginationMarker(t *testing.T) {
   149  	tests := []struct {
   150  		name     string
   151  		len      int
   152  		pos      int
   153  		width    int
   154  		expected string
   155  	}{
   156  		{
   157  			name:     "with zero pages",
   158  			len:      0,
   159  			pos:      1,
   160  			width:    5,
   161  			expected: "",
   162  		},
   163  		{
   164  			name:     "with one page",
   165  			len:      1,
   166  			pos:      1,
   167  			width:    5,
   168  			expected: "",
   169  		},
   170  		{
   171  			name:     "with multiple pages",
   172  			len:      3,
   173  			pos:      1,
   174  			width:    5,
   175  			expected: "[lightblue]*_*[white]",
   176  		},
   177  		{
   178  			name:     "with negative pages",
   179  			len:      -3,
   180  			pos:      1,
   181  			width:    5,
   182  			expected: "",
   183  		},
   184  	}
   185  
   186  	for _, tt := range tests {
   187  		t.Run(tt.name, func(t *testing.T) {
   188  			assert.Equal(t, tt.expected, testCfg.PaginationMarker(tt.len, tt.pos, tt.width))
   189  		})
   190  	}
   191  }
   192  
   193  func Test_Validations(t *testing.T) {
   194  	assert.Equal(t, 4, len(testCfg.Validations()))
   195  }