Skip to content

Commit c821959

Browse files
authored
Merge pull request #1 from utkarshg6/TestsIntoSrc
Moved tests and added comments
2 parents a25bf00 + 6652356 commit c821959

File tree

4 files changed

+82
-42
lines changed

4 files changed

+82
-42
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/target
2+
.idea/
3+
*.iml

src/lib.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,83 @@ pub fn sum(num1: i32, num2: i32) -> i32 {
55
pub fn mul(num1: i32, num2: i32) -> i32 {
66
num1 * num2
77
}
8+
9+
/*
10+
Why the move?
11+
12+
13+
It's possible to locate unit tests outside the production-code files (although you'd still want
14+
to tag them with #[cfg(test)]), but that's not the convention in Rust, and that's not the way we
15+
do it in MASQ.
16+
17+
Unit tests go in the same file as the code they test.
18+
19+
That isn't to say we never have test/ directories. We do, but it's _integration_ tests that go in
20+
the test/ directories. Integration tests don't have the same kind of tightly-coupled access to the
21+
production code that unit tests do. For integration tests, the production code is processed into
22+
a separate crate, and the integration tests are forced to interact with it only through the public
23+
crate interface.
24+
25+
That's quite useful--for integration tests--but you don't want to try writing unit tests that way.
26+
*/
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
/*
33+
Note:
34+
35+
I understand that tests are fun to write and even more fun to watch pass. In an exercise like
36+
this, there's no reason not to write all the tests you want.
37+
38+
However, in real life, unless you're making a specific rhetorical point, you want to confine
39+
yourself to tests that drive code. For example, you could drive either sum() or mul() by writing
40+
one of the tests below. You'd write the test, watch it fail, complete either sum() or mul(),
41+
watch the test succeed, and then...you're done. You can write the other three tests if you
42+
want, but they'll pass immediately: they won't drive any new code. Tests that don't drive code
43+
are overhead: they increase the cost of refactoring, if you decide on a refactoring that requires
44+
changing tests. That might be justified if the extra tests communicate something important to a
45+
reader; otherwise usually not.
46+
*/
47+
48+
#[test]
49+
fn sum_positives() {
50+
assert_eq!(sum(3, 4), 3 + 4)
51+
}
52+
53+
#[test]
54+
fn sum_negatives() {
55+
assert_eq!(sum(-2, -3), -2 + (-3))
56+
}
57+
58+
#[test]
59+
fn sum_positive_negative() {
60+
assert_eq!(sum(2, -3), 2 + (-3))
61+
}
62+
63+
#[test]
64+
fn sum_negative_positive() {
65+
assert_eq!(sum(-2, 3), -2 + 3)
66+
}
67+
68+
#[test]
69+
fn mul_positives() {
70+
assert_eq!(mul(3, 4), 3 * 4)
71+
}
72+
73+
#[test]
74+
fn mul_negatives() {
75+
assert_eq!(mul(-2, -3), -2 * (-3))
76+
}
77+
78+
#[test]
79+
fn mul_positive_negative() {
80+
assert_eq!(mul(2, -3), 2 * (-3))
81+
}
82+
83+
#[test]
84+
fn mul_negative_positive() {
85+
assert_eq!(mul(-2, 3), -2 * 3)
86+
}
87+
}

tests/mul_tests.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/sum_tests.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)