|
| 1 | +import 'dart:convert'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:opso/modals/osoc_modal.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + group('OsocModal Tests', () { |
| 7 | + const String testName = 'OpSo'; |
| 8 | + const String testImageUrl = 'https://example.com/image.png'; |
| 9 | + const String testDescription = 'Open Source Programs App'; |
| 10 | + const String testProjectUrl = 'https://github.com/andoriyaprashant/OpSo'; |
| 11 | + const int testYear = 2026; |
| 12 | + |
| 13 | + test('should construct model correctly with standard constructor', () { |
| 14 | + final model = OsocModal( |
| 15 | + name: testName, |
| 16 | + image_url: testImageUrl, |
| 17 | + description: testDescription, |
| 18 | + project_url: testProjectUrl, |
| 19 | + year: testYear, |
| 20 | + ); |
| 21 | + |
| 22 | + expect(model.name, testName); |
| 23 | + expect(model.image_url, testImageUrl); |
| 24 | + expect(model.description, testDescription); |
| 25 | + expect(model.project_url, testProjectUrl); |
| 26 | + expect(model.year, testYear); |
| 27 | + }); |
| 28 | + |
| 29 | + test('should copyWith modified fields correctly', () { |
| 30 | + final model = OsocModal( |
| 31 | + name: testName, |
| 32 | + image_url: testImageUrl, |
| 33 | + description: testDescription, |
| 34 | + project_url: testProjectUrl, |
| 35 | + year: testYear, |
| 36 | + ); |
| 37 | + |
| 38 | + final copiedModel = model.copyWith( |
| 39 | + name: 'New Name', |
| 40 | + year: 2027, |
| 41 | + ); |
| 42 | + |
| 43 | + expect(copiedModel.name, 'New Name'); |
| 44 | + expect(copiedModel.image_url, testImageUrl); |
| 45 | + expect(copiedModel.description, testDescription); |
| 46 | + expect(copiedModel.project_url, testProjectUrl); |
| 47 | + expect(copiedModel.year, 2027); |
| 48 | + }); |
| 49 | + |
| 50 | + test('should map representation toMap and fromMap correctly', () { |
| 51 | + final model = OsocModal( |
| 52 | + name: testName, |
| 53 | + image_url: testImageUrl, |
| 54 | + description: testDescription, |
| 55 | + project_url: testProjectUrl, |
| 56 | + year: testYear, |
| 57 | + ); |
| 58 | + |
| 59 | + final map = model.toMap(); |
| 60 | + expect(map['name'], testName); |
| 61 | + expect(map['image_url'], testImageUrl); |
| 62 | + expect(map['description'], testDescription); |
| 63 | + expect(map['project_url'], testProjectUrl); |
| 64 | + expect(map['year'], testYear); |
| 65 | + |
| 66 | + final parsedModel = OsocModal.fromMap(map); |
| 67 | + expect(parsedModel.name, testName); |
| 68 | + expect(parsedModel.image_url, testImageUrl); |
| 69 | + expect(parsedModel.description, testDescription); |
| 70 | + expect(parsedModel.project_url, testProjectUrl); |
| 71 | + expect(parsedModel.year, testYear); |
| 72 | + }); |
| 73 | + |
| 74 | + test('should serialize to JSON string and deserialize from JSON string correctly', () { |
| 75 | + final model = OsocModal( |
| 76 | + name: testName, |
| 77 | + image_url: testImageUrl, |
| 78 | + description: testDescription, |
| 79 | + project_url: testProjectUrl, |
| 80 | + year: testYear, |
| 81 | + ); |
| 82 | + |
| 83 | + final jsonString = model.toJson(); |
| 84 | + final decodedMap = json.decode(jsonString); |
| 85 | + |
| 86 | + expect(decodedMap['name'], testName); |
| 87 | + expect(decodedMap['image_url'], testImageUrl); |
| 88 | + expect(decodedMap['description'], testDescription); |
| 89 | + expect(decodedMap['project_url'], testProjectUrl); |
| 90 | + expect(decodedMap['year'], testYear); |
| 91 | + |
| 92 | + final modelFromJson = OsocModal.fromJson(jsonString); |
| 93 | + expect(modelFromJson, model); |
| 94 | + }); |
| 95 | + |
| 96 | + test('should output correct toString representation', () { |
| 97 | + final model = OsocModal( |
| 98 | + name: testName, |
| 99 | + image_url: testImageUrl, |
| 100 | + description: testDescription, |
| 101 | + project_url: testProjectUrl, |
| 102 | + year: testYear, |
| 103 | + ); |
| 104 | + |
| 105 | + final expectedString = |
| 106 | + 'OsocModal(name: $testName, image_url: $testImageUrl, description: $testDescription, project_url: $testProjectUrl, year: $testYear)'; |
| 107 | + expect(model.toString(), expectedString); |
| 108 | + }); |
| 109 | + |
| 110 | + test('should correctly compare objects via operator == and hashCode', () { |
| 111 | + final model1 = OsocModal( |
| 112 | + name: testName, |
| 113 | + image_url: testImageUrl, |
| 114 | + description: testDescription, |
| 115 | + project_url: testProjectUrl, |
| 116 | + year: testYear, |
| 117 | + ); |
| 118 | + |
| 119 | + final model2 = OsocModal( |
| 120 | + name: testName, |
| 121 | + image_url: testImageUrl, |
| 122 | + description: testDescription, |
| 123 | + project_url: testProjectUrl, |
| 124 | + year: testYear, |
| 125 | + ); |
| 126 | + |
| 127 | + final model3 = OsocModal( |
| 128 | + name: 'Different Name', |
| 129 | + image_url: testImageUrl, |
| 130 | + description: testDescription, |
| 131 | + project_url: testProjectUrl, |
| 132 | + year: testYear, |
| 133 | + ); |
| 134 | + |
| 135 | + expect(model1 == model2, isTrue); |
| 136 | + expect(model1 == model3, isFalse); |
| 137 | + expect(model1.hashCode == model2.hashCode, isTrue); |
| 138 | + expect(model1.hashCode == model3.hashCode, isFalse); |
| 139 | + }); |
| 140 | + }); |
| 141 | +} |
0 commit comments