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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « samples/raspberry_pi/basic/knight-rider.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE.md file.
4
5 import 'package:expect/expect.dart';
6 import 'package:gpio/gpio.dart';
7 import 'package:gpio/gpio_mock.dart';
8
9 main() {
10 test1();
11 test2();
12 test3();
13 }
14
15 void test1() {
16 var gpio = new MockGPIO();
17 Expect.equals(GPIO.defaultPins, gpio.pins);
18 for (int i = 0; i < gpio.pins; i++) {
19 Expect.isFalse(gpio.getPin(i));
20 gpio.setPin(i, true);
21 Expect.isTrue(gpio.getPin(i));
22 }
23 }
24
25 void test2() {
26 void checkRange(gpio, pins) {
27 bool isRangeError(e) => e is RangeError;
28 Expect.throws(() => gpio.getPin(-1), isRangeError);
29 Expect.throws(() => gpio.getPin(pins + 1), isRangeError);
30 Expect.throws(() => gpio.setPin(-1, true), isRangeError);
31 Expect.throws(() => gpio.setPin(pins + 1, true), isRangeError);
32 Expect.throws(() => gpio.getMode(-1), isRangeError);
33 Expect.throws(() => gpio.getMode(pins + 1), isRangeError);
34 Expect.throws(() => gpio.setMode(-1, Mode.output), isRangeError);
35 Expect.throws(() => gpio.setMode(pins + 1, Mode.output), isRangeError);
36 }
37 checkRange(new MockGPIO(), GPIO.defaultPins);
38 checkRange(new MockGPIO(1), 1);
39 checkRange(new MockGPIO(2), 2);
40 }
41
42 void test3() {
43 int getCount = 0;
44 int setCount = 0;
45
46 bool getPin(int pin) {
47 getCount++;
48 if (getCount == 1) {
49 throw new _MyException();
50 } else {
51 return true;
52 }
53 }
54
55 void setPin(int pin, bool value) {
56 setCount++;
57 if (setCount == 1) {
58 throw new _MyException();
59 }
60 }
61
62 var gpio = new MockGPIO();
63 gpio.registerGetPin(getPin);
64 gpio.registerSetPin(setPin);
65 Expect.throws(() => gpio.getPin(1), (e) => e is _MyException);
66 Expect.throws(() => gpio.setPin(1, false), (e) => e is _MyException);
67 Expect.isTrue(gpio.getPin(1));
68 gpio.setPin(1, false);
69 Expect.equals(2, getCount);
70 Expect.equals(2, setCount);
71 }
72
73 class _MyException implements Exception {}
OLDNEW
« 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