eaeabb8c40
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
139 lines
3.6 KiB
Dart
139 lines
3.6 KiB
Dart
import 'package:list_or/list_or.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
void main() {
|
|
group('ListOr Initialization', () {
|
|
test('creates from a single value', () {
|
|
final list = ListOr<String>('hello');
|
|
|
|
expect(list.length, equals(1));
|
|
expect(list.first, equals('hello'));
|
|
expect(list, equals(['hello']));
|
|
});
|
|
|
|
test('creates from a List', () {
|
|
final list = ListOr<int>([1, 2, 3]);
|
|
|
|
expect(list.length, equals(3));
|
|
expect(list, equals([1, 2, 3]));
|
|
});
|
|
|
|
test('creates from a Set (Iterable)', () {
|
|
final list = ListOr<int>({4, 5, 6});
|
|
|
|
expect(list.length, equals(3));
|
|
expect(list, equals([4, 5, 6]));
|
|
});
|
|
|
|
test('handles null correctly when T is nullable', () {
|
|
final list = ListOr<int?>(null);
|
|
|
|
expect(list.length, equals(1));
|
|
expect(list.first, isNull);
|
|
expect(list, equals([null]));
|
|
});
|
|
|
|
test('throws ArgumentError when null is passed but T is non-nullable', () {
|
|
expect(
|
|
() => ListOr<int>(null),
|
|
throwsA(
|
|
isA<ArgumentError>().having(
|
|
(e) => e.message,
|
|
'message',
|
|
contains('does not accept null values'),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
test('throws ArgumentError on incompatible types', () {
|
|
expect(
|
|
() => ListOr<String>(123),
|
|
throwsA(
|
|
isA<ArgumentError>().having(
|
|
(e) => e.message,
|
|
'message',
|
|
contains('Value must be of type'),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
});
|
|
|
|
group('ListOr Mutations (Bug Fix Verification)', () {
|
|
test('can add to a single-initialized list without throwing', () {
|
|
final list = ListOr<String>('Apple');
|
|
list.add('Banana'); // This would throw prior to the explicit override fix
|
|
|
|
expect(list, equals(['Apple', 'Banana']));
|
|
});
|
|
|
|
test('can addAll from another iterable', () {
|
|
final list = ListOr<int>(1);
|
|
list.addAll([2, 3, 4]);
|
|
|
|
expect(list, equals([1, 2, 3, 4]));
|
|
});
|
|
|
|
test('can insert at a specific index', () {
|
|
final list = ListOr<String>(['A', 'C']);
|
|
list.insert(1, 'B');
|
|
|
|
expect(list, equals(['A', 'B', 'C']));
|
|
});
|
|
|
|
test('can update elements using index operator', () {
|
|
final list = ListOr<int>([10, 20, 30]);
|
|
list[1] = 99;
|
|
|
|
expect(list, equals([10, 99, 30]));
|
|
});
|
|
|
|
test('can remove a specific element', () {
|
|
final list = ListOr<String>(['A', 'B', 'C']);
|
|
final result = list.remove('B');
|
|
|
|
expect(result, isTrue);
|
|
expect(list, equals(['A', 'C']));
|
|
});
|
|
|
|
test('can remove at a specific index', () {
|
|
final list = ListOr<int>([1, 2, 3]);
|
|
final removed = list.removeAt(0);
|
|
|
|
expect(removed, equals(1));
|
|
expect(list, equals([2, 3]));
|
|
});
|
|
|
|
test('can clear the list entirely', () {
|
|
final list = ListOr<String>(['A', 'B']);
|
|
list.clear();
|
|
|
|
expect(list, isEmpty);
|
|
expect(list.length, equals(0));
|
|
});
|
|
});
|
|
|
|
group('ListOr toString formatting', () {
|
|
test('formats a single element as just the element string', () {
|
|
final list = ListOr<String>('hello');
|
|
expect(list.toString(), equals('hello'));
|
|
});
|
|
|
|
test('formats multiple elements with standard list brackets', () {
|
|
final list = ListOr<int>([1, 2, 3]);
|
|
expect(list.toString(), equals('[1, 2, 3]'));
|
|
});
|
|
|
|
test('formats empty list with standard empty brackets', () {
|
|
final list = ListOr<String>(<String>[]);
|
|
expect(list.toString(), equals('[]'));
|
|
});
|
|
|
|
test('formats single null element correctly', () {
|
|
final list = ListOr<String?>(null);
|
|
expect(list.toString(), equals('null'));
|
|
});
|
|
});
|
|
}
|