From 3d3b04709795fe236519941c92d839ca07a34b1f Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Sat, 17 Jun 2023 21:14:17 +0200 Subject: [PATCH] feat: Lint and format --- analysis_options.yaml | 9 +--- flake.lock | 18 +++---- flake.nix | 2 +- lib/awaitabledatasender.dart | 74 +++++++++++++------------- pubspec.yaml | 4 +- test/awaitabledatasender_test.dart | 83 ++++++++++++++---------------- test/math_test.dart | 18 +++---- 7 files changed, 100 insertions(+), 108 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 6b2506f..02e975f 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -5,11 +5,4 @@ linter: lines_longer_than_80_chars: false use_setters_to_change_properties: false avoid_positional_boolean_parameters: false - avoid_bool_literals_in_conditional_expressions: false - -analyzer: - exclude: - - "**/*.g.dart" - - "**/*.freezed.dart" - - "test/" - - "integration_test/" + avoid_bool_literals_in_conditional_expressions: false \ No newline at end of file diff --git a/flake.lock b/flake.lock index 842552d..1ecb793 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "flake-utils": { "locked": { - "lastModified": 1667395993, - "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "lastModified": 1656065134, + "narHash": "sha256-oc6E6ByIw3oJaIyc67maaFcnjYOz1mMcOtHxbEf9NwQ=", "owner": "numtide", "repo": "flake-utils", - "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "rev": "bee6a7250dd1b01844a2de7e02e4df7d8a0a206c", "type": "github" }, "original": { @@ -17,16 +17,16 @@ }, "nixpkgs": { "locked": { - "lastModified": 1667610399, - "narHash": "sha256-XZd0f4ZWAY0QOoUSdiNWj/eFiKb4B9CJPtl9uO9SYY4=", - "owner": "NixOS", + "lastModified": 1657540956, + "narHash": "sha256-ihGbOFWtAkENwxBE5kV/yWt2MncvW+BObLDsmxCLo/Q=", + "owner": "NANASHI0X74", "repo": "nixpkgs", - "rev": "1dd8696f96db47156e1424a49578fe7dd4ce99a4", + "rev": "043de04db8a6b0391b3fefaaade160514d866946", "type": "github" }, "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", + "owner": "NANASHI0X74", + "ref": "flutter-3-0-0", "repo": "nixpkgs", "type": "github" } diff --git a/flake.nix b/flake.nix index 47cee57..5a958be 100644 --- a/flake.nix +++ b/flake.nix @@ -1,7 +1,7 @@ { description = "moxlib"; inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + nixpkgs.url = "github:NANASHI0X74/nixpkgs/flutter-3-0-0"; flake-utils.url = "github:numtide/flake-utils"; }; diff --git a/lib/awaitabledatasender.dart b/lib/awaitabledatasender.dart index 256d32e..5ee8b53 100644 --- a/lib/awaitabledatasender.dart +++ b/lib/awaitabledatasender.dart @@ -13,7 +13,7 @@ class JsonImplementation { factory JsonImplementation.fromJson(Map json) { return JsonImplementation(); } - + Map toJson() => {}; } @@ -29,17 +29,17 @@ class DataWrapper { /// The actual data. final T data; - - Map toJson() => { - 'id': id, - 'data': data.toJson() - }; - static DataWrapper fromJson(Map json) => DataWrapper( - json['id']! as String, - json['data']! as T, - ); - + Map toJson() => {'id': id, 'data': data.toJson()}; + + static DataWrapper fromJson( + Map json, + ) => + DataWrapper( + json['id']! as String, + json['data']! as T, + ); + DataWrapper reply(T newData) => DataWrapper(id, newData); } @@ -49,16 +49,14 @@ class DataWrapper { /// /// awaiting [sendData] will return a [Future] that will resolve to the reresponse when /// received via [onData]. -abstract class AwaitableDataSender< - S extends JsonImplementation, - R extends JsonImplementation -> { +abstract class AwaitableDataSender { @mustCallSuper AwaitableDataSender(); /// A mapping of ID to completer for pending requests. final Map> _awaitables = {}; - + /// Critical section for accessing [AwaitableDataSender._awaitables]. final Lock _lock = Lock(); @@ -67,7 +65,7 @@ abstract class AwaitableDataSender< /// A logger. final Logger _log = Logger('AwaitableDataSender'); - + @visibleForTesting Map> getAwaitables() => _awaitables; @@ -78,34 +76,38 @@ abstract class AwaitableDataSender< /// NOTE: Must be overwritten by the actual implementation @visibleForOverriding Future sendDataImpl(DataWrapper data); - + /// Sends [data] using [sendDataImpl]. If [awaitable] is true, then a /// Future will be returned that can be used to await a response. If it /// is false, then null will be imediately resolved. - Future sendData(S data, { bool awaitable = true, @visibleForTesting String? id }) async { + Future sendData( + S data, { + bool awaitable = true, + @visibleForTesting String? id, + }) async { // ignore: no_leading_underscores_for_local_identifiers final _id = id ?? _uuid.v4(); var future = Future.value(); _log.fine('sendData: Waiting to acquire lock...'); await _lock.synchronized(() async { - _log.fine('sendData: Done'); - if (awaitable) { - _awaitables[_id] = Completer(); - onAdd(); - } - - await sendDataImpl( - DataWrapper( - _id, - data, - ), - ); + _log.fine('sendData: Done'); + if (awaitable) { + _awaitables[_id] = Completer(); + onAdd(); + } - if (awaitable) { - future = _awaitables[_id]!.future; - } + await sendDataImpl( + DataWrapper( + _id, + data, + ), + ); - _log.fine('sendData: Releasing lock...'); + if (awaitable) { + future = _awaitables[_id]!.future; + } + + _log.fine('sendData: Releasing lock...'); }); return future; @@ -128,7 +130,7 @@ abstract class AwaitableDataSender< }); completer?.complete(data.data); - + return completer != null; } } diff --git a/pubspec.yaml b/pubspec.yaml index 5944a58..295685c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,11 +1,11 @@ name: moxlib -description: A collection of code for sharing between various moxxy libraries. Not inteded for outside use. +description: A collection of code for sharing between various moxxy libraries. Not intended for outside use. version: 0.1.5 homepage: https://codeberg.org/moxxy/moxlib publish_to: https://git.polynom.me/api/packages/Moxxy/pub environment: - sdk: ">=2.17.0-266.1.beta <3.0.0" + sdk: ">=2.17.0 <3.0.0" dependencies: logging: ^1.0.2 diff --git a/test/awaitabledatasender_test.dart b/test/awaitabledatasender_test.dart index a5531fa..c444063 100644 --- a/test/awaitabledatasender_test.dart +++ b/test/awaitabledatasender_test.dart @@ -1,29 +1,24 @@ -import "package:moxlib/awaitabledatasender.dart"; +import 'package:moxlib/awaitabledatasender.dart'; -import "package:test/test.dart"; +import 'package:test/test.dart'; class TestDataType implements JsonImplementation { - final String data; - TestDataType(this.data); - @override - Map toJson() => { - "data": data - }; + factory TestDataType.fromJson(Map json) => + TestDataType(json['data']! as String); - factory TestDataType.fromJson(Map json) => TestDataType( - json["data"]! - ); + final String data; + + @override + Map toJson() => {'data': data}; } -class FakeAwaitableDataSender< - S extends JsonImplementation, - R extends JsonImplementation -> extends AwaitableDataSender { - final void Function()? onAddFunc; +class FakeAwaitableDataSender extends AwaitableDataSender { + FakeAwaitableDataSender({this.onAddFunc}) : super(); - FakeAwaitableDataSender({ this.onAddFunc }) : super(); + final void Function()? onAddFunc; @override Future sendDataImpl(DataWrapper data) async {} @@ -35,39 +30,41 @@ class FakeAwaitableDataSender< } void main() { - test("Sending an event without awaiting it", () async { - final handler = FakeAwaitableDataSender(); - final result = await handler.sendData(TestDataType("hallo"), awaitable: false); + test('Sending an event without awaiting it', () async { + final handler = FakeAwaitableDataSender(); + final result = + await handler.sendData(TestDataType('hallo'), awaitable: false); - expect(result, null); - expect(handler.getAwaitables().length, 0); + expect(result, null); + expect(handler.getAwaitables().length, 0); }); - test("Sending an event without awaiting it", () async { - final handler = FakeAwaitableDataSender(); - const id = "abc123"; - final result = handler.sendData(TestDataType("hallo"), awaitable: true, id: id); - await handler.onData(DataWrapper(id, TestDataType("welt"))); + test('Sending an event without awaiting it', () async { + final handler = FakeAwaitableDataSender(); + const id = 'abc123'; + final result = + handler.sendData(TestDataType('hallo'), awaitable: true, id: id); + await handler.onData(DataWrapper(id, TestDataType('welt'))); - expect((await result)!.data, "welt"); - expect(handler.getAwaitables().length, 0); + expect((await result)!.data, 'welt'); + expect(handler.getAwaitables().length, 0); }); - test("Queue multiple data packets and resolve in reverse order", () async { - int i = 0; - final handler = FakeAwaitableDataSender( - onAddFunc: () { - i++; - expect(i <= 2, true); - } - ); - final a = handler.sendData(TestDataType("1"), id: "1"); - final b = handler.sendData(TestDataType("2"), id: "2"); + test('Queue multiple data packets and resolve in reverse order', () async { + var i = 0; + final handler = FakeAwaitableDataSender( + onAddFunc: () { + i++; + expect(i <= 2, true); + }, + ); + final a = handler.sendData(TestDataType('1'), id: '1'); + final b = handler.sendData(TestDataType('2'), id: '2'); - await handler.onData(DataWrapper("2", TestDataType("4"))); - await handler.onData(DataWrapper("1", TestDataType("1"))); + await handler.onData(DataWrapper('2', TestDataType('4'))); + await handler.onData(DataWrapper('1', TestDataType('1'))); - expect((await a)!.data, "1"); - expect((await b)!.data, "4"); + expect((await a)!.data, '1'); + expect((await b)!.data, '4'); }); } diff --git a/test/math_test.dart b/test/math_test.dart index 0e97b81..40c5fb8 100644 --- a/test/math_test.dart +++ b/test/math_test.dart @@ -1,14 +1,14 @@ -import "package:moxlib/math.dart"; +import 'package:moxlib/math.dart'; -import "package:test/test.dart"; +import 'package:test/test.dart'; void main() { - group("implies", () { - test("Truth table test", () { - expect(implies(true, true), true); - expect(implies(true, false), false); - expect(implies(false, true), true); - expect(implies(false, false), true); - }); + group('implies', () { + test('Truth table test', () { + expect(implies(true, true), true); + expect(implies(true, false), false); + expect(implies(false, true), true); + expect(implies(false, false), true); + }); }); }