]> piware.de Git - learn-rust.git/blobdiff - concepts/src/main.rs
concepts: Add alternative Post implementation with states as types
[learn-rust.git] / concepts / src / main.rs
index f9eafe95978b7bb802c7f1b8ffd3088cf2c19792..4b6df6447be9930e5773202f24601cd817722eb8 100644 (file)
@@ -256,10 +256,33 @@ fn test_dyn_traits() {
     post.request_review();
     assert_eq!("", post.content());
 
     post.request_review();
     assert_eq!("", post.content());
 
-    post.approve();
+    post.reject();
+    assert_eq!("", post.content());
+
+    post.request_review();
+    assert_eq!("", post.content());
+
+    post.approve();  // first
+    assert_eq!("", post.content());
+
+    post.approve();  // second
+    assert_eq!(text, post.content());
+
+    post.reject();  // no-op
     assert_eq!(text, post.content());
 }
 
     assert_eq!(text, post.content());
 }
 
+fn test_state_types() {
+    let mut post = TPost::new();
+    post.add_text("I ate a salad for lunch");
+    let post = post.request_review();
+    let mut post = post.reject();
+    post.add_text(" today");
+    let post = post.request_review();
+    let post = post.approve();
+    assert_eq!(post.content(), "I ate a salad for lunch today");
+}
+
 fn main() {
     test_strings();
     test_vectors();
 fn main() {
     test_strings();
     test_vectors();
@@ -270,4 +293,5 @@ fn main() {
     test_iterators();
     test_threads();
     test_dyn_traits();
     test_iterators();
     test_threads();
     test_dyn_traits();
+    test_state_types();
 }
 }