summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-11-writing-automated-tests
diff options
context:
space:
mode:
authorgarhve <git@garhve.com>2023-01-09 18:34:39 +0800
committergarhve <git@garhve.com>2023-01-09 18:34:39 +0800
commitbcc303880f7078d612207571e196697a0d0b35bf (patch)
tree162b1cc2d12ee30c2749d4fb8bda4122d8767319 /rust/theBook/chapter-11-writing-automated-tests
parentfb2299d7e10f63af36a35e0b1c16adbcdd416b02 (diff)
finish chapter 11
Diffstat (limited to 'rust/theBook/chapter-11-writing-automated-tests')
-rw-r--r--rust/theBook/chapter-11-writing-automated-tests/adder/Cargo.toml8
-rw-r--r--rust/theBook/chapter-11-writing-automated-tests/adder/src/lib.rs51
2 files changed, 59 insertions, 0 deletions
diff --git a/rust/theBook/chapter-11-writing-automated-tests/adder/Cargo.toml b/rust/theBook/chapter-11-writing-automated-tests/adder/Cargo.toml
new file mode 100644
index 0000000..b7d36d4
--- /dev/null
+++ b/rust/theBook/chapter-11-writing-automated-tests/adder/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "adder"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/rust/theBook/chapter-11-writing-automated-tests/adder/src/lib.rs b/rust/theBook/chapter-11-writing-automated-tests/adder/src/lib.rs
new file mode 100644
index 0000000..55c024c
--- /dev/null
+++ b/rust/theBook/chapter-11-writing-automated-tests/adder/src/lib.rs
@@ -0,0 +1,51 @@
+#[derive(Debug)]
+struct Rectangle {
+ width: u32,
+ height: u32,
+}
+
+impl Rectangle {
+ fn can_hold(&self, other: &Rectangle) -> bool {
+ self.width >= other.width && self.height >= other.height
+ }
+}
+
+pub fn greeting(name: &str) -> String {
+ format!("hello {}!", name)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn exception() {
+ let result = 2+2;
+ assert_eq!(result, 4);
+ }
+ #[test]
+ fn another() {
+ println!("this one will fail");
+ }
+
+ #[test]
+ fn hold() {
+ let larger = Rectangle {
+ width: 7,
+ height: 8,
+ };
+
+ let smaller = Rectangle {
+ width: 5,
+ height: 4,
+ };
+
+ assert!(larger.can_hold(&smaller));
+ }
+
+ #[test]
+ fn greeting_contain_names() {
+ let result = greeting("caol");
+ assert!(result.contains("carol"), "gretting did not contain name, the value is {}", result);
+ }
+}