diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 00000000..929890df --- /dev/null +++ b/domain-model.md @@ -0,0 +1,15 @@ +| Classes | Methods | Scenario | Output | +|---------|-------------------|----------------------------------------------|------------------------| +|TodoList | AddTask | I want to add tasks to my todo list | task added | +| | ShowTasks | I want to see all the tasks in my todo list | List | +| | ToggleStatus | I want to change the status of a task | String output | +| | GetComplet | I want to be able to get only the complete | +| | GetIncomplet | I want to be able to get only the inccomplete| +| | FindTask | I want to search for a task and receive a | +| | | message that says it wasn't found if | +| | | it doesn't exist. | +| | RemoveTask | I want to remove tasks from my list. | +| | TaskAscending | I want to see all the tasks in my list | +| | | ordered alphabetically in ascending order | +| | TaskDescending | I want to see all the tasks in my list | +| | | ordered alphabetically in descending order. | diff --git a/tdd-todo-list.CSharp.Main/Extension.cs b/tdd-todo-list.CSharp.Main/Extension.cs index e4c08912..f3738ebc 100644 --- a/tdd-todo-list.CSharp.Main/Extension.cs +++ b/tdd-todo-list.CSharp.Main/Extension.cs @@ -8,5 +8,19 @@ namespace tdd_todo_list.CSharp.Main { public class TodoListExtension { + private List tasks = new List(); + private int nextId = 1; + + public void AddTask(string name) + { + tasks.Add(new Task(nextId++, name)); + } + + public string FindTaskByID(int id) + { + var task = tasks.Find(x => x.Id == id); + + return task == null ? $"no task with id: {id}" : $"{task.Name} - {task.IsComplete}"; + } } } diff --git a/tdd-todo-list.CSharp.Main/Program.cs b/tdd-todo-list.CSharp.Main/Program.cs index 3751555c..b2694c5c 100644 --- a/tdd-todo-list.CSharp.Main/Program.cs +++ b/tdd-todo-list.CSharp.Main/Program.cs @@ -1,2 +1,23 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using NUnit.Framework.Interfaces; +using System; +using tdd_todo_list.CSharp.Main; + + +TodoList toDoList = new TodoList(); + +TodoListExtension extension = new TodoListExtension(); + +toDoList.AddTask("Buy food"); +toDoList.AddTask("exercise"); +toDoList.AddTask("auy food"); +toDoList.AddTask("cxercise"); + +extension.AddTask("check"); + +var allTasks = toDoList.ShowTasks(); +Console.WriteLine(string.Join("\n", allTasks)); + +var idTasks = extension.FindTaskByID(1); +Console.WriteLine(string.Join("\n", idTasks)); + + diff --git a/tdd-todo-list.CSharp.Main/ToDoList.cs b/tdd-todo-list.CSharp.Main/ToDoList.cs index 835cb600..3dc9dc05 100644 --- a/tdd-todo-list.CSharp.Main/ToDoList.cs +++ b/tdd-todo-list.CSharp.Main/ToDoList.cs @@ -6,7 +6,128 @@ namespace tdd_todo_list.CSharp.Main { + public class Task + { + public int Id { get; set; } + public string Name { get; set; } + public bool IsComplete { get; set; } + + public Task(int id, string name) + { + Id = id; + Name = name; + IsComplete = false; + } + + public void ToggleStatus() + { + IsComplete = !IsComplete; + } + } public class TodoList { + private List tasks = new List(); + private int nextId = 1; + + public void AddTask(string name) + { + tasks.Add(new Task(nextId++, name)); + } + + public List ShowTasks() + { + if (tasks.Count == 0) + { + return new List { "no tasks" }; + } + + return tasks.Select(task => $"{task.Id}. {task.Name} - {task.IsComplete}").ToList(); + } + + public string FindTask(string name) + { + var task = tasks.FirstOrDefault(task => task.Name == name); + + return task == null ? $"no task named: {name}" : $"{task.Name} - {task.IsComplete}"; + } + + public string RemoveTask(string name) + { + var taskToRemove = tasks.FirstOrDefault(task => task.Name == name); + + if (taskToRemove != null) + { + tasks.Remove(taskToRemove); + return $"removed {name}"; + } + else + { + return $"could not find: '{name}'"; + } + } + + public bool ToggleStatus(string name) + { + var taskToChange = tasks.FirstOrDefault(task => task.Name == name); + + if (taskToChange != null) + { + taskToChange.ToggleStatus(); + return true; + } + return false; + } + + public List ShowComplete() + { + if (tasks.Count == 0) + { + return new List { "no tasks" }; + } + + return tasks + .Where(task => task.IsComplete) + .Select(task => $"{task.Id}. {task.Name} - {task.IsComplete}") + .ToList(); + } + + public List ShowInComplete() + { + if (tasks.Count == 0) + { + return new List { "no tasks" }; + } + + return tasks + .Where(task => !task.IsComplete) + .Select(task => $"{task.Id}. {task.Name} - {task.IsComplete}") + .ToList(); + } + + public List ShowAscending() + { + if (tasks.Count == 0) + { + return new List { "no tasks" }; + } + + return tasks + .OrderBy(t => t.Name) + .Select(task => $"{task.Name}, {task.IsComplete}") + .ToList(); + } + + public List ShowDescending() + { + if (tasks.Count == 0) + { + return new List { "no tasks" }; + } + + return tasks + .OrderByDescending(t => t.Name) + .Select(task => $"{task.Name}, {task.IsComplete}") + .ToList(); + } } } diff --git a/tdd-todo-list.CSharp.Test/CoreTests.cs b/tdd-todo-list.CSharp.Test/CoreTests.cs index 084cce19..1216b315 100644 --- a/tdd-todo-list.CSharp.Test/CoreTests.cs +++ b/tdd-todo-list.CSharp.Test/CoreTests.cs @@ -6,12 +6,121 @@ namespace tdd_todo_list.CSharp.Test [TestFixture] public class CoreTests { - [Test] public void FirstTest() { TodoList core = new TodoList(); Assert.Pass(); } + + [Test] + public void AddTask() + { + TodoList toDoList = new TodoList(); + + toDoList.AddTask("Buy groceries"); + var tasks = toDoList.ShowTasks(); + + var expected = new List { "1. Buy groceries - False" }; + CollectionAssert.AreEqual(expected, tasks); + } + + [Test] + public void FindTask() + { + TodoList toDoList = new TodoList(); + toDoList.AddTask("Buy groceries"); + toDoList.AddTask("Call"); + + var taskFound = toDoList.FindTask("Buy groceries"); + var taskNotFound = toDoList.FindTask("Non-existent task"); + + Assert.AreEqual("Buy groceries - False", taskFound); + Assert.AreEqual("no task named: Non-existent task", taskNotFound); + } + + [Test] + public void RemoveTask() + { + TodoList toDoList = new TodoList(); + toDoList.AddTask("Buy groceries"); + + var removeSuccess = toDoList.RemoveTask("Buy groceries"); + var removeFailure = toDoList.RemoveTask("tasks"); + var tasksAfterRemoval = toDoList.ShowTasks(); + + Assert.AreEqual("removed Buy groceries", removeSuccess); + Assert.AreEqual("could not find: 'tasks'", removeFailure); + CollectionAssert.AreEqual(new List { "no tasks" }, tasksAfterRemoval); + } + + [Test] + public void ToggleStatus() + { + TodoList toDoList = new TodoList(); + toDoList.AddTask("Buy groceries"); + + var toggleSuccess = toDoList.ToggleStatus("Buy groceries"); + var taskStatusAfterToggle = toDoList.FindTask("Buy groceries"); + var toggleFailure = toDoList.ToggleStatus("Non-existent task"); + + Assert.IsTrue(toggleSuccess); + Assert.AreEqual("Buy groceries - True", taskStatusAfterToggle); + Assert.IsFalse(toggleFailure); + } + + [Test] + public void ShowComplete() + { + TodoList toDoList = new TodoList(); + toDoList.AddTask("Buy groceries"); + toDoList.AddTask("run"); + toDoList.ToggleStatus("run"); + + var completeTasks = toDoList.ShowComplete(); + + var expected = new List { "2. run - True" }; + CollectionAssert.AreEqual(expected, completeTasks); + } + + [Test] + public void ShowInComplete() + { + TodoList toDoList = new TodoList(); + toDoList.AddTask("Buy groceries"); + toDoList.AddTask("run"); + toDoList.ToggleStatus("run"); + + var incompleteTasks = toDoList.ShowInComplete(); + + var expected = new List { "1. Buy groceries - False" }; + CollectionAssert.AreEqual(expected, incompleteTasks); + } + + [Test] + public void ShowAscending() + { + TodoList toDoList = new TodoList(); + toDoList.AddTask("Buy groceries"); + toDoList.AddTask("run"); + + var ascendingTasks = toDoList.ShowAscending(); + + var expected = new List { "Buy groceries, False", "run, False" }; + CollectionAssert.AreEqual(expected, ascendingTasks); + } + + [Test] + public void ShowDescending() + { + TodoList toDoList = new TodoList(); + toDoList.AddTask("Buy groceries"); + toDoList.AddTask("run"); + + var descendingTasks = toDoList.ShowDescending(); + + var expected = new List { "run, False", "Buy groceries, False" }; + CollectionAssert.AreEqual(expected, descendingTasks); + } } -} \ No newline at end of file +} diff --git a/tdd-todo-list.CSharp.Test/ExtensionTests.cs b/tdd-todo-list.CSharp.Test/ExtensionTests.cs index bdc82ad7..eb1530f0 100644 --- a/tdd-todo-list.CSharp.Test/ExtensionTests.cs +++ b/tdd-todo-list.CSharp.Test/ExtensionTests.cs @@ -4,9 +4,11 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using NUnit.Framework; namespace tdd_todo_list.CSharp.Test { + [TestFixture] public class ExtensionTests { private TodoListExtension _extension; @@ -14,5 +16,31 @@ public ExtensionTests() { _extension = new TodoListExtension(); } + [Test] + public void FindByID() + { + TodoListExtension toDoList = new TodoListExtension(); + toDoList.AddTask("Buy groceries"); + + var taskFound = toDoList.FindTaskByID(1); + var taskNotFound = toDoList.FindTaskByID(100); + + Assert.AreEqual("Buy groceries - False", taskFound); + Assert.AreEqual("no task with id: 100", taskNotFound); + } + [Test] + public void UpdateName() + { + TodoListExtension toDoList = new TodoListExtension(); + toDoList.AddTask("Buy groceries"); + + var taskupdated = toDoList.UpdateName(); + var taskNotFound = toDoList.FindTaskByID(100); + + Assert.AreEqual("Buy update - False", taskupdated); + Assert.AreEqual("no task with id: 100", taskNotFound); + } + + } } 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