Skip to content

Commit

Permalink
feat(actionbasedpolicy): allowing set and get conditionResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Oct 18, 2020
1 parent 3420d87 commit 10bee79
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 30 deletions.
26 changes: 26 additions & 0 deletions src/Policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,30 @@ describe('Policy Class', () => {
expect(policy.getContext()).toBe(context);
});
});

describe('when getConditionResolver', () => {
it('returns conditionResolver attribute', () => {
const conditionResolver = {
greaterThan: (data: number, expected: number): boolean => {
return data > expected;
}
};
expect(new Policy({ conditionResolver }).getConditionResolver()).toBe(
conditionResolver
);
});
});

describe('when setConditionResolver', () => {
it('sets conditionResolver attribute', () => {
const conditionResolver = {
greaterThan: (data: number, expected: number): boolean => {
return data > expected;
}
};
const policy = new Policy({});
policy.setConditionResolver(conditionResolver);
expect(policy.getConditionResolver()).toBe(conditionResolver);
});
});
});
18 changes: 13 additions & 5 deletions src/Policy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PolicyInterface, Context } from './types';
import { MatchConditionInterface, ConditionResolver, Context } from './types';

class Policy {
protected context: Context;
protected conditionResolver: ConditionResolver;

constructor({ context }: PolicyInterface) {
if (context) {
this.context = context;
}
constructor({ context, conditionResolver }: MatchConditionInterface) {
this.context = context;
this.conditionResolver = conditionResolver;
}

setContext(context: Context): void {
Expand All @@ -16,6 +16,14 @@ class Policy {
getContext(): Context {
return this.context;
}

setConditionResolver(conditionResolver: ConditionResolver): void {
this.conditionResolver = conditionResolver;
}

getConditionResolver(): ConditionResolver {
return this.conditionResolver;
}
}

export { Policy };
1 change: 1 addition & 0 deletions src/Statement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('Statement Class', () => {
describe('when match conditions', () => {
it('returns true', () => {
const firstStatementConfig = {
sid: 'first',
condition: {
greaterThan: { 'user.age': 30 }
}
Expand Down
4 changes: 0 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ export interface StatementInterface {
condition?: ConditionBlock;
}

export interface PolicyInterface {
context?: Context;
}

export interface DecomposeString {
start: number;
end: number;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ import { MemoizeInterface } from '../types';
* ```
*/
export function memoize(func: Function, resolver?: Function): MemoizeInterface {
const memoized = function(this: Function, ...args: any): MemoizeInterface {
const memoized = function (this: Function, ...args: any): MemoizeInterface {
const key = resolver ? resolver.apply(this, args) : args[0];
const cache = memoized.cache;

if (cache.has(key)) {
return cache.get(key);
}
const result = func.apply(this, args);
memoized.cache = cache.set(key, result) || cache;
cache.set(key, result);
return result;
};
memoized.cache = new Map();
Expand Down
54 changes: 36 additions & 18 deletions src/utils/mersenneTwister.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import { MersenneTwister } from './mersenneTwister';

describe('MersenneTwister', () => {
it('should repeat random sequence on same seed', () => {
const mersenne = new MersenneTwister();
describe('when using an empty array as seed', () => {
it('sets 5489 as seed', () => {
const mersenne = new MersenneTwister([]);

const seed = 123;
expect(mersenne.randomInt32()).toEqual(3499211612);
});
});

describe('when using empty with length greater or equal than 624', () => {
it('returns a MersenneTwister instance', () => {
expect(new MersenneTwister(new Array(624))).toBeInstanceOf(
MersenneTwister
);
});
});

describe('when using initSeed', () => {
it('repeats random sequence on same seed', () => {
const mersenne = new MersenneTwister();

mersenne.initSeed(seed);
const first1 = mersenne.randomReal2();
const first2 = mersenne.randomReal2();
const seed = 123;

mersenne.initSeed(seed);
const second1 = mersenne.randomReal2();
const second2 = mersenne.randomReal2();
mersenne.initSeed(seed);
const first1 = mersenne.randomReal2();
const first2 = mersenne.randomReal2();

expect(first1).toEqual(second1);
expect(first2).toEqual(second2);
mersenne.initSeed(seed);
const second1 = mersenne.randomReal2();
const second2 = mersenne.randomReal2();

expect(first1).toEqual(second1);
expect(first2).toEqual(second2);
});
});

it('should allow seeding via constructor', () => {
it('allows seeding via constructor', () => {
const seed = 325;
const mersenne1 = new MersenneTwister(seed);
const mersenne2 = new MersenneTwister(seed);
Expand All @@ -36,7 +54,7 @@ describe('MersenneTwister', () => {
Modify the mtTest.c file to try each function with the proper seed
*/
describe('when comparing with results from C language', () => {
it('should generate 100 Int32 values as C', () => {
it('generates 100 Int32 values as C', () => {
const seeds = [0x123, 0x234, 0x345, 0x456];
const mersenne = new MersenneTwister(seeds);

Expand Down Expand Up @@ -148,7 +166,7 @@ describe('MersenneTwister', () => {
}
});

it('should generate 100 Int31 values as C', () => {
it('generates 100 Int31 values as C', () => {
const seeds = [0x123, 0x234, 0x345, 0x456];
const mersenne = new MersenneTwister(seeds);

Expand Down Expand Up @@ -260,7 +278,7 @@ describe('MersenneTwister', () => {
}
});

it('should generate 100 Real1 values as C', () => {
it('generates 100 Real1 values as C', () => {
const seeds = [0x123, 0x234, 0x345, 0x456];
const mersenne = new MersenneTwister(seeds);

Expand Down Expand Up @@ -372,7 +390,7 @@ describe('MersenneTwister', () => {
}
});

it('should generate 100 Real2 values as C', () => {
it('generates 100 Real2 values as C', () => {
const seeds = [0x123, 0x234, 0x345, 0x456];
const mersenne = new MersenneTwister(seeds);

Expand Down Expand Up @@ -484,7 +502,7 @@ describe('MersenneTwister', () => {
}
});

it('should generate 100 Real3 values as C', () => {
it('generates 100 Real3 values as C', () => {
const seeds = [0x123, 0x234, 0x345, 0x456];
const mersenne = new MersenneTwister(seeds);

Expand Down Expand Up @@ -596,7 +614,7 @@ describe('MersenneTwister', () => {
}
});

it('should generate 100 Real3 values as C', () => {
it('generates 100 Real3 values as C', () => {
const seeds = [0x123, 0x234, 0x345, 0x456];
const mersenne = new MersenneTwister(seeds);

Expand Down
2 changes: 1 addition & 1 deletion src/utils/mersenneTwister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class MersenneTwister {
this.mti = this.N + 1; /* mti==N+1 means mt[N] is not initialized */

if (Array.isArray(seed)) {
this.initByArray(seed, seed.length);
if (seed.length > 0) this.initByArray(seed, seed.length);
} else {
if (seed === undefined) {
this.initSeed(new Date().getTime());
Expand Down

0 comments on commit 10bee79

Please sign in to comment.