| 1 | #! /bin/bash |
| 2 | # Released under GPL licence v2 or upper |
| 3 | # |
| 4 | # When executed ($ ./tests.sh), no errors should occur in the |
| 5 | # two first sections. The third one should result in only error messages, |
| 6 | # each one different. Segfault or default error messages must not appear. |
| 7 | # |
| 8 | # The following lines are the expected results: |
| 9 | # Use argv |
| 10 | #2 |
| 11 | #3 |
| 12 | #Roll #1: (2) = 2 |
| 13 | #Roll #1: (1) = 1 |
| 14 | #Roll #1: (1) = 1 |
| 15 | #2 |
| 16 | #2 |
| 17 | # Use stdin |
| 18 | #1 |
| 19 | #1 |
| 20 | #4 |
| 21 | #Roll #1: (1) = 1 |
| 22 | #Roll #1: (1) = 1 |
| 23 | #Roll #1: (2 1) + 2 = 5 |
| 24 | # Error messages handle numbers that are too large |
| 25 | #rolldice: Requested number of dice faces is too large |
| 26 | #rolldice: Requested number of dropped dice is too large |
| 27 | #rolldice: Requested number of rolled dice is too large |
| 28 | #rolldice: Requested multiplier is too large |
| 29 | #rolldice: Requested add modifier is too large |
| 30 | #rolldice: Requested minus modifier is too large |
| 31 | |
| 32 | |
| 33 | function check_result { |
| 34 | OUTPUT=`../rolldice $1` |
| 35 | EXIT_VAL=$? |
| 36 | TEST_SUCCESS=true |
| 37 | |
| 38 | if [[ $EXIT_VAL != 0 ]] |
| 39 | then |
| 40 | echo ${1}": ERROR (return value)" |
| 41 | TEST_SUCCESS=false |
| 42 | fi |
| 43 | |
| 44 | if echo ${OUTPUT} | grep -q -v $2 |
| 45 | then |
| 46 | echo ${1}": ERROR ("${OUTPUT}")" |
| 47 | TEST_SUCCESS=false |
| 48 | fi |
| 49 | |
| 50 | if $TEST_SUCCESS; |
| 51 | then |
| 52 | echo ${1}": OK" |
| 53 | fi |
| 54 | } |
| 55 | |
| 56 | function check_error { |
| 57 | OUTPUT=`../rolldice $1` |
| 58 | EXIT_VAL=$? |
| 59 | |
| 60 | # 65 == EX_DATAERR /* data format error */ |
| 61 | if [[ $EXIT_VAL != 65 ]] |
| 62 | then |
| 63 | echo ${1}": ERROR ("$EXIT_VAL" instead of 65)" |
| 64 | fi |
| 65 | |
| 66 | } |
| 67 | |
| 68 | echo -e "\tResult between right limits" |
| 69 | check_result "1d2" "^[12]" |
| 70 | check_result "1d2+1" "^[23]" |
| 71 | check_result "1d2*2" "^[24]" |
| 72 | check_result "1d2-1" "^[01]" |
| 73 | |
| 74 | |
| 75 | echo -e "\tUse argv" |
| 76 | ../rolldice 1d2 |
| 77 | ../rolldice 1d2+1 |
| 78 | ../rolldice -s 1d2 |
| 79 | ../rolldice -s -u 1d2 |
| 80 | ../rolldice -s -r 1d2 |
| 81 | ../rolldice 1d2 1d3 |
| 82 | ../rolldice 1d% |
| 83 | ../rolldice 1d%+1 |
| 84 | |
| 85 | echo -e "\tUse stdin" |
| 86 | cat rollfile | ../rolldice |
| 87 | cat rollfile | ../rolldice -s |
| 88 | |
| 89 | echo -e "\tError messages handle numbers that are too large" |
| 90 | check_error "1d123456789" |
| 91 | check_error "2d3s123456789" |
| 92 | check_error "123456789x2d2" |
| 93 | check_error "2d2*123456789" |
| 94 | check_error "2d2+123456789" |
| 95 | check_error "2d2-123456789" |
| 96 |