From 96ed242c6d64f02d206359d681b9f9995d5aa798 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Wed, 10 May 2023 00:24:08 +0000 Subject: [PATCH] Add error for empty groups Closes #1961 After declaring a group check for declared tests, if there were none add a synthetic test that fails with an error about the empty group. --- pkgs/test/CHANGELOG.md | 1 + pkgs/test_api/CHANGELOG.md | 1 + pkgs/test_api/lib/src/backend/declarer.dart | 6 ++++++ pkgs/test_api/test/backend/declarer_test.dart | 11 +++++++++++ pkgs/test_core/CHANGELOG.md | 1 + 5 files changed, 20 insertions(+) diff --git a/pkgs/test/CHANGELOG.md b/pkgs/test/CHANGELOG.md index 8c0254eca..5038c6fcf 100644 --- a/pkgs/test/CHANGELOG.md +++ b/pkgs/test/CHANGELOG.md @@ -1,6 +1,7 @@ ## 1.24.3-wip * Fix compatibility with wasm number semantics. +* Consider empty `group` to be test failures. ## 1.24.2 diff --git a/pkgs/test_api/CHANGELOG.md b/pkgs/test_api/CHANGELOG.md index 4d79f0a59..972cd189e 100644 --- a/pkgs/test_api/CHANGELOG.md +++ b/pkgs/test_api/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.5.3-wip * Fix compatibility with wasm number semantics. +* Consider empty `group` to be test failures. ## 0.5.2 diff --git a/pkgs/test_api/lib/src/backend/declarer.dart b/pkgs/test_api/lib/src/backend/declarer.dart index 7a37552c6..999e3be13 100644 --- a/pkgs/test_api/lib/src/backend/declarer.dart +++ b/pkgs/test_api/lib/src/backend/declarer.dart @@ -13,6 +13,7 @@ import 'group_entry.dart'; import 'invoker.dart'; import 'metadata.dart'; import 'test.dart'; +import 'test_failure.dart'; /// A class that manages the state of tests as they're declared. /// @@ -327,6 +328,11 @@ class Declarer { } return entry; }).toList(); + if (entries.isEmpty) { + entries.add(LocalTest(_name ?? 'Empty group', _metadata, () { + throw TestFailure('No tests declared in group'); + })); + } return Group(_name ?? '', entries, metadata: _metadata, diff --git a/pkgs/test_api/test/backend/declarer_test.dart b/pkgs/test_api/test/backend/declarer_test.dart index 0fe848a05..23e736d89 100644 --- a/pkgs/test_api/test/backend/declarer_test.dart +++ b/pkgs/test_api/test/backend/declarer_test.dart @@ -410,6 +410,17 @@ void main() { }); }); + test('disallows empty groups', () async { + var entries = declare(() { + group('group', () {}); + }); + + expect(entries, hasLength(1)); + var testGroup = entries.single as Group; + expect(testGroup.entries, hasLength(1)); + await _runTest(testGroup.entries.single as Test, shouldFail: true); + }); + group('.setUp()', () { test('is scoped to the group', () async { var setUpRun = false; diff --git a/pkgs/test_core/CHANGELOG.md b/pkgs/test_core/CHANGELOG.md index 2f6be0845..bc456b2d0 100644 --- a/pkgs/test_core/CHANGELOG.md +++ b/pkgs/test_core/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.5.3-wip * Fix compatibility with wasm number semantics. +* Consider empty `group` to be test failures. ## 0.5.2