1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
|
#!/usr/bin/env ruby
# coding: utf-8
# tc_highline.rb
#
# Created by James Edward Gray II on 2005-04-26.
# Copyright 2005 Gray Productions. All rights reserved.
#
# This is Free Software. See LICENSE and COPYING for details.
require "test_helper"
require "highline"
require "stringio"
require "tempfile"
# if HighLine::CHARACTER_MODE == "Win32API"
# class HighLine
# # Override Windows' character reading so it's not tied to STDIN.
# def get_character( input = STDIN )
# input.getc
# end
# end
# end
class TestHighLine < Minitest::Test
def setup
HighLine.reset
@input = StringIO.new
@output = StringIO.new
@terminal = HighLine.new(@input, @output)
end
def test_agree_valid_yes_answers
valid_yes_answers = %w[y yes Y YES]
valid_yes_answers.each do |user_input|
@input << "#{user_input}\n"
@input.rewind
assert_equal true, @terminal.agree("Yes or no? ")
assert_equal "Yes or no? ", @output.string
@input.truncate(@input.rewind)
@output.truncate(@output.rewind)
end
end
def test_agree_valid_no_answers
valid_no_answers = %w[n no N NO]
valid_no_answers.each do |user_input|
@input << "#{user_input}\n"
@input.rewind
assert_equal false, @terminal.agree("Yes or no? ")
assert_equal "Yes or no? ", @output.string
@input.truncate(@input.rewind)
@output.truncate(@output.rewind)
end
end
def test_agree_invalid_answers
invalid_answers = ["ye", "yuk", "nope", "Oh yes", "Oh no", "Hell no!"]
invalid_answers.each do |user_input|
# Each invalid answer, should be followed by a 'y'
# (as the question is reasked)
@input << "#{user_input}\ny\n"
@input.rewind
assert_equal true, @terminal.agree("Yes or no? ")
# It reasks the question if the answer is invalid
assert_equal "Yes or no? Please enter \"yes\" or \"no\".\nYes or no? ",
@output.string
@input.truncate(@input.rewind)
@output.truncate(@output.rewind)
end
end
def test_agree_with_getc
@input.truncate(@input.rewind)
@input << "yellow"
@input.rewind
assert_equal(true, @terminal.agree("Yes or no? ", :getc))
end
def test_agree_with_block
@input << "\n\n"
@input.rewind
assert_equal(true, @terminal.agree("Yes or no? ") { |q| q.default = "y" })
assert_equal(false, @terminal.agree("Yes or no? ") { |q| q.default = "n" })
end
def test_ask
name = "James Edward Gray II"
@input << name << "\n"
@input.rewind
assert_equal(name, @terminal.ask("What is your name? "))
assert_raises(EOFError) { @terminal.ask("Any input left? ") }
end
def test_ask_string
name = "James Edward Gray II"
@input << name << "\n"
@input.rewind
assert_equal(name, @terminal.ask("What is your name? ", String))
assert_raises(EOFError) { @terminal.ask("Any input left? ", String) }
end
def test_ask_string_converting
name = "James Edward Gray II"
@input << name << "\n"
@input.rewind
answer = @terminal.ask("What is your name? ", String)
assert_instance_of HighLine::String, answer
@input.rewind
answer = @terminal.ask("What is your name? ", HighLine::String)
assert_instance_of HighLine::String, answer
assert_raises(EOFError) do
@terminal.ask("Any input left? ", HighLine::String)
end
end
def test_indent
text = "Testing...\n"
@terminal.indent_level = 1
@terminal.say(text)
assert_equal(" " * 3 + text, @output.string)
@output.truncate(@output.rewind)
@terminal.indent_level = 3
@terminal.say(text)
assert_equal(" " * 9 + text, @output.string)
@output.truncate(@output.rewind)
@terminal.indent_level = 0
@terminal.indent_size = 5
@terminal.indent(2, text)
assert_equal(" " * 10 + text, @output.string)
@output.truncate(@output.rewind)
@terminal.indent_level = 0
@terminal.indent_size = 4
@terminal.indent do
@terminal.say(text)
end
assert_equal(" " * 4 + text, @output.string)
@output.truncate(@output.rewind)
@terminal.indent_size = 2
@terminal.indent(3) do |t|
t.say(text)
end
assert_equal(" " * 6 + text, @output.string)
@output.truncate(@output.rewind)
@terminal.indent do |t|
t.indent do
t.indent do
t.indent do |tt|
tt.say(text)
end
end
end
end
assert_equal(" " * 8 + text, @output.string)
text = "Multi\nLine\nIndentation\n"
indent = " " * 4
@terminal.indent_level = 2
@output.truncate(@output.rewind)
@terminal.say(text)
assert_equal("#{indent}Multi\n#{indent}Line\n#{indent}Indentation\n",
@output.string)
@output.truncate(@output.rewind)
@terminal.multi_indent = false
@terminal.say(text)
assert_equal("#{indent}Multi\nLine\nIndentation\n", @output.string)
@output.truncate(@output.rewind)
@terminal.indent(0, text, true)
assert_equal("#{indent}Multi\n#{indent}Line\n#{indent}Indentation\n",
@output.string)
end
def test_newline
@terminal.newline
@terminal.newline
assert_equal("\n\n", @output.string)
end
def test_bug_fixes
# auto-complete bug
@input << "ruby\nRuby\n"
@input.rewind
languages = [:Perl, :Python, :Ruby]
answer = @terminal.ask("What is your favorite programming language? ",
languages)
assert_equal(languages.last, answer)
@input.truncate(@input.rewind)
@input << "ruby\n"
@input.rewind
answer = @terminal.ask("What is your favorite programming language? ",
languages) do |q|
q.case = :capitalize
end
assert_equal(languages.last, answer)
# poor auto-complete error message
@input.truncate(@input.rewind)
@input << "lisp\nruby\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("What is your favorite programming language? ",
languages) do |q|
q.case = :capitalize
end
assert_equal(languages.last, answer)
assert_equal("What is your favorite programming language? " \
"You must choose one of [Perl, Python, Ruby].\n" \
"? ", @output.string)
end
def test_case_changes
@input << "jeg2\n"
@input.rewind
answer = @terminal.ask("Enter your initials ") do |q|
q.case = :up
end
assert_equal("JEG2", answer)
@input.truncate(@input.rewind)
@input << "cRaZY\n"
@input.rewind
answer = @terminal.ask("Enter a search string: ") do |q|
q.case = :down
end
assert_equal("crazy", answer)
end
def test_ask_with_overwrite
@input << "Yes, sure!\n"
@input.rewind
answer = @terminal.ask("Do you like Ruby? ") do |q|
q.overwrite = true
q.echo = false
end
assert_equal("Yes, sure!", answer)
erase_sequence = "\r#{HighLine.Style(:erase_line).code}"
assert_equal("Do you like Ruby? #{erase_sequence}", @output.string)
end
def test_ask_with_overwrite_and_character_mode
@input << "Y"
@input.rewind
answer = @terminal.ask("Do you like Ruby (Y/N)? ") do |q|
q.overwrite = true
q.echo = false
q.character = true
end
assert_equal("Y", answer)
erase_sequence = "\r#{HighLine.Style(:erase_line).code}"
assert_equal("Do you like Ruby (Y/N)? #{erase_sequence}", @output.string)
end
def test_character_echo
@input << "password\r"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = "*"
end
assert_equal("password", answer)
assert_equal("Please enter your password: ********\n", @output.string)
@input.truncate(@input.rewind)
@input << "2"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Select an option (1, 2 or 3): ",
Integer) do |q|
q.echo = "*"
q.character = true
end
assert_equal(2, answer)
assert_equal("Select an option (1, 2 or 3): *\n", @output.string)
end
def test_backspace_does_not_enter_prompt
@input << "\b\b"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = "*"
end
assert_equal("", answer)
assert_equal("Please enter your password: \n", @output.string)
end
def test_after_some_chars_backspace_does_not_enter_prompt_when_ascii
@input << "apple\b\b\b\b\b\b\b\b\b\b"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = "*"
end
assert_equal("", answer)
# There's only enough backspaces to clear the given string
assert_equal(5, @output.string.count("\b"))
end
def test_after_some_chars_backspace_does_not_enter_prompt_when_utf8
@input << "maçã\b\b\b\b\b\b\b\b"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = "*"
end
assert_equal("", answer)
# There's only enough backspaces to clear the given string
assert_equal(4, @output.string.count("\b"))
end
def test_erase_line_does_not_enter_prompt
@input << "\cU\cU\cU\cU\cU\cU\cU\cU\cU"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = "*"
end
assert_equal("", answer)
assert_equal("Please enter your password: \n", @output.string)
# There's no backspaces as the line is already empty
assert_equal(0, @output.string.count("\b"))
end
def test_after_some_chars_erase_line_does_not_enter_prompt_when_ascii
@input << "apple\cU\cU\cU\cU\cU\cU\cU\cU"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = "*"
end
assert_equal("", answer)
# There's only enough backspaces to clear the given string
assert_equal(5, @output.string.count("\b"))
end
def test_after_some_chars_erase_line_does_not_enter_prompt_when_utf8
@input << "maçã\cU\cU\cU\cU\cU\cU\cU\cU"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = "*"
end
assert_equal("", answer)
# There's only enough backspaces to clear the given string
assert_equal(4, @output.string.count("\b"))
end
def test_character_reading
# WARNING: This method does NOT cover Unix and Windows savvy testing!
@input << "12345"
@input.rewind
answer = @terminal.ask("Enter a single digit: ", Integer) do |q|
q.character = :getc
end
assert_equal(1, answer)
end
def test_frozen_statement
@terminal.say("This is a frozen statement".freeze)
assert_equal("This is a frozen statement\n", @output.string)
end
def test_color
@terminal.say("This should be <%= BLUE %>blue<%= CLEAR %>!")
assert_equal("This should be \e[34mblue\e[0m!\n", @output.string)
@output.truncate(@output.rewind)
@terminal.say("This should be " \
"<%= BOLD + ON_WHITE %>bold on white<%= CLEAR %>!")
assert_equal("This should be \e[1m\e[47mbold on white\e[0m!\n",
@output.string)
@output.truncate(@output.rewind)
@terminal.say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string)
@output.truncate(@output.rewind)
@terminal.say("This should be " \
"<%= color('blinking on red', :blink, :on_red) %>!")
assert_equal("This should be \e[5m\e[41mblinking on red\e[0m!\n",
@output.string)
@output.truncate(@output.rewind)
@terminal.say("This should be <%= NONE %>none<%= CLEAR %>!")
assert_equal("This should be \e[38mnone\e[0m!\n", @output.string)
@output.truncate(@output.rewind)
@terminal.say("This should be <%= RGB_906030 %>rgb_906030<%= CLEAR %>!")
assert_equal("This should be \e[38;5;137mrgb_906030\e[0m!\n",
@output.string)
@output.truncate(@output.rewind)
@terminal.say(
"This should be <%= ON_RGB_C06030 %>on_rgb_c06030<%= CLEAR %>!"
)
assert_equal("This should be \e[48;5;173mon_rgb_c06030\e[0m!\n",
@output.string)
# Relying on const_missing
assert_instance_of HighLine::Style, HighLine::ON_RGB_C06031_STYLE
assert_instance_of String, HighLine::ON_RGB_C06032
assert_raises(NameError) { HighLine::ON_RGB_ZZZZZZ }
# Retrieving color_code from a style
assert_equal "\e[41m", @terminal.color_code([HighLine::ON_RED_STYLE])
@output.truncate(@output.rewind)
# Does class method work, too?
@terminal.say(
"This should be <%= HighLine.color('reverse underlined magenta', " \
":reverse, :underline, :magenta) %>!"
)
assert_equal(
"This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n",
@output.string
)
@output.truncate(@output.rewind)
# turn off color
old_setting = HighLine.use_color?
HighLine.use_color = false
@terminal.use_color = false
@terminal.say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be cyan!\n", @output.string)
HighLine.use_color = old_setting
@terminal.use_color = old_setting
end
def test_color_setting_per_instance
require "highline/import"
old_glob_instance = HighLine.default_instance
old_setting = HighLine.use_color?
gterm_input = StringIO.new
gterm_output = StringIO.new
HighLine.default_instance = HighLine.new(gterm_input, gterm_output)
# It can set coloring at HighLine class
cli_input = StringIO.new
cli_output = StringIO.new
cli = HighLine.new(cli_input, cli_output)
# Testing with both use_color setted to true
HighLine.use_color = true
@terminal.use_color = true
cli.use_color = true
say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be \e[36mcyan\e[0m!\n", gterm_output.string)
@terminal.say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string)
cli.say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be \e[36mcyan\e[0m!\n", cli_output.string)
gterm_output.truncate(gterm_output.rewind)
@output.truncate(@output.rewind)
cli_output.truncate(cli_output.rewind)
# Testing with both use_color setted to false
HighLine.use_color = false
@terminal.use_color = false
cli.use_color = false
say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be cyan!\n", gterm_output.string)
@terminal.say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be cyan!\n", @output.string)
cli.say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be cyan!\n", cli_output.string)
gterm_output.truncate(gterm_output.rewind)
@output.truncate(@output.rewind)
cli_output.truncate(cli_output.rewind)
# Now check when class and instance doesn't agree about use_color
# Class false, instance true
HighLine.use_color = false
@terminal.use_color = false
cli.use_color = true
say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be cyan!\n", gterm_output.string)
@terminal.say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be cyan!\n", @output.string)
cli.say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be \e[36mcyan\e[0m!\n", cli_output.string)
gterm_output.truncate(gterm_output.rewind)
@output.truncate(@output.rewind)
cli_output.truncate(cli_output.rewind)
# Class true, instance false
HighLine.use_color = true
@terminal.use_color = true
cli.use_color = false
say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be \e[36mcyan\e[0m!\n", gterm_output.string)
@terminal.say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string)
cli.say("This should be <%= color('cyan', CYAN) %>!")
assert_equal("This should be cyan!\n", cli_output.string)
gterm_output.truncate(gterm_output.rewind)
@output.truncate(@output.rewind)
cli_output.truncate(cli_output.rewind)
HighLine.use_color = old_setting
@terminal.use_color = old_setting
HighLine.default_instance = old_glob_instance
end
def test_reset_use_color
HighLine.use_color = false
refute HighLine.use_color?
HighLine.reset_use_color
assert HighLine.use_color?
end
def test_reset_use_color_when_highline_reset
HighLine.use_color = false
refute HighLine.use_color?
HighLine.reset
assert HighLine.use_color?
end
def test_uncolor
# instance method
assert_equal(
"This should be reverse underlined magenta!\n",
@terminal.uncolor(
"This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n"
)
)
@output.truncate(@output.rewind)
# class method
assert_equal(
"This should be reverse underlined magenta!\n",
HighLine.uncolor(
"This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n"
)
)
@output.truncate(@output.rewind)
# RGB color
assert_equal(
"This should be rgb_906030!\n",
@terminal.uncolor(
"This should be \e[38;5;137mrgb_906030\e[0m!\n"
)
)
end
def test_grey_is_the_same_of_gray
@terminal.say("<%= GRAY %>")
gray_code = @output.string.dup
@output.truncate(@output.rewind)
@terminal.say("<%= GREY %>")
grey_code = @output.string.dup
@output.truncate(@output.rewind)
assert_equal gray_code, grey_code
end
def test_light_is_the_same_as_bright
@terminal.say("<%= BRIGHT_BLUE %>")
bright_blue_code = @output.string.dup
@output.truncate(@output.rewind)
@terminal.say("<%= LIGHT_BLUE %>")
light_blue_code = @output.string.dup
@output.truncate(@output.rewind)
assert_equal bright_blue_code, light_blue_code
end
def test_confirm
@input << "junk.txt\nno\nsave.txt\ny\n"
@input.rewind
answer = @terminal.ask("Enter a filename: ") do |q|
q.confirm = "Are you sure you want to overwrite <%= answer %>? "
q.responses[:ask_on_error] = :question
end
assert_equal("save.txt", answer)
assert_equal("Enter a filename: " \
"Are you sure you want to overwrite junk.txt? " \
"Enter a filename: " \
"Are you sure you want to overwrite save.txt? ",
@output.string)
@input.truncate(@input.rewind)
@input << "junk.txt\nyes\nsave.txt\nn\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Enter a filename: ") do |q|
q.confirm = "Are you sure you want to overwrite <%= answer %>? "
end
assert_equal("junk.txt", answer)
assert_equal("Enter a filename: " \
"Are you sure you want to overwrite junk.txt? ",
@output.string)
@input.truncate(@input.rewind)
@input << "junk.txt\nyes\nsave.txt\nn\n"
@input.rewind
@output.truncate(@output.rewind)
scoped_variable = { "junk.txt" => "20mb" }
answer = @terminal.ask("Enter a filename: ") do |q|
q.confirm = proc do |checking_answer|
"Are you sure you want to overwrite #{checking_answer} with size " \
"of #{scoped_variable[checking_answer]}? "
end
end
assert_equal("junk.txt", answer)
assert_equal("Enter a filename: " \
"Are you sure you want to overwrite junk.txt " \
"with size of 20mb? ",
@output.string)
end
def test_generic_confirm_with_true
@input << "junk.txt\nno\nsave.txt\ny\n"
@input.rewind
answer = @terminal.ask("Enter a filename: ") do |q|
q.confirm = true
q.responses[:ask_on_error] = :question
end
assert_equal("save.txt", answer)
assert_equal("Enter a filename: " \
"Are you sure? " \
"Enter a filename: " \
"Are you sure? ",
@output.string)
@input.truncate(@input.rewind)
@input << "junk.txt\nyes\nsave.txt\nn\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Enter a filename: ") do |q|
q.confirm = true
end
assert_equal("junk.txt", answer)
assert_equal("Enter a filename: " \
"Are you sure? ",
@output.string)
end
def test_defaults
@input << "\nNo Comment\n"
@input.rewind
answer = @terminal.ask("Are you sexually active? ") do |q|
q.validate = /\Ay(?:es)?|no?|no comment\Z/i
end
assert_equal("No Comment", answer)
@input.truncate(@input.rewind)
@input << "\nYes\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Are you sexually active? ") do |q|
q.default = "No Comment"
q.validate = /\Ay(?:es)?|no?|no comment\Z/i
end
assert_equal("No Comment", answer)
assert_equal("Are you sexually active? |No Comment| ",
@output.string)
end
def test_default_with_String
@input << "\n"
@input.rewind
answer = @terminal.ask("Question: ") do |q|
q.default = "string"
end
assert_equal "string", answer
assert_equal "Question: |string| ", @output.string
end
def test_default_with_Symbol
# With a Symbol, it should show up the String version
# at prompt, but return the Symbol as answer
@input << "\n"
@input.rewind
answer = @terminal.ask("Question: ") do |q|
q.default = :string
end
assert_equal :string, answer
assert_equal "Question: |string| ", @output.string
end
def test_default_with_non_String_objects
# With a non-string object, it should try to coerce it to String.
# If the coercion is not possible, do not show
# any 'default' at prompt line. And should
# return the "default" object, without conversion.
@input << "\n"
@input.rewind
default_integer_object = 42
answer = @terminal.ask("Question: ") do |q|
q.default = default_integer_object
end
assert_equal default_integer_object, answer
assert_equal "Question: |42| ", @output.string
@input.truncate(@input.rewind)
@input << "\n"
@input.rewind
@output.truncate(@output.rewind)
default_non_string_object = Object.new
answer = @terminal.ask("Question: ") do |q|
q.default = default_non_string_object
end
assert_equal default_non_string_object, answer
assert_equal "Question: |#{default_non_string_object}| ", @output.string
@input.truncate(@input.rewind)
@input << "\n"
@input.rewind
@output.truncate(@output.rewind)
default_non_string_object = Object.new
answer = @terminal.ask("Question: ") do |q|
q.default = default_non_string_object
q.default_hint_show = false
end
assert_equal default_non_string_object, answer
assert_equal "Question: ", @output.string
end
def test_string_preservation
@input << "Maybe\nYes\n"
@input.rewind
my_string = "Is that your final answer? "
@terminal.ask(my_string) { |q| q.default = "Possibly" }
@terminal.ask(my_string) { |q| q.default = "Maybe" }
assert_equal("Is that your final answer? ", my_string)
end
def test_empty
@input << "\n"
@input.rewind
answer = @terminal.ask("") do |q|
q.default = "yes"
q.validate = /\Ay(?:es)?|no?\Z/i
end
assert_equal("yes", answer)
end
def test_erb
@terminal.say("The integers from 1 to 10 are:\n" \
"% (1...10).each do |n|\n" \
"\t<%= n %>,\n" \
"% end\n" \
"\tand 10")
assert_equal("The integers from 1 to 10 are:\n" \
"\t1,\n\t2,\n\t3,\n\t4,\n\t5,\n" \
"\t6,\n\t7,\n\t8,\n\t9,\n\tand 10\n",
@output.string)
end
def test_files
@input << "#{File.basename(__FILE__)[0, 7]}\n"
@input.rewind
assert_equal "test_hi\n", @input.read
@input.rewind
file = @terminal.ask("Select a file: ", File) do |q|
q.directory = File.expand_path(File.dirname(__FILE__))
q.glob = "*.rb"
end
assert_instance_of(File, file)
assert_equal("#!/usr/bin/env ruby\n", file.gets)
file.close
@input.rewind
pathname = @terminal.ask("Select a file: ", Pathname) do |q|
q.directory = File.expand_path(File.dirname(__FILE__))
q.glob = "*.rb"
end
assert_instance_of(Pathname, pathname)
assert_equal(File.size(__FILE__), pathname.size)
end
def test_gather_with_integer
@input << "James\nDana\nStorm\nGypsy\n\n"
@input.rewind
answers = @terminal.ask("Enter four names:") do |q|
q.gather = 4
end
assert_equal(%w[James Dana Storm Gypsy], answers)
assert_equal("\n", @input.gets)
assert_equal("Enter four names:\n", @output.string)
end
def test_gather_with_an_empty_string
@input << "James\nDana\nStorm\nGypsy\n\n"
@input.rewind
answers = @terminal.ask("Enter four names:") do |q|
q.gather = ""
end
assert_equal(%w[James Dana Storm Gypsy], answers)
end
def test_gather_with_regexp
@input << "James\nDana\nStorm\nGypsy\n\n"
@input.rewind
answers = @terminal.ask("Enter four names:") do |q|
q.gather = /^\s*$/
end
assert_equal(%w[James Dana Storm Gypsy], answers)
end
def test_gather_with_hash
@input << "29\n49\n30\n"
@input.rewind
answers = @terminal.ask("<%= key %>: ", Integer) do |q|
q.gather = { "Age" => 0, "Wife's Age" => 0, "Father's Age" => 0 }
end
assert_equal({ "Age" => 29, "Wife's Age" => 30, "Father's Age" => 49 },
answers)
assert_equal("Age: Father's Age: Wife's Age: ", @output.string)
end
def test_typing_verification
@input << "all work and no play makes jack a dull boy\n" * 3
@input.rewind
answer = @terminal.ask("How's work? ") do |q|
q.gather = 3
q.verify_match = true
end
assert_equal("all work and no play makes jack a dull boy", answer)
@input.truncate(@input.rewind)
@input << "all play and no work makes jack a mere toy\n"
@input << "all work and no play makes jack a dull boy\n" * 5
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("How are things going? ") do |q|
q.gather = 3
q.verify_match = true
q.responses[:mismatch] = "Typing mismatch!"
q.responses[:ask_on_error] = ""
end
assert_equal("all work and no play makes jack a dull boy", answer)
# now try using a hash for gather
@input.truncate(@input.rewind)
@input << "Password\nPassword\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("<%= key %>: ") do |q|
q.verify_match = true
q.gather = { "Enter a password" => "", "Please type it again" => "" }
end
assert_equal("Password", answer)
@input.truncate(@input.rewind)
@input << "Password\nMistake\nPassword\nPassword\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("<%= key %>: ") do |q|
q.verify_match = true
q.responses[:mismatch] = "Typing mismatch!"
q.responses[:ask_on_error] = ""
q.gather = { "Enter a password" => "", "Please type it again" => "" }
end
assert_equal("Password", answer)
assert_equal("Enter a password: " \
"Please type it again: " \
"Typing mismatch!\n" \
"Enter a password: " \
"Please type it again: ", @output.string)
end
def test_lists
digits = %w[Zero One Two Three Four Five Six Seven Eight Nine]
erb_digits = digits.dup
erb_digits[erb_digits.index("Five")] = "<%= color('Five', :blue) %%>"
@terminal.say("<%= list(#{digits.inspect}) %>")
assert_equal(digits.map { |d| "#{d}\n" }.join, @output.string)
@output.truncate(@output.rewind)
@terminal.say("<%= list(#{digits.inspect}, :inline) %>")
assert_equal(digits[0..-2].join(", ") + " or #{digits.last}\n",
@output.string)
@output.truncate(@output.rewind)
@terminal.say("<%= list(#{digits.inspect}, :inline, ' and ') %>")
assert_equal(digits[0..-2].join(", ") + " and #{digits.last}\n",
@output.string)
@output.truncate(@output.rewind)
@terminal.say("<%= list(#{digits.inspect}, :columns_down, 3) %>")
assert_equal("Zero Four Eight\n" \
"One Five Nine \n" \
"Two Six \n" \
"Three Seven\n",
@output.string)
@output.truncate(@output.rewind)
@terminal.say("<%= list(#{erb_digits.inspect}, :columns_down, 3) %>")
assert_equal("Zero Four Eight\n" \
"One \e[34mFive\e[0m Nine \n" \
"Two Six \n" \
"Three Seven\n",
@output.string)
colums_of_twenty = ["12345678901234567890"] * 5
@output.truncate(@output.rewind)
@terminal.say("<%= list(#{colums_of_twenty.inspect}, :columns_down) %>")
assert_equal("12345678901234567890 12345678901234567890 " \
"12345678901234567890\n" \
"12345678901234567890 12345678901234567890\n",
@output.string)
@output.truncate(@output.rewind)
colums_of_81 = ["1234567890" * (81 / 10) + "1"]
@terminal.say("<%= list(#{colums_of_81.inspect}, :columns_down) %>")
assert_equal("1234567890123456789" \
"01234567890123456789" \
"01234567890123456789" \
"0123456789012345678901\n",
@output.string)
@output.truncate(@output.rewind)
@terminal.say("<%= list(#{digits.inspect}, :columns_across, 3) %>")
assert_equal("Zero One Two \n" \
"Three Four Five \n" \
"Six Seven Eight\n" \
"Nine \n",
@output.string)
colums_of_twenty.pop
@output.truncate(@output.rewind)
@terminal.say("<%= list( #{colums_of_twenty.inspect}, :columns_across ) %>")
assert_equal("12345678901234567890 12345678901234567890 " \
"12345678901234567890\n" \
"12345678901234567890\n",
@output.string)
@output.truncate(@output.rewind)
wide = %w[0123456789 a b c d e f g h i j k l m n o p q r s t u v w x y z]
@terminal.say("<%= list( #{wide.inspect}, :uneven_columns_across ) %>")
assert_equal("0123456789 a b c d e f g h i j k l m n o " \
"p q r s t u v w\n" \
"x y z\n",
@output.string)
@output.truncate(@output.rewind)
@terminal.say("<%= list( #{wide.inspect}, :uneven_columns_across, 10 ) %>")
assert_equal("0123456789 a b c d e f g h i\n" \
"j k l m n o p q r s\n" \
"t u v w x y z\n",
@output.string)
@output.truncate(@output.rewind)
@terminal.say("<%= list( #{wide.inspect}, :uneven_columns_down ) %>")
assert_equal("0123456789 b d f h j l n p r t v x z\n" \
"a c e g i k m o q s u w y\n",
@output.string)
@output.truncate(@output.rewind)
@terminal.say("<%= list( #{wide.inspect}, :uneven_columns_down, 10 ) %>")
assert_equal("0123456789 c f i l o r u x\n" \
"a d g j m p s v y\n" \
"b e h k n q t w z\n",
@output.string)
end
def test_lists_with_zero_items
modes = [nil, :rows, :inline, :columns_across, :columns_down]
modes.each do |mode|
result = @terminal.list([], mode)
assert_equal("", result)
end
end
def test_lists_with_nil_items
modes = [nil]
modes.each do |mode|
result = @terminal.list([nil], mode)
assert_equal("\n", result)
end
end
def test_lists_with_one_item
items = ["Zero"]
modes = { nil => "Zero\n",
:rows => "Zero\n",
:inline => "Zero",
:columns_across => "Zero\n",
:columns_down => "Zero\n" }
modes.each do |mode, expected|
result = @terminal.list(items, mode)
assert_equal(expected, result)
end
end
def test_lists_with_two_items
items = %w[Zero One]
modes = { nil => "Zero\nOne\n",
:rows => "Zero\nOne\n",
:inline => "Zero or One",
:columns_across => "Zero One \n",
:columns_down => "Zero One \n" }
modes.each do |mode, expected|
result = @terminal.list(items, mode)
assert_equal(expected, result)
end
end
def test_lists_with_three_items
items = %w[Zero One Two]
modes = { nil => "Zero\nOne\nTwo\n",
:rows => "Zero\nOne\nTwo\n",
:inline => "Zero, One or Two",
:columns_across => "Zero One Two \n",
:columns_down => "Zero One Two \n" }
modes.each do |mode, expected|
result = @terminal.list(items, mode)
assert_equal(expected, result)
end
end
def test_mode
main_char_modes =
%w[ HighLine::Terminal::IOConsole
HighLine::Terminal::NCurses
HighLine::Terminal::UnixStty ]
assert(
main_char_modes.include?(@terminal.terminal.character_mode),
"#{@terminal.terminal.character_mode} not in list"
)
end
class NameClass
def self.parse(string)
raise ArgumentError, "Invalid name format." unless
string =~ /^\s*(\w+),\s*(\w+)\s+(\w+)\s*$/
new(Regexp.last_match(2), Regexp.last_match(3), Regexp.last_match(1))
end
def initialize(first, middle, last)
@first = first
@middle = middle
@last = last
end
attr_reader :first, :middle, :last
end
def test_my_class_conversion
@input << "Gray, James Edward\n"
@input.rewind
answer = @terminal.ask("Your name? ", NameClass) do |q|
q.validate = lambda do |name|
names = name.split(/,\s*/)
return false unless names.size == 2
return false if names.first =~ /\s/
names.last.split.size == 2
end
end
assert_instance_of(NameClass, answer)
assert_equal("Gray", answer.last)
assert_equal("James", answer.first)
assert_equal("Edward", answer.middle)
end
def test_no_echo
@input << "password\r"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = false
end
assert_equal("password", answer)
assert_equal("Please enter your password: \n", @output.string)
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Pick a letter or number: ") do |q|
q.character = true
q.echo = false
end
assert_equal("p", answer)
assert_equal("a", @input.getc.chr)
assert_equal("Pick a letter or number: \n", @output.string)
end
def test_correct_string_encoding_when_echo_false
@input << "ação\r" # An UTF-8 portuguese word for 'action'
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = false
end
assert_equal "ação", answer
assert_equal Encoding::UTF_8, answer.encoding
end
def test_backspace_with_ascii_when_echo_false
@input << "password\b\r"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = false
end
refute_equal("password", answer)
assert_equal("passwor", answer)
end
def test_backspace_with_utf8_when_echo_false
@input << "maçã\b\r"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = false
end
refute_equal("maçã", answer)
assert_equal("maç", answer)
end
def test_echoing_with_utf8_when_echo_is_star
@input << "maçã\r"
@input.rewind
answer = @terminal.ask("Type: ") do |q|
q.echo = "*"
end
assert_equal("Type: ****\n", @output.string)
assert_equal("maçã", answer)
end
def test_echo_false_with_ctrl_c_interrupts
@input << "String with a ctrl-c at the end \u0003 \n"
@input.rewind
@answer = nil
assert_raises(Interrupt) do
@answer = @terminal.ask("Type: ") do |q|
q.echo = false
end
end
assert_nil @answer
end
def test_range_requirements
@input << "112\n-541\n28\n"
@input.rewind
answer = @terminal.ask("Tell me your age.", Integer) do |q|
q.in = 0..105
end
assert_equal(28, answer)
assert_equal("Tell me your age.\n" \
"Your answer isn't within the expected range " \
"(included in 0..105).\n" \
"? " \
"Your answer isn't within the expected range " \
"(included in 0..105).\n" \
"? ", @output.string)
@input.truncate(@input.rewind)
@input << "1\n-541\n28\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Tell me your age.", Integer) do |q|
q.above = 3
end
assert_equal(28, answer)
assert_equal("Tell me your age.\n" \
"Your answer isn't within the expected range " \
"(above 3).\n" \
"? " \
"Your answer isn't within the expected range " \
"(above 3).\n" \
"? ", @output.string)
@input.truncate(@input.rewind)
@input << "1\n28\n-541\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Lowest numer you can think of?", Integer) do |q|
q.below = 0
end
assert_equal(-541, answer)
assert_equal("Lowest numer you can think of?\n" \
"Your answer isn't within the expected range " \
"(below 0).\n" \
"? " \
"Your answer isn't within the expected range " \
"(below 0).\n" \
"? ", @output.string)
@input.truncate(@input.rewind)
@input << "-541\n11\n6\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Enter a low even number: ", Integer) do |q|
q.above = 0
q.below = 10
end
assert_equal(6, answer)
assert_equal("Enter a low even number: " \
"Your answer isn't within the expected range " \
"(above 0 and below 10).\n" \
"? " \
"Your answer isn't within the expected range " \
"(above 0 and below 10).\n" \
"? ", @output.string)
@input.truncate(@input.rewind)
@input << "1\n-541\n6\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Enter a low even number: ", Integer) do |q|
q.above = 0
q.below = 10
q.in = [2, 4, 6, 8]
end
assert_equal(6, answer)
assert_equal("Enter a low even number: " \
"Your answer isn't within the expected range " \
"(above 0, below 10, and included in [2, 4, 6, 8]).\n" \
"? " \
"Your answer isn't within the expected range " \
"(above 0, below 10, and included in [2, 4, 6, 8]).\n" \
"? ", @output.string)
end
def test_range_requirements_with_array_of_strings
@input.truncate(@input.rewind)
@input << "z\nx\nb\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Letter a, b, or c? ") do |q|
q.in = %w[ a b c ]
end
assert_equal("b", answer)
assert_equal("Letter a, b, or c? " \
"Your answer isn't within the expected range " \
"(included in [\"a\", \"b\", \"c\"]).\n" \
"? " \
"Your answer isn't within the expected range " \
"(included in [\"a\", \"b\", \"c\"]).\n" \
"? ", @output.string)
end
def test_reask
number = 61_676
@input << "Junk!\n" << number << "\n"
@input.rewind
answer = @terminal.ask("Favorite number? ", Integer)
assert_kind_of(Integer, number)
assert_equal(number, answer)
assert_equal("Favorite number? " \
"You must enter a valid Integer.\n" \
"? ", @output.string)
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Favorite number? ", Integer) do |q|
q.responses[:ask_on_error] = :question
q.responses[:invalid_type] = "Not a valid number!"
end
assert_kind_of(Integer, number)
assert_equal(number, answer)
assert_equal("Favorite number? " \
"Not a valid number!\n" \
"Favorite number? ", @output.string)
@input.truncate(@input.rewind)
@input << "gen\ngene\n"
@input.rewind
@output.truncate(@output.rewind)
answer = @terminal.ask("Select a mode: ", [:generate, :gentle])
assert_instance_of(Symbol, answer)
assert_equal(:generate, answer)
assert_equal("Select a mode: " \
"Ambiguous choice. " \
"Please choose one of [generate, gentle].\n" \
"? ", @output.string)
end
def test_response_embedding
@input << "112\n-541\n28\n"
@input.rewind
answer = @terminal.ask("Tell me your age.", Integer) do |q|
q.in = 0..105
q.responses[:not_in_range] = "Need a #{q.answer_type}" \
" #{q.expected_range}."
end
assert_equal(28, answer)
assert_equal("Tell me your age.\n" \
"Need a Integer included in 0..105.\n" \
"? " \
"Need a Integer included in 0..105.\n" \
"? ", @output.string)
end
def test_say
@terminal.say("This will have a newline.")
assert_equal("This will have a newline.\n", @output.string)
@output.truncate(@output.rewind)
@terminal.say("This will also have one newline.\n")
assert_equal("This will also have one newline.\n", @output.string)
@output.truncate(@output.rewind)
@terminal.say("This will not have a newline. ")
assert_equal("This will not have a newline. ", @output.string)
@output.truncate(@output.rewind)
@terminal.say("This will not have a newline.\t")
assert_equal("This will not have a newline.\t", @output.string)
@output.truncate(@output.rewind)
@terminal.say("This will not\n end with a newline. ")
assert_equal("This will not\n end with a newline. ", @output.string)
@output.truncate(@output.rewind)
@terminal.say("This will \nend with a newline.")
assert_equal("This will \nend with a newline.\n", @output.string)
@output.truncate(@output.rewind)
colorized = @terminal.color("This will not have a newline. ", :green)
@terminal.say(colorized)
assert_equal("\e[32mThis will not have a newline. \e[0m", @output.string)
@output.truncate(@output.rewind)
colorized = @terminal.color("This will have a newline.", :green)
@terminal.say(colorized)
assert_equal("\e[32mThis will have a newline.\e[0m\n", @output.string)
@output.truncate(@output.rewind)
@terminal.say(nil)
assert_equal("", @output.string)
end
def test_say_handles_non_string_argument
integer = 10
hash = { a: 20 }
@terminal.say(integer)
assert_equal String(integer), @output.string.chomp
@output.truncate(@output.rewind)
@terminal.say(hash)
assert_equal String(hash), @output.string.chomp
end
def test_terminal_size
assert(@terminal.terminal.terminal_size[0] > 0)
assert(@terminal.terminal.terminal_size[1] > 0)
end
def test_type_conversion
number = 61_676
@input << number << "\n"
@input.rewind
answer = @terminal.ask("Favorite number? ", Integer)
assert_kind_of(Integer, answer)
assert_equal(number, answer)
@input.truncate(@input.rewind)
number = 1_000_000_000_000_000_000_000_000_000_000
@input << number << "\n"
@input.rewind
answer = @terminal.ask("Favorite number? ", Integer)
assert_kind_of(Integer, answer)
assert_equal(number, answer)
@input.truncate(@input.rewind)
number = 10.5002
@input << number << "\n"
@input.rewind
answer = @terminal.ask("Favorite number? ",
->(n) { n.to_f.abs.round })
assert_kind_of(Integer, answer)
assert_equal(11, answer)
@input.truncate(@input.rewind)
animal = :dog
@input << animal << "\n"
@input.rewind
answer = @terminal.ask("Favorite animal? ", Symbol)
assert_instance_of(Symbol, answer)
assert_equal(animal, answer)
@input.truncate(@input.rewind)
@input << "16th June 1976\n"
@input.rewind
answer = @terminal.ask("Enter your birthday.", Date)
assert_instance_of(Date, answer)
assert_equal(16, answer.day)
assert_equal(6, answer.month)
assert_equal(1976, answer.year)
@input.truncate(@input.rewind)
pattern = "^yes|no$"
@input << pattern << "\n"
@input.rewind
answer = @terminal.ask("Give me a pattern to match with: ", Regexp)
assert_instance_of(Regexp, answer)
assert_equal(/#{pattern}/, answer)
@input.truncate(@input.rewind)
@input << "gen\n"
@input.rewind
answer = @terminal.ask("Select a mode: ", [:generate, :run])
assert_instance_of(Symbol, answer)
assert_equal(:generate, answer)
end
def test_validation
@input << "system 'rm -rf /'\n105\n0b101_001\n"
@input.rewind
answer = @terminal.ask("Enter a binary number: ") do |q|
q.validate = /\A(?:0b)?[01_]+\Z/
end
assert_equal("0b101_001", answer)
assert_equal("Enter a binary number: " \
"Your answer isn't valid " \
"(must match /\\A(?:0b)?[01_]+\\Z/).\n" \
"? " \
"Your answer isn't valid " \
"(must match /\\A(?:0b)?[01_]+\\Z/).\n" \
"? ", @output.string)
@input.truncate(@input.rewind)
@input << "Gray II, James Edward\n" \
"Gray, Dana Ann Leslie\n" \
"Gray, James Edward\n"
@input.rewind
answer = @terminal.ask("Your name? ") do |q|
q.validate = lambda do |name|
names = name.split(/,\s*/)
return false unless names.size == 2
return false if names.first =~ /\s/
names.last.split.size == 2
end
end
assert_equal("Gray, James Edward", answer)
end
class ZeroToTwentyFourValidator
def self.valid?(answer)
(0..24).include? answer.to_i
end
def self.inspect
"(0..24) rule"
end
end
def test_validation_with_custom_validator_class
@input << "26\n25\n24\n"
@input.rewind
answer = @terminal.ask("What hour of the day is it?: ", Integer) do |q|
q.validate = ZeroToTwentyFourValidator
end
assert_equal(24, answer)
assert_equal("What hour of the day is it?: " \
"Your answer isn't valid (must match (0..24) rule).\n" \
"? Your answer isn't valid (must match (0..24) rule).\n" \
"? ", @output.string)
end
require 'dry/types'
module Types
include Dry.Types
end
def test_validation_with_dry_types
@input << "random string\nanother uncoercible string\n42\n"
@input.rewind
answer = @terminal.ask("Type a number: ", Integer) do |q|
q.validate = Types::Coercible::Integer
end
assert_equal(42, answer)
assert_match(Regexp.new(<<~REGEXP.chomp),
Type a number: Your answer isn't valid .must match .*Dry.*Types.*Integer.*..
\\\? Your answer isn't valid .must match .*Dry.*Types.*Integer.*..
\\\?
REGEXP
@output.string
)
end
def test_validation_with_overriding_static_message
@input << "Not valid answer\n" \
"42\n"
@input.rewind
answer = @terminal.ask("Enter only numbers: ") do |question|
question.validate = ->(ans) { ans =~ /\d+/ }
question.responses[:not_valid] = "We accept only numbers over here!"
end
assert_equal("42", answer)
assert_equal(
"Enter only numbers: We accept only numbers over here!\n" \
"? ",
@output.string
)
end
def test_validation_with_overriding_dynamic_message
@input << "Forty two\n" \
"42\n"
@input.rewind
answer = @terminal.ask("Enter only numbers: ") do |question|
question.validate = ->(ans) { ans =~ /\d+/ }
question.responses[:not_valid] =
->(ans) { "#{ans} is not a valid answer" }
end
assert_equal("42", answer)
assert_equal(
"Enter only numbers: " \
"Forty two is not a valid answer\n" \
"? ",
@output.string
)
end
def test_whitespace
@input << " A lot\tof \t space\t \there! \n"
@input.rewind
answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
q.whitespace = :chomp
end
assert_equal(" A lot\tof \t space\t \there! ", answer)
@input.rewind
answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
q.whitespace = :strip
end
assert_equal("A lot\tof \t space\t \there!", answer)
@input.rewind
answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
q.whitespace = :collapse
end
assert_equal(" A lot of space here! ", answer)
@input.rewind
answer = @terminal.ask("Enter a whitespace filled string: ")
assert_equal("A lot\tof \t space\t \there!", answer)
@input.rewind
answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
q.whitespace = :strip_and_collapse
end
assert_equal("A lot of space here!", answer)
@input.rewind
answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
q.whitespace = :remove
end
assert_equal("Alotofspacehere!", answer)
@input.rewind
answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
q.whitespace = :none
end
assert_equal(" A lot\tof \t space\t \there! \n", answer)
@input.rewind
answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
q.whitespace = nil
end
assert_equal(" A lot\tof \t space\t \there! \n", answer)
end
def test_track_eof
assert_raises(EOFError) { @terminal.ask("Any input left? ") }
# turn EOF tracking
old_instance = HighLine.default_instance
HighLine.default_instance = HighLine.new(StringIO.new, StringIO.new)
HighLine.track_eof = false
begin
require "highline/import"
# this will still blow up, nothing available
ask("And now? ")
rescue StandardError
# but HighLine's safe guards are off
refute_equal(EOFError, $ERROR_INFO.class)
end
HighLine.default_instance = old_instance
end
def test_version
refute_nil(HighLine::VERSION)
assert_instance_of(String, HighLine::VERSION)
assert(HighLine::VERSION.frozen?)
assert_match(/\A\d+\.\d+\.\d+(-.*)?/, HighLine::VERSION)
end
end
|