TestUnit class

Provide methods of testing units.

Example:

Branch -------------

class Branch extends Unit<BranchPaper> {
  Branch(super.agent, {super.listener});

  @override
  Script<BranchPaper, UnitState<BranchPaper, Unit<BranchPaper>>>
      createScript() => BranchScript();

  @override
  UnitState<BranchPaper, Unit<BranchPaper>> createState() => BranchState();
}

class BranchState extends UnitState<BranchPaper, Branch> {
  final bigLeaf = UnitAgent<LeafPaper>();
  final smallLeaf = UnitAgent<LeafPaper>();

  String? smallLeafColor;

  @override
  Set<Agent> register() => {
        bigLeaf.log((a) => Leaf(a)),
        smallLeaf.log((a) => Leaf(a, listener: smallLeafListener)),
      };

  PaperListener<LeafPaper, BranchPaper> get smallLeafListener => PaperListener(
        (r) => r.on<LeafColor>((p) {
          return BranchLeafColor((p.color));
        }),
      );
}

class BranchScript extends Script<BranchPaper, BranchState> {
  @override
  void map() => on<BranchLeafColor>(onLeafColor);

  void onLeafColor(
    BranchLeafColor p,
    BranchState s,
    SourceVerifier ifFrom,
  ) async {
    await s.bigLeaf.process(LeafColor(p.color));

    final childPaper = await s.smallLeaf.report<LeafColor>();
    s.smallLeafColor = childPaper?.color;
  }
}

abstract class BranchPaper extends Paper {}

class BranchLeafColor extends BranchPaper {
  BranchLeafColor(this.color);

  final String color;
}

Leaf -------------

class Leaf extends Unit<LeafPaper> {
  Leaf(super.agent, {super.listener});

  @override
  Script<LeafPaper, UnitState<LeafPaper, Unit<LeafPaper>>> createScript() =>
      LeafScript();

  @override
  UnitState<LeafPaper, Unit<LeafPaper>> createState() => LeafState();
}

class LeafState extends UnitState<LeafPaper, Leaf> {
  String color = 'green';
}

class LeafScript extends Script<LeafPaper, LeafState> {
  @override
  void map() => on<LeafColor>(onColor, onReportColor);

  void onColor(
    LeafColor p,
    LeafState s,
    SourceVerifier ifFrom,
  ) async {
    /// Implement function's body
  }

  LeafColor onReportColor(LeafState s) {
    return LeafColor(s.color);
  }
}

abstract class LeafPaper extends Paper {}

class LeafColor extends LeafPaper {
  LeafColor(this.color);

  final String color;
}

Unit testing

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:paper/paper.dart';

void main(){
  TestUnit.toProcessPaper<Branch, BranchState>(
    'testing [BranchLeafColor]',
    unit: Branch(UnitAgent<BranchPaper>()),
    setup: (state, agentValidator) {
      when(agentValidator.report(state.smallLeaf, LeafColor))
        .thenAnswer((_) async => LeafColor('yellow'));
    },
    paper: BranchLeafColor('red'),
    test: (state, agentValidator) {
      verify(
        agentValidator.process(
          state.bigLeaf,
          argThat(isA<LeafColor>().having((e) => e.color, 'color', 'red')),
        ),
      ).called(1);
      expect(state.smallLeafColor, 'yellow');
    },
  );

  TestUnit.toReceivePaper<BranchState>(
    'testing [LeafColor] from [state.smallLeaf]',
    unit: Branch(UnitAgent<BranchPaper>()),
    paper: LeafColor('red'),
    from: (state) => state.smallLeaf,
    expected: isA<BranchLeafColor>().having(
      (e) => e.color,
      'color',
      'red',
    ),
  );
}

Constructors

TestUnit()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

toProcessPaper<U extends Unit<Paper>, S extends UnitState<Paper, U>>(String description, {required U unit, void setup(S state, AgentValidator agentValidator)?, required Paper? paper, required void test(S state, AgentValidator agentValidator), String? testOn, Timeout? timeout, dynamic skip, dynamic tags, Map<String, dynamic>? onPlatform, int? retry}) → void
Create a test to verify the operations when U unit handles paper with the given description. U is type the of target unit. S is the type of corresponding state of U.
toReceivePaper<U extends Unit<Paper>, S extends UnitState<Paper, U>>(String description, {required Unit<Paper> unit, void setup(S state, AgentValidator agentValidator)?, required Paper paper, required Agent<Paper> from(S state), required dynamic expected, String? testOn, Timeout? timeout, dynamic skip, dynamic tags, Map<String, dynamic>? onPlatform, int? retry}) → void
Create a test to verify the operations when U unit receive paper that reported by from with the given description. U is type the of target unit. S is the type of corresponding state of U.