Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Unified Diff: tests/pkg/gpio/gpio_mock_test.dart

Issue 1389573002: Refactor Raspberry Pi GPIO and add GPIO mock (Closed) Base URL: git@github.com:dart-lang/fletch.git@master
Patch Set: Remove unused constant Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « samples/raspberry_pi/basic/knight-rider.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/pkg/gpio/gpio_mock_test.dart
diff --git a/tests/pkg/gpio/gpio_mock_test.dart b/tests/pkg/gpio/gpio_mock_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..c71a04b643e06210179cb29fca84b1010162d729
--- /dev/null
+++ b/tests/pkg/gpio/gpio_mock_test.dart
@@ -0,0 +1,73 @@
+// Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE.md file.
+
+import 'package:expect/expect.dart';
+import 'package:gpio/gpio.dart';
+import 'package:gpio/gpio_mock.dart';
+
+main() {
+ test1();
+ test2();
+ test3();
+}
+
+void test1() {
+ var gpio = new MockGPIO();
+ Expect.equals(GPIO.defaultPins, gpio.pins);
+ for (int i = 0; i < gpio.pins; i++) {
+ Expect.isFalse(gpio.getPin(i));
+ gpio.setPin(i, true);
+ Expect.isTrue(gpio.getPin(i));
+ }
+}
+
+void test2() {
+ void checkRange(gpio, pins) {
+ bool isRangeError(e) => e is RangeError;
+ Expect.throws(() => gpio.getPin(-1), isRangeError);
+ Expect.throws(() => gpio.getPin(pins + 1), isRangeError);
+ Expect.throws(() => gpio.setPin(-1, true), isRangeError);
+ Expect.throws(() => gpio.setPin(pins + 1, true), isRangeError);
+ Expect.throws(() => gpio.getMode(-1), isRangeError);
+ Expect.throws(() => gpio.getMode(pins + 1), isRangeError);
+ Expect.throws(() => gpio.setMode(-1, Mode.output), isRangeError);
+ Expect.throws(() => gpio.setMode(pins + 1, Mode.output), isRangeError);
+ }
+ checkRange(new MockGPIO(), GPIO.defaultPins);
+ checkRange(new MockGPIO(1), 1);
+ checkRange(new MockGPIO(2), 2);
+}
+
+void test3() {
+ int getCount = 0;
+ int setCount = 0;
+
+ bool getPin(int pin) {
+ getCount++;
+ if (getCount == 1) {
+ throw new _MyException();
+ } else {
+ return true;
+ }
+ }
+
+ void setPin(int pin, bool value) {
+ setCount++;
+ if (setCount == 1) {
+ throw new _MyException();
+ }
+ }
+
+ var gpio = new MockGPIO();
+ gpio.registerGetPin(getPin);
+ gpio.registerSetPin(setPin);
+ Expect.throws(() => gpio.getPin(1), (e) => e is _MyException);
+ Expect.throws(() => gpio.setPin(1, false), (e) => e is _MyException);
+ Expect.isTrue(gpio.getPin(1));
+ gpio.setPin(1, false);
+ Expect.equals(2, getCount);
+ Expect.equals(2, setCount);
+}
+
+class _MyException implements Exception {}
« no previous file with comments | « samples/raspberry_pi/basic/knight-rider.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698