From 2e4342408bceb50d57da4d5e83a3ace59bc1c06a Mon Sep 17 00:00:00 2001 From: Vegard Stigen Date: Sun, 10 Aug 2025 21:45:29 +0200 Subject: [PATCH] completed core with tests --- domain-model.md | 31 ++++ tdd-todo-list.CSharp.Main/ToDoList.cs | 199 +++++++++++++++++++++- tdd-todo-list.CSharp.Test/CoreTests.cs | 227 +++++++++++++++++++++++++ tdd-todo-list.sln | 1 + 4 files changed, 457 insertions(+), 1 deletion(-) create mode 100644 domain-model.md diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 00000000..367e9c2b --- /dev/null +++ b/domain-model.md @@ -0,0 +1,31 @@ +Core requirements. + +| Classes | Methods | Scenario | Output | +|----------|--------------------------------|----------------------------------------------------------|--------| +| ToDoList | Add(string task) | Add task to list | void | +| ToDoList | GetAll | Print all tasks in list | List | +| Task | ChangeStatus(int id) | Changes the status between incomplete and complete | bool | +| ToDoList | GetCompleted | Shows only the completed tasks | List | +| ToDoList | GetIncomplete | Shows only the incomplete tasks | List | +| ToDoList | SearchTask(string task) | Searches for task and gives status on found or not found | bool | +| ToDoList | Remove(string task) | removes task from list | void | +| ToDoList | ShowAlpAscending | see all in alphabetical ascending order | List | +| ToDoList | ShowAlpDecending | see all in alphabetical descending order | List | +| Task | SetPriority(int priority) | sets the priority for the task | bool | +| ToDoList | ShowByPriority | see all sorted by priority, highest to lowest | List | + +Extension requirement: + +| Classes | Methods | Scenario | Output | +|----------|-------------------------------------|------------------------------------------------------------|-----------| +| ToDoList | GetTaskById(int id) | get task by in number | task | +| Task | SetId(int id) | Set id number for task | bool | +| ToDoList | UpdataName(int id, string newName) | Update task name by id and new name | bool | +| ToDoList | ChangeStatus(int id, string status) | change the status of a task by providing id and new status | bool | +| Task | GetTimeCreated(int id) | get the date and time a task was created | Time/Date | +| Task | GetTimeCompleted(int id) | get the date and time a task war completed | Time/Date | +| ToDoList | LongestTime | show which task took the longest to complete | task | +| ToDoList | ShortestTime | show which task took the shortest to complete | task | +| ToDoList | LongerThanFiveToComplete | show which task took linger than 5 days to complete | task | +| ToDoList | CategoriseTasks(string category) | categorise tasks (study, work, admin) | bool | +| ToDoList | ListByCategory | show all tasks sorted by category | List | \ No newline at end of file diff --git a/tdd-todo-list.CSharp.Main/ToDoList.cs b/tdd-todo-list.CSharp.Main/ToDoList.cs index 835cb600..c2fafc54 100644 --- a/tdd-todo-list.CSharp.Main/ToDoList.cs +++ b/tdd-todo-list.CSharp.Main/ToDoList.cs @@ -1,12 +1,209 @@ -using System; +using NUnit.Framework.Internal; +using System; using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace tdd_todo_list.CSharp.Main { + + public enum Priority + { + Low, + Medium, + High + } + + public class TaskItem + { + + private string _taskName; + //flase = incomplete, true = complete + private bool _status = false; + //set to low as a default + private Priority _priority = Priority.Low; + private string _category; + + + + public TaskItem(string taskName) + { + _taskName = taskName; + } + + + + + public string TaskName { get { return _taskName; } set { _taskName = value; } } + + public bool Status { get { return _status; } set { _status = value; } } + + public Priority Priority { get { return _priority; } set { _priority = value; } } + + + + //public Guid Id { get; set; } = Guid.NewGuid(); + //public Category Category { get { return _category; } set { _category = value; } } + + } + + + + + + public class TodoList { + public List _tasks = new List(); + + + public void Add(string taskName) + { + TaskItem taskAdd = new TaskItem(taskName); + _tasks.Add(taskAdd); + } + + + public void RemoveTask(string task) + { + foreach (var item in _tasks) + { + if (item.TaskName == task) + { + _tasks.RemoveAt(_tasks.IndexOf(item)); + break; + } + } + } + + public List GetAll() + { + return _tasks; + } + + public void ChangeStatus(string task, bool status) + { + foreach (var item in _tasks) + { + if (item.TaskName == task) + { + item.Status = status; + break; + } + } + } + + public List GetCompleted() + { + List result = new List(); + foreach (var item in _tasks) + { + if (item.Status) + { + result.Add(item); + } + } + return result; + } + + public List GetIncomplete() + { + List result = new List(); + foreach (var item in _tasks) + { + if (!item.Status) + { + result.Add(item); + } + } + return result; + + } + + + public bool SearchTask(string task) + { + foreach (var item in _tasks) + { + if (item.TaskName == task) + { + return true; + + } + else + { + Console.WriteLine($"{task} is not in the list"); + return false; + } + } + return false; + } + + + public TodoList ShowAlpAscending() + { + TodoList result = new TodoList(); + List taskNames = new List(); + + foreach (var item in _tasks) + { + taskNames.Add(item.TaskName); + } + + taskNames.Sort(); + + foreach (var item in taskNames) + { + result.Add(item); + } + return result; + } + + + public TodoList ShowAlpDecending() + { + TodoList result = new TodoList(); + List taskNames = new List(); + + foreach (var item in _tasks) + { + taskNames.Add(item.TaskName); + } + + taskNames.Sort(); + taskNames.Reverse(); + + foreach (var item in taskNames) + { + result.Add(item); + } + return result; + } + + public bool SetPriority(string name, Priority priority) + { + foreach (var item in _tasks) + { + if (item.TaskName == name) + { + item.Priority = priority; + return true; + } + } + return false; + } + + + public List ShowByPriority() + { + return _tasks.OrderBy(task => task.Priority).ToList(); + } + + + + } } diff --git a/tdd-todo-list.CSharp.Test/CoreTests.cs b/tdd-todo-list.CSharp.Test/CoreTests.cs index 084cce19..a4136e39 100644 --- a/tdd-todo-list.CSharp.Test/CoreTests.cs +++ b/tdd-todo-list.CSharp.Test/CoreTests.cs @@ -1,5 +1,6 @@ using tdd_todo_list.CSharp.Main; using NUnit.Framework; +using System.ComponentModel.Design; namespace tdd_todo_list.CSharp.Test { @@ -7,11 +8,237 @@ namespace tdd_todo_list.CSharp.Test public class CoreTests { + /* [Test] public void FirstTest() { TodoList core = new TodoList(); Assert.Pass(); } + */ + + [Test] + public void AddTask() + { + //arrange + TodoList core = new TodoList(); + + + //act + core.Add("task1"); + + //assert + Assert.IsTrue(1 == core._tasks.Count); + } + + [Test] + public void RemoveTask() + { + //arrange + TodoList core = new TodoList(); + + + //act + core.Add("task1"); + core.Add("task2"); + core.RemoveTask("task1"); + + //assert + Assert.IsTrue(1 == core._tasks.Count); + } + + [Test] + public void GetAll() + { + //arrange + TodoList core = new TodoList(); + core.Add("task1"); + core.Add("task2"); + + + List result = core.GetAll(); + + //act / assert + + for (int i = 0; i < core._tasks.Count; i++) + { + Assert.That(core._tasks[i].TaskName, Is.EqualTo(result[i].TaskName)); + } + + } + + + [Test] + public void ChangeStatus() + { + + + //arrange + TodoList core = new TodoList(); + core.Add("task1"); + core.Add("task2"); + + //act + core.ChangeStatus("task1", true); + + + //assert + Assert.That(core._tasks[0].Status, Is.True); + Assert.That(core._tasks[1].Status, Is.False); + + + } + + + [Test] + public void GetCompleted() + { + //arrange + TodoList core = new TodoList(); + core.Add("task1"); + core.Add("task2"); + + //act + core.ChangeStatus("task1", true); + + List tasks = core.GetCompleted(); + TaskItem result = tasks[0]; + + //assert + Assert.IsTrue(result.TaskName == "task1"); + } + + + + + [Test] + public void GetIncomplete() + { + //arrange + TodoList core = new TodoList(); + core.Add("task1"); + core.Add("task2"); + + //act + core.ChangeStatus("task1", true); + + List tasks = core.GetIncomplete(); + TaskItem result = tasks[0]; + + //assert + Assert.IsTrue(result.TaskName == "task2"); + } + + + [Test] + public void SearchTask() + { + //arrange + TodoList core = new TodoList(); + core.Add("task1"); + core.Add("task2"); + + //act + core.RemoveTask("task1"); + + + //assert + Assert.That(core.SearchTask("task1"), Is.False); + Assert.That(core.SearchTask("task2"), Is.True); + } + + + + [Test] + public void ShowAlpAscending() + { + //arrange + TodoList core = new TodoList(); + core.Add("b"); + core.Add("a"); + core.Add("c"); + + //act + TodoList result = core.ShowAlpAscending(); + + + //assert + Assert.That(result._tasks[0].TaskName, Is.EqualTo("a")); + Assert.That(result._tasks[1].TaskName, Is.EqualTo("b")); + Assert.That(result._tasks[2].TaskName, Is.EqualTo("c")); + + } + + [Test] + public void ShowAlpDecending() + { + //arrange + TodoList core = new TodoList(); + core.Add("b"); + core.Add("a"); + core.Add("c"); + + //act + TodoList result = core.ShowAlpDecending(); + + + //assert + Assert.That(result._tasks[0].TaskName, Is.EqualTo("c")); + Assert.That(result._tasks[1].TaskName, Is.EqualTo("b")); + Assert.That(result._tasks[2].TaskName, Is.EqualTo("a")); + + + } + + [Test] + public void SetPriority() + { + //arrange + TodoList core = new TodoList(); + core.Add("task1"); + core.Add("task2"); + + //act + core.SetPriority("task1", Priority.High); + + + //assert + Assert.That(core._tasks[0].Priority, Is.EqualTo(Priority.High)); + Assert.That(core._tasks[1].Priority, Is.EqualTo(Priority.Low)); + } + + [Test] + public void ShowByPriority() + { + //arrange + TodoList core = new TodoList(); + core.Add("task1"); + core.Add("task2"); + core.Add("task3"); + core.Add("task4"); + + core.SetPriority("task1", Priority.Low); + core.SetPriority("task2", Priority.High); + core.SetPriority("task3", Priority.Medium); + core.SetPriority("task4", Priority.High); + + + //act + List result = core.ShowByPriority(); + + TodoList expected = new TodoList(); + expected.Add("task1"); + expected.Add("task3"); + expected.Add("task2"); + expected.Add("task4"); + + + //assert + for (int i = 0; i < result.Count; i++) + { + Assert.That(result[i].TaskName, Is.EqualTo(expected._tasks[i].TaskName)); + } + } + } } \ No newline at end of file diff --git a/tdd-todo-list.sln b/tdd-todo-list.sln index 66d24763..2a5e0bbd 100644 --- a/tdd-todo-list.sln +++ b/tdd-todo-list.sln @@ -10,6 +10,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{663B0373-6031-46F8-ADD5-9AF01A5E82D5}" ProjectSection(SolutionItems) = preProject .github\workflows\core-criteria.yml = .github\workflows\core-criteria.yml + domain-model.md = domain-model.md .github\workflows\extension-criteria.yml = .github\workflows\extension-criteria.yml README.md = README.md EndProjectSection