Skip to content

Commit

Permalink
chore: share duplicate mocks across testsuite
Browse files Browse the repository at this point in the history
(only for the generic mocks, with 0 implementation detail leakage)
  • Loading branch information
XavierChanth committed Nov 15, 2023
1 parent 0369740 commit ecc9b0b
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:noports_core/sshnp_params.dart';
import 'package:test/test.dart';
import 'package:mocktail/mocktail.dart';

class MockAtClient extends Mock implements AtClient {}
import '../sshnp_mocks.dart';

void main() {
group('ConfigKeyRepository', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import 'package:noports_core/sshnp.dart';
import 'package:socket_connector/socket_connector.dart';
import 'package:test/test.dart';

class MockProcess extends Mock implements Process {}

class MockSocketConnector extends Mock implements SocketConnector {}
import '../sshnp_mocks.dart';

void main() {
group('SshnpResult', () {
Expand Down
18 changes: 1 addition & 17 deletions packages/noports_core/test/sshnp/sshnp_core_mocks.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import 'package:at_client/at_client.dart';
import 'package:mocktail/mocktail.dart';
import 'package:noports_core/sshnp_foundation.dart';

import 'sshnp_mocks.dart';
import 'sshnp_core_constants.dart';

/// Function Stubbing
abstract class FunctionCaller {
void call();
}

class FunctionStub extends Mock implements FunctionCaller {}

/// Mocked Classes
class MockAtClient extends Mock implements AtClient {}

class MockSshnpParams extends Mock implements SshnpParams {}

class MockSshnpdChannel extends Mock implements SshnpdChannel {}

class MockSshrvdChannel extends Mock implements SshrvdChannel {}

/// Stubbed [SshnpCore] (minimum viable implementation of [SshnpCore])
class StubbedSshnpCore extends SshnpCore with StubbedAsyncInitializationMixin {
StubbedSshnpCore({
Expand Down
1 change: 1 addition & 0 deletions packages/noports_core/test/sshnp/sshnp_core_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:noports_core/sshnp_foundation.dart';
import 'package:test/test.dart';
import 'package:mocktail/mocktail.dart';

import 'sshnp_mocks.dart';
import 'sshnp_core_mocks.dart';

void main() {
Expand Down
41 changes: 41 additions & 0 deletions packages/noports_core/test/sshnp/sshnp_mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:at_client/at_client.dart';
import 'package:mocktail/mocktail.dart';
import 'package:noports_core/src/common/io_types.dart';
import 'package:noports_core/sshnp_foundation.dart';
import 'package:socket_connector/socket_connector.dart';

/// A [void Function()] stub
abstract class FunctionCaller {
void call();
}

class FunctionStub extends Mock implements FunctionCaller {}

/// The basic mocks that are repeated countless times throughout the test suite
class MockAtClient extends Mock implements AtClient {}

class MockSshnpParams extends Mock implements SshnpParams {}

class MockSshnpdChannel extends Mock implements SshnpdChannel {}

class MockSshrvdChannel extends Mock implements SshrvdChannel {}

/// [dart:io] Mocks
class MockProcess extends Mock implements Process {}

class MockSocketConnector extends Mock implements SocketConnector {}


/// Stubbing for [Process.start]
abstract class StartProcessCaller {
Future<Process> call(
String executable,
List<String> arguments, {
bool runInShell,
ProcessStartMode mode,
});
}

class StartProcessStub extends Mock implements StartProcessCaller {}

7 changes: 2 additions & 5 deletions packages/noports_core/test/sshnp/sshnp_test.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import 'package:at_client/at_client.dart';
import 'package:mocktail/mocktail.dart';
import 'package:noports_core/sshnp_foundation.dart';
import 'package:test/test.dart';

class StubbedSshnp extends Mock implements Sshnp {}

class MockAtClient extends Mock implements AtClient {}
import 'sshnp_mocks.dart';

class MockSshnpParams extends Mock implements SshnpParams {}
class StubbedSshnp extends Mock implements Sshnp {}

class MySshnpResult extends SshnpResult {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
import 'package:at_client/at_client.dart';
import 'package:mocktail/mocktail.dart';
import 'package:noports_core/src/common/io_types.dart';
import 'package:noports_core/sshnp_foundation.dart';

import '../../sshnp_mocks.dart';

/// Function Stubbing
abstract class StartInitialTunnelCaller {
void call();
}

class StartInitialTunnelStub extends Mock implements StartInitialTunnelCaller {}

abstract class StartProcessCaller {
Future<Process> call(
String executable,
List<String> arguments, {
bool runInShell,
ProcessStartMode mode,
});
}

class StartProcessStub extends Mock implements StartProcessCaller {}

/// Mocked Classes
class MockAtClient extends Mock implements AtClient {}

class MockSshnpParams extends Mock implements SshnpParams {}

class MockSshnpdChannel extends Mock implements SshnpdChannel {}
/// Stubbed Mixin that we are testing
mixin StubbedSshnpOpensshInitialTunnelHandler
on SshnpOpensshInitialTunnelHandler {
late StartInitialTunnelStub _stubbedStartInitialTunnel;
late StartProcessStub _stubbedStartProcess;

class MockSshrvdChannel extends Mock implements SshrvdChannel {}
void stubSshnpOpensshInitialTunnelHandler({
required StartInitialTunnelStub stubbedStartInitialTunnel,
required StartProcessStub stubbedStartProcess,
}) {
_stubbedStartInitialTunnel = stubbedStartInitialTunnel;
_stubbedStartProcess = stubbedStartProcess;
}

class MockProcess extends Mock implements Process {}
@override
Future<Process?> startInitialTunnel({
required String identifier,
ProcessStarter startProcess = Process.start,
}) {
_stubbedStartInitialTunnel.call();
return super.startInitialTunnel(
identifier: identifier,
startProcess: _stubbedStartProcess.call,
);
}
}

/// Stubbed Sshnp instance with the mixin
class StubbedSshnp extends SshnpCore
with
SshnpOpensshInitialTunnelHandler,
Expand Down Expand Up @@ -61,29 +68,3 @@ class StubbedSshnp extends SshnpCore
SshrvdChannel get sshrvdChannel => _sshrvdChannel;
final SshrvdChannel _sshrvdChannel;
}

mixin StubbedSshnpOpensshInitialTunnelHandler
on SshnpOpensshInitialTunnelHandler {
late StartInitialTunnelStub _stubbedStartInitialTunnel;
late StartProcessStub _stubbedStartProcess;

void stubSshnpOpensshInitialTunnelHandler({
required StartInitialTunnelStub stubbedStartInitialTunnel,
required StartProcessStub stubbedStartProcess,
}) {
_stubbedStartInitialTunnel = stubbedStartInitialTunnel;
_stubbedStartProcess = stubbedStartProcess;
}

@override
Future<Process?> startInitialTunnel({
required String identifier,
ProcessStarter startProcess = Process.start,
}) {
_stubbedStartInitialTunnel.call();
return super.startInitialTunnel(
identifier: identifier,
startProcess: _stubbedStartProcess.call,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:mocktail/mocktail.dart';
import 'package:noports_core/sshnp_foundation.dart';
import 'package:test/test.dart';

import '../../sshnp_mocks.dart';
import 'sshnp_openssh_initial_tunnel_handler_mocks.dart';

void main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:mocktail/mocktail.dart';
import 'package:noports_core/sshnp_foundation.dart';
import 'package:noports_core/sshrv.dart';

/// Stubbing for [SshrvGenerator] typedef
abstract class SshrvGeneratorCaller<T> {
Sshrv<T> call(String host, int port, {int localSshdPort});
}

class SshrvGeneratorStub extends Mock implements SshrvGeneratorCaller {}

/// Stubbed [SshrvdChannel] which we are testing
class StubbedSshrvdChannel<T> extends SshrvdChannel<T> {
StubbedSshrvdChannel({
required super.atClient,
required super.params,
required super.sessionId,
required super.sshrvGenerator,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:test/test.dart';
import 'package:uuid/uuid.dart';

import '../../sshnp_mocks.dart';
import 'sshrvd_channel_mocks.dart';

void main() {
group('SshrvdChannel', () {
late SshrvGeneratorStub sshrvGeneratorStub;
late MockAtClient mockAtClient;
late MockSshnpParams mockParams;
late String sessionId;
late StubbedSshrvdChannel stubbedSshrvdChannel;

setUp(() {
sshrvGeneratorStub = SshrvGeneratorStub();
mockAtClient = MockAtClient();
mockParams = MockSshnpParams();
sessionId = Uuid().v4();

stubbedSshrvdChannel = StubbedSshrvdChannel(
atClient: mockAtClient,
params: mockParams,
sessionId: sessionId,
sshrvGenerator: sshrvGeneratorStub,
);
});


}); // group SshrvdChannel
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void main() {}

0 comments on commit ecc9b0b

Please sign in to comment.