Create Task class + tests

This commit is contained in:
hunteraraujo
2023-08-20 20:26:04 +02:00
parent d65e7ab830
commit cf4aa4fe9c
2 changed files with 98 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
/// Represents a task or topic the user wants to discuss with the agent.
class Task {
final int id;
String _title;
Task({required this.id, required String title})
: assert(title.isNotEmpty, 'Title cannot be empty'),
_title = title;
String get title => _title;
set title(String newTitle) {
if (newTitle.isNotEmpty) {
_title = newTitle;
} else {
throw ArgumentError('Title cannot be empty.');
}
}
// Convert a Map (usually from JSON) to a Task object
factory Task.fromMap(Map<String, dynamic> map) {
return Task(
id: map['id'],
title: map['title'],
);
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Task &&
runtimeType == other.runtimeType &&
id == other.id &&
title == other.title;
@override
int get hashCode => id.hashCode ^ title.hashCode;
@override
String toString() => 'Task(id: $id, title: $title)';
}

57
test/task_test.dart Normal file
View File

@@ -0,0 +1,57 @@
import 'package:auto_gpt_flutter_client/models/task.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('Task', () {
// Test the properties of the Task class
test('Task properties', () {
final task = Task(id: 1, title: 'Test Task');
expect(task.id, 1);
expect(task.title, 'Test Task');
});
// Test Task.fromMap method
test('Task.fromMap', () {
final task = Task.fromMap({'id': 1, 'title': 'Test Task'});
expect(task.id, 1);
expect(task.title, 'Test Task');
});
// Test creating a Task with an empty title
test('Task with empty title', () {
expect(() => Task(id: 2, title: ''), throwsA(isA<AssertionError>()));
});
// Test that two Task objects with the same id and title are equal
test('Two tasks with same properties are equal', () {
final task1 = Task(id: 4, title: 'Same Task');
final task2 = Task(id: 4, title: 'Same Task');
expect(task1, task2);
});
// Test that toString() returns a string representation of the Task
test('toString returns string representation', () {
final task = Task(id: 5, title: 'Test toString');
expect(task.toString(), 'Task(id: 5, title: Test toString)');
});
// Test that title of Task can be modified
test('Modify task title', () {
final task = Task(id: 6, title: 'Initial Title');
task.title = 'Modified Title';
expect(task.title, 'Modified Title');
});
// Test that setting an empty title throws an error
test('Set empty title', () {
final task = Task(id: 7, title: 'Valid Title');
expect(() => task.title = '', throwsA(isA<ArgumentError>()));
});
});
}