-
Notifications
You must be signed in to change notification settings - Fork 70
/
FOXStateMachineGenerators.m
59 lines (50 loc) · 1.94 KB
/
FOXStateMachineGenerators.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#import "FOXStateMachineGenerators.h"
#import "FOXGenerator.h"
#import "FOXCoreGenerators.h"
#import "FOXStateMachine.h"
#import "FOXArrayGenerators.h"
#import "FOXStateTransition.h"
#import "FOXRoseTree.h"
#import "FOXCommand.h"
#import "FOXExecutedCommand.h"
/**
Returns a generator of commands to execute from a given state machine.
*/
FOX_EXPORT id<FOXGenerator> FOXGenCommands(id<FOXStateMachine> stateMachine) {
return FOXGenBind(FOXElements([stateMachine allTransitions]), ^id<FOXGenerator>(FOXRoseTree *generatorTree) {
id<FOXStateTransition> transition = generatorTree.value;
id<FOXGenerator> argGenerator = nil;
if ([transition respondsToSelector:@selector(generator)]) {
argGenerator = [transition generator];
} else {
argGenerator = FOXReturn(@[]);
}
return FOXMap(FOXTuple(@[FOXReturn(transition), argGenerator]), ^id(NSArray *commandTuple) {
return [[FOXCommand alloc] initWithTransition:commandTuple[0] generatedValue:commandTuple[1]];
});
});
}
FOX_EXPORT id<FOXGenerator> FOXCommands(id<FOXStateMachine> stateMachine) {
return FOXSuchThat(FOXNonEmptyArray(FOXGenCommands(stateMachine)), ^BOOL(NSArray *commands) {
return [stateMachine isValidCommandSequence:commands];
});
}
/**
Returns a generator of executed commands using a given state machine and subject factory.
*/
FOX_EXPORT id<FOXGenerator> FOXExecuteCommands(id<FOXStateMachine> stateMachine, id (^subject)(void)) {
return FOXMap(FOXCommands(stateMachine), ^id(NSArray *commands) {
return [stateMachine executeCommandSequence:commands subject:subject()];
});
}
/**
Verifies if a given array of executed commands completed successfully.
*/
FOX_EXPORT BOOL FOXExecutedSuccessfully(NSArray *executedCommands) {
for (FOXExecutedCommand *cmd in executedCommands) {
if (![cmd wasSuccessfullyExecuted]) {
return NO;
}
}
return YES;
}