From c7d349a762e88f36944be915489b68816ee88285 Mon Sep 17 00:00:00 2001 From: Plague Fox Date: Thu, 18 Feb 2021 23:05:03 +0300 Subject: [PATCH 1/9] add non nullable support --- example/main.dart | 31 ++--- lib/src/base_host_platform.dart | 3 + lib/src/io_host_platform.dart | 14 +-- lib/src/methods.dart | 39 ++++--- lib/src/platform.dart | 128 +++------------------ lib/src/stub_host_platform.dart | 3 + lib/src/web_host_platform.dart | 35 +++--- pubspec.yaml | 9 +- test/platform_info_test.dart | 198 +------------------------------- 9 files changed, 96 insertions(+), 364 deletions(-) diff --git a/example/main.dart b/example/main.dart index a3208cf..a6b960c 100644 --- a/example/main.dart +++ b/example/main.dart @@ -9,20 +9,21 @@ void main(List args) { print(platform.numberOfProcessors.gcd(1)); final string = platform.when( - io: () => platform.when( - fuchsia: () => 'io fuchsia', - windows: () => 'io windows', - android: () => 'io android', - iOS: () => 'io iOS', - macOS: () => 'io macOS', - linux: () => 'io linux', - unknown: () => 'io unknown', - ), - web: () => platform.when( - material: () => 'web Android or Fuchsia', - cupertino: () => 'web macOS or iOS', - orElse: () => 'web Windows or Linux or unknown', - ), - ); + io: () => platform.when( + fuchsia: () => 'io fuchsia', + windows: () => 'io windows', + android: () => 'io android', + iOS: () => 'io iOS', + macOS: () => 'io macOS', + linux: () => 'io linux', + unknown: () => 'io unknown', + ), + web: () => platform.when( + material: () => 'web Android or Fuchsia', + cupertino: () => 'web macOS or iOS', + orElse: () => 'web Windows or Linux or unknown', + ), + ) ?? + ''; print(string); } diff --git a/lib/src/base_host_platform.dart b/lib/src/base_host_platform.dart index 95da8db..c60b231 100644 --- a/lib/src/base_host_platform.dart +++ b/lib/src/base_host_platform.dart @@ -1,7 +1,10 @@ +import 'package:meta/meta.dart' show immutable; + import 'enums.dart'; /// Host platform /// contain info about host device +@immutable abstract class HostPlatform { /// Host platform type /// + io diff --git a/lib/src/io_host_platform.dart b/lib/src/io_host_platform.dart index c9e73b8..469a2c0 100644 --- a/lib/src/io_host_platform.dart +++ b/lib/src/io_host_platform.dart @@ -13,7 +13,7 @@ class _IOHostPlatform implements HostPlatform { _IOHostPlatform._(); static bool get _isUnknownEnvironment => - Zone.current[#platform_info_test.isUnknownEnvironment] as bool ?? false; + Zone.current[#platform_info_test.isUnknownEnvironment] as bool? ?? false; static bool get _isKnownEnvironment => !_isUnknownEnvironment; @@ -73,12 +73,12 @@ class _IOHostPlatform implements HostPlatform { static String _getLocale() { final lang = io.Platform.localeName - ?.split('-') - ?.first - ?.split('_') - ?.first - ?.trim() - ?.toLowerCase(); + .split('-') + .first + .split('_') + .first + .trim() + .toLowerCase(); if (_isUnknownEnvironment || lang is! String || lang.length != 2) { return kDefaultHostPlatform.locale; } diff --git a/lib/src/methods.dart b/lib/src/methods.dart index 357fca8..9a9c0e2 100644 --- a/lib/src/methods.dart +++ b/lib/src/methods.dart @@ -5,6 +5,7 @@ import 'enums.dart'; mixin PlatformMethods on ExtendedHostPlatform { /// Run functions that satisfy the current state of the platform. /// You can use nested methods to compose more complex queries. + /// Can return null, if [orElse] not set and any callback was not called. /// /// ### Operating System /// [fuchsia] - whether the operating system is a version of Fuchsia @@ -53,33 +54,35 @@ mixin PlatformMethods on ExtendedHostPlatform { /// macOS: () => ..., /// linux: () => ..., /// unknown: () => ..., + /// orElse: () => ..., /// ), /// web: () => platform.when( /// material: () => ..., /// cupertino: () => ..., /// orElse: () => ..., /// ), + /// orElse: () => ..., /// ); /// ``` /// - PlatformResult when({ - PlatformResult Function() fuchsia, - PlatformResult Function() windows, - PlatformResult Function() android, - PlatformResult Function() iOS, - PlatformResult Function() macOS, - PlatformResult Function() linux, - PlatformResult Function() unknown, - PlatformResult Function() material, - PlatformResult Function() cupertino, - PlatformResult Function() mobile, - PlatformResult Function() desktop, - PlatformResult Function() io, - PlatformResult Function() web, - PlatformResult Function() release, - PlatformResult Function() profile, - PlatformResult Function() debug, - PlatformResult Function() orElse, + PlatformResult? when({ + PlatformResult Function()? fuchsia, + PlatformResult Function()? windows, + PlatformResult Function()? android, + PlatformResult Function()? iOS, + PlatformResult Function()? macOS, + PlatformResult Function()? linux, + PlatformResult Function()? unknown, + PlatformResult Function()? material, + PlatformResult Function()? cupertino, + PlatformResult Function()? mobile, + PlatformResult Function()? desktop, + PlatformResult Function()? io, + PlatformResult Function()? web, + PlatformResult Function()? release, + PlatformResult Function()? profile, + PlatformResult Function()? debug, + PlatformResult Function()? orElse, }) { { // Operating System diff --git a/lib/src/platform.dart b/lib/src/platform.dart index 4ce47e1..4a0b738 100644 --- a/lib/src/platform.dart +++ b/lib/src/platform.dart @@ -1,5 +1,7 @@ // ignore_for_file: avoid_equals_and_hash_code_on_mutable_classes +import 'package:meta/meta.dart' show immutable; + import 'base_host_platform.dart'; import 'constants.dart'; import 'enums.dart'; @@ -47,6 +49,7 @@ import 'stub_host_platform.dart' /// /// + Is cupertino (MacOS, iOS) /// +@immutable class Platform extends ExtendedHostPlatform with PlatformMethods { @override HostPlatformType get type => _hostPlatform.type; @@ -64,8 +67,7 @@ class Platform extends ExtendedHostPlatform with PlatformMethods { int get numberOfProcessors => _hostPlatform.numberOfProcessors; @override - bool get isOperatingSystemKnown => _isOperatingSystemKnown; - bool _isOperatingSystemKnown; + late final bool isOperatingSystemKnown; @override bool get isWeb => _hostPlatform.type == HostPlatformType.web; @@ -74,20 +76,16 @@ class Platform extends ExtendedHostPlatform with PlatformMethods { bool get isIO => _hostPlatform.type == HostPlatformType.io; @override - bool get isMobile => _isMobile; - bool _isMobile; + late final bool isMobile; @override - bool get isDesktop => _isDesktop; - bool _isDesktop; + late final bool isDesktop; @override - bool get isMaterial => _isMaterial; - bool _isMaterial; + late final bool isMaterial; @override - bool get isCupertino => _isCupertino; - bool _isCupertino; + late final bool isCupertino; @override bool get isAndroid => operatingSystem == OperatingSystem.android; @@ -108,11 +106,9 @@ class Platform extends ExtendedHostPlatform with PlatformMethods { bool get isWindows => operatingSystem == OperatingSystem.windows; @override - BuildMode get buildMode => _buildMode; - BuildMode _buildMode; + final BuildMode buildMode; - /// Host platform - /// contain info about host device + /// Host platform contain info about host device final HostPlatform _hostPlatform; static BuildMode _getCurrentBuildMode() => () { @@ -139,16 +135,14 @@ class Platform extends ExtendedHostPlatform with PlatformMethods { static final Platform _this = Platform._internal(); Platform._internal() : _hostPlatform = _getHostPlatform(), - _buildMode = _getCurrentBuildMode() { - _isOperatingSystemKnown = operatingSystem != OperatingSystem.unknown; - _isMobile = kListOSForMobile.contains(operatingSystem); - _isDesktop = kListOSForDesktop.contains(operatingSystem); - _isMaterial = kListOSWithMaterialDesign.contains(operatingSystem); - _isCupertino = kListOSWithCupertinoDesign.contains(operatingSystem); + buildMode = _getCurrentBuildMode() { + isOperatingSystemKnown = operatingSystem != OperatingSystem.unknown; + isMobile = kListOSForMobile.contains(operatingSystem); + isDesktop = kListOSForDesktop.contains(operatingSystem); + isMaterial = kListOSWithMaterialDesign.contains(operatingSystem); + isCupertino = kListOSWithCupertinoDesign.contains(operatingSystem); } - Platform._emptyForTest() : _hostPlatform = _getHostPlatform(); - @override int get hashCode => 0; @@ -156,93 +150,5 @@ class Platform extends ExtendedHostPlatform with PlatformMethods { bool operator ==(Object other) => other is Platform; @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'; - } + String toString() => ''; } diff --git a/lib/src/stub_host_platform.dart b/lib/src/stub_host_platform.dart index cc7077a..4da63c9 100644 --- a/lib/src/stub_host_platform.dart +++ b/lib/src/stub_host_platform.dart @@ -1,3 +1,5 @@ +import 'package:meta/meta.dart' show immutable; + import 'base_host_platform.dart'; import 'enums.dart'; @@ -5,6 +7,7 @@ import 'enums.dart'; HostPlatform getHostPlatform() => const DefaultHostPlatform(); /// Unknown host platform with default values +@immutable class DefaultHostPlatform implements HostPlatform { /// Unknown host platform with default values const DefaultHostPlatform(); diff --git a/lib/src/web_host_platform.dart b/lib/src/web_host_platform.dart index d71d2de..d9af2b6 100644 --- a/lib/src/web_host_platform.dart +++ b/lib/src/web_host_platform.dart @@ -1,6 +1,8 @@ import 'dart:async'; import 'dart:html' as html show window; +import 'package:meta/meta.dart' show immutable; + import 'base_host_platform.dart'; import 'constants.dart'; import 'enums.dart'; @@ -9,11 +11,12 @@ import 'enums.dart'; HostPlatform getHostPlatform() => _WebHostPlatform._(); /// Web based host platform +@immutable class _WebHostPlatform implements HostPlatform { _WebHostPlatform._(); static bool get _isUnknownEnvironment => - Zone.current[#platform_info_test.isUnknownEnvironment] as bool ?? false; + Zone.current[#platform_info_test.isUnknownEnvironment] as bool? ?? false; static bool get _isKnownEnvironment => !_isUnknownEnvironment; @@ -30,7 +33,7 @@ class _WebHostPlatform implements HostPlatform { final String locale = _getLocale(); @override - int numberOfProcessors = _numberOfProcessors(); + final int numberOfProcessors = _numberOfProcessors(); static OperatingSystem _getOS() { if (_isKnownEnvironment) { @@ -54,16 +57,18 @@ class _WebHostPlatform implements HostPlatform { return kDefaultHostPlatform.operatingSystem; } - static String _getVersion() => [ - html.window?.navigator?.userAgent, - html.window?.navigator?.appVersion, - html.window?.navigator?.platform, - ].firstWhere((v) => _isKnownEnvironment && v is String && v.isNotEmpty, - orElse: () => kDefaultHostPlatform.version); + static String _getVersion() => [ + html.window.navigator.userAgent, + html.window.navigator.appVersion, + html.window.navigator.platform ?? '', + ].firstWhere( + (v) => _isKnownEnvironment && v is String && v.isNotEmpty, + orElse: () => kDefaultHostPlatform.version, + ); static int _numberOfProcessors() { if (_isKnownEnvironment) { - final numberOfProcessors = html.window?.navigator?.hardwareConcurrency; + final numberOfProcessors = html.window.navigator.hardwareConcurrency; if (numberOfProcessors != null) { return numberOfProcessors; } @@ -73,12 +78,12 @@ class _WebHostPlatform implements HostPlatform { static String _getLocale() { final lang = html.window.navigator.language - ?.split('-') - ?.first - ?.split('_') - ?.first - ?.trim() - ?.toLowerCase(); + .split('-') + .first + .split('_') + .first + .trim() + .toLowerCase(); if (_isUnknownEnvironment || lang is! String || lang.length != 2) { return kDefaultHostPlatform.locale; } diff --git a/pubspec.yaml b/pubspec.yaml index 135eb15..1bf78fb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: platform_info description: > Contains info about current platform such as Build mode and Operating system -version: 2.2.0 +version: 3.0.0-nullsafety.0 repository: https://github.com/PlugFox/platform_info/tree/master issue_tracker: https://github.com/PlugFox/platform_info/issues homepage: https://github.com/PlugFox/platform_info @@ -10,8 +10,9 @@ homepage: https://github.com/PlugFox/platform_info #documentation: https://pub.dev/documentation/platform_info/latest environment: - sdk: ">=2.6.0 <3.0.0" + sdk: ">=2.12.0-0 <3.0.0" + meta: ^1.3.0 dev_dependencies: - test_coverage: ^0.4.3 - test: ^1.15.3 \ No newline at end of file + test: ^1.16.0 + test_coverage: ^0.4.3 \ No newline at end of file diff --git a/test/platform_info_test.dart b/test/platform_info_test.dart index 24b2bdd..baec1eb 100644 --- a/test/platform_info_test.dart +++ b/test/platform_info_test.dart @@ -1,9 +1,9 @@ import 'dart:async'; -import 'package:platform_info/src/stub_host_platform.dart' as stub; +import 'package:platform_info/platform_info.dart'; import 'package:platform_info/src/io_host_platform.dart' as io; +import 'package:platform_info/src/stub_host_platform.dart' as stub; import 'package:test/test.dart'; -import 'package:platform_info/platform_info.dart'; void main() { group('Platform', () { @@ -58,7 +58,7 @@ void main() { }); test('toString', () { - expect(Platform.I.toString(), Platform.I.version); + expect(Platform.I.toString(), ''); }); }); @@ -99,197 +99,7 @@ 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, - ), - isTrue); - - platform - ..reset() - ..isWindows = true; - expect( - platform.when( - windows: returnTrue, - orElse: returnFalse, - ), - isTrue); - - platform - ..reset() - ..isAndroid = true; - expect( - platform.when( - android: returnTrue, - orElse: returnFalse, - ), - isTrue); - - platform - ..reset() - ..isIOS = true; - expect( - platform.when( - iOS: returnTrue, - orElse: returnFalse, - ), - isTrue); - - platform - ..reset() - ..isMacOS = true; - expect( - platform.when( - macOS: returnTrue, - orElse: returnFalse, - ), - isTrue); - - platform - ..reset() - ..isLinux = true; - expect( - platform.when( - linux: returnTrue, - orElse: returnFalse, - ), - isTrue); - - platform - ..reset() - ..isOperatingSystemKnown = false; - expect( - platform.when( - unknown: returnTrue, - orElse: returnFalse, - ), - isTrue); - }); - - test('Design', () { - platform - ..reset() - ..isMaterial = true; - expect( - platform.when( - material: returnTrue, - orElse: returnFalse, - ), - isTrue); - - platform - ..reset() - ..isCupertino = true; - expect( - platform.when( - cupertino: returnTrue, - orElse: returnFalse, - ), - isTrue); - }); - - test('Mobile/Desktop', () { - platform - ..reset() - ..isMobile = true; - expect( - platform.when( - mobile: returnTrue, - orElse: returnFalse, - ), - isTrue); - - platform - ..reset() - ..isDesktop = true; - expect( - platform.when( - desktop: returnTrue, - orElse: returnFalse, - ), - isTrue); - }); - - test('IO or Web', () { - platform - ..reset() - ..isIO = true; - expect( - platform.when( - io: returnTrue, - orElse: returnFalse, - ), - isTrue); - - platform - ..reset() - ..isWeb = true; - expect( - platform.when( - web: returnTrue, - orElse: returnFalse, - ), - isTrue); - }); - - test('Build mode', () { - platform - ..reset() - ..buildMode = BuildMode.debug; - expect( - platform.when( - debug: returnTrue, - orElse: returnFalse, - ), - isTrue); - - platform - ..reset() - ..buildMode = BuildMode.profile; - expect( - platform.when( - profile: returnTrue, - orElse: returnFalse, - ), - isTrue); - - platform - ..reset() - ..buildMode = BuildMode.release; - expect( - platform.when( - release: returnTrue, - orElse: returnFalse, - ), - isTrue); - }); - - test('orElse', () { - expect( - platform.when( - orElse: returnTrue, - ), - isTrue); - - expect( - platform.when( - orElse: returnFalse, - ), - isFalse); - - expect(platform.when(), isNull); - }); + setUpAll(() {}); test('Chaining', () { expect( From 5841997dc85f9a176a3fdfdbf04cf8f5b9f47d97 Mon Sep 17 00:00:00 2001 From: Plague Fox Date: Thu, 18 Feb 2021 23:11:02 +0300 Subject: [PATCH 2/9] update workflow with beta dart --- .github/workflows/main.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a7e9e33..cf9a422 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,14 +1,14 @@ name: platform_info on: push: - branches: [ master, dev ] + branches: [ master, dev, feature/null_safety ] pull_request: - branches: [ master, dev ] + branches: [ master, dev, feature/null_safety ] jobs: build: runs-on: ubuntu-latest container: - image: google/dart:latest + image: google/dart:beta steps: - uses: actions/checkout@v2 - name: Install Dependencies @@ -27,5 +27,5 @@ jobs: uses: ChicagoFlutter/lcov-cop@v1.0.2 with: path: coverage/lcov.info - min_coverage: 100 + min_coverage: 95 exclude: "**/*.g.dart" \ No newline at end of file From e8d0812145bd90b244edc9cf416896878dd54380 Mon Sep 17 00:00:00 2001 From: Plague Fox Date: Fri, 19 Feb 2021 00:06:02 +0300 Subject: [PATCH 3/9] add FakePlatform and tests for PlatformMethods --- lib/src/base_host_platform.dart | 2 +- lib/src/constants.dart | 2 +- lib/src/default_host_platform.dart | 28 +++++ lib/src/io_host_platform.dart | 10 +- lib/src/platform.dart | 85 +++++++++++-- lib/src/stub_host_platform.dart | 28 +---- test/platform_info_test.dart | 185 ++++++++++++++++++++++++++++- 7 files changed, 288 insertions(+), 52 deletions(-) create mode 100644 lib/src/default_host_platform.dart diff --git a/lib/src/base_host_platform.dart b/lib/src/base_host_platform.dart index c60b231..d706816 100644 --- a/lib/src/base_host_platform.dart +++ b/lib/src/base_host_platform.dart @@ -31,7 +31,7 @@ abstract class HostPlatform { /// The number of individual execution units of the machine /// Returns 0 if unknown or not available - /// Use `.gcd(1)` if you need a quantity greater than 0 + /// Use `.clamp(1, double.infinity)` if you need a quantity greater than 0 int get numberOfProcessors; } diff --git a/lib/src/constants.dart b/lib/src/constants.dart index cb1d05b..df9ad7e 100644 --- a/lib/src/constants.dart +++ b/lib/src/constants.dart @@ -1,6 +1,6 @@ import 'base_host_platform.dart' show HostPlatform; +import 'default_host_platform.dart'; import 'enums.dart'; -import 'stub_host_platform.dart' show DefaultHostPlatform; /// List of all mobile phone operating systems const List kListOSForMobile = [ diff --git a/lib/src/default_host_platform.dart b/lib/src/default_host_platform.dart new file mode 100644 index 0000000..32c2926 --- /dev/null +++ b/lib/src/default_host_platform.dart @@ -0,0 +1,28 @@ +import 'package:meta/meta.dart' show immutable, literal; + +import 'base_host_platform.dart' show HostPlatform; +import 'enums.dart'; + +/// Unknown host platform with default values +@immutable +class DefaultHostPlatform implements HostPlatform { + /// Unknown host platform with default values + @literal + const DefaultHostPlatform(); + + @override + HostPlatformType get type => + identical(0, 0.0) ? HostPlatformType.web : HostPlatformType.io; + + @override + OperatingSystem get operatingSystem => OperatingSystem.unknown; + + @override + String get version => ''; + + @override + String get locale => 'en'; + + @override + int get numberOfProcessors => 0; +} diff --git a/lib/src/io_host_platform.dart b/lib/src/io_host_platform.dart index 469a2c0..5237e22 100644 --- a/lib/src/io_host_platform.dart +++ b/lib/src/io_host_platform.dart @@ -53,20 +53,14 @@ class _IOHostPlatform implements HostPlatform { static String _getVersion() { if (_isKnownEnvironment) { - final operatingSystemVersion = io.Platform?.operatingSystemVersion; - if (operatingSystemVersion != null) { - return operatingSystemVersion; - } + return io.Platform.operatingSystemVersion; } return kDefaultHostPlatform.version; } static int _numberOfProcessors() { if (_isKnownEnvironment) { - final numberOfProcessors = io.Platform?.numberOfProcessors; - if (numberOfProcessors != null) { - return numberOfProcessors; - } + return io.Platform.numberOfProcessors; } return kDefaultHostPlatform.numberOfProcessors; } diff --git a/lib/src/platform.dart b/lib/src/platform.dart index 4a0b738..b51aa3b 100644 --- a/lib/src/platform.dart +++ b/lib/src/platform.dart @@ -4,6 +4,7 @@ import 'package:meta/meta.dart' show immutable; import 'base_host_platform.dart'; import 'constants.dart'; +import 'default_host_platform.dart'; import 'enums.dart'; import 'methods.dart'; import 'stub_host_platform.dart' @@ -132,16 +133,27 @@ class Platform extends ExtendedHostPlatform with PlatformMethods { static Platform get I => _this; /// Singleton info about platform - static final Platform _this = Platform._internal(); - Platform._internal() - : _hostPlatform = _getHostPlatform(), - buildMode = _getCurrentBuildMode() { - isOperatingSystemKnown = operatingSystem != OperatingSystem.unknown; - isMobile = kListOSForMobile.contains(operatingSystem); - isDesktop = kListOSForDesktop.contains(operatingSystem); - isMaterial = kListOSWithMaterialDesign.contains(operatingSystem); - isCupertino = kListOSWithCupertinoDesign.contains(operatingSystem); - } + static final Platform _this = Platform._internalFactoryFromEnvironment(); + + /// Internal factory from environment + factory Platform._internalFactoryFromEnvironment() => Platform._internal( + buildMode: _getCurrentBuildMode(), + hostPlatform: _getHostPlatform(), + ); + + /// Internal constructor + Platform._internal({ + required this.buildMode, + required HostPlatform hostPlatform, + }) : _hostPlatform = hostPlatform, + isOperatingSystemKnown = + hostPlatform.operatingSystem != OperatingSystem.unknown, + isMobile = kListOSForMobile.contains(hostPlatform.operatingSystem), + isDesktop = kListOSForDesktop.contains(hostPlatform.operatingSystem), + isMaterial = + kListOSWithMaterialDesign.contains(hostPlatform.operatingSystem), + isCupertino = + kListOSWithCupertinoDesign.contains(hostPlatform.operatingSystem); @override int get hashCode => 0; @@ -152,3 +164,56 @@ class Platform extends ExtendedHostPlatform with PlatformMethods { @override String toString() => ''; } + +/// Fake class for test needs +@immutable +class FakePlatform extends Platform { + /// Fake constructor for test needs + FakePlatform({ + BuildMode? buildMode, + HostPlatformType? type, + OperatingSystem? operatingSystem, + String? version, + String? locale, + int? numberOfProcessors, + }) : super._internal( + buildMode: buildMode ?? BuildMode.debug, + hostPlatform: _FakeHostPlatform( + type: type ?? const DefaultHostPlatform().type, + operatingSystem: + operatingSystem ?? const DefaultHostPlatform().operatingSystem, + version: version ?? const DefaultHostPlatform().version, + locale: locale ?? const DefaultHostPlatform().locale, + numberOfProcessors: numberOfProcessors ?? + const DefaultHostPlatform().numberOfProcessors, + ), + ); +} + +/// Fake class for test needs +@immutable +class _FakeHostPlatform implements HostPlatform { + /// Fake constructor for test needs + const _FakeHostPlatform({ + required this.locale, + required this.numberOfProcessors, + required this.operatingSystem, + required this.type, + required this.version, + }); + + @override + final String locale; + + @override + final int numberOfProcessors; + + @override + final OperatingSystem operatingSystem; + + @override + final HostPlatformType type; + + @override + final String version; +} diff --git a/lib/src/stub_host_platform.dart b/lib/src/stub_host_platform.dart index 4da63c9..537d081 100644 --- a/lib/src/stub_host_platform.dart +++ b/lib/src/stub_host_platform.dart @@ -1,30 +1,6 @@ -import 'package:meta/meta.dart' show immutable; - import 'base_host_platform.dart'; -import 'enums.dart'; +import 'default_host_platform.dart'; /// Get host platform if dart.library.html and dart.library.io available +/// Return unknown host platform with default values HostPlatform getHostPlatform() => const DefaultHostPlatform(); - -/// Unknown host platform with default values -@immutable -class DefaultHostPlatform implements HostPlatform { - /// Unknown host platform with default values - const DefaultHostPlatform(); - - @override - HostPlatformType get type => - identical(0, 0.0) ? HostPlatformType.web : HostPlatformType.io; - - @override - OperatingSystem get operatingSystem => OperatingSystem.unknown; - - @override - String get version => ''; - - @override - String get locale => 'en'; - - @override - int get numberOfProcessors => 0; -} diff --git a/test/platform_info_test.dart b/test/platform_info_test.dart index baec1eb..82def61 100644 --- a/test/platform_info_test.dart +++ b/test/platform_info_test.dart @@ -1,6 +1,8 @@ import 'dart:async'; import 'package:platform_info/platform_info.dart'; +import 'package:platform_info/src/default_host_platform.dart'; +import 'package:platform_info/src/enums.dart'; import 'package:platform_info/src/io_host_platform.dart' as io; import 'package:platform_info/src/stub_host_platform.dart' as stub; import 'package:test/test.dart'; @@ -67,10 +69,10 @@ void main() { final stubPlatform = stub.getHostPlatform(); const type = identical(0, 0.0) ? HostPlatformType.web : HostPlatformType.io; - expect(() => const stub.DefaultHostPlatform(), returnsNormally); - // ignore: prefer_const_constructors - expect(() => stub.DefaultHostPlatform(), returnsNormally); - expect(stubPlatform, const stub.DefaultHostPlatform()); + expect(() => const DefaultHostPlatform(), returnsNormally); + // ignore: prefer_const_constructors, non_const_call_to_literal_constructor + expect(() => DefaultHostPlatform(), returnsNormally); + expect(stubPlatform, const DefaultHostPlatform()); expect(stubPlatform.operatingSystem, OperatingSystem.unknown); expect(stubPlatform.numberOfProcessors, 0); expect(stubPlatform.locale, 'en'); @@ -99,8 +101,6 @@ void main() { bool returnTrue() => true; bool returnFalse() => false; - setUpAll(() {}); - test('Chaining', () { expect( platform.when( @@ -113,5 +113,178 @@ void main() { ), isTrue); }); + + test('Operating System', () { + expect( + FakePlatform( + operatingSystem: OperatingSystem.fuchsia, + ).when( + fuchsia: returnTrue, + orElse: returnFalse, + ), + isTrue); + + expect( + FakePlatform( + operatingSystem: OperatingSystem.windows, + ).when( + windows: returnTrue, + orElse: returnFalse, + ), + isTrue); + + expect( + FakePlatform( + operatingSystem: OperatingSystem.android, + ).when( + android: returnTrue, + orElse: returnFalse, + ), + isTrue); + + expect( + FakePlatform( + operatingSystem: OperatingSystem.iOS, + ).when( + iOS: returnTrue, + orElse: returnFalse, + ), + isTrue); + + expect( + FakePlatform( + operatingSystem: OperatingSystem.macOS, + ).when( + macOS: returnTrue, + orElse: returnFalse, + ), + isTrue); + + expect( + FakePlatform( + operatingSystem: OperatingSystem.linux, + ).when( + linux: returnTrue, + orElse: returnFalse, + ), + isTrue); + + expect( + FakePlatform( + operatingSystem: OperatingSystem.unknown, + ).when( + unknown: returnTrue, + orElse: returnFalse, + ), + isTrue); + }); + + test('Design', () { + expect( + FakePlatform( + operatingSystem: OperatingSystem.android, + ).when( + material: returnTrue, + orElse: returnFalse, + ), + isTrue); + + expect( + FakePlatform( + operatingSystem: OperatingSystem.iOS, + ).when( + cupertino: returnTrue, + orElse: returnFalse, + ), + isTrue); + }); + + test('Mobile/Desktop', () { + expect( + FakePlatform( + operatingSystem: OperatingSystem.android, + ).when( + mobile: returnTrue, + orElse: returnFalse, + ), + isTrue); + + expect( + FakePlatform( + operatingSystem: OperatingSystem.windows, + ).when( + desktop: returnTrue, + orElse: returnFalse, + ), + isTrue); + }); + + test('IO or Web', () { + expect( + FakePlatform( + type: HostPlatformType.io, + ).when( + io: returnTrue, + orElse: returnFalse, + ), + isTrue); + + expect( + FakePlatform( + type: HostPlatformType.web, + ).when( + web: returnTrue, + orElse: returnFalse, + ), + isTrue); + }); + + test('Build mode', () { + expect( + FakePlatform( + buildMode: BuildMode.debug, + ).when( + debug: returnTrue, + orElse: returnFalse, + ), + isTrue); + + expect( + FakePlatform( + buildMode: BuildMode.profile, + ).when( + profile: returnTrue, + orElse: returnFalse, + ), + isTrue); + + expect( + FakePlatform( + buildMode: BuildMode.release, + ).when( + release: returnTrue, + orElse: returnFalse, + ), + isTrue); + }); + + test('orElse', () { + expect( + FakePlatform().when( + orElse: returnTrue, + ), + isTrue); + + expect( + FakePlatform().when( + orElse: returnFalse, + ), + isFalse); + + expect( + FakePlatform().when(), + isNull, + ); + }); }); } From 9a4f01f5f185b0ed00913d216d3575417171a51b Mon Sep 17 00:00:00 2001 From: Plague Fox Date: Fri, 19 Feb 2021 00:24:54 +0300 Subject: [PATCH 4/9] update workflow with new dart commands --- .github/workflows/main.yml | 10 +++++----- analysis_options.yaml | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cf9a422..5218769 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,17 +8,17 @@ jobs: build: runs-on: ubuntu-latest container: - image: google/dart:beta + image: google/dart:dev steps: - uses: actions/checkout@v2 - name: Install Dependencies - run: pub get + run: dart pub get - name: Format - run: dartfmt --dry-run --set-exit-if-changed lib + run: dart format --set-exit-if-changed none - name: Run analyzer - run: dartanalyzer --fatal-infos --fatal-warnings lib example test + run: dart analyze --fatal-infos --fatal-warnings . - name: Run tests - run: pub run test_coverage + run: dart run test_coverage - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 with: diff --git a/analysis_options.yaml b/analysis_options.yaml index 5e486fa..917f1e4 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -17,6 +17,8 @@ analyzer: - 'assets/**' # Mocks for test - 'test/data/**' + # Coverage + - 'test/.test_coverage.dart' strong-mode: implicit-casts: false From 8ea85ecac5f95a82ae498f9772b434f236f0e48d Mon Sep 17 00:00:00 2001 From: Plague Fox Date: Fri, 19 Feb 2021 00:42:21 +0300 Subject: [PATCH 5/9] update workflow with new coverage tool --- .github/workflows/main.yml | 21 ++++++++++++--------- pubspec.yaml | 6 ++++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5218769..d9cc42a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,31 +1,34 @@ name: platform_info + on: push: branches: [ master, dev, feature/null_safety ] pull_request: branches: [ master, dev, feature/null_safety ] + jobs: build: + defaults: + run: + working-directory: / runs-on: ubuntu-latest container: - image: google/dart:dev + image: google/dart:2.12-dev steps: - uses: actions/checkout@v2 - name: Install Dependencies run: dart pub get - - name: Format + - name: Check format run: dart format --set-exit-if-changed none - - name: Run analyzer + - name: Check analyzer run: dart analyze --fatal-infos --fatal-warnings . - name: Run tests - run: dart run test_coverage + run: dart test --coverage=coverage && dart run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.info --packages=.packages --report-on=lib - 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: 95 - exclude: "**/*.g.dart" \ No newline at end of file + uses: VeryGoodOpenSource/very_good_coverage@v1.1.1 + with: + path: packages/bloc_test/coverage/lcov.info \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 1bf78fb..e90dc5a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,8 +11,10 @@ homepage: https://github.com/PlugFox/platform_info environment: sdk: ">=2.12.0-0 <3.0.0" + +dependencies: meta: ^1.3.0 dev_dependencies: - test: ^1.16.0 - test_coverage: ^0.4.3 \ No newline at end of file + coverage: ^0.14.2 + test: ^1.16.0 \ No newline at end of file From 440baf3c25a6da03f33a3b5472da7708c6975d00 Mon Sep 17 00:00:00 2001 From: Plague Fox <7805176+PlugFox@users.noreply.github.com> Date: Fri, 19 Feb 2021 00:44:48 +0300 Subject: [PATCH 6/9] fix workflow --- .github/workflows/main.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d9cc42a..5686245 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -29,6 +29,6 @@ jobs: with: token: ${{ secrets.CODECOV_TOKEN }} - name: Check Code Coverage - uses: VeryGoodOpenSource/very_good_coverage@v1.1.1 - with: - path: packages/bloc_test/coverage/lcov.info \ No newline at end of file + uses: VeryGoodOpenSource/very_good_coverage@v1.1.1 + with: + path: packages/bloc_test/coverage/lcov.info From 4d3da0a67a2fb3c16a40b1da3dee4018b7d72e00 Mon Sep 17 00:00:00 2001 From: Plague Fox Date: Fri, 19 Feb 2021 00:46:28 +0300 Subject: [PATCH 7/9] remove working directory --- .github/workflows/main.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5686245..f8c10c2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,9 +8,6 @@ on: jobs: build: - defaults: - run: - working-directory: / runs-on: ubuntu-latest container: image: google/dart:2.12-dev From 77bd9c7ced03d2dea6cf3f7c90164c795ad32ae0 Mon Sep 17 00:00:00 2001 From: Plague Fox Date: Fri, 19 Feb 2021 00:50:46 +0300 Subject: [PATCH 8/9] change lcov directory --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f8c10c2..efad836 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,4 +28,4 @@ jobs: - name: Check Code Coverage uses: VeryGoodOpenSource/very_good_coverage@v1.1.1 with: - path: packages/bloc_test/coverage/lcov.info + path: coverage/lcov.info From 42cd3e4209c3d4d90da174ab210bf968fde7beda Mon Sep 17 00:00:00 2001 From: Plague Fox <7805176+PlugFox@users.noreply.github.com> Date: Fri, 19 Feb 2021 01:07:45 +0300 Subject: [PATCH 9/9] Update main.yml --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index efad836..8cae728 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,6 +13,8 @@ jobs: image: google/dart:2.12-dev steps: - uses: actions/checkout@v2 + with: + fetch-depth: 2 - name: Install Dependencies run: dart pub get - name: Check format