Skip to content

Commit 237ec2b

Browse files
author
Gonzalo Diaz
committed
Sample function and test re-organized.
Source code and Unit Test file splitted. https://doc.rust-lang.org/book/ch11-03-test-organization.html
1 parent 0208dc7 commit 237ec2b

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

src/add.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn add(left: u64, right: u64) -> u64 {
2+
left + right
3+
}

src/lib.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1 @@
1-
pub fn add(left: u64, right: u64) -> u64 {
2-
left + right
3-
}
4-
5-
#[cfg(test)]
6-
mod tests {
7-
use super::*;
8-
9-
#[test]
10-
fn it_works() {
11-
let result = add(2, 2);
12-
assert_eq!(result, 4);
13-
}
14-
}
1+
pub mod add;

tests/add.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"title": "Example",
4+
"a": 2,
5+
"b": 2,
6+
"result": 4
7+
}
8+
]

tests/add.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use exercises::add::add;
2+
use once_cell::sync::Lazy;
3+
use serde::Deserialize;
4+
5+
mod common;
6+
use common::utils::load_json;
7+
8+
#[derive(Debug, Deserialize)]
9+
struct AdderTestCase {
10+
title: String,
11+
a: u64,
12+
b: u64,
13+
result: u64
14+
}
15+
16+
static TEST_DATA: Lazy<Vec<AdderTestCase>> =
17+
Lazy::new(|| load_json("tests/add.json"));
18+
19+
#[cfg(test)]
20+
mod tests {
21+
use super::*;
22+
23+
#[test]
24+
fn test_adder() {
25+
for test_case in TEST_DATA.iter() {
26+
println!("Probando AdderTestCase : {:?}", test_case.title);
27+
28+
let result = add(test_case.a, test_case.b);
29+
assert_eq!(result, test_case.result);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)