Skip to content

Commit

Permalink
Polish whenPushSatisfies
Browse files Browse the repository at this point in the history
  • Loading branch information
Rod Johnson committed May 17, 2018
1 parent 6e19209 commit f06ebb9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
18 changes: 11 additions & 7 deletions src/blueprint/dsl/pushTestPredicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@
* limitations under the License.
*/

import { isBoolean } from "util";
import { PushToValue } from "../../common/listener/PushMapping";
import { PushTest } from "../../common/listener/PushTest";
import { isPushMapping, pushTest } from "../../index";

/**
* Predicate that can be used in our PushTest DSL
* Predicate that can be used in our PushTest DSL.
* Can be a PushTest, a function or computed boolean
*/
export type PushTestPredicate = PushTest | boolean | (() => (boolean | Promise<boolean>));
export type PushTestPredicate = PushTest | PushToValue<boolean> | boolean | (() => (boolean | Promise<boolean>));

export function toPushTest(p: PushTestPredicate): PushTest {
return isPushMapping(p) ? p :
isBoolean(p) ?
pushTest(p + "", async () => p) :
pushTest(p + "", async () => p()) ;
if (isPushMapping(p)) {
return p;
}
if (typeof p === "boolean") {
return pushTest(p + "", async () => p);
}
return pushTest(p + "", p as any);
}
26 changes: 24 additions & 2 deletions src/common/listener/PushMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ export type NeverMatch = null;
*/
export const DoNotSetAnyGoals: NeverMatch = null;

/**
* Result of a PushMapping. A result of the desired type if
* we match. undefined if we don't match.
* Return DoNotSetAnyGoals (null) to shortcut evaluation of the present set of rules,
* terminating evaluation and guarantee the return of undefined if we've reached this point.
* Only do so if you are sure
*/
export type PushMappingResult<V> = Promise<V | undefined | NeverMatch>;

/**
* Compute a value for the given push. Return undefined
* if we don't find a mapped value.
* Return DoNotSetAnyGoals (null) to shortcut evaluation of the present set of rules,
* terminating evaluation and guarantee the return of undefined if we've reached this point.
* Only do so if you are sure
* that this evaluation must be short circuited if it has reached this point.
* If a previous rule has matched, it will still be used.
* The value may be static
* or computed on demand, depending on the implementation.
* @param {PushListenerInvocation} p
*/
export type PushToValue<V> = (p: PushListenerInvocation) => PushMappingResult<V>;

/**
* Mapping from push to value, id it can be resolved.
* This is a central interface used throughout the SDM.
Expand All @@ -46,9 +69,8 @@ export interface PushMapping<V> {
* The value may be static
* or computed on demand, depending on the implementation.
* @param {PushListenerInvocation} p
* @return {Promise<V | undefined | NeverMatch>}
*/
valueForPush(p: PushListenerInvocation): Promise<V | undefined | NeverMatch>;
valueForPush: PushToValue<V>;
}

export function isPushMapping(a: any): a is PushMapping<any> {
Expand Down
13 changes: 12 additions & 1 deletion test/blueprint/dsl/whenPushSatisfiesTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/

import * as assert from "power-assert";
import { HttpServiceGoals, whenPushSatisfies } from "../../../src";
import { whenPushSatisfies } from "../../../src/blueprint/dsl/goalDsl";
import { HttpServiceGoals } from "../../../src/common/delivery/goals/common/httpServiceGoals";
import { FalsePushTest, TruePushTest } from "../../common/listener/support/pushTestUtilsTest";
import { fakePush } from "./decisionTreeTest";

Expand Down Expand Up @@ -65,4 +66,14 @@ describe("whenPushSatisfies", () => {
const test = whenPushSatisfies(TruePushTest, FalsePushTest).setGoals(HttpServiceGoals);
assert.equal(test.name, TruePushTest.name + " && " + FalsePushTest.name);
});

it("should allow simple function", async () => {
const test = whenPushSatisfies(async p => true).setGoals(HttpServiceGoals);
assert.equal(await test.valueForPush(fakePush()), HttpServiceGoals);
});

it("should allow simple function returning false", async () => {
const test = whenPushSatisfies(async p => p.push.id === "notThis").setGoals(HttpServiceGoals);
assert.equal(await test.valueForPush(fakePush()), undefined);
});
});

0 comments on commit f06ebb9

Please sign in to comment.