forAll<T> function

void forAll<T>(
  1. Arbitrary<T> arbitrary,
  2. FutureOr<void> block(
    1. T
    ), {
  3. int? maxExamples,
  4. int? maxTries,
  5. int? maxShrinkingTries,
  6. RandomContext? random,
  7. int? seed,
  8. GenerationPolicy? generationPolicy,
  9. ShrinkingPolicy? shrinkingPolicy,
  10. EdgeCasePolicy? edgeCasePolicy,
  11. FutureOr<void> setUp()?,
  12. FutureOr<void> tearDown()?,
  13. FutureOr<void> setUpAll()?,
  14. FutureOr<void> tearDownAll()?,
  15. FutureOr<void> onGenerate(
    1. T
    )?,
  16. FutureOr<void> onShrink(
    1. T
    )?,
  17. FutureOr<void> onFalsify(
    1. T
    )?,
  18. bool? ignoreFalsify,
})

Executes a test for all generated inputs that meet the specified properties, leveraging an arbitrary to produce a wide range of inputs.

Parameters:

  • arbitrary: An arbitrary instance used to generate test cases. This is where the type of input and how it's generated are defined.
  • block: The test block to execute with each generated input. This function is where you define the assertions or checks to validate the properties of your code.
  • maxExamples: The maximum number of examples to generate and test.
  • maxTries: The maximum number of attempts to find a valid example if the first try doesn't meet the property requirements.
  • maxShrinkingTries: The maximum number of attempts to shrink a failing example to its simplest form.
  • random: A custom random for generating examples.
  • seed: A seed value for the random to ensure reproducibility of tests.
  • generationPolicy: The policy for generating examples.
  • shrinkingPolicy: The policy for shrinking failing examples.
  • edgeCasePolicy: The policy for including or excluding edge cases in the generated examples.
  • setUp: A function to run before each test case.
  • tearDown: A function to run after each test case.
  • setUpAll: A function to run before all test cases.
  • tearDownAll: A function to run after all test cases.
  • onGenerate: A callback function that is called after each example is generated, allowing inspection of the generated input.
  • onShrink: A callback function that is called after each shrink operation, useful for logging or analysis.
  • onFalsify: A callback function that is called when a counterexample is found, before any shrinking has occurred.
  • ignoreFalsify: If set to true, the test will not be marked as failed even if a counterexample that falsifies the property is found. This allows the test to continue executing and attempting to verify the property with other examples, useful for logging or analysis purposes.

Implementation

void forAll<T>(
  Arbitrary<T> arbitrary,
  FutureOr<void> Function(T) block, {
  int? maxExamples,
  int? maxTries,
  int? maxShrinkingTries,
  RandomContext? random,
  int? seed,
  GenerationPolicy? generationPolicy,
  ShrinkingPolicy? shrinkingPolicy,
  EdgeCasePolicy? edgeCasePolicy,
  FutureOr<void> Function()? setUp,
  FutureOr<void> Function()? tearDown,
  FutureOr<void> Function()? setUpAll,
  FutureOr<void> Function()? tearDownAll,
  FutureOr<void> Function(T)? onGenerate,
  FutureOr<void> Function(T)? onShrink,
  FutureOr<void> Function(T)? onFalsify,
  bool? ignoreFalsify,
}) {
  final property = StatelessProperty(
    arbitrary: arbitrary as ArbitraryInternal<T>,
    settings: PropertySettings<T>(
      maxExamples: maxExamples,
      maxTries: maxTries,
      maxShrinkingTries: maxShrinkingTries,
      random: random,
      seed: seed,
      generationPolicy: generationPolicy,
      shrinkingPolicy: shrinkingPolicy,
      edgeCasePolicy: edgeCasePolicy,
      onGenerate: onGenerate,
      onShrink: onShrink,
      onFalsify: onFalsify,
      ignoreFalsify: ignoreFalsify,
    ),
    block: block,
    setUp: setUp,
    tearDown: tearDown,
    setUpAll: setUpAll,
    tearDownAll: tearDownAll,
  );
  PropertyTestManager.addProperty(property);
}