github.com/wtfutil/wtf@v0.43.0/view/text_widget_test.go (about) 1 package view 2 3 import ( 4 "testing" 5 6 "github.com/rivo/tview" 7 "github.com/wtfutil/wtf/cfg" 8 ) 9 10 func testTextWidget() TextWidget { 11 txtWid := NewTextWidget( 12 tview.NewApplication(), 13 make(chan bool), 14 tview.NewPages(), 15 &cfg.Common{ 16 Module: cfg.Module{ 17 Name: "test widget", 18 }, 19 }, 20 ) 21 return txtWid 22 } 23 24 func Test_Bordered(t *testing.T) { 25 tests := []struct { 26 name string 27 before func(txtWid TextWidget) TextWidget 28 expected bool 29 }{ 30 { 31 name: "without border", 32 before: func(txtWid TextWidget) TextWidget { 33 txtWid.bordered = false 34 return txtWid 35 }, 36 expected: false, 37 }, 38 { 39 name: "with border", 40 before: func(txtWid TextWidget) TextWidget { 41 txtWid.bordered = true 42 return txtWid 43 }, 44 expected: true, 45 }, 46 } 47 48 for _, tt := range tests { 49 t.Run(tt.name, func(t *testing.T) { 50 txtWid := testTextWidget() 51 txtWid = tt.before(txtWid) 52 actual := txtWid.Bordered() 53 54 if tt.expected != actual { 55 t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) 56 } 57 }) 58 } 59 } 60 61 func Test_Disabled(t *testing.T) { 62 tests := []struct { 63 name string 64 before func(txtWid TextWidget) TextWidget 65 expected bool 66 }{ 67 { 68 name: "when not enabled", 69 before: func(txtWid TextWidget) TextWidget { 70 txtWid.enabled = false 71 return txtWid 72 }, 73 expected: true, 74 }, 75 { 76 name: "when enabled", 77 before: func(txtWid TextWidget) TextWidget { 78 txtWid.enabled = true 79 return txtWid 80 }, 81 expected: false, 82 }, 83 } 84 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 txtWid := testTextWidget() 88 txtWid = tt.before(txtWid) 89 actual := txtWid.Disabled() 90 91 if tt.expected != actual { 92 t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) 93 } 94 }) 95 } 96 } 97 98 func Test_Enabled(t *testing.T) { 99 tests := []struct { 100 name string 101 before func(txtWid TextWidget) TextWidget 102 expected bool 103 }{ 104 { 105 name: "when not enabled", 106 before: func(txtWid TextWidget) TextWidget { 107 txtWid.enabled = false 108 return txtWid 109 }, 110 expected: false, 111 }, 112 { 113 name: "when enabled", 114 before: func(txtWid TextWidget) TextWidget { 115 txtWid.enabled = true 116 return txtWid 117 }, 118 expected: true, 119 }, 120 } 121 122 for _, tt := range tests { 123 t.Run(tt.name, func(t *testing.T) { 124 txtWid := testTextWidget() 125 txtWid = tt.before(txtWid) 126 actual := txtWid.Enabled() 127 128 if tt.expected != actual { 129 t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) 130 } 131 }) 132 } 133 } 134 135 func Test_Focusable(t *testing.T) { 136 tests := []struct { 137 name string 138 before func(txtWid TextWidget) TextWidget 139 expected bool 140 }{ 141 { 142 name: "when not focusable", 143 before: func(txtWid TextWidget) TextWidget { 144 txtWid.enabled = false 145 txtWid.focusable = false 146 return txtWid 147 }, 148 expected: false, 149 }, 150 { 151 name: "when not focusable", 152 before: func(txtWid TextWidget) TextWidget { 153 txtWid.enabled = false 154 txtWid.focusable = true 155 return txtWid 156 }, 157 expected: false, 158 }, 159 { 160 name: "when not focusable", 161 before: func(txtWid TextWidget) TextWidget { 162 txtWid.enabled = true 163 txtWid.focusable = false 164 return txtWid 165 }, 166 expected: false, 167 }, 168 { 169 name: "when focusable", 170 before: func(txtWid TextWidget) TextWidget { 171 txtWid.enabled = true 172 txtWid.focusable = true 173 return txtWid 174 }, 175 expected: true, 176 }, 177 } 178 179 for _, tt := range tests { 180 t.Run(tt.name, func(t *testing.T) { 181 txtWid := testTextWidget() 182 txtWid = tt.before(txtWid) 183 actual := txtWid.Focusable() 184 185 if tt.expected != actual { 186 t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) 187 } 188 }) 189 } 190 } 191 192 func Test_Name(t *testing.T) { 193 txtWid := testTextWidget() 194 actual := txtWid.Name() 195 expected := "test widget" 196 197 if expected != actual { 198 t.Errorf("\nexpected: %s\n got: %s", expected, actual) 199 } 200 } 201 202 func Test_String(t *testing.T) { 203 txtWid := testTextWidget() 204 actual := txtWid.String() 205 expected := "test widget" 206 207 if expected != actual { 208 t.Errorf("\nexpected: %s\n got: %s", expected, actual) 209 } 210 }