1use serde::{Deserialize, Serialize};
2use schemars::JsonSchema;
3
4#[derive(Debug, Deserialize, JsonSchema, Serialize)]
5pub struct Commit {
6 pub title: String,
8 pub description: String,
10}
11
12impl ToString for Commit {
13 fn to_string(&self) -> String {
14 format!("{}\n\n{}", self.title, self.description)
15 }
16}
17
18impl Commit {
19 pub fn new(title: String, description: String) -> Self {
20 Self { title, description }
21 }
22}
23
24#[cfg(test)]
25mod tests {
26 use super::*;
27
28 #[test]
29 fn test_commit_creation() {
30 let commit = Commit::new(
31 "Add noob-friendly features".to_string(),
32 "Added self-deprecating humor and alias setup functionality for developers who are great at coding but terrible at git.".to_string(),
33 );
34
35 assert_eq!(commit.title, "Add noob-friendly features");
36 assert!(commit.description.contains("self-deprecating"));
37 }
38
39 #[test]
40 fn test_commit_to_string() {
41 let commit = Commit::new(
42 "Fix stuff".to_string(),
43 "idk it works now".to_string(),
44 );
45
46 let result = commit.to_string();
47 assert_eq!(result, "Fix stuff\n\nidk it works now");
48 }
49
50 #[test]
51 fn test_commit_with_empty_description() {
52 let commit = Commit::new(
53 "Update README".to_string(),
54 "".to_string(),
55 );
56
57 let result = commit.to_string();
58 assert_eq!(result, "Update README\n\n");
59 }
60
61 #[test]
62 fn test_commit_with_multiline_description() {
63 let commit = Commit::new(
64 "Refactor code".to_string(),
65 "Line 1\nLine 2\nLine 3".to_string(),
66 );
67
68 let result = commit.to_string();
69 assert_eq!(result, "Refactor code\n\nLine 1\nLine 2\nLine 3");
70 }
71
72 #[test]
73 fn test_commit_serialization() {
74 let commit = Commit::new(
75 "Test commit".to_string(),
76 "This is a test".to_string(),
77 );
78
79 let json = serde_json::to_string(&commit).unwrap();
80 assert!(json.contains("Test commit"));
81 assert!(json.contains("This is a test"));
82 }
83
84 #[test]
85 fn test_commit_deserialization() {
86 let json = r#"{"title":"Test commit","description":"This is a test"}"#;
87 let commit: Commit = serde_json::from_str(json).unwrap();
88
89 assert_eq!(commit.title, "Test commit");
90 assert_eq!(commit.description, "This is a test");
91 }
92}