Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving test coverage #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules
build
test
.vscode
44 changes: 17 additions & 27 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
module.exports = {
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
};
env: {
es6: true,
node: true,
'jest/globals': true,
},
plugins: ['jest', 'prettier'],
extends: ['eslint:recommended', 'plugin:jest/recommended', 'prettier'],
parserOptions: {
sourceType: 'module',
},
rules: {
indent: ['error', 'tab'],
'linebreak-style': ['error', 'unix'],
quotes: ['error', 'single'],
semi: ['error', 'always'],
},
};
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"babel-minify": "^0.2.0",
"babel-preset-minify": "^0.2.0",
"coveralls": "^3.0.0",
"eslint-plugin-jest": "^21.15.1",
"eslint-plugin-prettier": "^2.6.0",
"jest": "^21.2.1",
"np": "^2.16.1"
},
Expand Down
207 changes: 128 additions & 79 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,164 @@
const KeywordProcessor = require('../build/index');

test('extract keywords test', () => {
const processor = new KeywordProcessor();
describe('basic functionalities', () => {
test('extract keywords test', () => {
const processor = new KeywordProcessor();

processor.addKeyword('Big Apple', 'New York');
processor.addKeyword('Bay Area');
processor.addKeyword('Big Apple', 'New York');
processor.addKeyword('Bay Area');

const keywordsFound = processor.extractKeywords(
'I love Big Apple and Bay Area.'
);
const expected = ['New York', 'Bay Area'];
const keywordsFound = processor.extractKeywords('I love Big Apple and Bay Area.');
const expected = ['New York', 'Bay Area'];

expect(keywordsFound).toEqual(expected);
});
expect(keywordsFound).toEqual(expected);
});

test('replace keywords test', () => {
const processor = new KeywordProcessor();
test('replace keywords test', () => {
const processor = new KeywordProcessor();

processor.addKeyword('Big Apple', 'New York');
processor.addKeyword('New Delhi', 'NCR Region');
processor.addKeyword('Big Apple', 'New York');
processor.addKeyword('New Delhi', 'NCR Region');

const newSentence = processor.replaceKeywords(
'I love Big Apple and new delhi.'
);
const expected = 'I love New York and NCR Region.';
const newSentence = processor.replaceKeywords('I love Big Apple and new delhi.');
const expected = 'I love New York and NCR Region.';

expect(newSentence).toEqual(expected);
});
expect(newSentence).toEqual(expected);
});

test('case sensitive example test', () => {
const processor = new KeywordProcessor(true);
test('case sensitive example test', () => {
const processor = new KeywordProcessor(true);

processor.addKeyword('Big Apple', 'New York');
processor.addKeyword('Bay Area');
processor.addKeyword('Big Apple', 'New York');
processor.addKeyword('Bay Area');

const keywordsFound = processor.extractKeywords(
'I love big Apple and Bay Area.'
);
const expected = ['Bay Area'];
const keywordsFound = processor.extractKeywords('I love big Apple and Bay Area.');
const expected = ['Bay Area'];

expect(keywordsFound).toEqual(expected);
});
expect(keywordsFound).toEqual(expected);
});

test('add multiple keywords using Object test', () => {
const processor = new KeywordProcessor();

test('add multiple keywords using Object test', () => {
const processor = new KeywordProcessor();
processor.addKeywordsFromObject({
java: ['java_2e', 'java programming'],
python: ['python2.7', 'python programming'],
});

processor.addKeywordsFromObject({
java: ['java_2e', 'java programming'],
python: ['python2.7', 'python programming'],
const keywordsFound = processor.extractKeywords('I love java_2e and python programming.');
expect(keywordsFound).toEqual(['java', 'python']);

const newSentence = processor.replaceKeywords('I prefer python over java programming.');
expect(newSentence).toEqual('I prefer python over java.');
});

const keywordsFound = processor.extractKeywords(
'I love java_2e and python programming.'
);
expect(keywordsFound).toEqual(['java', 'python']);
test('add multiple keywords using Array test', () => {
const processor = new KeywordProcessor();

const newSentence = processor.replaceKeywords(
'I prefer python over java programming.'
);
expect(newSentence).toEqual('I prefer python over java.');
});
processor.addKeywordsFromArray(['java', 'product manager']);

test('add multiple keywords using Array test', () => {
const processor = new KeywordProcessor();
const keywordsFound = processor.extractKeywords('I am a product manager for a java platform');
expect(keywordsFound).toEqual(['product manager', 'java']);
});

processor.addKeywordsFromArray(['java', 'product manager']);
test('remove keywords test', () => {
const processor = new KeywordProcessor();

const keywordsFound = processor.extractKeywords(
'I am a product manager for a java platform'
);
expect(keywordsFound).toEqual(['product manager', 'java']);
});
processor.addKeywordsFromArray(['react', 'angular', 'vue', 'javascript']);
processor.removeKeywordsFromArray(['angular', 'vue']);
processor.removeKeyword('javascript');

const keywordsFound = processor.extractKeywords('I know React, Angular and Vue. I love JavaScript.');

expect(keywordsFound).toEqual(['react']);
});

test('remove keywords test', () => {
const processor = new KeywordProcessor();
test('remove keywords from object test', () => {
const processor = new KeywordProcessor();

processor.addKeywordsFromArray(['react', 'angular', 'vue', 'javascript']);
processor.removeKeywordsFromArray(['angular', 'vue']);
processor.removeKeyword('javascript');
processor.addKeywordsFromObject({
java: ['java_2e', 'java programming'],
python: ['python2.7', 'python programming'],
});

const keywordsFound = processor.extractKeywords(
'I know React, Angular and Vue. I love JavaScript.'
);
let keywordsFound = processor.extractKeywords('I love java_2e and python programming.');
expect(keywordsFound).toEqual(['java', 'python']);

expect(keywordsFound).toEqual(['react']);
processor.removeKeywordsFromObject({ java: ['java_2e', 'java programming'] });

keywordsFound = processor.extractKeywords('I love java_2e and python programming.');
expect(keywordsFound).toEqual(['python']);

const sentence = 'I prefer python over java programming.';
const newSentence = processor.replaceKeywords(sentence);
expect(newSentence).toEqual(sentence);
});

test('add characters as part of word characters', () => {
const processor = new KeywordProcessor();

processor.addKeyword('Big Apple');

const before = processor.extractKeywords('I love Big Apple/Bay Area.');
expect(before).toEqual(['Big Apple']);

processor.addNonWordBoundaries('/');
const after = processor.extractKeywords('I love Big Apple/Bay Area.');
expect(after).toEqual([]);
});

test('set non word boundaries', () => {
const processor = new KeywordProcessor();
const arrayOfKeywords = ['java-script', 'javascript', 'Javascript'];
processor.addKeywordsFromObject({
JavaScript: arrayOfKeywords,
});

const result = 'I love JavaScript';
arrayOfKeywords.forEach(keyword => {
const sentence = `I love ${keyword}`;
expect(processor.replaceKeywords(sentence)).toEqual(result);
});

processor.setNonWordBoundaries([' ']);
const sentence = 'I love java-script';
expect(processor.extractKeywords(sentence)).toEqual([]);
expect(processor.replaceKeywords(sentence)).toEqual(sentence);
});
});

test('add characters as part of word characters', () => {
const processor = new KeywordProcessor();
describe('common use cases', () => {
test('shorter search string then keywords', () => {
const processorCaseInsensitive = new KeywordProcessor();
const processorCaseSensitive = new KeywordProcessor(true);

processor.addKeyword('Big Apple');
const before = 'I work at Google';
const after = 'I work at Alphabet';

const before = processor.extractKeywords('I love Big Apple/Bay Area.');
expect(before).toEqual(['Big Apple']);
processorCaseInsensitive.addKeyword('Google', 'Alphabet');
expect(processorCaseInsensitive.replaceKeywords(before)).toEqual(after);

processor.addNonWordBoundaries('/');
const after = processor.extractKeywords('I love Big Apple/Bay Area.');
expect(after).toEqual([]);
const sentence = 'I work google';

processorCaseSensitive.addKeyword('Google', 'Alphabet');
expect(processorCaseSensitive.replaceKeywords(before)).toEqual(after);
expect(processorCaseSensitive.replaceKeywords(sentence)).toEqual(sentence);
});
});

test('recreating a bug related to sentence ending with a half matching keyword', () => {
const processor = new KeywordProcessor();
describe('reported edge cases', () => {
test('recreating a bug related to sentence ending with a half matching keyword', () => {
const processor = new KeywordProcessor();

processor.addKeyword('Appl1', 'app_1');
processor.addKeyword('Appl2', 'app_2');
processor.addKeyword('Appl1', 'app_1');
processor.addKeyword('Appl2', 'app_2');
expect(processor.replaceKeywords('appl1')).toEqual('app_1');
expect(processor.replaceKeywords('appl2')).toEqual('app_2');
expect(processor.replaceKeywords('app')).toEqual('app');

expect(processor.replaceKeywords('appl1')).toEqual('app_1');
expect(processor.replaceKeywords('appl2')).toBe('app_2');
const sentence = 'I am using an App';
const after = processor.replaceKeywords(sentence);

expect(processor.replaceKeywords('app')).toEqual('app');
expect(processor.replaceKeywords('I am using an App')).toEqual(
'I am using an App'
);
expect(after).toEqual(sentence);
});
});