From e59fff2419219720ea520f18e9c5acaa24a3cdac Mon Sep 17 00:00:00 2001 From: Oleksii Shtanko Date: Mon, 2 Dec 2024 23:15:21 +0000 Subject: [PATCH] Add unit tests --- Makefile | 18 ++++++------ test/function_test.dart | 61 ++++++++++++++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 4858e73..876ed39 100644 --- a/Makefile +++ b/Makefile @@ -1,37 +1,37 @@ -.PHONY: format doc analyze info test publish coverage code +.PHONY: format fmt doc d analyze a info i test t publish p coverage cvg code cd # Format the Dart code using dart_style package # https://pub.dev/packages/dart_style/install -format: +format fmt: dart format . # Generate documentation using dartdoc package # https://pub.dev/packages/dartdoc -doc: +doc d: dart doc . # Analyze the Dart code to find issues and warnings -analyze: +analyze a: dart analyze # Display information about the Dart project -info: +info f: dart info # Run all tests in the Dart project -test: +test t: dart test # Publish the Dart package to pub.dev -publish: +publish p: dart pub publish # Generate test coverage report using the coverage package -coverage: +coverage cvg: dart pub global run coverage:test_with_coverage # Count lines of code in the lib and test directories using cloc -code: +code cd: cloc lib test all: diff --git a/test/function_test.dart b/test/function_test.dart index 3c19a7d..51d258c 100644 --- a/test/function_test.dart +++ b/test/function_test.dart @@ -22,21 +22,62 @@ void main() { .callIfNotNull(['Hello']); // Should not throw or change state. }); - test('callWithOneArg - invokes a single-argument function if not null', () { - var capturedMessage = ''; - void singleArgFunction(String message) { - capturedMessage = message; + test('callWithOneArg should do nothing when the function is null', () { + // ignore: unnecessary_nullable_for_final_variable_declarations + const Function? func = null; + expect(() => func.callWithOneArg('test'), returnsNormally); + }); + + test( + 'callWithOneArg should do nothing when the function is not callable with one argument', + () { + // ignore: unnecessary_nullable_for_final_variable_declarations + void func() { + // ignore: avoid_print + print('No args'); + } + + expect(() => func.callWithOneArg('test'), returnsNormally); + }); + + test( + 'callWithOneArg should call the function with one argument when compatible', + () { + var called = false; + Object? receivedArg; + void func(Object? arg1) { + called = true; + receivedArg = arg1; + } + + // ignore: unnecessary_nullable_for_final_variable_declarations + final Function? nullableFunc = func; + nullableFunc.callWithOneArg('test'); + + expect(called, isTrue); + expect(receivedArg, 'test'); + }); + + test( + 'callWithOneArg should do nothing when the function expects more than one argument', + () { + void func(Object? arg1, Object? arg2) { + // Should not be called } // ignore: unnecessary_nullable_for_final_variable_declarations - final Function? nullableFunction = singleArgFunction; - nullableFunction.callWithOneArg('Test Message'); - expect(capturedMessage, ''); + final Function? nullableFunc = func; + expect(() => nullableFunc.callWithOneArg('test'), returnsNormally); }); - test('callWithOneArg - does nothing if the function is null', () { - const Function? nullableFunction = null; - nullableFunction.callWithOneArg('Should not crash'); // Should do nothing. + test('should do nothing when the function expects no arguments', () { + void func() { + // Should not be called + } + + // ignore: unnecessary_nullable_for_final_variable_declarations + final Function? nullableFunc = func; + expect(() => nullableFunc.callWithOneArg('test'), returnsNormally); }); test('callOrDefault - returns result when function is not null', () {