From 9cb6346c4d94d8b878330d42af0f9c45e5ce772d Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Sun, 12 Mar 2023 19:19:53 +0100 Subject: [PATCH] fix(style): Format and lint test helpers --- packages/moxxmpp/test/helpers/logging.dart | 4 +- packages/moxxmpp/test/helpers/manager.dart | 11 ++- packages/moxxmpp/test/helpers/xml.dart | 33 ++++--- packages/moxxmpp/test/helpers/xmpp.dart | 101 ++++++++++++--------- 4 files changed, 91 insertions(+), 58 deletions(-) diff --git a/packages/moxxmpp/test/helpers/logging.dart b/packages/moxxmpp/test/helpers/logging.dart index 08c1e8d..8eed154 100644 --- a/packages/moxxmpp/test/helpers/logging.dart +++ b/packages/moxxmpp/test/helpers/logging.dart @@ -5,6 +5,8 @@ void initLogger() { Logger.root.level = Level.ALL; Logger.root.onRecord.listen((record) { // ignore: avoid_print - print('[${record.level.name}] (${record.loggerName}) ${record.time}: ${record.message}'); + print( + '[${record.level.name}] (${record.loggerName}) ${record.time}: ${record.message}', + ); }); } diff --git a/packages/moxxmpp/test/helpers/manager.dart b/packages/moxxmpp/test/helpers/manager.dart index dbb41cb..3466109 100644 --- a/packages/moxxmpp/test/helpers/manager.dart +++ b/packages/moxxmpp/test/helpers/manager.dart @@ -29,7 +29,14 @@ class TestingManagerHolder { allowPlainAuth: true, ); - Future _sendStanza(stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async { + Future _sendStanza( + stanza, { + StanzaFromType addFrom = StanzaFromType.full, + bool addId = true, + bool awaitable = true, + bool encrypted = false, + bool forceEncryption = false, + }) async { return XMLNode.fromString(''); } @@ -60,4 +67,4 @@ class TestingManagerHolder { await manager.postRegisterCallback(); _managers[manager.id] = manager; } -} \ No newline at end of file +} diff --git a/packages/moxxmpp/test/helpers/xml.dart b/packages/moxxmpp/test/helpers/xml.dart index 6749662..c98bc2f 100644 --- a/packages/moxxmpp/test/helpers/xml.dart +++ b/packages/moxxmpp/test/helpers/xml.dart @@ -1,28 +1,37 @@ import 'package:moxxmpp/moxxmpp.dart'; -bool compareXMLNodes(XMLNode actual, XMLNode expectation, { bool ignoreId = true}) { +bool compareXMLNodes( + XMLNode actual, + XMLNode expectation, { + bool ignoreId = true, +}) { // Compare attributes if (expectation.tag != actual.tag) return false; final attributesEqual = expectation.attributes.keys.every((key) { - // Ignore the stanza ID - if (key == 'id' && ignoreId) return true; + // Ignore the stanza ID + if (key == 'id' && ignoreId) return true; - return actual.attributes[key] == expectation.attributes[key]; + return actual.attributes[key] == expectation.attributes[key]; }); if (!attributesEqual) return false; - final actualAttributeLength = !ignoreId ? actual.attributes.length : ( - actual.attributes.containsKey('id') ? actual.attributes.length - 1 : actual.attributes.length - ); - final expectedAttributeLength = !ignoreId ? expectation.attributes.length : ( - expectation.attributes.containsKey('id') ? expectation.attributes.length - 1 : expectation.attributes.length - ); + final actualAttributeLength = !ignoreId + ? actual.attributes.length + : (actual.attributes.containsKey('id') + ? actual.attributes.length - 1 + : actual.attributes.length); + final expectedAttributeLength = !ignoreId + ? expectation.attributes.length + : (expectation.attributes.containsKey('id') + ? expectation.attributes.length - 1 + : expectation.attributes.length); if (actualAttributeLength != expectedAttributeLength) return false; - if (expectation.innerText() != '' && actual.innerText() != expectation.innerText()) return false; + if (expectation.innerText() != '' && + actual.innerText() != expectation.innerText()) return false; return expectation.children.every((childe) { - return actual.children.any((childa) => compareXMLNodes(childa, childe)); + return actual.children.any((childa) => compareXMLNodes(childa, childe)); }); } diff --git a/packages/moxxmpp/test/helpers/xmpp.dart b/packages/moxxmpp/test/helpers/xmpp.dart index c76bab9..b120b23 100644 --- a/packages/moxxmpp/test/helpers/xmpp.dart +++ b/packages/moxxmpp/test/helpers/xmpp.dart @@ -25,19 +25,23 @@ abstract class ExpectationBase { /// Literally compare the input with the expectation class StringExpectation extends ExpectationBase { - StringExpectation(String expectation, String response) : super(expectation, response); + StringExpectation(super.expectation, super.response); @override bool matches(String input) => input == expectation; } -/// +/// class StanzaExpectation extends ExpectationBase { - StanzaExpectation(String expectation, String response, {this.ignoreId = false, this.adjustId = false }) : super(expectation, response); - + StanzaExpectation( + super.expectation, + super.response, { + this.ignoreId = false, + this.adjustId = false, + }); final bool ignoreId; final bool adjustId; - + @override bool matches(String input) { final ex = XMLNode.fromString(expectation); @@ -52,7 +56,9 @@ class StanzaExpectation extends ExpectationBase { List buildAuthenticatedPlay(ConnectionSettings settings) { assert(settings.allowPlainAuth, 'SASL PLAIN must be allowed'); - final plain = base64.encode(utf8.encode('\u0000${settings.jid.local}\u0000${settings.password}')); + final plain = base64.encode( + utf8.encode('\u0000${settings.jid.local}\u0000${settings.password}'), + ); return [ StringExpectation( "", @@ -68,14 +74,14 @@ List buildAuthenticatedPlay(ConnectionSettings settings) { PLAIN ''', - ), - StringExpectation( - "$plain", - '' - ), - StringExpectation( - "", - ''' + ), + StringExpectation( + "$plain", + '', + ), + StringExpectation( + "", + ''' buildAuthenticatedPlay(ConnectionSettings settings) { ''', - ), - StanzaExpectation( - '', - '${settings.jid.toBare()}/MU29eEZn', - ignoreId: true, - ), - StanzaExpectation( - "chat", - '', - ), + ), + StanzaExpectation( + '', + '${settings.jid.toBare()}/MU29eEZn', + ignoreId: true, + ), + StanzaExpectation( + "chat", + '', + ), ]; } -class StubTCPSocket extends BaseSocketWrapper { // Request -> Response(s) +class StubTCPSocket extends BaseSocketWrapper { + // Request -> Response(s) StubTCPSocket(this._play); - StubTCPSocket.authenticated(ConnectionSettings settings, List play) : _play = [ - ...buildAuthenticatedPlay(settings), - ...play, - ]; + StubTCPSocket.authenticated( + ConnectionSettings settings, + List play, + ) : _play = [ + ...buildAuthenticatedPlay(settings), + ...play, + ]; int _state = 0; - final StreamController _dataStream = StreamController.broadcast(); - final StreamController _eventStream = StreamController.broadcast(); + final StreamController _dataStream = + StreamController.broadcast(); + final StreamController _eventStream = + StreamController.broadcast(); final List _play; String? lastId; @@ -120,24 +132,26 @@ class StubTCPSocket extends BaseSocketWrapper { // Request -> Response(s) @override Future secure(String domain) async => true; - + @override - Future connect(String domain, { String? host, int? port }) async => true; + Future connect(String domain, {String? host, int? port}) async => true; @override Stream getDataStream() => _dataStream.stream.asBroadcastStream(); @override - Stream getEventStream() => _eventStream.stream.asBroadcastStream(); + Stream getEventStream() => + _eventStream.stream.asBroadcastStream(); /// Let the "connection" receive [data]. void injectRawXml(String data) { + // ignore: avoid_print print('<== $data'); _dataStream.add(data); } - + @override - void write(Object? object, { String? redact }) { - var str = object as String; + void write(Object? object, {String? redact}) { + var str = object! as String; // ignore: avoid_print print('==> $str'); @@ -148,7 +162,7 @@ class StubTCPSocket extends BaseSocketWrapper { // Request -> Response(s) final expectation = _play[_state]; - // TODO: Implement an XML matcher + // TODO(Unknown): Implement an XML matcher if (str.startsWith("")) { str = str.substring(21); } @@ -174,18 +188,19 @@ class StubTCPSocket extends BaseSocketWrapper { // Request -> Response(s) if (expectation.adjustId) { final outputNode = XMLNode.fromString(response); - outputNode.attributes['id'] = inputNode.attributes['id']!; + outputNode.attributes['id'] = inputNode.attributes['id']; response = outputNode.toXml(); } } - - print("<== $response"); + + // ignore: avoid_print + print('<== $response'); _dataStream.add(response); } @override void close() {} - + int getState() => _state; void resetState() => _state = 0; @@ -194,4 +209,4 @@ class StubTCPSocket extends BaseSocketWrapper { // Request -> Response(s) @override bool managesKeepalives() => false; -} \ No newline at end of file +}