-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from haensl/77
Add a profile for comparison statements.
- Loading branch information
Showing
5 changed files
with
370 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
lib/profiles/comparison-statements/comparison-statements.profile.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const expect = require('chai').expect; | ||
const comparisonStatements = require('./'); | ||
|
||
describe('Comparison statements', () => { | ||
let result; | ||
let data; | ||
|
||
describe('data: 0', () => { | ||
beforeEach(() => { | ||
data = 0; | ||
}); | ||
|
||
comparisonStatements.functions.forEach((fn) => { | ||
describe(`${fn.description}`, () => { | ||
beforeEach(() => { | ||
result = fn.f(data); | ||
}); | ||
|
||
it('returns 0', () => { | ||
expect(result).to.equal(0); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('data: 2', () => { | ||
beforeEach(() => { | ||
data = 2; | ||
}); | ||
|
||
comparisonStatements.functions.forEach((fn) => { | ||
describe(`${fn.description}`, () => { | ||
beforeEach(() => { | ||
result = fn.f(data); | ||
}); | ||
|
||
it('returns 0', () => { | ||
expect(result).to.equal(1); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const unique = require('../../support/array').unique; | ||
|
||
const compIf = { | ||
description: 'if statement', | ||
codeSample: 'if (d > 0) { return d / 2; }', | ||
keywords: [ | ||
'if', | ||
'comparison', | ||
'statement', | ||
'else', | ||
'else if', | ||
'branching', | ||
'control structure', | ||
'control flow', | ||
'flow' | ||
], | ||
f: (d) => { | ||
if (d === 0) { | ||
return d; | ||
} else if (d > 0) { | ||
return d / 2; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
}; | ||
|
||
const compSwitch = { | ||
description: 'switch statement', | ||
codeSample: 'switch (d) { case 0: return d; default: return d / 2; }', | ||
keywords: [ | ||
'switch', | ||
'comparison', | ||
'statement', | ||
'break', | ||
'branching', | ||
'control structure', | ||
'control flow', | ||
'flow' | ||
], | ||
f: (d) => { | ||
switch (d) { | ||
case 0: | ||
return d; | ||
default: | ||
return d / 2; | ||
} | ||
} | ||
}; | ||
|
||
const compTernary = { | ||
description: 'ternary expression', | ||
keywords: [ | ||
'ternary', | ||
'expression', | ||
'comparison', | ||
'statement', | ||
'branching', | ||
'control structure', | ||
'control flow', | ||
'flow' | ||
], | ||
codeSample: 'd > 0 ? d / 2 : d', | ||
f: (d) => d > 0 ? d / 2 : d | ||
}; | ||
|
||
const compAnd = { | ||
description: 'and-or, && ||', | ||
keywords: [ | ||
'and', | ||
'or', | ||
'comparison', | ||
'statement', | ||
'expression', | ||
'branching', | ||
'control structure', | ||
'control flow', | ||
'flow' | ||
], | ||
codeSample: '(d > 0 && d / 2) || d', | ||
f: (d) => (d > 0 && d / 2) || d | ||
}; | ||
|
||
const functions = [ | ||
compIf, | ||
compSwitch, | ||
compTernary, | ||
compAnd | ||
]; | ||
|
||
module.exports = { | ||
name: 'comparison statements', | ||
description: { | ||
long: 'Comparison statements: conditionally branching in a function based on simple comparisons.', | ||
short: 'Comparison statements: if vs. switch vs. ternary vs. logical.' | ||
}, | ||
functions, | ||
keywords: unique( | ||
functions.map((fn) => fn.keywords) | ||
.reduce((keywords, fnKeywords) => [...keywords, ...fnKeywords]) | ||
).sort(), | ||
testDataType: 'number' | ||
}; |
Oops, something went wrong.