Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtanko committed Dec 2, 2024
1 parent f1a1986 commit e59fff2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 19 deletions.
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
61 changes: 51 additions & 10 deletions test/function_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down

0 comments on commit e59fff2

Please sign in to comment.