test_loop.bats
1 load $(basher package-path ztombol/bats-support)/load.bash 2 load $(basher package-path ztombol/bats-assert)/load.bash 3 4 setup() { 5 test_loop=loop1 6 } 7 8 teardown() { 9 loop stop $test_loop || true 10 } 11 12 @test "errors without arguments" { 13 run loop 14 assert_failure 15 } 16 17 @test "correctly initializes a loop" { 18 run loop init $test_loop 19 assert_success 20 21 run loop exists $test_loop 22 assert_success 23 } 24 25 @test "correctly stops a loop" { 26 loop init $test_loop 27 28 run loop stop $test_loop 29 assert_success 30 31 run loop dead $test_loop 32 assert_success 33 } 34 35 @test "correctly pauses a loop" { 36 loop init $test_loop 37 38 run loop pause $test_loop 39 assert_success 40 41 run loop paused $test_loop 42 assert_success 43 } 44 45 @test "correctly resumes a loop" { 46 loop init $test_loop 47 loop pause $test_loop 48 49 run loop resume $test_loop 50 assert_success 51 52 run loop alive $test_loop 53 assert_success 54 } 55 56 @test "a just initialized loop is alive" { 57 loop init $test_loop 58 run loop alive $test_loop 59 assert_success 60 } 61 62 @test "can't initialize an already existing loop" { 63 loop init $test_loop 64 run loop init $test_loop 65 assert_failure 66 assert_output "loop: '$test_loop' already initialized" ] 67 } 68 69 @test "can't stop a dead loop" { 70 run loop stop $test_loop 71 assert_failure 72 } 73 74 @test "can't pause a dead loop" { 75 run loop pause $test_loop 76 assert_failure 77 } 78 79 @test "can't resume a dead loop" { 80 run loop resume $test_loop 81 assert_failure 82 } 83 84 @test "pausing a paused loop has no effect" { 85 loop init $test_loop 86 loop pause $test_loop 87 88 run loop pause $test_loop 89 assert_success 90 91 run loop paused $test_loop 92 assert_success 93 } 94 95 @test "resuming an alive loop has no effect" { 96 loop init $test_loop 97 98 run loop resume $test_loop 99 assert_success 100 101 run loop alive $test_loop 102 assert_success 103 } 104 105 @test "control returns 1 with dead loop" { 106 run loop control $test_loop 107 assert_failure 108 } 109 110 @test "control correctly breaks a loop" { 111 breaking_loop() { 112 for l in line1 line2; do 113 echo $l 114 loop control $test_loop || break 115 done 116 } 117 run breaking_loop 118 assert_success 119 assert_equal ${#lines[@]} 1 120 assert_line --index 0 "line1" 121 } 122 123 @test "control lets loop run when alive" { 124 loop init $test_loop 125 running_loop() { 126 for l in line1 line2; do 127 echo $l 128 loop control $test_loop 129 done 130 } 131 run running_loop 132 assert_success 133 assert_equal ${#lines[@]} 2 134 assert_line --index 0 "line1" 135 assert_line --index 1 "line2" 136 }