Skip to content

Commit ed73ebf

Browse files
committed
refs #15 Refactor the way reading config
1 parent ac6503b commit ed73ebf

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

lib/config-store.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ class ConfigStore {
77
this._workspace = params.workspace;
88
}
99

10+
get preComparisonTextNormalizationRules() {
11+
return this._get('preComparisonTextNormalizationRules');
12+
}
13+
1014
get hasPreComparisonTextNormalizationRules() {
11-
return this.get('preComparisonTextNormalizationRules').length !== 0;
15+
return this.preComparisonTextNormalizationRules.length !== 0;
1216
}
1317

14-
get(configName) {
18+
_get(configName) {
1519
const extensionConfig = this._workspace.getConfiguration(EXTENSION_ID);
1620
return extensionConfig.get(configName);
1721
}

lib/text-process-rule-applier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class TextProcessRuleApplier {
66
}
77

88
applyTo(text) {
9-
const rules = this._configStore.get('preComparisonTextNormalizationRules');
9+
const rules = this._configStore.preComparisonTextNormalizationRules;
1010
return rules.length !== 0 ? this._applyRulesToText(rules, text) : text;
1111
}
1212

test/lib/config-store.test.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,24 @@ const ConfigStore = require('../../lib/config-store');
33

44
suite('ConfigStore', () => {
55

6-
test('it returns the current config from vscode.workspace', () => {
7-
const extensionConfig = {get: stubWithArgs(['CONFIG_NAME'], 'CONFIG_VALUE')};
6+
test('it reads text normalisation rules from vscode.workspace', () => {
7+
const extensionConfig = {get: stubWithArgs(['preComparisonTextNormalizationRules'], 'RULES')};
88
const workspace = {getConfiguration: stubWithArgs(['partialDiff'], extensionConfig)};
99
const configStore = new ConfigStore({workspace});
10-
expect(configStore.get('CONFIG_NAME')).to.eql('CONFIG_VALUE');
10+
expect(configStore.preComparisonTextNormalizationRules).to.eql('RULES');
11+
});
12+
13+
test('it tells if text normalisation rules are specified', () => {
14+
const extensionConfig = {get: stubWithArgs(['preComparisonTextNormalizationRules'], ['RULE1'])};
15+
const workspace = {getConfiguration: stubWithArgs(['partialDiff'], extensionConfig)};
16+
const configStore = new ConfigStore({workspace});
17+
expect(configStore.hasPreComparisonTextNormalizationRules).to.be.true;
18+
});
19+
20+
test('it tells if text normalisation rules are not specified', () => {
21+
const extensionConfig = {get: stubWithArgs(['preComparisonTextNormalizationRules'], [])};
22+
const workspace = {getConfiguration: stubWithArgs(['partialDiff'], extensionConfig)};
23+
const configStore = new ConfigStore({workspace});
24+
expect(configStore.hasPreComparisonTextNormalizationRules).to.be.false;
1125
});
1226
});

test/lib/content-provider.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ suite('ContentProvider', () => {
6565
function retrieveEditorContent({selectionInfoRegistry, preComparisonTextNormalizationRules, registeredText}) {
6666
const defaultSelectionInfoRegistry = {get: key => ({text: registeredText || `TEXT_${key}`})};
6767
const textResourceUtil = {getTextKey: uri => uri.replace('URI_', '')};
68-
const configStore = {
69-
get: key => key === 'preComparisonTextNormalizationRules' && (preComparisonTextNormalizationRules || [])
70-
};
68+
const configStore = {preComparisonTextNormalizationRules: preComparisonTextNormalizationRules || []};
7169
const contentProvider = new ContentProvider({
7270
selectionInfoRegistry: selectionInfoRegistry || defaultSelectionInfoRegistry,
7371
textResourceUtil,

test/lib/diff-presenter.test.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ suite('DiffPresenter', () => {
55

66
test('it passes URI of 2 texts to compare', async () => {
77
const commands = fakeCommands();
8-
const selectionInfoRegistry = {get: () => {}};
9-
const textTitleBuilder = {build: () => {}};
108
const textResourceUtil = {
119
getUri: stubWithArgs(
1210
['TEXT1'], 'URI_INSTANCE_1',
1311
['TEXT2'], 'URI_INSTANCE_2'
1412
)
1513
};
16-
const diffPresenter = new DiffPresenter({commands, textTitleBuilder, selectionInfoRegistry, textResourceUtil});
14+
const diffPresenter = new DiffPresenter({
15+
commands,
16+
configStore: {},
17+
textTitleBuilder: {build: () => {}},
18+
selectionInfoRegistry: {get: () => {}},
19+
textResourceUtil
20+
});
1721

1822
await diffPresenter.takeDiff('TEXT1', 'TEXT2');
1923

@@ -30,9 +34,13 @@ suite('DiffPresenter', () => {
3034
['TEXT2'], 'TEXT_INFO_2'
3135
)
3236
};
33-
const textTitleBuilder = {build: textKey => `TITLE_${textKey}`};
34-
const textResourceUtil = {getUri: () => {}};
35-
const diffPresenter = new DiffPresenter({commands, textTitleBuilder, selectionInfoRegistry, textResourceUtil});
37+
const diffPresenter = new DiffPresenter({
38+
commands,
39+
configStore: {},
40+
textTitleBuilder: {build: textKey => `TITLE_${textKey}`},
41+
selectionInfoRegistry,
42+
textResourceUtil: {getUri: () => {}}
43+
});
3644

3745
await diffPresenter.takeDiff('TEXT1', 'TEXT2');
3846

0 commit comments

Comments
 (0)