From 435c45fb4eedac1d3f01dc8faf6f671607f3ea2a Mon Sep 17 00:00:00 2001 From: alireza easazade Date: Sat, 16 Sep 2023 09:26:35 +0330 Subject: [PATCH] Add boolean resource --- CHANGELOG.md | 2 +- lib/src/base/fake_collection.dart | 3 +++ lib/src/base/utility_resources/boolean.dart | 5 +++++ lib/src/base/utils.dart | 1 + scripts/lib_imports.dart | 1 + templates/fake_collection_file.mustache | 3 +++ test/manual/utility_resources/boolean_test.dart | 17 +++++++++++++++++ 7 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 lib/src/base/utility_resources/boolean.dart create mode 100644 test/manual/utility_resources/boolean_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index a0366c3..6213159 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ - Fix emailFrom generator ## 0.5.14 -- Update RAEDME +- Update README - Fix missing method argument type in Numbers class - Sort metadata lists read from code so generated code will be the same on windows as well - Add code document for scripts diff --git a/lib/src/base/fake_collection.dart b/lib/src/base/fake_collection.dart index bd15e8b..b379917 100644 --- a/lib/src/base/fake_collection.dart +++ b/lib/src/base/fake_collection.dart @@ -6,6 +6,7 @@ /// NOTE: Some part of some of the generated files are generated using mustache /// templates in templates directory. +import 'package:faker_x/src/base/utility_resources/boolean.dart'; import 'package:faker_x/src/base/utility_resources/number.dart'; import 'package:faker_x/src/base/resources.dart'; import 'package:faker_x/src/base/locale.dart'; @@ -17,6 +18,7 @@ class FakeCollection { FakeCollection({required this.locale}) : number = Number(), time = Time(), + boolean = Boolean(), address = Address(locale), animal = Animal(locale), automotive = Automotive(locale), @@ -31,6 +33,7 @@ class FakeCollection { final Number number; final Time time; + final Boolean boolean; final Address address; final Animal animal; final Automotive automotive; diff --git a/lib/src/base/utility_resources/boolean.dart b/lib/src/base/utility_resources/boolean.dart new file mode 100644 index 0000000..6cbbf4f --- /dev/null +++ b/lib/src/base/utility_resources/boolean.dart @@ -0,0 +1,5 @@ +import 'package:faker_x/faker_x.dart'; + +class Boolean { + bool get random => coinToss(true, false); +} diff --git a/lib/src/base/utils.dart b/lib/src/base/utils.dart index e5a395e..15ad4cd 100644 --- a/lib/src/base/utils.dart +++ b/lib/src/base/utils.dart @@ -31,6 +31,7 @@ String get randomLetterOrNumber => String randomString(int length) => List.generate(length, (index) => randomLetterOrNumber).join(); +/// randomly returns either [object1] or [object2] T coinToss(T object1, T object2) => (_random.nextInt(100) > 50) ? object1 : object2; diff --git a/scripts/lib_imports.dart b/scripts/lib_imports.dart index 45fd261..456827e 100644 --- a/scripts/lib_imports.dart +++ b/scripts/lib_imports.dart @@ -21,6 +21,7 @@ import 'package:faker_x/src/base/provider_context.dart'; import 'package:faker_x/src/base/registered_data_sources.dart'; import 'package:faker_x/src/base/resources.dart'; import 'package:faker_x/src/base/types.dart'; +import 'package:faker_x/src/base/utility_resources/boolean.dart'; import 'package:faker_x/src/base/utility_resources/number.dart'; import 'package:faker_x/src/base/utility_resources/time.dart'; import 'package:faker_x/src/base/utils.dart'; diff --git a/templates/fake_collection_file.mustache b/templates/fake_collection_file.mustache index 617ff59..41995d2 100644 --- a/templates/fake_collection_file.mustache +++ b/templates/fake_collection_file.mustache @@ -1,4 +1,5 @@ {{=<% %>=}} +import 'package:faker_x/src/base/utility_resources/boolean.dart'; import 'package:faker_x/src/base/utility_resources/number.dart'; import 'package:faker_x/src/base/resources.dart'; import 'package:faker_x/src/base/locale.dart'; @@ -10,10 +11,12 @@ class FakeCollection { FakeCollection({required this.locale }): number = Number(), time = Time(), + boolean = Boolean(), <%assignings%>; final Number number; final Time time; + final Boolean boolean; <%#fields%> <%&.%> <%/fields%> diff --git a/test/manual/utility_resources/boolean_test.dart b/test/manual/utility_resources/boolean_test.dart new file mode 100644 index 0000000..e76d412 --- /dev/null +++ b/test/manual/utility_resources/boolean_test.dart @@ -0,0 +1,17 @@ +import 'package:faker_x/faker_x.dart'; +import 'package:test/test.dart'; + +import '../../utils.dart'; + +void main() { + test("boolean test random", () async { + final allResults = []; + for (var i = 0; i < testRepeatCount; i++) { + final result = FakerX.defaultInstance.boolean.random; + allResults.add(result); + assertFakeValue(result); + } + expect(allResults.contains(true), isTrue); + expect(allResults.contains(false), isTrue); + }); +}