Skip to content

Instantly share code, notes, and snippets.

@tonyhallett
Last active March 27, 2019 12:49
Show Gist options
  • Save tonyhallett/fe543fcb4d55ddd58513b68d4dfacdda to your computer and use it in GitHub Desktop.
Save tonyhallett/fe543fcb4d55ddd58513b68d4dfacdda to your computer and use it in GitHub Desktop.
Resolved your issue with RuleRegister test requiring implementation of Rule
Sorry do not have time to fork and pull.
----------------------------------------------------------
rule-register.ts to change to:
//exported for testing
export class RegisterFactory<T>{
setUpForRegister(){
const implementations: Array<Constructor<T>>=[];
return {
register(ctor: Constructor<T>) {
implementations.push(ctor);
},
implementations,
};
}
}
export class RuleRegisterFactory extends RegisterFactory<Rule>{}
const registerFactory=new RuleRegisterFactory();
const setUp=registerFactory.setUpForRegister()
export const rule=setUp.register;
export const implementations=setUp.implementations;`
---------------------------------------------------------------------------
change the class decorators on all Rule classes:
e.g
import { rule } from './rule-register';
...
@rule
export class Husky {...
-------------------------------------------
list-rules.ts to change
import { implementations } from './rules/rule-register';
...
export class ListRules {
static findRulesToApplyIn(rootPath: string): Rule[] {
return implementations
.map(rule => new rule(rootPath))
.filter(rule => rule.shouldBeApplied());
}
}
of course don't forget to construct these rules !
---------------------------------------------------------------
test the factory in rule-register.test
import {RegisterFactory} from '../../rules/rule-register'
interface IDummyRule{dummy:string}
class TestRegisterFactory extends RegisterFactory<IDummyRule>{}
const factory=new TestRegisterFactory();
const setUp=factory.setUpForRegister();
@setUp.register
class DummyRule implements IDummyRule{
dummy!: string;
}
describe('rule-register',()=>{
it('should magically work',()=>{
new DummyRule();
expect(setUp.implementations.length).toBe(1);
});
});
********************* I'd probably change the name from RegisterFactory to RegisterGenerator
Solution number 2 perhaps better
export interface Register<T>{
register(ctor: Constructor<T>):void
}
export class RegisterGenerator<T>{
getRegisterDecorator(reg:Register<T>){
return function register(ctor: Constructor<T>) {
reg.register(ctor);
}
};
}
export class RuleRegisterGenerator extends RegisterFactory<Rule>{}
var ruleRegisterGenerator=new RuleRegisterGenerator();
class RuleRegister implements Register<Rule>{
instances:Array<Constructor<Rule>>=[]
register(ctor: Constructor<Rule>): void {
this.instances.push(ctor);
}
}
describe('generator',()=>{
it('registers',()=>{
interface IRegistrable{
registerMe:true
}
class RegistrableRegister implements Register<IRegistrable>{
registered:Array<Constructor<IRegistrable>>=[];
register(ctor: Constructor<IRegistrable>): void {
this.registered.push(ctor);
}
}
class RegistrableGenerator extends RegisterGenerator<IRegistrable>{}
const registrableGenerator=new RegistrableGenerator();
const registrableRegister=new RegistrableRegister();
const register=registrableGenerator.getRegisterDecorator(registrableRegister);
@register
class Registrable implements IRegistrable{
registerMe: true=true
}
new Registrable();
expect(registrableRegister.registered.length).toBe(1);
expect(registrableRegister.registered[0]).toBe(Registrable);
})
})
now you can have this context
Solution number 3
export interface Register<T>{
implementations: Array<Constructor<T>>
register?(ctor: Constructor<T>):void
}
export class RegisterGenerator<T>{
setUpForRegister(reg:Register<T>){
return function register(ctor: Constructor<T>) {
if(!reg.implementations){
reg.implementations=[];
}
reg.implementations.push(ctor);
if(reg.register){
reg.register(ctor);
}
}
};
}
describe('generator',()=>{
it('registers',()=>{
interface IRegistrable{
registerMe:true
}
class RegistrableRegister{
implementations!:Array<Constructor<IRegistrable>>;
}
class RegistrableGenerator extends RegisterGenerator<IRegistrable>{}
const registrableGenerator=new RegistrableGenerator();
const registrableRegister=new RegistrableRegister();
const register=registrableGenerator.getRegisterDecorator(registrableRegister);
@register
class Registrable implements IRegistrable{
registerMe: true=true
}
new Registrable();
expect(registrableRegister.implementations.length).toBe(1);
expect(registrableRegister.implementations[0]).toBe(Registrable);
})
})
add test that register is called if you decide to keep it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment