|
1 | 1 | use super::basic_types::SizeType;
|
| 2 | +use super::field_info::FieldInfo; |
2 | 3 | use super::results::{FlagResult, OpenInfo, OpenResult};
|
3 | 4 | use super::table::{BasicTable, Table};
|
4 | 5 | use hrsw::Stopwatch;
|
@@ -132,12 +133,19 @@ impl Game {
|
132 | 133 | pub fn get_elapsed(&self) -> Duration {
|
133 | 134 | self.stopwatch.elapsed()
|
134 | 135 | }
|
| 136 | + |
| 137 | + pub fn get_field_info(&self, row: SizeType, col: SizeType) -> Result<FieldInfo, &'static str> { |
| 138 | + self.table.get_field_info(row, col) |
| 139 | + } |
135 | 140 | }
|
136 | 141 |
|
137 | 142 | #[cfg(test)]
|
138 | 143 | mod test {
|
| 144 | + use super::super::field_info::{FieldState, FieldType}; |
139 | 145 | use super::super::table::MockTable;
|
140 | 146 | use super::*;
|
| 147 | + use mockall::predicate::eq; |
| 148 | + use mockall::Sequence; |
141 | 149 | use std::collections::HashMap;
|
142 | 150 | use std::thread;
|
143 | 151 | use std::time::Instant;
|
@@ -331,4 +339,83 @@ mod test {
|
331 | 339 | let mut game = Game::new_from_table(Box::new(mock_table));
|
332 | 340 | let _ = game.open_neighbors(1, 1);
|
333 | 341 | }
|
| 342 | + |
| 343 | + #[test] |
| 344 | + fn test_get_field_info() { |
| 345 | + let mut mock_table = MockTable::new(); |
| 346 | + let expected_field_info_1 = FieldInfo { |
| 347 | + state: FieldState::Flagged, |
| 348 | + field_type: FieldType::Numbered(4), |
| 349 | + }; |
| 350 | + let expected_field_info_2 = FieldInfo { |
| 351 | + state: FieldState::Closed, |
| 352 | + field_type: FieldType::Mine, |
| 353 | + }; |
| 354 | + let expected_field_info_3 = FieldInfo { |
| 355 | + state: FieldState::Opened, |
| 356 | + field_type: FieldType::Empty, |
| 357 | + }; |
| 358 | + let row_1 = 0; |
| 359 | + let col_1 = 1; |
| 360 | + let row_2 = 2; |
| 361 | + let col_2 = 3; |
| 362 | + let row_3 = 5; |
| 363 | + let col_3 = 4; |
| 364 | + let mut seq = Sequence::new(); |
| 365 | + |
| 366 | + mock_table |
| 367 | + .expect_get_field_info() |
| 368 | + .with(eq(row_1), eq(col_1)) |
| 369 | + .times(1) |
| 370 | + .in_sequence(&mut seq) |
| 371 | + .return_const(Ok(expected_field_info_1.clone())); |
| 372 | + |
| 373 | + mock_table |
| 374 | + .expect_open_field() |
| 375 | + .with(eq(row_1), eq(col_1)) |
| 376 | + .times(1) |
| 377 | + .in_sequence(&mut seq) |
| 378 | + .returning(create_default_open_result); |
| 379 | + |
| 380 | + mock_table |
| 381 | + .expect_get_field_info() |
| 382 | + .with(eq(row_2), eq(col_2)) |
| 383 | + .times(1) |
| 384 | + .in_sequence(&mut seq) |
| 385 | + .return_const(Ok(expected_field_info_2.clone())); |
| 386 | + |
| 387 | + mock_table |
| 388 | + .expect_open_field() |
| 389 | + .with(eq(row_2), eq(col_2)) |
| 390 | + .times(1) |
| 391 | + .in_sequence(&mut seq) |
| 392 | + .returning(|_, _| { |
| 393 | + Ok(OpenInfo { |
| 394 | + result: OpenResult::Boom, |
| 395 | + newly_opened_fields: HashMap::new(), |
| 396 | + }) |
| 397 | + }); |
| 398 | + |
| 399 | + mock_table |
| 400 | + .expect_get_field_info() |
| 401 | + .with(eq(row_3), eq(col_3)) |
| 402 | + .times(1) |
| 403 | + .in_sequence(&mut seq) |
| 404 | + .return_const(Ok(expected_field_info_3.clone())); |
| 405 | + |
| 406 | + let mut game = Game::new_from_table(Box::new(mock_table)); |
| 407 | + |
| 408 | + let mut field_info = game.get_field_info(row_1, col_1).unwrap(); |
| 409 | + assert_eq!(expected_field_info_1, field_info); |
| 410 | + |
| 411 | + let _ = game.open(row_1, col_1); |
| 412 | + |
| 413 | + field_info = game.get_field_info(row_2, col_2).unwrap(); |
| 414 | + assert_eq!(expected_field_info_2, field_info); |
| 415 | + |
| 416 | + let _ = game.open(row_2, col_2); |
| 417 | + |
| 418 | + field_info = game.get_field_info(row_3, col_3).unwrap(); |
| 419 | + assert_eq!(expected_field_info_3, field_info); |
| 420 | + } |
334 | 421 | }
|
0 commit comments