TestCollective<E, C extends Collective<E>>.create constructor

TestCollective<E, C extends Collective<E>>.create({
  1. bool elementDisallow(
    1. E? e,
    2. C collective
    )?,
  2. Iterable<Function> actionDisallow<C>(
    1. C collective
    )?,
  3. int maxLength = TestCollective.asBigAsDartAllows,
})

Factory constructor for creating a TestCollective with common validation scenarios.

Parameters:

  • elementDisallow: Optional function to reject specific elements
  • actionDisallow: Optional function to reject specific actions
  • maxLength: Maximum allowed collection size (use asBigAsDartAllows for no limit)

Implementation

factory TestCollective.create({
  bool Function(E? e, C collective)? elementDisallow,
  Iterable<Function> Function<C>(C collective)? actionDisallow,
  int maxLength = TestCollective.asBigAsDartAllows
}) {

  final rules = <TestRule>[];

  if (elementDisallow != null) {
    elementRule(element, base, {action, user}) {
      return elementDisallow(element, base) ? true : false;
    }
    final rule = TestCollectiveElementRule<E,C>(rule: elementRule);
    rules.add(rule);
  }

  if (actionDisallow != null) {
    actionRuleFunc(action, base, {arguments, user}) {
      return actionDisallow(base).contains(action) ? true : false;
    }
    final rule = TestActionRule<C>(rule: actionRuleFunc);
    rules.add(rule);
  }

  if (maxLength != TestCollective.asBigAsDartAllows) {
    actionRuleFunc(Function action, C collective, {Arguments? arguments, user}) {
      if (collective is Set) {
        final modifiable = <Function>[(collective as Set).add, (collective as Set).addAll];
        return modifiable.contains(action) && collective.length >= maxLength ? true : false;
      } else if (collective is List) {
        final list = collective as List;
        final modifiable = <Function>[list.add, list.addAll, list.insert, list.insertAll];
        return modifiable.contains(action) && collective.length >= maxLength ? true : false;
      } else if (collective is Queue) {
        final modifiable = <Function>[(collective as Queue).add, (collective as Queue).addAll];
        return modifiable.contains(action) && collective.length >= maxLength ? true : false;
      }
      return false;
    }
    final rule = TestActionRule<C>(rule: actionRuleFunc);
    rules.add(rule);
  }

  if (rules.isNotEmpty) {
    return TestCollective<E,C>(rules: rules);
  }

  return const TestCollectiveTrue();
}