Skip to content

Commit

Permalink
further improved implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LowLevelSubmarine committed Mar 26, 2024
1 parent 56e72e3 commit e75d8c0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
9 changes: 0 additions & 9 deletions example/test.dart

This file was deleted.

17 changes: 6 additions & 11 deletions lib/src/ilist/ilist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import "l_add_all.dart";
import "l_flat.dart";

@immutable
class IListEmpty<T> extends IList<T> {
class IListEmpty<T> // ignore: must_be_immutable
extends IList<T> {
/// Creates a empty list.
///
/// IMPORTANT: You must always use the `const` keyword.
/// It's always wrong to use an `IListEmpty()` which is not constant.
@literal
const IListEmpty([this.config = const ConfigList()])
: super._gen();
Expand All @@ -29,7 +34,6 @@ class IListEmpty<T> extends IList<T> {

/// Nothing happens when you flush a empty list, by definition.
@override
// ignore: non_const_call_to_literal_constructor
IListEmpty<T> get flush => this;

@override
Expand Down Expand Up @@ -78,15 +82,6 @@ class IListConst<T> // ignore: must_be_immutable
[this.config = const ConfigList()])
: super._gen();

/// Creates a empty constant list.
///
/// IMPORTANT: You must always use the `const` keyword.
/// It's always wrong to use an `IListConst.empty()` which is not constant.
@literal
const IListConst.empty([this.config = const ConfigList()])
: _list = const [],
super._gen();

final List<T> _list;

@override
Expand Down
49 changes: 49 additions & 0 deletions test/ilist/ilist_empty_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Developed by Marcelo Glasberg (2021) https://glasberg.dev and https://github.com/marcglasberg
// and Philippe Fanaro https://github.com/psygo
// For more info, see: https://pub.dartlang.org/packages/fast_immutable_collections
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:test/test.dart';

void main() {
setUp(() {
ImmutableCollection.resetAllConfigurations();
ImmutableCollection.autoFlush = false;
});


test("Runtime Type", () {
expect(const IListEmpty(), isA<IListEmpty>());
expect(const IListEmpty(), isA<IListEmpty>());
expect(const IListEmpty<String>(), isA<IListEmpty<String>>());
expect(const IListEmpty<int>(), isA<IListEmpty<int>>());

expect(const IListEmpty(), isA<IList>());
expect(const IListEmpty(), isA<IList>());
expect(const IListEmpty<String>(), isA<IList<String>>());
expect(const IListEmpty<int>(), isA<IList<int>>());
});

test("Make sure the IListEmpty can be modified and later iterated", () {
IList<String> list = const IList.empty();
list = list.addAll(["a", "b", "c"]);
list.forEach((_) { });
list = list.add("d");
list.forEach((_) { });
list = list.remove("a");
list.forEach((_) { });
});

test("Make sure the internal list is List<int>, and not List<Never>", () {
const l1 = IListEmpty<int>();
expect(l1.runtimeType.toString(), 'IListEmpty<int>');

const l2 = IListConst<int>([1, 2, 3]);
expect(l2.runtimeType.toString(), 'IListConst<int>');

final l3 = l1.addAll(l2);
expect(l3.runtimeType.toString(), 'IListImpl<int>');

final result = l3.where((int i) => i == 2).toList();
expect(result, [2]);
});
}

0 comments on commit e75d8c0

Please sign in to comment.