Skip to content

Instantly share code, notes, and snippets.

@stoplion
Created October 14, 2018 01:12
Show Gist options
  • Save stoplion/f7a408bbcd7558fffcd992ecb43dca90 to your computer and use it in GitHub Desktop.
Save stoplion/f7a408bbcd7558fffcd992ecb43dca90 to your computer and use it in GitHub Desktop.
const parser = require('../percentage_parser');
describe('Parser', () => {
describe('returns math equations with no percentages with no alternations', () => {
it('does nothing to non percentage formulas', () => {
const eq = '4 + 4 - 123 / 32';
const result = parser.feed(eq).results;
const expectedResultStr = {
val: '4 + 4 - 123 / 32',
type: null
};
expect(result).toEqual(expectedResultStr);
});
it('does nothing to non percentage formulas', () => {
const eq = 'round(900) + 10 / floor(90.2)';
const result = parser.feed(eq).results;
const expectedResultStr = {
val: 'round(900) + 10 / floor(90.2)',
type: null
};
expect(result).toEqual(expectedResultStr);
});
});
describe(' percentages added / substracted to numbers', () => {
it('alters numbers added from percentages', () => {
const eq = '4 + 25%';
const result = parser.feed(eq).results;
const expectedResultStr = {
val: '4 + 25 / 100 * (4)',
type: null
};
expect(expectedResultStr).toEqual(result);
});
it('alters numbers subtracted from percentages', () => {
const eq = '4 - 25%';
const result = parser.feed(eq).results;
const expectedResultStr = {
val: '4 - 25 / 100 * (4)',
type: null
};
expect(expectedResultStr).toEqual(result);
});
it('alters numbers added from percentages with parenthesis', () => {
const eq = '(4 - 80) + 25%';
const result = parser.feed(eq).results;
const expectedResultStr = {
val: '(4 - 80) + 25 / 100 * ((4 - 80))',
type: null
};
expect(expectedResultStr).toEqual(result);
});
it('alters numbers added from percentages with parenthesis', () => {
const eq = '12 - (4 * 80) + 25%';
const result = parser.feed(eq).results;
const expectedResultStr = {
val: '12 - (4 * 80) + 25 / 100 × (12 - (4 * 80))',
type: null
};
expect(expectedResultStr).toEqual(result);
});
it('alters decimals subtracted from percentages', () => {
const eq = '100.25 - 25%';
const result = parser.feed(eq).results;
const expectedResultStr = {
val: '100.25 - 25 / 100 * (100.25)',
type: null
};
expect(result).toEqual(expectedResultStr);
});
it('alters numbers subtracted from percentages', () => {
const eq = '1 + 23 / 100 - 25%';
const result = parser.feed(eq).results;
const expectedResultStr = {
val: '1 + 23 / 100 - 25 / 100 * (1 + 23 / 100)',
type: null
};
expect(result).toEqual(expectedResultStr);
});
it('alters equation when two percentages appear in the equation', () => {
const eq = '1 + 23% / 100 + 25%';
const result = parser.feed(eq).results;
const expectedResultStr = {
val: '1 + 23 / 100 * (1) / 100 + 25 / 100 * (1 + 23 / 100 * (1) / 100)',
type: null
};
expect(result).toEqual(expectedResultStr);
});
it('alters equation with percentages in various combinations', () => {
const eq = '11 + 23% + 890 / 12.32';
const result = parser.feed(eq).results;
const expectedResultStr = {
val: '11 + 23/100 × (11) + 890 / 12.32',
type: null
};
expect(result).toEqual(expectedResultStr);
});
});
describe('subtracting and adding percentages with percentages', () => {
it('alters equation when adding two percentages together', () => {
const eq = '13% + 23%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(13/100 + 23/100) * 100',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
it('alters equation when adding two percentages together with decimals', () => {
const eq = '13.24% + 23.33%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(13.24/100 + 23.33/100) * 100',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
it('alters equation when subtracting percentages together', () => {
const eq = '13.24% - 23.33% - 89% - 989%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(13.24/100 - 23.33/100 - 89/100 - 989/100) × 100',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
it('alters equation when adding multiple percentages together', () => {
const eq = '34% + 901% + 80% - 90% + 10%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(34/100 + 901/100 + 80/100 - 90/100 + 10/100) × 100',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
});
describe('multiplying percentages with percentages', () => {
it('alters equation when multiplying percentages together', () => {
const eq = '34% × 78%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(34/100 × 78/100) × 100',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
it('alters equation when multiplying multiple percentages together', () => {
const eq = '34% × 78% × 89% × 198%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(34/100 × 78/100 × 89/100 × 198/100) * 100',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
it('alters equation when multiplying percentages together', () => {
const eq = '34.32% × 78.098%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(34.32/100 × 78.098/100) * 100',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
it('alters equation when multiplying percentages with signs together', () => {
const eq = '34.32% × -78%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(34.32/100 × - 78/100) × 100',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
it('alters equation when multiplying percentages together', () => {
const eq = '34% × 78%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(34/100 × 78/100) × 100',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
it('alters equation when multiplying percentages together with signs', () => {
const eq = '34% × -78%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(34/100 × -78/100) * 100',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
});
describe('dividing percentages and percentages', () => {
it('alters equation when dividing percentages together', () => {
const eq = '34% / 78%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(34 / 78)',
type: null
};
expect(result).toEqual(expectedResult);
});
it('alters equation when dividing percentages together', () => {
const eq = '-934% / 178% / 4.23%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(-934 / 178 / 4.23)',
type: null
};
expect(result).toEqual(expectedResult);
});
it('alters equation when dividing percentages with functions together', () => {
const eq = 'round(12.9%) / 3% / 2%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '(round(12.9) / 3 / 2)',
type: null
};
expect(result).toEqual(expectedResult);
});
});
describe('percentages added/subtracted from numbers', () => {
it('alters equation when adding/subtracting percentages to numbers', () => {
const eq = '78% - 423';
const result = parser.feed(eq).results;
const expectedResult = {
val: '78 - 423',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
it('alters equation when adding/subtracting percentages to numbers', () => {
const eq = '90% - 78% - 423';
const result = parser.feed(eq).results;
const expectedResult = {
val: '90 - 78 - 423',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
it('alters equation when adding/subtracting percentages to numbers', () => {
const eq = '2% + 90% - 78% - 423';
const result = parser.feed(eq).results;
const expectedResult = {
val: '2 + 90 - 78 - 423',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
it('alters equation when adding/subtracting percentages to numbers', () => {
const eq = '2% + 90% - 78.21% - round(423.212)';
const result = parser.feed(eq).results;
const expectedResult = {
val: '2 + 90 - 78.21 - round(423.212)',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
});
describe('percentages and functions', () => {
it('returns equation when percentages and functions are used', () => {
const eq = 'sqrt(2%) - round(423.212%)';
const result = parser.feed(eq).results;
const expectedResult = {
val: 'sqrt(2%) - round(423.212%)',
type: 'perc'
};
expect(result).toEqual(expectedResult);
});
});
describe('percentages and powers', () => {
it('returns equation when percentages and functions are used', () => {
const eq = '2^2%';
const result = parser.feed(eq).results;
const expectedResult = {
val: '2^2',
type: null
};
expect(result).toEqual(expectedResult);
});
it('returns equation when percentages and functions are used', () => {
const eq = '2^2% + 3';
const result = parser.feed(eq).results;
const expectedResult = {
val: '2^2 + 3',
type: null
};
expect(result).toEqual(expectedResult);
});
it('returns equation when percentages and functions are used', () => {
const eq = '2^2 + 3 + 80% + round(9.212)';
const result = parser.feed(eq).results;
const expectedResult = {
val: '2^2 + 3 + 80/100 × (2^2 + 3) + round(9.212)',
type: null
};
expect(result).toEqual(expectedResult);
});
});
describe('percentages and units', () => {
// add tests
});
describe('works well when mixed together with other equations', () => {
// add tests
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment