Skip to content

Commit

Permalink
Complete builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ekarademir committed Oct 13, 2023
1 parent 5ab5d29 commit 8441a77
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
26 changes: 21 additions & 5 deletions packages/concerto-analysis/src/compare-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,58 +100,74 @@ export class CompareConfigBuilder {
/**
* Adds default comparer configuration onto the configuration
* being built.
*
* @returns {CompareConfigBuilder} A reference to the builder object to chain
*/
public default() {
public default(): CompareConfigBuilder {
this._config = {
comparerFactories: [...this._config.comparerFactories, ...defaultCompareConfig.comparerFactories],
rules: { ...this._config.rules, ...defaultCompareConfig.rules },
};

return this;
}

/**
* Extends existing configuration that's built upto this point
* with the provided config.
*
* @param {CompareConfig} config - The configuration to extend with
* @returns {CompareConfigBuilder} A reference to the builder object to chain
*/
public extend(config: CompareConfig) {
public extend(config: CompareConfig): CompareConfigBuilder {
this._config = {
comparerFactories: [...this._config.comparerFactories, ...config.comparerFactories],
rules: { ...this._config.rules, ...config.rules },
};

return this;
}

/**
* Adds a comparison outcome rule to the configuration
*
* @param {string} ruleKey - A key that is referenced from one of the comparer factories
* @param {CompareResult} result - A version diff outcome based on this rule
* @returns {CompareConfigBuilder} A reference to the builder object to chain
*/
public addRule(ruleKey: string, result: CompareResult) {
public addRule(ruleKey: string, result: CompareResult): CompareConfigBuilder {
this._config.rules[ruleKey] = result;

return this;
}

/**
* Removes a comparison outcome rule from the configuration
*
* @param {string} ruleKey - A key that is referenced from one of the comparer factories
* @returns {CompareConfigBuilder} A reference to the builder object to chain
* @throws {ReferenceError}
* Thrown if the `ruleKey` does not exist in the configuration
*/
public removeRule(ruleKey: string) {
public removeRule(ruleKey: string): CompareConfigBuilder {
if (!this._config.rules[ruleKey]) {
throw new ReferenceError(`ruleKey '${ruleKey}' does not exist`);
}

delete this._config.rules[ruleKey];

return this;
}

/**
* Add a {@link ComparerFactory} to the configuration.
*
* @param {ComparerFactory} f - A {@link ComparerFactory} that should reference the rules in the configuration
* @returns {CompareConfigBuilder} A reference to the builder object to chain
*/
public addComparerFactory(f: ComparerFactory) {
public addComparerFactory(f: ComparerFactory): CompareConfigBuilder {
this._config.comparerFactories = [...this._config.comparerFactories, f];

return this;
}
}
22 changes: 6 additions & 16 deletions packages/concerto-analysis/test/unit/compare-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ describe('CompareConfigBuilder', () => {

it('Should add default config with `default`', () => {
const builder = new CompareConfigBuilder();
builder.default();

const actual = builder.build();
const actual = builder.default().build();

expect(actual.comparerFactories.length).toEqual(11);
expect(Object.keys(actual.rules).length).toEqual(20);
Expand All @@ -34,10 +33,8 @@ describe('CompareConfigBuilder', () => {
rules: newRules
};
const builder = new CompareConfigBuilder();
builder.default();
builder.extend(toExtend);

const actual = builder.build();
const actual = builder.default().extend(toExtend).build();

expect(actual.comparerFactories.length).toEqual(12);
expect(Object.keys(actual.rules).length).toEqual(21);
Expand All @@ -46,21 +43,17 @@ describe('CompareConfigBuilder', () => {

it('Should add a new comparer factory', () => {
const builder = new CompareConfigBuilder();
builder.default();
builder.addComparerFactory(() => ({}));

const actual = builder.build();
const actual = builder.default().addComparerFactory(() => ({})).build();

expect(actual.comparerFactories.length).toEqual(12);
expect(Object.keys(actual.rules).length).toEqual(20);
});

it('Should add a new rule', () => {
const builder = new CompareConfigBuilder();
builder.default();
builder.addRule('a-new-rule', CompareResult.MAJOR);

const actual = builder.build();
const actual = builder.default().addRule('a-new-rule', CompareResult.MAJOR).build();

expect(actual.comparerFactories.length).toEqual(11);
expect(Object.keys(actual.rules).length).toEqual(21);
Expand All @@ -69,10 +62,8 @@ describe('CompareConfigBuilder', () => {

it('Should remove an existing rule', () => {
const builder = new CompareConfigBuilder();
builder.default();
builder.removeRule('optional-property-added');

const actual = builder.build();
const actual = builder.default().removeRule('optional-property-added').build();

expect(actual.comparerFactories.length).toEqual(11);
expect(Object.keys(actual.rules).length).toEqual(19);
Expand All @@ -81,7 +72,6 @@ describe('CompareConfigBuilder', () => {

it('Should throw while removing a rule that does not exist', () => {
const builder = new CompareConfigBuilder();
builder.default();
expect(() => builder.removeRule('does-not-exist')).toThrow('ruleKey \'does-not-exist\' does not exist');
expect(() => builder.default().removeRule('does-not-exist')).toThrow('ruleKey \'does-not-exist\' does not exist');
});
});

0 comments on commit 8441a77

Please sign in to comment.