mirror of
https://github.com/hanskokx/arcane_helper_utils.git
synced 2026-05-14 02:19:09 +02:00
v1.3.1
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import "package:arcane_helper_utils/arcane_helper_utils.dart";
|
||||
import "package:test/test.dart";
|
||||
|
||||
void main() {
|
||||
group("StartAndEndOfPeriod", () {
|
||||
test("startOfHour returns correct DateTime", () {
|
||||
final dateTime = DateTime(2023, 1, 1, 12, 30, 45);
|
||||
final result = dateTime.startOfHour;
|
||||
expect(result, DateTime(2023, 1, 1, 12, 0, 0));
|
||||
});
|
||||
|
||||
test("endOfHour returns correct DateTime", () {
|
||||
final dateTime = DateTime(2023, 1, 1, 12, 30, 45);
|
||||
final result = dateTime.endOfHour;
|
||||
expect(result, DateTime(2023, 1, 1, 12, 59, 59, 999, 999));
|
||||
});
|
||||
|
||||
test("startOfDay returns correct DateTime", () {
|
||||
final dateTime = DateTime(2023, 1, 1, 12, 30, 45);
|
||||
final result = dateTime.startOfDay;
|
||||
expect(result, DateTime(2023, 1, 1));
|
||||
});
|
||||
|
||||
test("endOfDay returns correct DateTime", () {
|
||||
final dateTime = DateTime(2023, 1, 1, 12, 30, 45);
|
||||
final result = dateTime.endOfDay;
|
||||
expect(result, DateTime(2023, 1, 1, 23, 59, 59, 999, 999));
|
||||
});
|
||||
});
|
||||
|
||||
group("DaysInMonth", () {
|
||||
test("returns correct days for regular months", () {
|
||||
expect(DateTime(2023, 1, 1).daysInMonth, 31); // January
|
||||
expect(DateTime(2023, 3, 1).daysInMonth, 31); // March
|
||||
expect(DateTime(2023, 4, 1).daysInMonth, 30); // April
|
||||
expect(DateTime(2023, 5, 1).daysInMonth, 31); // May
|
||||
expect(DateTime(2023, 6, 1).daysInMonth, 30); // June
|
||||
expect(DateTime(2023, 7, 1).daysInMonth, 31); // July
|
||||
expect(DateTime(2023, 8, 1).daysInMonth, 31); // August
|
||||
expect(DateTime(2023, 9, 1).daysInMonth, 30); // September
|
||||
expect(DateTime(2023, 10, 1).daysInMonth, 31); // October
|
||||
expect(DateTime(2023, 11, 1).daysInMonth, 30); // November
|
||||
expect(DateTime(2023, 12, 1).daysInMonth, 31); // December
|
||||
});
|
||||
|
||||
test("returns correct days for February in leap year", () {
|
||||
expect(DateTime(2020, 2, 1).daysInMonth, 29);
|
||||
});
|
||||
|
||||
test("returns correct days for February in non-leap year", () {
|
||||
expect(DateTime(2023, 2, 1).daysInMonth, 28);
|
||||
});
|
||||
});
|
||||
|
||||
group("IsToday", () {
|
||||
test("returns true for current date", () {
|
||||
final now = DateTime.now();
|
||||
final today = DateTime(now.year, now.month, now.day);
|
||||
expect(today.isToday, true);
|
||||
});
|
||||
|
||||
test("returns false for past date", () {
|
||||
final yesterday = DateTime.now().subtract(const Duration(days: 1));
|
||||
expect(yesterday.isToday, false);
|
||||
});
|
||||
|
||||
test("returns false for future date", () {
|
||||
final tomorrow = DateTime.now().add(const Duration(days: 1));
|
||||
expect(tomorrow.isToday, false);
|
||||
});
|
||||
});
|
||||
|
||||
group("YesterdayAndTomorrow", () {
|
||||
test("yesterday returns one day before current date", () {
|
||||
final now = DateTime.now();
|
||||
final expectedYesterday = DateTime(now.year, now.month, now.day - 1);
|
||||
expect(DateTime.now().yesterday, expectedYesterday);
|
||||
});
|
||||
|
||||
test("yesterday handles start of month correctly", () {
|
||||
final now = DateTime.now();
|
||||
final expected = DateTime(now.year, now.month, now.day)
|
||||
.subtract(const Duration(days: 1));
|
||||
expect(DateTime.now().yesterday, expected);
|
||||
});
|
||||
|
||||
test("tomorrow returns one day after current date", () {
|
||||
final now = DateTime.now();
|
||||
final expectedTomorrow = DateTime(now.year, now.month, now.day + 1);
|
||||
expect(DateTime.now().tomorrow, expectedTomorrow);
|
||||
});
|
||||
|
||||
test("tomorrow handles end of month correctly", () {
|
||||
final now = DateTime.now();
|
||||
final expected =
|
||||
DateTime(now.year, now.month, now.day).add(const Duration(days: 1));
|
||||
expect(DateTime.now().tomorrow, expected);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import "dart:async";
|
||||
|
||||
import "package:arcane_helper_utils/arcane_helper_utils.dart";
|
||||
import "package:test/test.dart";
|
||||
|
||||
void main() {
|
||||
group("DynamicPrintExtension", () {
|
||||
late List<String> printLog;
|
||||
late ZoneSpecification spec;
|
||||
late Zone testZone;
|
||||
|
||||
setUp(() {
|
||||
// Create a buffer to store print output
|
||||
printLog = [];
|
||||
|
||||
// Create a custom zone specification that captures print output
|
||||
spec = ZoneSpecification(
|
||||
print: (_, __, ___, String msg) {
|
||||
printLog.add(msg);
|
||||
},
|
||||
);
|
||||
|
||||
// Create a test zone with the custom specification
|
||||
testZone = Zone.current.fork(specification: spec);
|
||||
});
|
||||
|
||||
test("printValue prints and returns the value without label", () {
|
||||
testZone.run(() {
|
||||
const testValue = "test string";
|
||||
// Ignore deprecation warning in test
|
||||
// ignore: deprecated_member_use_from_same_package
|
||||
final result = testValue.printValue<String>();
|
||||
|
||||
expect(result, equals(testValue));
|
||||
expect(printLog, hasLength(1));
|
||||
expect(printLog.first, equals(testValue));
|
||||
});
|
||||
});
|
||||
|
||||
test("printValue prints and returns the value with label", () {
|
||||
testZone.run(() {
|
||||
const testValue = 42;
|
||||
const label = "number";
|
||||
// ignore: deprecated_member_use_from_same_package
|
||||
final result = testValue.printValue<int>(label);
|
||||
|
||||
expect(result, equals(testValue));
|
||||
expect(printLog, hasLength(1));
|
||||
expect(printLog.first, equals("$label: $testValue"));
|
||||
});
|
||||
});
|
||||
|
||||
test("printValue works with null values", () {
|
||||
testZone.run(() {
|
||||
const String? testValue = null;
|
||||
// ignore: deprecated_member_use_from_same_package
|
||||
final result = testValue.printValue<String?>("nullable");
|
||||
|
||||
expect(result, isNull);
|
||||
expect(printLog, hasLength(1));
|
||||
expect(printLog.first, equals("nullable: null"));
|
||||
});
|
||||
});
|
||||
|
||||
test("printValue works with complex objects", () {
|
||||
testZone.run(() {
|
||||
final testValue = {"key": "value"};
|
||||
// ignore: deprecated_member_use_from_same_package
|
||||
final result = testValue.printValue<Map<String, String>>();
|
||||
|
||||
expect(result, equals(testValue));
|
||||
expect(printLog, hasLength(1));
|
||||
expect(printLog.first, equals(testValue.toString()));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import "package:arcane_helper_utils/arcane_helper_utils.dart";
|
||||
import "package:test/test.dart";
|
||||
|
||||
void main() {
|
||||
group("Unique", () {
|
||||
test("removes duplicates from simple list", () {
|
||||
final list = [1, 2, 2, 3, 3, 3];
|
||||
expect(list.unique(), [1, 2, 3]);
|
||||
});
|
||||
|
||||
test("removes duplicates using custom identifier", () {
|
||||
final list = [
|
||||
{"id": 1, "name": "John"},
|
||||
{"id": 2, "name": "Jane"},
|
||||
{"id": 1, "name": "John Copy"},
|
||||
];
|
||||
final result = list.unique((item) => item["id"]);
|
||||
expect(result.length, 2);
|
||||
expect(result.map((e) => e["id"]).toList(), [1, 2]);
|
||||
});
|
||||
|
||||
test("handles empty list", () {
|
||||
final List<int> list = [];
|
||||
expect(list.unique(), []);
|
||||
});
|
||||
|
||||
test("preserves order of elements", () {
|
||||
final list = [3, 1, 2, 1, 3];
|
||||
expect(list.unique(), [3, 1, 2]);
|
||||
});
|
||||
});
|
||||
|
||||
group("ListNullability", () {
|
||||
test("isNullOrEmpty returns true for null list", () {
|
||||
List<int>? list;
|
||||
expect(list.isNullOrEmpty, true);
|
||||
});
|
||||
|
||||
test("isNullOrEmpty returns true for empty list", () {
|
||||
final List<int> list = [];
|
||||
expect(list.isNullOrEmpty, true);
|
||||
});
|
||||
|
||||
test("isNullOrEmpty returns false for non-empty list", () {
|
||||
final list = [1, 2, 3];
|
||||
expect(list.isNullOrEmpty, false);
|
||||
});
|
||||
|
||||
test("isNotNullOrEmpty returns opposite of isNullOrEmpty", () {
|
||||
List<int>? nullList;
|
||||
final emptyList = <int>[];
|
||||
final nonEmptyList = [1, 2, 3];
|
||||
|
||||
expect(nullList.isNotNullOrEmpty, false);
|
||||
expect(emptyList.isNotNullOrEmpty, false);
|
||||
expect(nonEmptyList.isNotNullOrEmpty, true);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import "package:arcane_helper_utils/arcane_helper_utils.dart";
|
||||
import "package:test/test.dart";
|
||||
|
||||
void main() {
|
||||
// Example JWT token for testing
|
||||
const String validToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
|
||||
"eyJzdWIiOiJ0ZXN0QGV4YW1wbGUuY29tIiwiZXhwIjoxNzM1Njg5NjAwLCJ1aWQiOiIxMjM0NTYifQ."
|
||||
"signature";
|
||||
|
||||
group("JWTUtility", () {
|
||||
test("jwtEmail extracts email correctly", () {
|
||||
expect(validToken.jwtEmail(), "test@example.com");
|
||||
});
|
||||
|
||||
test("jwtEmail returns null for invalid token", () {
|
||||
expect("invalid.token".jwtEmail(), null);
|
||||
expect("".jwtEmail(), null);
|
||||
});
|
||||
|
||||
test("jwtExpiryTime extracts expiry time correctly", () {
|
||||
final DateTime? expiry = validToken.jwtExpiryTime();
|
||||
expect(expiry, isNotNull);
|
||||
expect(expiry?.year, 2025); // Based on the exp value in the test token
|
||||
});
|
||||
|
||||
test("jwtExpiryTime returns null for invalid token", () {
|
||||
expect("invalid.token".jwtExpiryTime(), null);
|
||||
expect("".jwtExpiryTime(), null);
|
||||
});
|
||||
|
||||
test("jwtUserId extracts user ID correctly", () {
|
||||
expect(validToken.jwtUserId(), "123456");
|
||||
});
|
||||
|
||||
test("jwtUserId returns null for invalid token", () {
|
||||
expect("invalid.token".jwtUserId(), null);
|
||||
expect("".jwtUserId(), null);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import "package:arcane_helper_utils/arcane_helper_utils.dart";
|
||||
import "package:test/test.dart";
|
||||
|
||||
void main() {
|
||||
group("CommonString", () {
|
||||
test("contains correct constant values", () {
|
||||
expect(CommonString.emDash, "—");
|
||||
expect(CommonString.bulletPoint, "•");
|
||||
expect(CommonString.nbsp, "\u00A0");
|
||||
});
|
||||
});
|
||||
|
||||
group("String Nullability", () {
|
||||
test("isNotNullOrEmpty returns correct values", () {
|
||||
String? nullString;
|
||||
expect(nullString.isNotNullOrEmpty, false);
|
||||
expect("".isNotNullOrEmpty, false);
|
||||
expect(" ".isNotNullOrEmpty, false);
|
||||
expect("text".isNotNullOrEmpty, true);
|
||||
});
|
||||
|
||||
test("isNullOrEmpty returns correct values", () {
|
||||
String? nullString;
|
||||
expect(nullString.isNullOrEmpty, true);
|
||||
expect("".isNullOrEmpty, true);
|
||||
expect(" ".isNullOrEmpty, true);
|
||||
expect("text".isNullOrEmpty, false);
|
||||
});
|
||||
});
|
||||
|
||||
group("String Split", () {
|
||||
test("splitByLength splits string correctly", () {
|
||||
expect("abcdef".splitByLength(2), ["ab", "cd", "ef"]);
|
||||
expect("abcde".splitByLength(2), ["ab", "cd", "e"]);
|
||||
expect("abcd".splitByLength(4), ["abcd"]);
|
||||
expect("abc".splitByLength(4), ["abc"]);
|
||||
expect("".splitByLength(2), []);
|
||||
});
|
||||
|
||||
test("handles edge cases", () {
|
||||
expect("a".splitByLength(1), ["a"]);
|
||||
expect("abc".splitByLength(10), ["abc"]);
|
||||
expect("\u00A0".splitByLength(1), ["\u00A0"]);
|
||||
});
|
||||
});
|
||||
|
||||
group("String TextManipulation", () {
|
||||
test("capitalize handles various cases", () {
|
||||
expect("hello".capitalize, "Hello");
|
||||
expect("HELLO".capitalize, "Hello");
|
||||
expect("h".capitalize, "H");
|
||||
expect("".capitalize, null);
|
||||
String? nullString;
|
||||
expect(nullString.capitalize, null);
|
||||
});
|
||||
|
||||
test("capitalizeWords handles various cases", () {
|
||||
expect("hello world".capitalizeWords, "Hello World");
|
||||
expect("HELLO WORLD".capitalizeWords, "Hello World");
|
||||
expect("hello".capitalizeWords, "Hello");
|
||||
expect("".capitalizeWords, null);
|
||||
String? nullString;
|
||||
expect(nullString.capitalizeWords, null);
|
||||
});
|
||||
|
||||
test("spacePascalCase handles various cases", () {
|
||||
expect("HelloWorld".spacePascalCase, "Hello World");
|
||||
expect("ABC".spacePascalCase, "A B C");
|
||||
expect("helloWorld".spacePascalCase, "hello World");
|
||||
expect("".spacePascalCase, null);
|
||||
String? nullString;
|
||||
expect(nullString.spacePascalCase, null);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user