Skip to content

Commit

Permalink
Add event-driven input tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ClockVapor committed Jul 30, 2020
1 parent a182c9c commit 25c4408
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rpi_gpio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def self.watch(channel, on:, bounce_time: nil, &block)

def self.stop_watching(channel)
gpio = get_gpio_number(channel)
ensure_gpio_input(gpio)
remove_edge_detect(gpio)
remove_callbacks(gpio)
end
Expand Down
209 changes: 209 additions & 0 deletions spec/gpio_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,213 @@
end
end
end

describe "watch" do
context "before numbering is set" do
it "raises an error" do
expect { RPi::GPIO.watch(11, :on => :rising) { |pin, value| } } .to raise_error RuntimeError
end
end

context "after numbering is set" do
before :each do
RPi::GPIO.set_numbering :board
end

context "given a valid output channel" do
before :each do
RPi::GPIO.setup 11, :as => :output
end

it "raises an error" do
expect { RPi::GPIO.watch(11, :on => :rising) { |pin, value| } } .to raise_error RuntimeError
end
end

context "given a valid input channel" do
before :each do
RPi::GPIO.setup 11, :as => :input
end

context "that isn't being watched yet" do
it "accepts :rising" do
expect { RPi::GPIO.watch(11, :on => :rising) { |pin, value| } } .to_not raise_error
end

it "accepts :falling" do
expect { RPi::GPIO.watch(11, :on => :falling) { |pin, value| } } .to_not raise_error
end

it "accepts :both" do
expect { RPi::GPIO.watch(11, :on => :both) { |pin, value| } } .to_not raise_error
end

it "accepts a numeric bounce_time" do
expect { RPi::GPIO.watch(11, :on => :rising, :bounce_time => 200) { |pin, value| } }
.to_not raise_error
end
end

context "that's already being watched" do
before :each do
RPi::GPIO.watch(11, :on => :rising) { |pin, value| }
end

it "raises an error" do
expect { RPi::GPIO.watch(11, :on => :rising) { |pin, value| } } .to raise_error RuntimeError
end
end
end

context "given a valid, unset channel" do
it "raises an error" do
expect { RPi::GPIO.watch(11, :on => :rising) { |pin, value| } } .to raise_error RuntimeError
end
end

context "given an invalid channel" do
it "raises an error" do
expect { RPi::GPIO.watch(0, :on => :rising) { |pin, value| } } .to raise_error ArgumentError
end
end
end
end

describe "stop_watching" do
context "before numbering is set" do
it "raises an error" do
expect { RPi::GPIO.stop_watching 11 } .to raise_error RuntimeError
end
end

context "after numbering is set" do
before :each do
RPi::GPIO.set_numbering :board
end

context "given a valid output channel" do
before :each do
RPi::GPIO.setup 11, :as => :output
end

it "raises an error" do
expect { RPi::GPIO.stop_watching 11 } .to raise_error RuntimeError
end
end

context "given a valid input channel" do
before :each do
RPi::GPIO.setup 11, :as => :input
end

context "that isn't being watched yet" do
it "doesn't raise an error" do
expect { RPi::GPIO.stop_watching 11 } .to_not raise_error
end
end

context "that's being watched" do
before :each do
RPi::GPIO.watch(11, :on => :rising) { |pin, value| }
end

it "doesn't raise an error" do
expect { RPi::GPIO.stop_watching 11 } .to_not raise_error
end

it "lets the user re-watch the channel" do
RPi::GPIO.stop_watching 11
expect { RPi::GPIO.watch(11, :on => :rising) { |pin, value| } } .to_not raise_error
end
end
end

context "given a valid, unset channel" do
it "raises an error" do
expect { RPi::GPIO.stop_watching 11 } .to raise_error RuntimeError
end
end

context "given an invalid channel" do
it "raises an error" do
expect { RPi::GPIO.stop_watching 0 } .to raise_error ArgumentError
end
end
end
end

describe "wait_for_edge" do
context "before numbering is set" do
it "raises an error" do
expect { RPi::GPIO.wait_for_edge 11, :rising } .to raise_error RuntimeError
end
end

context "after numbering is set" do
before :each do
RPi::GPIO.set_numbering :board
end

context "given a valid output channel" do
before :each do
RPi::GPIO.setup 11, :as => :output
end

it "raises an error" do
expect { RPi::GPIO.wait_for_edge 11, :rising } .to raise_error RuntimeError
end
end

context "given a valid input channel" do
before :each do
RPi::GPIO.setup 11, :as => :input
end

context "that isn't being watched yet" do
it "accepts :rising" do
expect { RPi::GPIO.wait_for_edge 11, :rising, :timeout => 100 } .to_not raise_error
end

it "accepts :falling" do
expect { RPi::GPIO.wait_for_edge 11, :falling, :timeout => 100 } .to_not raise_error
end

it "accepts :both" do
expect { RPi::GPIO.wait_for_edge 11, :both, :timeout => 100 } .to_not raise_error
end

it "returns nil on timeout" do
RPi::GPIO.wait_for_edge(11, :both, :timeout => 100).should eq nil
end
end

context "that's being watched" do
before :each do
RPi::GPIO.watch(11, :on => :rising) { |pin, value| }
end

it "doesn't raise an error" do
expect { RPi::GPIO.stop_watching 11 } .to_not raise_error
end

it "lets the user re-watch the channel" do
RPi::GPIO.stop_watching 11
expect { RPi::GPIO.watch(11, :on => :rising) { |pin, value| } } .to_not raise_error
end
end
end

context "given a valid, unset channel" do
it "raises an error" do
expect { RPi::GPIO.stop_watching 11 } .to raise_error RuntimeError
end
end

context "given an invalid channel" do
it "raises an error" do
expect { RPi::GPIO.stop_watching 0 } .to raise_error ArgumentError
end
end
end
end
end

0 comments on commit 25c4408

Please sign in to comment.