summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJordan Gong <jordan.gong@protonmail.com>2020-09-04 20:46:24 +0800
committerJordan Gong <jordan.gong@protonmail.com>2020-09-04 20:46:24 +0800
commit8f14544c14efc7e591ef1dd73ac028a2c2e5e2d6 (patch)
tree5c2f3441cec0692e25eae28b777d1881c0547bfb /tests
parentf23a4fe4bcb7ae0b47d625f89abb281646ea33a7 (diff)
Write tests
Diffstat (limited to 'tests')
-rw-r--r--tests/.gitignore18
-rw-r--r--tests/Cargo.toml9
-rw-r--r--tests/src/lib.rs126
3 files changed, 153 insertions, 0 deletions
diff --git a/tests/.gitignore b/tests/.gitignore
new file mode 100644
index 0000000..e629269
--- /dev/null
+++ b/tests/.gitignore
@@ -0,0 +1,18 @@
+
+# Created by https://www.toptal.com/developers/gitignore/api/rust
+# Edit at https://www.toptal.com/developers/gitignore?templates=rust
+
+### Rust ###
+# Generated by Cargo
+# will have compiled files and executables
+/target/
+
+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
+# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
+Cargo.lock
+
+# These are backup files generated by rustfmt
+**/*.rs.bk
+
+# End of https://www.toptal.com/developers/gitignore/api/rust
+
diff --git a/tests/Cargo.toml b/tests/Cargo.toml
new file mode 100644
index 0000000..f81c142
--- /dev/null
+++ b/tests/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "tests"
+version = "0.1.0"
+authors = ["Jordan Gong <jordan.gong@protonmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/tests/src/lib.rs b/tests/src/lib.rs
new file mode 100644
index 0000000..8742a00
--- /dev/null
+++ b/tests/src/lib.rs
@@ -0,0 +1,126 @@
+#[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 add_two(a: i32) -> i32 {
+ a + 2
+}
+
+pub fn greeting(name: &str) -> String {
+ format!("Hello {}!", name)
+ // format!("Hello!")
+}
+
+pub struct Guess {
+ value: i32,
+}
+
+impl Guess {
+ pub fn new(value: i32) -> Guess {
+ // if value < 1 || value > 100 {
+ // panic!("Guess value must be between 1 and 100, got {}.", value);
+ // }
+ if value < 1 {
+ panic!(
+ "Guess value must be greater than or equal to 1, got {}.",
+ value
+ );
+ } else if value > 100 {
+ panic!(
+ "Guess value must be less than or equal to 100, got {}.",
+ value
+ );
+ }
+
+ Guess { value }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ // #[test]
+ // fn exploration() {
+ // assert_eq!(2 + 2, 4);
+ // }
+
+ // #[test]
+ // fn another() {
+ // panic!("Make this test fail");
+ // }
+
+ use super::*;
+
+ #[test]
+ fn lager_can_hold_smaller() {
+ let larger = Rectangle {
+ width: 8,
+ height: 7,
+ };
+
+ let smaller = Rectangle {
+ width: 5,
+ height: 1,
+ };
+
+ assert!(larger.can_hold(&smaller));
+ }
+
+ #[test]
+ fn smaller_can_hold_larger() {
+ let larger = Rectangle {
+ width: 8,
+ height: 7,
+ };
+
+ let smaller = Rectangle {
+ width: 5,
+ height: 1,
+ };
+
+ assert!(!smaller.can_hold(&larger));
+ }
+
+ #[test]
+ fn it_adds_two() {
+ assert_eq!(4, add_two(2));
+ }
+
+ #[test]
+ fn greeting_contains_name() {
+ let result = greeting("Jordan");
+ assert!(
+ result.contains("Jordan"),
+ "Greeting did not contain name, value was `{}`",
+ result
+ );
+ }
+
+ // #[test]
+ // #[should_panic]
+ // fn greater_than_100() {
+ // Guess::new(200);
+ // }
+
+ #[test]
+ #[should_panic(expected = "Guess value must be less than or equal to 100")]
+ fn greater_than_100() {
+ Guess::new(200);
+ }
+
+ #[test]
+ fn it_works() -> Result<(), String> {
+ if 2 + 2 == 4 {
+ Ok(())
+ } else {
+ Err(String::from("two plus two does not equal to four"))
+ }
+ }
+}