| 1 | package generator |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "regexp" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/stretchr/testify/assert" |
| 11 | "github.com/stretchr/testify/require" |
| 12 | ) |
| 13 | |
| 14 | func TestNewUserDialog(t *testing.T) { |
| 15 | t.Parallel() |
| 16 | |
| 17 | type args struct { |
| 18 | reader io.Reader |
| 19 | props map[string]string |
| 20 | } |
| 21 | |
| 22 | tests := []struct { |
| 23 | name string |
| 24 | args args |
| 25 | wantWriter string |
| 26 | }{ |
| 27 | { |
| 28 | name: "with nil props creates empty map", |
| 29 | args: args{ |
| 30 | reader: bytes.NewBufferString("test input\n"), |
| 31 | props: nil, |
| 32 | }, |
| 33 | wantWriter: "", |
| 34 | }, |
| 35 | { |
| 36 | name: "with existing props preserves them", |
| 37 | args: args{ |
| 38 | reader: bytes.NewBufferString("test input\n"), |
| 39 | props: map[string]string{ |
| 40 | "key1": "value1", |
| 41 | "key2": "value2", |
| 42 | }, |
| 43 | }, |
| 44 | wantWriter: "", |
| 45 | }, |
| 46 | { |
| 47 | name: "with empty props map", |
| 48 | args: args{ |
| 49 | reader: bytes.NewBufferString("test input\n"), |
| 50 | props: map[string]string{}, |
| 51 | }, |
| 52 | wantWriter: "", |
| 53 | }, |
| 54 | } |
| 55 | for _, tt := range tests { |
| 56 | t.Run(tt.name, func(t *testing.T) { |
| 57 | t.Parallel() |
| 58 | |
| 59 | writer := &bytes.Buffer{} |
| 60 | got := NewUserDialog(tt.args.reader, writer, tt.args.props) |
| 61 | |
| 62 | require.NotNil(t, got) |
| 63 | assert.NotNil(t, got.scanner) |
| 64 | assert.Equal(t, tt.args.reader, got.reader) |
| 65 | assert.Equal(t, writer, got.writer) |
| 66 | |
| 67 | if tt.args.props == nil { |
| 68 | assert.NotNil(t, got.props) |
| 69 | assert.Empty(t, got.props) |
| 70 | } else { |
| 71 | assert.Equal(t, tt.args.props, got.props) |
| 72 | } |
| 73 | |
| 74 | assert.Equal(t, tt.wantWriter, writer.String()) |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestUserDialog_Query(t *testing.T) { |
| 80 | t.Parallel() |
| 81 | |
| 82 | emailRegex := regexp.MustCompile(`^([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,})$`) |
| 83 | ipRegex := regexp.MustCompile(`^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$`) |
| 84 | testRegex := regexp.MustCompile(`^test_(\w+)$`) |
| 85 | |
| 86 | type args struct { |
| 87 | prompt string |
| 88 | validator *regexp.Regexp |
| 89 | key string |
| 90 | } |
| 91 | |
| 92 | tests := []struct { |
| 93 | name string |
| 94 | ud *UserDialog |
| 95 | args args |
| 96 | want []string |
| 97 | wantErr bool |
| 98 | wantOut string |
| 99 | }{ |
| 100 | { |
| 101 | name: "valid email with capture groups", |
| 102 | ud: NewUserDialog( |
| 103 | bytes.NewBufferString("[email protected]\n"), |
| 104 | &bytes.Buffer{}, |
| 105 | nil, |
| 106 | ), |
| 107 | args: args{ |
| 108 | prompt: "Enter email:", |
| 109 | validator: emailRegex, |
| 110 | key: "email", |
| 111 | }, |
| 112 | want: []string{"[email protected]", "user", "example", "com"}, |
| 113 | }, |
| 114 | { |
| 115 | name: "valid IP address with capture groups", |
| 116 | ud: NewUserDialog( |
| 117 | bytes.NewBufferString("192.168.1.1\n"), |
| 118 | &bytes.Buffer{}, |
| 119 | nil, |
| 120 | ), |
| 121 | args: args{ |
| 122 | prompt: "Enter IP:", |
| 123 | validator: ipRegex, |
| 124 | key: "ip", |
| 125 | }, |
| 126 | want: []string{"192.168.1.1", "192", "168", "1", "1"}, |
| 127 | }, |
| 128 | { |
| 129 | name: "invalid input then valid input", |
| 130 | ud: NewUserDialog( |
| 131 | bytes.NewBufferString("invalid\[email protected]\n"), |
| 132 | &bytes.Buffer{}, |
| 133 | nil, |
| 134 | ), |
| 135 | args: args{ |
| 136 | prompt: "Enter email:", |
| 137 | validator: emailRegex, |
| 138 | key: "email", |
| 139 | }, |
| 140 | want: []string{"[email protected]", "user", "example", "com"}, |
| 141 | wantOut: "invalid format\n\n", |
| 142 | }, |
| 143 | { |
| 144 | name: "using prop value", |
| 145 | ud: NewUserDialog( |
| 146 | bytes.NewBufferString(""), |
| 147 | &bytes.Buffer{}, |
| 148 | map[string]string{"email": "[email protected]"}, |
| 149 | ), |
| 150 | args: args{ |
| 151 | prompt: "Enter email:", |
| 152 | validator: emailRegex, |
| 153 | key: "email", |
| 154 | }, |
| 155 | want: []string{"[email protected]", "prop", "example", "com"}, |
| 156 | wantOut: "Using prop value [email protected] for email", |
| 157 | }, |
| 158 | { |
| 159 | name: "invalid prop value falls back to interactive", |
| 160 | ud: NewUserDialog( |
| 161 | bytes.NewBufferString("[email protected]\n"), |
| 162 | &bytes.Buffer{}, |
| 163 | map[string]string{"email": "not-an-email"}, |
| 164 | ), |
| 165 | args: args{ |
| 166 | prompt: "Enter email:", |
| 167 | validator: emailRegex, |
| 168 | key: "email", |
| 169 | }, |
| 170 | want: []string{"[email protected]", "valid", "example", "com"}, |
| 171 | wantOut: "Supplied prop value not-an-email is not valid for email: " + |
| 172 | "invalid format\nEnter email: ", |
| 173 | }, |
| 174 | { |
| 175 | name: "regex with word capture", |
| 176 | ud: NewUserDialog( |
| 177 | bytes.NewBufferString("test_hello\n"), |
| 178 | &bytes.Buffer{}, |
| 179 | nil, |
| 180 | ), |
| 181 | args: args{ |
| 182 | prompt: "Enter test:", |
| 183 | validator: testRegex, |
| 184 | key: "test", |
| 185 | }, |
| 186 | want: []string{"test_hello", "hello"}, |
| 187 | }, |
| 188 | } |
| 189 | for _, tt := range tests { |
| 190 | t.Run(tt.name, func(t *testing.T) { |
| 191 | t.Parallel() |
| 192 | |
| 193 | got := tt.ud.Query(tt.args.prompt, tt.args.validator, tt.args.key) |
| 194 | assert.Equal(t, tt.want, got) |
| 195 | |
| 196 | if tt.wantOut != "" { |
| 197 | writer, ok := tt.ud.writer.(*bytes.Buffer) |
| 198 | require.True(t, ok) |
| 199 | assert.Contains(t, writer.String(), tt.wantOut) |
| 200 | } |
| 201 | }) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | func TestUserDialog_QueryAll(t *testing.T) { |
| 206 | t.Parallel() |
| 207 | |
| 208 | // Matches comma-separated words |
| 209 | wordRegex := regexp.MustCompile(`\b(\w+)\b`) |
| 210 | // Matches email addresses |
| 211 | emailRegex := regexp.MustCompile(`([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})`) |
| 212 | |
| 213 | type args struct { |
| 214 | prompt string |
| 215 | validator *regexp.Regexp |
| 216 | key string |
| 217 | maxMatches int |
| 218 | } |
| 219 | |
| 220 | tests := []struct { |
| 221 | name string |
| 222 | ud *UserDialog |
| 223 | args args |
| 224 | want [][]string |
| 225 | wantOut string |
| 226 | }{ |
| 227 | { |
| 228 | name: "multiple words with max matches", |
| 229 | ud: NewUserDialog( |
| 230 | bytes.NewBufferString("apple, banana, cherry\n"), |
| 231 | &bytes.Buffer{}, |
| 232 | nil, |
| 233 | ), |
| 234 | args: args{ |
| 235 | prompt: "Enter fruits:", |
| 236 | validator: wordRegex, |
| 237 | key: "fruits", |
| 238 | maxMatches: 3, |
| 239 | }, |
| 240 | want: [][]string{ |
| 241 | {"apple", "apple"}, |
| 242 | {"banana", "banana"}, |
| 243 | {"cherry", "cherry"}, |
| 244 | }, |
| 245 | }, |
| 246 | { |
| 247 | name: "multiple emails with unlimited matches", |
| 248 | ud: NewUserDialog( |
| 249 | bytes.NewBufferString("[email protected], [email protected]\n"), |
| 250 | &bytes.Buffer{}, |
| 251 | nil, |
| 252 | ), |
| 253 | args: args{ |
| 254 | prompt: "Enter emails:", |
| 255 | validator: emailRegex, |
| 256 | key: "emails", |
| 257 | maxMatches: -1, |
| 258 | }, |
| 259 | want: [][]string{ |
| 260 | {"[email protected]", "[email protected]"}, |
| 261 | {"[email protected]", "[email protected]"}, |
| 262 | }, |
| 263 | }, |
| 264 | { |
| 265 | name: "limited matches", |
| 266 | ud: NewUserDialog( |
| 267 | bytes.NewBufferString("one two three four\n"), |
| 268 | &bytes.Buffer{}, |
| 269 | nil, |
| 270 | ), |
| 271 | args: args{ |
| 272 | prompt: "Enter words:", |
| 273 | validator: wordRegex, |
| 274 | key: "words", |
| 275 | maxMatches: 2, |
| 276 | }, |
| 277 | want: [][]string{ |
| 278 | {"one", "one"}, |
| 279 | {"two", "two"}, |
| 280 | }, |
| 281 | }, |
| 282 | { |
| 283 | name: "using prop value", |
| 284 | ud: NewUserDialog( |
| 285 | bytes.NewBufferString(""), |
| 286 | &bytes.Buffer{}, |
| 287 | map[string]string{"items": "a, b, c"}, |
| 288 | ), |
| 289 | args: args{ |
| 290 | prompt: "Enter items:", |
| 291 | validator: wordRegex, |
| 292 | key: "items", |
| 293 | maxMatches: 3, |
| 294 | }, |
| 295 | want: [][]string{ |
| 296 | {"a", "a"}, |
| 297 | {"b", "b"}, |
| 298 | {"c", "c"}, |
| 299 | }, |
| 300 | }, |
| 301 | { |
| 302 | name: "no matches returns nil", |
| 303 | ud: NewUserDialog( |
| 304 | bytes.NewBufferString("!!!###@@@\nvalid\n"), |
| 305 | &bytes.Buffer{}, |
| 306 | nil, |
| 307 | ), |
| 308 | args: args{ |
| 309 | prompt: "Enter words:", |
| 310 | validator: wordRegex, |
| 311 | key: "words", |
| 312 | maxMatches: 1, |
| 313 | }, |
| 314 | want: [][]string{{"valid", "valid"}}, |
| 315 | wantOut: "invalid format\n\n", |
| 316 | }, |
| 317 | } |
| 318 | for _, tt := range tests { |
| 319 | t.Run(tt.name, func(t *testing.T) { |
| 320 | t.Parallel() |
| 321 | |
| 322 | got := tt.ud.QueryAll(tt.args.prompt, tt.args.validator, tt.args.key, tt.args.maxMatches) |
| 323 | assert.Equal(t, tt.want, got) |
| 324 | |
| 325 | if tt.wantOut != "" { |
| 326 | writer, ok := tt.ud.writer.(*bytes.Buffer) |
| 327 | require.True(t, ok) |
| 328 | assert.Contains(t, writer.String(), tt.wantOut) |
| 329 | } |
| 330 | }) |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | func TestUserDialog_QueryBool(t *testing.T) { |
| 335 | t.Parallel() |
| 336 | |
| 337 | type args struct { |
| 338 | prompt string |
| 339 | key string |
| 340 | } |
| 341 | |
| 342 | tests := []struct { |
| 343 | name string |
| 344 | ud *UserDialog |
| 345 | args args |
| 346 | want bool |
| 347 | wantOut string |
| 348 | }{ |
| 349 | { |
| 350 | name: "yes returns true", |
| 351 | ud: NewUserDialog( |
| 352 | bytes.NewBufferString("yes\n"), |
| 353 | &bytes.Buffer{}, |
| 354 | nil, |
| 355 | ), |
| 356 | args: args{ |
| 357 | prompt: "Continue?", |
| 358 | key: "continue", |
| 359 | }, |
| 360 | want: true, |
| 361 | }, |
| 362 | { |
| 363 | name: "y returns true", |
| 364 | ud: NewUserDialog( |
| 365 | bytes.NewBufferString("y\n"), |
| 366 | &bytes.Buffer{}, |
| 367 | nil, |
| 368 | ), |
| 369 | args: args{ |
| 370 | prompt: "Continue?", |
| 371 | key: "continue", |
| 372 | }, |
| 373 | want: true, |
| 374 | }, |
| 375 | { |
| 376 | name: "true returns true", |
| 377 | ud: NewUserDialog( |
| 378 | bytes.NewBufferString("true\n"), |
| 379 | &bytes.Buffer{}, |
| 380 | nil, |
| 381 | ), |
| 382 | args: args{ |
| 383 | prompt: "Enable?", |
| 384 | key: "enable", |
| 385 | }, |
| 386 | want: true, |
| 387 | }, |
| 388 | { |
| 389 | name: "1 returns true", |
| 390 | ud: NewUserDialog( |
| 391 | bytes.NewBufferString("1\n"), |
| 392 | &bytes.Buffer{}, |
| 393 | nil, |
| 394 | ), |
| 395 | args: args{ |
| 396 | prompt: "Enable?", |
| 397 | key: "enable", |
| 398 | }, |
| 399 | want: true, |
| 400 | }, |
| 401 | { |
| 402 | name: "no returns false", |
| 403 | ud: NewUserDialog( |
| 404 | bytes.NewBufferString("no\n"), |
| 405 | &bytes.Buffer{}, |
| 406 | nil, |
| 407 | ), |
| 408 | args: args{ |
| 409 | prompt: "Skip?", |
| 410 | key: "skip", |
| 411 | }, |
| 412 | want: false, |
| 413 | }, |
| 414 | { |
| 415 | name: "n returns false", |
| 416 | ud: NewUserDialog( |
| 417 | bytes.NewBufferString("n\n"), |
| 418 | &bytes.Buffer{}, |
| 419 | nil, |
| 420 | ), |
| 421 | args: args{ |
| 422 | prompt: "Skip?", |
| 423 | key: "skip", |
| 424 | }, |
| 425 | want: false, |
| 426 | }, |
| 427 | { |
| 428 | name: "false returns false", |
| 429 | ud: NewUserDialog( |
| 430 | bytes.NewBufferString("false\n"), |
| 431 | &bytes.Buffer{}, |
| 432 | nil, |
| 433 | ), |
| 434 | args: args{ |
| 435 | prompt: "Disable?", |
| 436 | key: "disable", |
| 437 | }, |
| 438 | want: false, |
| 439 | }, |
| 440 | { |
| 441 | name: "0 returns false", |
| 442 | ud: NewUserDialog( |
| 443 | bytes.NewBufferString("0\n"), |
| 444 | &bytes.Buffer{}, |
| 445 | nil, |
| 446 | ), |
| 447 | args: args{ |
| 448 | prompt: "Disable?", |
| 449 | key: "disable", |
| 450 | }, |
| 451 | want: false, |
| 452 | }, |
| 453 | { |
| 454 | name: "invalid then valid", |
| 455 | ud: NewUserDialog( |
| 456 | bytes.NewBufferString("maybe\nyes\n"), |
| 457 | &bytes.Buffer{}, |
| 458 | nil, |
| 459 | ), |
| 460 | args: args{ |
| 461 | prompt: "Continue?", |
| 462 | key: "continue", |
| 463 | }, |
| 464 | want: true, |
| 465 | wantOut: "answer must be yes or no", |
| 466 | }, |
| 467 | { |
| 468 | name: "using prop value yes", |
| 469 | ud: NewUserDialog( |
| 470 | bytes.NewBufferString(""), |
| 471 | &bytes.Buffer{}, |
| 472 | map[string]string{"enabled": "yes"}, |
| 473 | ), |
| 474 | args: args{ |
| 475 | prompt: "Enabled?", |
| 476 | key: "enabled", |
| 477 | }, |
| 478 | want: true, |
| 479 | }, |
| 480 | { |
| 481 | name: "using prop value no", |
| 482 | ud: NewUserDialog( |
| 483 | bytes.NewBufferString(""), |
| 484 | &bytes.Buffer{}, |
| 485 | map[string]string{"disabled": "no"}, |
| 486 | ), |
| 487 | args: args{ |
| 488 | prompt: "Disabled?", |
| 489 | key: "disabled", |
| 490 | }, |
| 491 | want: false, |
| 492 | }, |
| 493 | } |
| 494 | for _, tt := range tests { |
| 495 | t.Run(tt.name, func(t *testing.T) { |
| 496 | t.Parallel() |
| 497 | |
| 498 | got := tt.ud.QueryBool(tt.args.prompt, tt.args.key) |
| 499 | assert.Equal(t, tt.want, got) |
| 500 | |
| 501 | if tt.wantOut != "" { |
| 502 | writer, ok := tt.ud.writer.(*bytes.Buffer) |
| 503 | require.True(t, ok) |
| 504 | assert.Contains(t, writer.String(), tt.wantOut) |
| 505 | } |
| 506 | }) |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | func TestUserDialog_QueryInt(t *testing.T) { |
| 511 | t.Parallel() |
| 512 | |
| 513 | type args struct { |
| 514 | prompt string |
| 515 | key string |
| 516 | bitSize int |
| 517 | } |
| 518 | |
| 519 | tests := []struct { |
| 520 | name string |
| 521 | ud *UserDialog |
| 522 | args args |
| 523 | want int64 |
| 524 | wantOut string |
| 525 | }{ |
| 526 | { |
| 527 | name: "decimal positive number", |
| 528 | ud: NewUserDialog( |
| 529 | bytes.NewBufferString("42\n"), |
| 530 | &bytes.Buffer{}, |
| 531 | nil, |
| 532 | ), |
| 533 | args: args{ |
| 534 | prompt: "Enter number:", |
| 535 | key: "number", |
| 536 | bitSize: 64, |
| 537 | }, |
| 538 | want: 42, |
| 539 | }, |
| 540 | { |
| 541 | name: "decimal negative number", |
| 542 | ud: NewUserDialog( |
| 543 | bytes.NewBufferString("-123\n"), |
| 544 | &bytes.Buffer{}, |
| 545 | nil, |
| 546 | ), |
| 547 | args: args{ |
| 548 | prompt: "Enter number:", |
| 549 | key: "number", |
| 550 | bitSize: 64, |
| 551 | }, |
| 552 | want: -123, |
| 553 | }, |
| 554 | { |
| 555 | name: "zero", |
| 556 | ud: NewUserDialog( |
| 557 | bytes.NewBufferString("0\n"), |
| 558 | &bytes.Buffer{}, |
| 559 | nil, |
| 560 | ), |
| 561 | args: args{ |
| 562 | prompt: "Enter number:", |
| 563 | key: "number", |
| 564 | bitSize: 64, |
| 565 | }, |
| 566 | want: 0, |
| 567 | }, |
| 568 | { |
| 569 | name: "hex with 0x prefix", |
| 570 | ud: NewUserDialog( |
| 571 | bytes.NewBufferString("0xFF\n"), |
| 572 | &bytes.Buffer{}, |
| 573 | nil, |
| 574 | ), |
| 575 | args: args{ |
| 576 | prompt: "Enter hex:", |
| 577 | key: "hex", |
| 578 | bitSize: 64, |
| 579 | }, |
| 580 | want: 255, |
| 581 | }, |
| 582 | { |
| 583 | name: "hex with # prefix", |
| 584 | ud: NewUserDialog( |
| 585 | bytes.NewBufferString("#ffa080\n"), |
| 586 | &bytes.Buffer{}, |
| 587 | nil, |
| 588 | ), |
| 589 | args: args{ |
| 590 | prompt: "Enter color:", |
| 591 | key: "color", |
| 592 | bitSize: 64, |
| 593 | }, |
| 594 | want: 0xFFA080, |
| 595 | }, |
| 596 | { |
| 597 | name: "hex lowercase", |
| 598 | ud: NewUserDialog( |
| 599 | bytes.NewBufferString("0xabc\n"), |
| 600 | &bytes.Buffer{}, |
| 601 | nil, |
| 602 | ), |
| 603 | args: args{ |
| 604 | prompt: "Enter hex:", |
| 605 | key: "hex", |
| 606 | bitSize: 64, |
| 607 | }, |
| 608 | want: 2748, |
| 609 | }, |
| 610 | { |
| 611 | name: "invalid then valid", |
| 612 | ud: NewUserDialog( |
| 613 | bytes.NewBufferString("abc\n100\n"), |
| 614 | &bytes.Buffer{}, |
| 615 | nil, |
| 616 | ), |
| 617 | args: args{ |
| 618 | prompt: "Enter number:", |
| 619 | key: "number", |
| 620 | bitSize: 64, |
| 621 | }, |
| 622 | want: 100, |
| 623 | wantOut: "not a number\n\n", |
| 624 | }, |
| 625 | { |
| 626 | name: "using prop value", |
| 627 | ud: NewUserDialog( |
| 628 | bytes.NewBufferString(""), |
| 629 | &bytes.Buffer{}, |
| 630 | map[string]string{"port": "8080"}, |
| 631 | ), |
| 632 | args: args{ |
| 633 | prompt: "Enter port:", |
| 634 | key: "port", |
| 635 | bitSize: 16, |
| 636 | }, |
| 637 | want: 8080, |
| 638 | }, |
| 639 | { |
| 640 | name: "with int32 bitSize", |
| 641 | ud: NewUserDialog( |
| 642 | bytes.NewBufferString("12345\n"), |
| 643 | &bytes.Buffer{}, |
| 644 | nil, |
| 645 | ), |
| 646 | args: args{ |
| 647 | prompt: "Enter number:", |
| 648 | key: "number", |
| 649 | bitSize: 32, |
| 650 | }, |
| 651 | want: 12345, |
| 652 | }, |
| 653 | } |
| 654 | for _, tt := range tests { |
| 655 | t.Run(tt.name, func(t *testing.T) { |
| 656 | t.Parallel() |
| 657 | |
| 658 | got := tt.ud.QueryInt(tt.args.prompt, tt.args.key, tt.args.bitSize) |
| 659 | assert.Equal(t, tt.want, got) |
| 660 | |
| 661 | if tt.wantOut != "" { |
| 662 | writer, ok := tt.ud.writer.(*bytes.Buffer) |
| 663 | require.True(t, ok) |
| 664 | assert.Contains(t, writer.String(), tt.wantOut) |
| 665 | } |
| 666 | }) |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | func TestUserDialog_QueryString(t *testing.T) { |
| 671 | t.Parallel() |
| 672 | |
| 673 | noSpacesValidator := func(s string) error { |
| 674 | for _, r := range s { |
| 675 | if r == ' ' { |
| 676 | return errors.New("spaces not allowed") |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return nil |
| 681 | } |
| 682 | |
| 683 | type args struct { |
| 684 | prompt string |
| 685 | validator func(string) error |
| 686 | key string |
| 687 | } |
| 688 | |
| 689 | tests := []struct { |
| 690 | name string |
| 691 | ud *UserDialog |
| 692 | args args |
| 693 | want string |
| 694 | wantOut string |
| 695 | }{ |
| 696 | { |
| 697 | name: "valid input without validator", |
| 698 | ud: NewUserDialog( |
| 699 | bytes.NewBufferString("hello\n"), |
| 700 | &bytes.Buffer{}, |
| 701 | nil, |
| 702 | ), |
| 703 | args: args{ |
| 704 | prompt: "Enter name:", |
| 705 | validator: nil, |
| 706 | key: "name", |
| 707 | }, |
| 708 | want: "hello", |
| 709 | }, |
| 710 | { |
| 711 | name: "valid input with validator", |
| 712 | ud: NewUserDialog( |
| 713 | bytes.NewBufferString("testuser\n"), |
| 714 | &bytes.Buffer{}, |
| 715 | nil, |
| 716 | ), |
| 717 | args: args{ |
| 718 | prompt: "Enter username:", |
| 719 | validator: noSpacesValidator, |
| 720 | key: "username", |
| 721 | }, |
| 722 | want: "testuser", |
| 723 | }, |
| 724 | { |
| 725 | name: "invalid then valid", |
| 726 | ud: NewUserDialog( |
| 727 | bytes.NewBufferString("hello world\ngoodbye\n"), |
| 728 | &bytes.Buffer{}, |
| 729 | nil, |
| 730 | ), |
| 731 | args: args{ |
| 732 | prompt: "Enter name:", |
| 733 | validator: noSpacesValidator, |
| 734 | key: "name", |
| 735 | }, |
| 736 | want: "goodbye", |
| 737 | wantOut: "spaces not allowed\n\n", |
| 738 | }, |
| 739 | { |
| 740 | name: "empty input allowed without validator", |
| 741 | ud: NewUserDialog( |
| 742 | bytes.NewBufferString("\n"), |
| 743 | &bytes.Buffer{}, |
| 744 | nil, |
| 745 | ), |
| 746 | args: args{ |
| 747 | prompt: "Enter (optional):", |
| 748 | validator: nil, |
| 749 | key: "optional", |
| 750 | }, |
| 751 | want: "", |
| 752 | }, |
| 753 | { |
| 754 | name: "using prop value", |
| 755 | ud: NewUserDialog( |
| 756 | bytes.NewBufferString(""), |
| 757 | &bytes.Buffer{}, |
| 758 | map[string]string{"name": "propvalue"}, |
| 759 | ), |
| 760 | args: args{ |
| 761 | prompt: "Enter name:", |
| 762 | validator: nil, |
| 763 | key: "name", |
| 764 | }, |
| 765 | want: "propvalue", |
| 766 | wantOut: "Using prop value", |
| 767 | }, |
| 768 | { |
| 769 | name: "invalid prop value falls back to interactive", |
| 770 | ud: NewUserDialog( |
| 771 | bytes.NewBufferString("validinput\n"), |
| 772 | &bytes.Buffer{}, |
| 773 | map[string]string{"name": "has space"}, |
| 774 | ), |
| 775 | args: args{ |
| 776 | prompt: "Enter name:", |
| 777 | validator: noSpacesValidator, |
| 778 | key: "name", |
| 779 | }, |
| 780 | want: "validinput", |
| 781 | wantOut: "is not valid for", |
| 782 | }, |
| 783 | } |
| 784 | for _, tt := range tests { |
| 785 | t.Run(tt.name, func(t *testing.T) { |
| 786 | t.Parallel() |
| 787 | |
| 788 | got := tt.ud.QueryString(tt.args.prompt, tt.args.validator, tt.args.key) |
| 789 | assert.Equal(t, tt.want, got) |
| 790 | |
| 791 | if tt.wantOut != "" { |
| 792 | writer, ok := tt.ud.writer.(*bytes.Buffer) |
| 793 | require.True(t, ok) |
| 794 | assert.Contains(t, writer.String(), tt.wantOut) |
| 795 | } |
| 796 | }) |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | func TestUserDialog_QueryStringPattern(t *testing.T) { |
| 801 | t.Parallel() |
| 802 | |
| 803 | emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`) |
| 804 | alphaRegex := regexp.MustCompile(`^[a-zA-Z]+$`) |
| 805 | digitRegex := regexp.MustCompile(`^\d+$`) |
| 806 | |
| 807 | type args struct { |
| 808 | prompt string |
| 809 | validator *regexp.Regexp |
| 810 | key string |
| 811 | } |
| 812 | |
| 813 | tests := []struct { |
| 814 | name string |
| 815 | ud *UserDialog |
| 816 | args args |
| 817 | want string |
| 818 | wantPanic bool |
| 819 | wantOut string |
| 820 | }{ |
| 821 | { |
| 822 | name: "valid email", |
| 823 | ud: NewUserDialog( |
| 824 | bytes.NewBufferString("[email protected]\n"), |
| 825 | &bytes.Buffer{}, |
| 826 | nil, |
| 827 | ), |
| 828 | args: args{ |
| 829 | prompt: "Enter email:", |
| 830 | validator: emailRegex, |
| 831 | key: "email", |
| 832 | }, |
| 833 | want: "[email protected]", |
| 834 | }, |
| 835 | { |
| 836 | name: "valid alphabetic", |
| 837 | ud: NewUserDialog( |
| 838 | bytes.NewBufferString("HelloWorld\n"), |
| 839 | &bytes.Buffer{}, |
| 840 | nil, |
| 841 | ), |
| 842 | args: args{ |
| 843 | prompt: "Enter text:", |
| 844 | validator: alphaRegex, |
| 845 | key: "text", |
| 846 | }, |
| 847 | want: "HelloWorld", |
| 848 | }, |
| 849 | { |
| 850 | name: "valid digits", |
| 851 | ud: NewUserDialog( |
| 852 | bytes.NewBufferString("12345\n"), |
| 853 | &bytes.Buffer{}, |
| 854 | nil, |
| 855 | ), |
| 856 | args: args{ |
| 857 | prompt: "Enter number:", |
| 858 | validator: digitRegex, |
| 859 | key: "number", |
| 860 | }, |
| 861 | want: "12345", |
| 862 | }, |
| 863 | { |
| 864 | name: "invalid then valid", |
| 865 | ud: NewUserDialog( |
| 866 | bytes.NewBufferString("123\[email protected]\n"), |
| 867 | &bytes.Buffer{}, |
| 868 | nil, |
| 869 | ), |
| 870 | args: args{ |
| 871 | prompt: "Enter email:", |
| 872 | validator: emailRegex, |
| 873 | key: "email", |
| 874 | }, |
| 875 | want: "[email protected]", |
| 876 | wantOut: "invalid format\n\n", |
| 877 | }, |
| 878 | { |
| 879 | name: "using prop value", |
| 880 | ud: NewUserDialog( |
| 881 | bytes.NewBufferString(""), |
| 882 | &bytes.Buffer{}, |
| 883 | map[string]string{"email": "[email protected]"}, |
| 884 | ), |
| 885 | args: args{ |
| 886 | prompt: "Enter email:", |
| 887 | validator: emailRegex, |
| 888 | key: "email", |
| 889 | }, |
| 890 | want: "[email protected]", |
| 891 | wantOut: "Using prop value", |
| 892 | }, |
| 893 | { |
| 894 | name: "nil validator panics", |
| 895 | ud: NewUserDialog( |
| 896 | bytes.NewBufferString("test\n"), |
| 897 | &bytes.Buffer{}, |
| 898 | nil, |
| 899 | ), |
| 900 | args: args{ |
| 901 | prompt: "Enter value:", |
| 902 | validator: nil, |
| 903 | key: "value", |
| 904 | }, |
| 905 | wantPanic: true, |
| 906 | }, |
| 907 | } |
| 908 | for _, tt := range tests { |
| 909 | t.Run(tt.name, func(t *testing.T) { |
| 910 | t.Parallel() |
| 911 | |
| 912 | if tt.wantPanic { |
| 913 | assert.Panics(t, func() { |
| 914 | tt.ud.QueryStringPattern(tt.args.prompt, tt.args.validator, tt.args.key) |
| 915 | }) |
| 916 | |
| 917 | return |
| 918 | } |
| 919 | |
| 920 | got := tt.ud.QueryStringPattern(tt.args.prompt, tt.args.validator, tt.args.key) |
| 921 | assert.Equal(t, tt.want, got) |
| 922 | |
| 923 | if tt.wantOut != "" { |
| 924 | writer, ok := tt.ud.writer.(*bytes.Buffer) |
| 925 | require.True(t, ok) |
| 926 | assert.Contains(t, writer.String(), tt.wantOut) |
| 927 | } |
| 928 | }) |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | func TestUserDialog_Write(t *testing.T) { |
| 933 | t.Parallel() |
| 934 | |
| 935 | type args struct { |
| 936 | message string |
| 937 | v []any |
| 938 | } |
| 939 | |
| 940 | tests := []struct { |
| 941 | name string |
| 942 | ud *UserDialog |
| 943 | args args |
| 944 | wantWriter string |
| 945 | }{ |
| 946 | { |
| 947 | name: "simple message", |
| 948 | ud: NewUserDialog( |
| 949 | bytes.NewBufferString(""), |
| 950 | &bytes.Buffer{}, |
| 951 | nil, |
| 952 | ), |
| 953 | args: args{ |
| 954 | message: "Hello, World!", |
| 955 | v: nil, |
| 956 | }, |
| 957 | wantWriter: "Hello, World!", |
| 958 | }, |
| 959 | { |
| 960 | name: "formatted message with string", |
| 961 | ud: NewUserDialog( |
| 962 | bytes.NewBufferString(""), |
| 963 | &bytes.Buffer{}, |
| 964 | nil, |
| 965 | ), |
| 966 | args: args{ |
| 967 | message: "Hello, %s!", |
| 968 | v: []any{"Alice"}, |
| 969 | }, |
| 970 | wantWriter: "Hello, Alice!", |
| 971 | }, |
| 972 | { |
| 973 | name: "formatted message with integer", |
| 974 | ud: NewUserDialog( |
| 975 | bytes.NewBufferString(""), |
| 976 | &bytes.Buffer{}, |
| 977 | nil, |
| 978 | ), |
| 979 | args: args{ |
| 980 | message: "Count: %d", |
| 981 | v: []any{42}, |
| 982 | }, |
| 983 | wantWriter: "Count: 42", |
| 984 | }, |
| 985 | { |
| 986 | name: "formatted message with multiple values", |
| 987 | ud: NewUserDialog( |
| 988 | bytes.NewBufferString(""), |
| 989 | &bytes.Buffer{}, |
| 990 | nil, |
| 991 | ), |
| 992 | args: args{ |
| 993 | message: "Name: %s, Age: %d, Score: %.2f", |
| 994 | v: []any{"Bob", 25, 95.5}, |
| 995 | }, |
| 996 | wantWriter: "Name: Bob, Age: 25, Score: 95.50", |
| 997 | }, |
| 998 | { |
| 999 | name: "empty message", |
| 1000 | ud: NewUserDialog( |
| 1001 | bytes.NewBufferString(""), |
| 1002 | &bytes.Buffer{}, |
| 1003 | nil, |
| 1004 | ), |
| 1005 | args: args{ |
| 1006 | message: "", |
| 1007 | v: nil, |
| 1008 | }, |
| 1009 | wantWriter: "", |
| 1010 | }, |
| 1011 | { |
| 1012 | name: "message with special characters", |
| 1013 | ud: NewUserDialog( |
| 1014 | bytes.NewBufferString(""), |
| 1015 | &bytes.Buffer{}, |
| 1016 | nil, |
| 1017 | ), |
| 1018 | args: args{ |
| 1019 | message: "Special: %s", |
| 1020 | v: []any{"!@#$%^&*()"}, |
| 1021 | }, |
| 1022 | wantWriter: "Special: !@#$%^&*()", |
| 1023 | }, |
| 1024 | } |
| 1025 | for _, tt := range tests { |
| 1026 | t.Run(tt.name, func(t *testing.T) { |
| 1027 | t.Parallel() |
| 1028 | |
| 1029 | tt.ud.Write(tt.args.message, tt.args.v...) |
| 1030 | |
| 1031 | writer, ok := tt.ud.writer.(*bytes.Buffer) |
| 1032 | require.True(t, ok) |
| 1033 | assert.Equal(t, tt.wantWriter, writer.String()) |
| 1034 | }) |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | func TestUserDialog_Writelnf(t *testing.T) { |
| 1039 | t.Parallel() |
| 1040 | |
| 1041 | type args struct { |
| 1042 | fmtStr string |
| 1043 | v []any |
| 1044 | } |
| 1045 | |
| 1046 | tests := []struct { |
| 1047 | name string |
| 1048 | ud *UserDialog |
| 1049 | args args |
| 1050 | wantWriter string |
| 1051 | }{ |
| 1052 | { |
| 1053 | name: "simple message", |
| 1054 | ud: NewUserDialog( |
| 1055 | bytes.NewBufferString(""), |
| 1056 | &bytes.Buffer{}, |
| 1057 | nil, |
| 1058 | ), |
| 1059 | args: args{ |
| 1060 | fmtStr: "Hello, World!", |
| 1061 | v: nil, |
| 1062 | }, |
| 1063 | wantWriter: "Hello, World!\n", |
| 1064 | }, |
| 1065 | { |
| 1066 | name: "formatted message with string", |
| 1067 | ud: NewUserDialog( |
| 1068 | bytes.NewBufferString(""), |
| 1069 | &bytes.Buffer{}, |
| 1070 | nil, |
| 1071 | ), |
| 1072 | args: args{ |
| 1073 | fmtStr: "Hello, %s!", |
| 1074 | v: []any{"Alice"}, |
| 1075 | }, |
| 1076 | wantWriter: "Hello, Alice!\n", |
| 1077 | }, |
| 1078 | { |
| 1079 | name: "formatted message with integer", |
| 1080 | ud: NewUserDialog( |
| 1081 | bytes.NewBufferString(""), |
| 1082 | &bytes.Buffer{}, |
| 1083 | nil, |
| 1084 | ), |
| 1085 | args: args{ |
| 1086 | fmtStr: "Count: %d", |
| 1087 | v: []any{42}, |
| 1088 | }, |
| 1089 | wantWriter: "Count: 42\n", |
| 1090 | }, |
| 1091 | { |
| 1092 | name: "formatted message with multiple values", |
| 1093 | ud: NewUserDialog( |
| 1094 | bytes.NewBufferString(""), |
| 1095 | &bytes.Buffer{}, |
| 1096 | nil, |
| 1097 | ), |
| 1098 | args: args{ |
| 1099 | fmtStr: "Name: %s, Age: %d", |
| 1100 | v: []any{"Bob", 25}, |
| 1101 | }, |
| 1102 | wantWriter: "Name: Bob, Age: 25\n", |
| 1103 | }, |
| 1104 | { |
| 1105 | name: "empty message still adds newline", |
| 1106 | ud: NewUserDialog( |
| 1107 | bytes.NewBufferString(""), |
| 1108 | &bytes.Buffer{}, |
| 1109 | nil, |
| 1110 | ), |
| 1111 | args: args{ |
| 1112 | fmtStr: "", |
| 1113 | v: nil, |
| 1114 | }, |
| 1115 | wantWriter: "\n", |
| 1116 | }, |
| 1117 | { |
| 1118 | name: "error message", |
| 1119 | ud: NewUserDialog( |
| 1120 | bytes.NewBufferString(""), |
| 1121 | &bytes.Buffer{}, |
| 1122 | nil, |
| 1123 | ), |
| 1124 | args: args{ |
| 1125 | fmtStr: "Error: %s", |
| 1126 | v: []any{"something went wrong"}, |
| 1127 | }, |
| 1128 | wantWriter: "Error: something went wrong\n", |
| 1129 | }, |
| 1130 | } |
| 1131 | for _, tt := range tests { |
| 1132 | t.Run(tt.name, func(t *testing.T) { |
| 1133 | t.Parallel() |
| 1134 | |
| 1135 | tt.ud.Writelnf(tt.args.fmtStr, tt.args.v...) |
| 1136 | |
| 1137 | writer, ok := tt.ud.writer.(*bytes.Buffer) |
| 1138 | require.True(t, ok) |
| 1139 | assert.Equal(t, tt.wantWriter, writer.String()) |
| 1140 | }) |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | func TestValidateFormat(t *testing.T) { |
| 1145 | t.Parallel() |
| 1146 | |
| 1147 | type args struct { |
| 1148 | validator func(string) bool |
| 1149 | } |
| 1150 | |
| 1151 | tests := []struct { |
| 1152 | name string |
| 1153 | args args |
| 1154 | input string |
| 1155 | wantErr bool |
| 1156 | wantError error |
| 1157 | }{ |
| 1158 | { |
| 1159 | name: "validator returns true", |
| 1160 | args: args{ |
| 1161 | validator: func(s string) bool { |
| 1162 | return s == "valid" |
| 1163 | }, |
| 1164 | }, |
| 1165 | input: "valid", |
| 1166 | wantErr: false, |
| 1167 | }, |
| 1168 | { |
| 1169 | name: "validator returns false", |
| 1170 | args: args{ |
| 1171 | validator: func(s string) bool { |
| 1172 | return s == "valid" |
| 1173 | }, |
| 1174 | }, |
| 1175 | input: "invalid", |
| 1176 | wantErr: true, |
| 1177 | wantError: errInvalidFormat, |
| 1178 | }, |
| 1179 | { |
| 1180 | name: "validator checks non-empty", |
| 1181 | args: args{ |
| 1182 | validator: func(s string) bool { |
| 1183 | return s != "" |
| 1184 | }, |
| 1185 | }, |
| 1186 | input: "not empty", |
| 1187 | wantErr: false, |
| 1188 | }, |
| 1189 | { |
| 1190 | name: "validator fails on empty", |
| 1191 | args: args{ |
| 1192 | validator: func(s string) bool { |
| 1193 | return s != "" |
| 1194 | }, |
| 1195 | }, |
| 1196 | input: "", |
| 1197 | wantErr: true, |
| 1198 | wantError: errInvalidFormat, |
| 1199 | }, |
| 1200 | { |
| 1201 | name: "validator checks prefix", |
| 1202 | args: args{ |
| 1203 | validator: func(s string) bool { |
| 1204 | return len(s) >= 3 && s[:3] == "pre" |
| 1205 | }, |
| 1206 | }, |
| 1207 | input: "prefix", |
| 1208 | wantErr: false, |
| 1209 | }, |
| 1210 | } |
| 1211 | for _, tt := range tests { |
| 1212 | t.Run(tt.name, func(t *testing.T) { |
| 1213 | t.Parallel() |
| 1214 | |
| 1215 | got := ValidateFormat(tt.args.validator) |
| 1216 | require.NotNil(t, got) |
| 1217 | |
| 1218 | err := got(tt.input) |
| 1219 | if tt.wantErr { |
| 1220 | require.Error(t, err) |
| 1221 | |
| 1222 | if tt.wantError != nil { |
| 1223 | require.ErrorIs(t, err, tt.wantError) |
| 1224 | } |
| 1225 | } else { |
| 1226 | require.NoError(t, err) |
| 1227 | } |
| 1228 | }) |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | func TestRequired(t *testing.T) { |
| 1233 | t.Parallel() |
| 1234 | |
| 1235 | type args struct { |
| 1236 | answer string |
| 1237 | } |
| 1238 | |
| 1239 | tests := []struct { |
| 1240 | name string |
| 1241 | args args |
| 1242 | wantErr bool |
| 1243 | }{ |
| 1244 | { |
| 1245 | name: "non-empty string", |
| 1246 | args: args{answer: "hello"}, |
| 1247 | wantErr: false, |
| 1248 | }, |
| 1249 | { |
| 1250 | name: "single character", |
| 1251 | args: args{answer: "a"}, |
| 1252 | wantErr: false, |
| 1253 | }, |
| 1254 | { |
| 1255 | name: "empty string", |
| 1256 | args: args{answer: ""}, |
| 1257 | wantErr: true, |
| 1258 | }, |
| 1259 | { |
| 1260 | name: "whitespace only", |
| 1261 | args: args{answer: " "}, |
| 1262 | wantErr: false, |
| 1263 | }, |
| 1264 | { |
| 1265 | name: "string with spaces", |
| 1266 | args: args{answer: "hello world"}, |
| 1267 | wantErr: false, |
| 1268 | }, |
| 1269 | { |
| 1270 | name: "special characters", |
| 1271 | args: args{answer: "!@#$%"}, |
| 1272 | wantErr: false, |
| 1273 | }, |
| 1274 | { |
| 1275 | name: "numeric string", |
| 1276 | args: args{answer: "12345"}, |
| 1277 | wantErr: false, |
| 1278 | }, |
| 1279 | { |
| 1280 | name: "unicode characters", |
| 1281 | args: args{answer: "Hello 世界"}, |
| 1282 | wantErr: false, |
| 1283 | }, |
| 1284 | } |
| 1285 | for _, tt := range tests { |
| 1286 | t.Run(tt.name, func(t *testing.T) { |
| 1287 | t.Parallel() |
| 1288 | |
| 1289 | err := Required(tt.args.answer) |
| 1290 | if tt.wantErr { |
| 1291 | require.Error(t, err) |
| 1292 | assert.ErrorIs(t, err, errRequired) |
| 1293 | } else { |
| 1294 | require.NoError(t, err) |
| 1295 | } |
| 1296 | }) |
| 1297 | } |
| 1298 | } |