Skip to content

Commit 69db97b

Browse files
authored
Merge pull request #136 from simonsso/patch-3
Allow data from i2c write to be ignored
2 parents cf8ab28 + b165f21 commit 69db97b

1 file changed

Lines changed: 68 additions & 20 deletions

File tree

src/eh1/i2c.rs

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ pub struct Transaction {
9090
/// This is in addition to the mode to allow validation that the
9191
/// transaction mode is correct prior to returning the error.
9292
expected_err: Option<ErrorKind>,
93+
94+
/// Read data but ignore contents
95+
ignore_write: bool,
9396
}
9497

9598
impl Transaction {
@@ -101,6 +104,7 @@ impl Transaction {
101104
expected_data: expected,
102105
response_data: Vec::new(),
103106
expected_err: None,
107+
ignore_write: false,
104108
}
105109
}
106110

@@ -112,6 +116,7 @@ impl Transaction {
112116
expected_data: Vec::new(),
113117
response_data: response,
114118
expected_err: None,
119+
ignore_write: false,
115120
}
116121
}
117122

@@ -123,6 +128,7 @@ impl Transaction {
123128
expected_data: expected,
124129
response_data: response,
125130
expected_err: None,
131+
ignore_write: false,
126132
}
127133
}
128134

@@ -134,6 +140,7 @@ impl Transaction {
134140
expected_data: Vec::new(),
135141
response_data: Vec::new(),
136142
expected_err: None,
143+
ignore_write: false,
137144
}
138145
}
139146

@@ -145,6 +152,7 @@ impl Transaction {
145152
expected_data: Vec::new(),
146153
response_data: Vec::new(),
147154
expected_err: None,
155+
ignore_write: false,
148156
}
149157
}
150158

@@ -154,9 +162,20 @@ impl Transaction {
154162
///
155163
/// Note: When attaching this to a read transaction, the response in the
156164
/// expectation will not actually be written to the buffer.
157-
pub fn with_error(mut self, error: ErrorKind) -> Self {
158-
self.expected_err = Some(error);
159-
self
165+
pub fn with_error(self, error: ErrorKind) -> Self {
166+
Self {
167+
expected_err: Some(error),
168+
..self
169+
}
170+
}
171+
172+
/// Modify a transaction to ignre its contents
173+
///
174+
pub fn with_ignore_write(self) -> Self {
175+
Self {
176+
ignore_write: true,
177+
..self
178+
}
160179
}
161180
}
162181

@@ -202,12 +221,13 @@ impl I2c for Mock {
202221
.expect("no pending expectation for i2c::write call");
203222

204223
assert_eq!(e.expected_mode, Mode::Write, "i2c::write unexpected mode");
205-
assert_eq!(e.expected_addr, address, "i2c::write address mismatch");
206-
assert_eq!(
207-
e.expected_data, bytes,
208-
"i2c::write data does not match expectation"
209-
);
210-
224+
if !e.ignore_write {
225+
assert_eq!(e.expected_addr, address, "i2c::write address mismatch");
226+
assert_eq!(
227+
e.expected_data, bytes,
228+
"i2c::write data does not match expectation"
229+
);
230+
}
211231
match e.expected_err {
212232
Some(err) => Err(err),
213233
None => Ok(()),
@@ -229,17 +249,19 @@ impl I2c for Mock {
229249
Mode::WriteRead,
230250
"i2c::write_read unexpected mode"
231251
);
232-
assert_eq!(e.expected_addr, address, "i2c::write_read address mismatch");
233-
assert_eq!(
234-
e.expected_data, bytes,
235-
"i2c::write_read write data does not match expectation"
236-
);
237-
238-
assert_eq!(
239-
buffer.len(),
240-
e.response_data.len(),
241-
"i2c::write_read mismatched response length"
242-
);
252+
if !e.ignore_write {
253+
assert_eq!(e.expected_addr, address, "i2c::write_read address mismatch");
254+
assert_eq!(
255+
e.expected_data, bytes,
256+
"i2c::write_read write data does not match expectation"
257+
);
258+
259+
assert_eq!(
260+
buffer.len(),
261+
e.response_data.len(),
262+
"i2c::write_read mismatched response length"
263+
);
264+
}
243265

244266
match e.expected_err {
245267
Some(err) => Err(err),
@@ -331,6 +353,20 @@ mod test {
331353
i2c.done();
332354
}
333355

356+
#[test]
357+
fn write_ignore() {
358+
let expectations = [
359+
Transaction::write(0xaa, vec![]).with_ignore_write(),
360+
Transaction::write(0xaa, vec![0, 0x55]),
361+
];
362+
let mut i2c = Mock::new(&expectations);
363+
364+
i2c.write(0xaa, &vec![10, 11, 12]).unwrap();
365+
i2c.write(0xaa, &vec![0, 0x55]).unwrap();
366+
367+
i2c.done();
368+
}
369+
334370
#[test]
335371
fn read() {
336372
let expectations = [Transaction::read(0xaa, vec![1, 2])];
@@ -356,6 +392,18 @@ mod test {
356392
i2c.done();
357393
}
358394

395+
#[test]
396+
fn write_read_ignore_write() {
397+
let expectations = [Transaction::write_read(0xaa, vec![], vec![3, 4]).with_ignore_write()];
398+
let mut i2c = Mock::new(&expectations);
399+
400+
let v = vec![1, 2];
401+
let mut buf = vec![0; 2];
402+
i2c.write_read(0xaa, &v, &mut buf).unwrap();
403+
assert_eq!(vec![3, 4], buf);
404+
405+
i2c.done();
406+
}
359407
#[test]
360408
fn multiple_transactions() {
361409
let expectations = [

0 commit comments

Comments
 (0)