aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-01-05 21:59:32 +0100
committerRuben Beltran del Rio <git@r.bdr.sh>2025-01-05 21:59:32 +0100
commit6982afd6d0d5b43dcaa35f8d84b1d0c22ba1dfe2 (patch)
tree6071106a3ee79ba5a8246631f5b14999d5f25e51 /src
parenta2df493411146cac41d29e6d5fffadea96ec97cb (diff)
Add tests for post
Diffstat (limited to 'src')
-rw-r--r--src/post.rs90
-rw-r--r--src/template.rs2
2 files changed, 91 insertions, 1 deletions
diff --git a/src/post.rs b/src/post.rs
index 6f2120e..51614aa 100644
--- a/src/post.rs
+++ b/src/post.rs
@@ -10,6 +10,7 @@ pub struct Post {
}
impl Post {
+ /// Converts a post into a template `Context` object
pub fn to_template_context(posts: &[Post]) -> Context {
let mut context = HashMap::new();
@@ -70,3 +71,92 @@ impl Post {
.replace('\'', "&apos;")
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::template::Value;
+
+ #[test]
+ fn test_creates_context_with_empty_posts() {
+ let context = Post::to_template_context(&[]);
+
+ assert_eq!(context["has_posts"], Value::Bool(false));
+ assert_eq!(context["posts_length"], Value::Unsigned(0));
+ if let Value::Collection(posts) = &context["posts"] {
+ assert!(posts.is_empty());
+ } else {
+ panic!("The posts context was not the right type.");
+ }
+ }
+
+ #[test]
+ fn test_creates_context_with_post_slice() {
+ let post = Post {
+ metadata: Metadata {
+ id: "cool".to_string(),
+ created_on: 1736035200000,
+ },
+ index: 28,
+ html: "<p>beep boop</p>".to_string(),
+ raw: "beep boop".to_string(),
+ };
+
+ let context = Post::to_template_context(&[post]);
+
+ assert_eq!(context["has_posts"], Value::Bool(true));
+ assert_eq!(context["posts_length"], Value::Unsigned(1));
+ if let Value::Collection(posts) = &context["posts"] {
+ if let Some(post) = posts.first() {
+ assert_eq!(post["id"], Value::String("cool".to_string()));
+ } else {
+ panic!("The template context had no posts");
+ }
+ } else {
+ panic!("The posts context was not the right type.");
+ }
+ }
+
+ #[test]
+ fn test_converts_post_to_context() {
+ let post = Post {
+ metadata: Metadata {
+ id: "cool".to_string(),
+ created_on: 1736035200000,
+ },
+ index: 28,
+ html: "<p>beep boop</p>".to_string(),
+ raw: "\
+# Hello everybody
+beep boop"
+ .to_string(),
+ };
+
+ let context = post.to_template_value();
+
+ assert_eq!(context["id"], Value::String("cool".to_string()));
+ assert_eq!(context["created_on"], Value::Unsigned(1736035200000));
+ assert_eq!(
+ context["created_on_utc"],
+ Value::String("Sun, 05 Jan 2025 00:00:00 +0000".to_string())
+ );
+ assert_eq!(
+ context["title"],
+ Value::String("Hello everybody".to_string())
+ );
+ assert_eq!(context["index"], Value::Unsigned(28));
+ assert_eq!(
+ context["html"],
+ Value::String("<p>beep boop</p>".to_string())
+ );
+ assert_eq!(
+ context["raw"],
+ Value::String(
+ "\
+# Hello everybody
+beep boop"
+ .to_string()
+ )
+ );
+ }
+}
diff --git a/src/template.rs b/src/template.rs
index 57a2571..33bc662 100644
--- a/src/template.rs
+++ b/src/template.rs
@@ -57,7 +57,7 @@ impl std::fmt::Display for Token {
}
}
-#[derive(Clone)]
+#[derive(PartialEq, Debug, Clone)]
pub enum Value {
String(String),
Unsigned(u64),