-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alexandre Hanot <>
- Loading branch information
Showing
7 changed files
with
186 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use todolist::*; | ||
|
||
#[test] | ||
fn test_add_task() { | ||
let mut todo_list = TodoList::default(); | ||
let cmd = Command::AddTask { | ||
description: "Test task".to_string(), | ||
}; | ||
let events = todo_list.handle(cmd).unwrap(); | ||
todo_list.apply(events); | ||
|
||
assert_eq!(todo_list.tasks.len(), 1); | ||
assert_eq!(todo_list.tasks[0].description, "Test task"); | ||
assert_eq!(todo_list.tasks[0].done, false); | ||
} | ||
|
||
#[test] | ||
fn test_complete_task() { | ||
let mut todo_list = TodoList::default(); | ||
let cmd_add = Command::AddTask { | ||
description: "Test task".to_string(), | ||
}; | ||
let events_add = todo_list.handle(cmd_add).unwrap(); | ||
todo_list.apply(events_add); | ||
|
||
let cmd_complete = Command::CompleteTask { index: 0 }; | ||
let events_complete = todo_list.handle(cmd_complete).unwrap(); | ||
todo_list.apply(events_complete); | ||
|
||
assert_eq!(todo_list.tasks.len(), 1); | ||
assert_eq!(todo_list.tasks[0].done, true); | ||
} | ||
|
||
#[test] | ||
fn test_handle() { | ||
let todo_list = TodoList::default(); | ||
|
||
// Testing AddTask command | ||
match todo_list.handle(Command::AddTask { | ||
description: "Test task".to_string(), | ||
}) { | ||
Ok(events) => { | ||
assert_eq!(events.len(), 1); | ||
if let Event::TaskAdded { description } = &events[0] { | ||
assert_eq!(description, "Test task"); | ||
} else { | ||
panic!("Expected TaskAdded event"); | ||
} | ||
} | ||
Err(_) => panic!("Expected Ok, got Err"), | ||
} | ||
|
||
// Testing CompleteTask command | ||
match todo_list.handle(Command::CompleteTask { index: 0 }) { | ||
Ok(events) => { | ||
assert_eq!(events.len(), 1); | ||
if let Event::TaskCompleted { index } = &events[0] { | ||
assert_eq!(*index, 0); | ||
} else { | ||
panic!("Expected TaskCompleted event"); | ||
} | ||
} | ||
Err(_) => panic!("Expected Ok, got Err"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_apply() { | ||
let mut todo_list = TodoList::default(); | ||
|
||
let events = vec![ | ||
Event::TaskAdded { | ||
description: "Task 1".to_string(), | ||
}, | ||
Event::TaskAdded { | ||
description: "Task 2".to_string(), | ||
}, | ||
Event::TaskCompleted { index: 0 }, | ||
]; | ||
todo_list.apply(events); | ||
|
||
assert_eq!(todo_list.tasks.len(), 2); | ||
assert_eq!(todo_list.tasks[0].description, "Task 1"); | ||
assert_eq!(todo_list.tasks[0].done, true); | ||
assert_eq!(todo_list.tasks[1].description, "Task 2"); | ||
assert_eq!(todo_list.tasks[1].done, false); | ||
} | ||
|
||
#[test] | ||
fn test_debug_error() { | ||
let error = Error::TaskNotFound; | ||
|
||
assert_eq!(format!("{:?}", error), "TaskNotFound"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
import fileinput | ||
import re | ||
|
||
a = re.compile(r"^#\[derive\((\s*[a-zA-Z0-9_]+\s*,)*(\s*[a-zA-Z0-9_]+\s*)\)]$") | ||
|
||
special = [ | ||
"Copy", | ||
"Clone", | ||
"Default", | ||
"Debug", | ||
"PartialEq", | ||
"Eq", | ||
"PartialOrd", | ||
"Ord", | ||
"Serialize", | ||
"Deserialize", | ||
] | ||
|
||
special = {x: i for i, x in enumerate(special)} | ||
|
||
for root, dirs, files in os.walk("."): | ||
if "target" in root: | ||
continue | ||
for name in files: | ||
path = root + os.sep + name | ||
|
||
if name.endswith(".rs"): | ||
print(path) | ||
for line in fileinput.input(path, inplace=True): | ||
if a.match(line): | ||
derives = line[9:-3] | ||
derives = [ | ||
(special.get(x.strip(), 100), x.strip()) | ||
for x in derives.split(",") | ||
] | ||
derives.sort() | ||
line = "#[derive(" + ", ".join(x[1] for x in derives) + ")]" | ||
print(line) | ||
else: | ||
print(line, end="") |