diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8ac7e9b..a7e9e33 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,13 +19,13 @@ jobs: run: dartanalyzer --fatal-infos --fatal-warnings lib example test - name: Run tests run: pub run test_coverage + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v1 + with: + token: ${{ secrets.CODECOV_TOKEN }} - name: Check Code Coverage uses: ChicagoFlutter/lcov-cop@v1.0.2 with: path: coverage/lcov.info min_coverage: 100 - exclude: "**/*.g.dart" - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 - with: - token: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file + exclude: "**/*.g.dart" \ No newline at end of file diff --git a/lib/src/methods.dart b/lib/src/methods.dart index 90b5c1c..357fca8 100644 --- a/lib/src/methods.dart +++ b/lib/src/methods.dart @@ -81,51 +81,61 @@ mixin PlatformMethods on ExtendedHostPlatform { PlatformResult Function() debug, PlatformResult Function() orElse, }) { - // Operating System - if (fuchsia != null && isFuchsia) { - return fuchsia(); - } else if (windows != null && isWindows) { - return windows(); - } else if (android != null && isAndroid) { - return android(); - } else if (iOS != null && isIOS) { - return iOS(); - } else if (macOS != null && isMacOS) { - return macOS(); - } else if (linux != null && isLinux) { - return linux(); - } else if (unknown != null && !isOperatingSystemKnown) { - return unknown(); + { + // Operating System + if (fuchsia != null && isFuchsia) { + return fuchsia(); + } else if (windows != null && isWindows) { + return windows(); + } else if (android != null && isAndroid) { + return android(); + } else if (iOS != null && isIOS) { + return iOS(); + } else if (macOS != null && isMacOS) { + return macOS(); + } else if (linux != null && isLinux) { + return linux(); + } else if (unknown != null && !isOperatingSystemKnown) { + return unknown(); + } } - // Design - if (material != null && isMaterial) { - return material(); - } else if (cupertino != null && isCupertino) { - return cupertino(); + { + // Design + if (material != null && isMaterial) { + return material(); + } else if (cupertino != null && isCupertino) { + return cupertino(); + } } - // Mobile/Desktop - if (mobile != null && isMobile) { - return mobile(); - } else if (desktop != null && isDesktop) { - return desktop(); + { + // Mobile/Desktop + if (mobile != null && isMobile) { + return mobile(); + } else if (desktop != null && isDesktop) { + return desktop(); + } } - // IO/Web - if (io != null && isIO) { - return io(); - } else if (web != null && isWeb) { - return web(); + { + // IO/Web + if (io != null && isIO) { + return io(); + } else if (web != null && isWeb) { + return web(); + } } - // Build mode - if (debug != null && buildMode == BuildMode.debug) { - return debug(); - } else if (profile != null && buildMode == BuildMode.profile) { - return profile(); - } else if (release != null && buildMode == BuildMode.release) { - return release(); + { + // Build mode + if (debug != null && buildMode == BuildMode.debug) { + return debug(); + } else if (profile != null && buildMode == BuildMode.profile) { + return profile(); + } else if (release != null && buildMode == BuildMode.release) { + return release(); + } } // Any callback was not called diff --git a/lib/src/platform.dart b/lib/src/platform.dart index d4b4656..4ce47e1 100644 --- a/lib/src/platform.dart +++ b/lib/src/platform.dart @@ -108,7 +108,8 @@ class Platform extends ExtendedHostPlatform with PlatformMethods { bool get isWindows => operatingSystem == OperatingSystem.windows; @override - final BuildMode buildMode; + BuildMode get buildMode => _buildMode; + BuildMode _buildMode; /// Host platform /// contain info about host device @@ -138,7 +139,7 @@ class Platform extends ExtendedHostPlatform with PlatformMethods { static final Platform _this = Platform._internal(); Platform._internal() : _hostPlatform = _getHostPlatform(), - buildMode = _getCurrentBuildMode() { + _buildMode = _getCurrentBuildMode() { _isOperatingSystemKnown = operatingSystem != OperatingSystem.unknown; _isMobile = kListOSForMobile.contains(operatingSystem); _isDesktop = kListOSForDesktop.contains(operatingSystem); @@ -146,6 +147,8 @@ class Platform extends ExtendedHostPlatform with PlatformMethods { _isCupertino = kListOSWithCupertinoDesign.contains(operatingSystem); } + Platform._emptyForTest() : _hostPlatform = _getHostPlatform(); + @override int get hashCode => 0; @@ -155,3 +158,91 @@ class Platform extends ExtendedHostPlatform with PlatformMethods { @override String toString() => version; } + +/// Fake platform for test +class FakePlatform extends Platform with PlatformMethods { + /// @nodoc + FakePlatform() : super._emptyForTest() { + reset(); + } + + @override + BuildMode buildMode; + + @override + bool isAndroid; + + @override + bool isCupertino; + + @override + bool isDesktop; + + @override + bool isFuchsia; + + @override + bool isIO; + + @override + bool isIOS; + + @override + bool isLinux; + + @override + bool isMacOS; + + @override + bool isMaterial; + + @override + bool isMobile; + + @override + bool isOperatingSystemKnown; + + @override + bool isWeb; + + @override + bool isWindows; + + @override + String locale; + + @override + int numberOfProcessors; + + @override + OperatingSystem operatingSystem; + + @override + HostPlatformType type; + + @override + String version; + + /// @nodoc + void reset() { + buildMode = BuildMode.debug; + isFuchsia = false; + isWindows = false; + isAndroid = false; + isIOS = false; + isMacOS = false; + isLinux = true; + isMaterial = false; + isCupertino = false; + isMobile = false; + isDesktop = true; + locale = 'en'; + numberOfProcessors = 1; + operatingSystem = OperatingSystem.linux; + isOperatingSystemKnown = true; + isIO = true; + isWeb = false; + type = HostPlatformType.io; + version = '1'; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 87900d8..135eb15 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,5 +14,4 @@ environment: dev_dependencies: test_coverage: ^0.4.3 - test: ^1.15.3 - + test: ^1.15.3 \ No newline at end of file diff --git a/test/platform_info_test.dart b/test/platform_info_test.dart index 516ede5..24b2bdd 100644 --- a/test/platform_info_test.dart +++ b/test/platform_info_test.dart @@ -81,14 +81,17 @@ void main() { group('io platform test', () { test('unknown environment', () { - runZoned(() { - final ioPlatform = io.getHostPlatform(); - expect(ioPlatform.operatingSystem, OperatingSystem.unknown); - expect(ioPlatform.numberOfProcessors, 0); - expect(ioPlatform.locale, 'en'); - expect(ioPlatform.version, ''); - expect(ioPlatform.type, HostPlatformType.io); - }, zoneValues: {#platform_info_test.isUnknownEnvironment: true}); + runZoned( + () { + final ioPlatform = io.getHostPlatform(); + expect(ioPlatform.operatingSystem, OperatingSystem.unknown); + expect(ioPlatform.numberOfProcessors, 0); + expect(ioPlatform.locale, 'en'); + expect(ioPlatform.version, ''); + expect(ioPlatform.type, HostPlatformType.io); + }, + zoneValues: {#platform_info_test.isUnknownEnvironment: true}, + ); }); }); @@ -96,115 +99,180 @@ void main() { bool returnTrue() => true; bool returnFalse() => false; + FakePlatform platform; + + setUpAll(() { + platform = FakePlatform(); + }); + test('Operating System', () { + platform + ..reset() + ..isFuchsia = true; expect( platform.when( fuchsia: returnTrue, orElse: returnFalse, ), - platform.isFuchsia); + isTrue); + + platform + ..reset() + ..isWindows = true; expect( platform.when( windows: returnTrue, orElse: returnFalse, ), - platform.isWindows); + isTrue); + + platform + ..reset() + ..isAndroid = true; expect( platform.when( android: returnTrue, orElse: returnFalse, ), - platform.isAndroid); + isTrue); + + platform + ..reset() + ..isIOS = true; expect( platform.when( iOS: returnTrue, orElse: returnFalse, ), - platform.isIOS); + isTrue); + + platform + ..reset() + ..isMacOS = true; expect( platform.when( macOS: returnTrue, orElse: returnFalse, ), - platform.isMacOS); + isTrue); + + platform + ..reset() + ..isLinux = true; expect( platform.when( linux: returnTrue, orElse: returnFalse, ), - platform.isLinux); + isTrue); + + platform + ..reset() + ..isOperatingSystemKnown = false; expect( platform.when( unknown: returnTrue, orElse: returnFalse, ), - !platform.isOperatingSystemKnown); + isTrue); }); test('Design', () { + platform + ..reset() + ..isMaterial = true; expect( platform.when( material: returnTrue, orElse: returnFalse, ), - platform.isMaterial); + isTrue); + + platform + ..reset() + ..isCupertino = true; expect( platform.when( cupertino: returnTrue, orElse: returnFalse, ), - platform.isCupertino); + isTrue); }); test('Mobile/Desktop', () { + platform + ..reset() + ..isMobile = true; expect( platform.when( mobile: returnTrue, orElse: returnFalse, ), - platform.isMobile); + isTrue); + + platform + ..reset() + ..isDesktop = true; expect( platform.when( desktop: returnTrue, orElse: returnFalse, ), - platform.isDesktop); + isTrue); }); test('IO or Web', () { + platform + ..reset() + ..isIO = true; expect( platform.when( io: returnTrue, orElse: returnFalse, ), - platform.isIO); + isTrue); + + platform + ..reset() + ..isWeb = true; expect( platform.when( web: returnTrue, orElse: returnFalse, ), - platform.isWeb); + isTrue); }); test('Build mode', () { + platform + ..reset() + ..buildMode = BuildMode.debug; expect( platform.when( debug: returnTrue, orElse: returnFalse, ), - platform.buildMode == BuildMode.debug); + isTrue); + + platform + ..reset() + ..buildMode = BuildMode.profile; expect( platform.when( profile: returnTrue, orElse: returnFalse, ), - platform.buildMode == BuildMode.profile); + isTrue); + + platform + ..reset() + ..buildMode = BuildMode.release; expect( platform.when( release: returnTrue, orElse: returnFalse, ), - platform.buildMode == BuildMode.release); + isTrue); }); test('orElse', () { @@ -213,11 +281,13 @@ void main() { orElse: returnTrue, ), isTrue); + expect( platform.when( orElse: returnFalse, ), isFalse); + expect(platform.when(), isNull); }); @@ -229,7 +299,7 @@ void main() { cupertino: returnTrue, orElse: returnTrue, ), - orElse: returnTrue, + orElse: returnFalse, ), isTrue); });