-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update prefix color selector to use a generator
- Loading branch information
Showing
5 changed files
with
252 additions
and
132 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
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
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
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 |
---|---|---|
@@ -1,71 +1,189 @@ | ||
import { PrefixColorSelector } from './prefix-color-selector'; | ||
|
||
it('does not produce a color if it should not', () => { | ||
const prefixColorSelector = new PrefixColorSelector([], false); | ||
|
||
let selectedColor = prefixColorSelector.getNextColor(0); | ||
expect(selectedColor).toBe(''); | ||
selectedColor = prefixColorSelector.getNextColor(1); | ||
expect(selectedColor).toBe(''); | ||
selectedColor = prefixColorSelector.getNextColor(2); | ||
expect(selectedColor).toBe(''); | ||
}); | ||
function assertSelectedColors({ | ||
prefixColorSelector, | ||
expectedColors, | ||
}: { | ||
prefixColorSelector: PrefixColorSelector; | ||
expectedColors: string[]; | ||
}) { | ||
expectedColors.forEach(expectedColor => { | ||
expect(prefixColorSelector.getNextColor()).toBe(expectedColor); | ||
}); | ||
} | ||
|
||
it('uses user defined prefix colors only if not allowed to use auto colors', () => { | ||
const prefixColorSelector = new PrefixColorSelector(['red', 'green', 'blue'], false); | ||
|
||
let selectedColor = prefixColorSelector.getNextColor(0); | ||
expect(selectedColor).toBe('red'); | ||
selectedColor = prefixColorSelector.getNextColor(1); | ||
expect(selectedColor).toBe('green'); | ||
selectedColor = prefixColorSelector.getNextColor(2); | ||
expect(selectedColor).toBe('blue'); | ||
|
||
// uses last color if no more user defined colors | ||
selectedColor = prefixColorSelector.getNextColor(3); | ||
expect(selectedColor).toBe('blue'); | ||
selectedColor = prefixColorSelector.getNextColor(4); | ||
expect(selectedColor).toBe('blue'); | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('uses user defined colors then recurring auto colors without repeating consecutive colors', () => { | ||
const prefixColorSelector = new PrefixColorSelector(['red', 'green'], true); | ||
describe('#getNextColor', function () { | ||
it('does not produce a color if prefixColors empty', () => { | ||
assertSelectedColors({ | ||
prefixColorSelector: new PrefixColorSelector([]), | ||
expectedColors: ['', '', ''], | ||
}); | ||
}); | ||
|
||
jest.spyOn(prefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([ | ||
'green', | ||
'blue', | ||
]); | ||
it('does not produce a color if prefixColors undefined', () => { | ||
assertSelectedColors({ | ||
prefixColorSelector: new PrefixColorSelector(), | ||
expectedColors: ['', '', ''], | ||
}); | ||
}); | ||
|
||
let selectedColor = prefixColorSelector.getNextColor(0); | ||
expect(selectedColor).toBe('red'); | ||
selectedColor = prefixColorSelector.getNextColor(1); | ||
expect(selectedColor).toBe('green'); | ||
it('uses user defined prefix colors only, if no auto is used', () => { | ||
assertSelectedColors({ | ||
prefixColorSelector: new PrefixColorSelector(['red', 'green', 'blue']), | ||
expectedColors: [ | ||
'red', | ||
'green', | ||
'blue', | ||
|
||
// auto colors now, does not repeat last user color of green | ||
selectedColor = prefixColorSelector.getNextColor(2); | ||
expect(selectedColor).toBe('blue'); | ||
// uses last color if last color is not "auto" | ||
'blue', | ||
'blue', | ||
'blue', | ||
], | ||
}); | ||
}); | ||
|
||
selectedColor = prefixColorSelector.getNextColor(3); | ||
expect(selectedColor).toBe('green'); | ||
it('uses picks colors when user defines an auto color', () => { | ||
jest.spyOn(PrefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([ | ||
'green', | ||
'blue', | ||
]); | ||
|
||
selectedColor = prefixColorSelector.getNextColor(4); | ||
expect(selectedColor).toBe('blue'); | ||
}); | ||
assertSelectedColors({ | ||
prefixColorSelector: new PrefixColorSelector(['red', 'green', 'auto', 'orange']), | ||
expectedColors: [ | ||
// custom colors | ||
'red', | ||
'green', | ||
'blue', // picks auto color, not repeating consecutive "green" color | ||
'orange', | ||
|
||
it('has more than 1 auto color defined', () => { | ||
const prefixColorSelector = new PrefixColorSelector([], true); | ||
// ! code assumes this always has more than one entry, so make sure | ||
expect(prefixColorSelector.ACCEPTABLE_CONSOLE_COLORS.length).toBeGreaterThan(1); | ||
}); | ||
// uses last color if last color is not "auto" | ||
'orange', | ||
'orange', | ||
'orange', | ||
], | ||
}); | ||
}); | ||
|
||
it('can use only auto colors and does not repeat consecutive colors', () => { | ||
const prefixColorSelector = new PrefixColorSelector([], true); | ||
it('uses user defined colors then recurring auto colors without repeating consecutive colors', () => { | ||
jest.spyOn(PrefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([ | ||
'green', | ||
'blue', | ||
]); | ||
|
||
assertSelectedColors({ | ||
prefixColorSelector: new PrefixColorSelector(['red', 'green', 'auto']), | ||
expectedColors: [ | ||
// custom colors | ||
'red', | ||
'green', | ||
|
||
// picks auto colors, not repeating consecutive "green" color | ||
'blue', | ||
'green', | ||
'blue', | ||
'green', | ||
], | ||
}); | ||
}); | ||
|
||
it('can sometimes produce consecutive colors', () => { | ||
jest.spyOn(PrefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([ | ||
'green', | ||
'blue', | ||
]); | ||
|
||
assertSelectedColors({ | ||
prefixColorSelector: new PrefixColorSelector(['blue', 'auto']), | ||
expectedColors: [ | ||
// custom colors | ||
'blue', | ||
|
||
// picks auto colors | ||
'green', | ||
// does not repeat custom colors for initial auto colors, ie does not use "blue" again so soon | ||
'green', // consecutive color picked, however practically there would be a lot of colors that need to be set in a particular order for this to occur | ||
'blue', | ||
'green', | ||
'blue', | ||
'green', | ||
'blue', | ||
], | ||
}); | ||
}); | ||
|
||
it('considers the Bright variants of colors equal to the normal colors to avoid similar colors', function () { | ||
jest.spyOn(PrefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([ | ||
'greenBright', | ||
'blueBright', | ||
'green', | ||
'blue', | ||
'magenta', | ||
]); | ||
|
||
assertSelectedColors({ | ||
prefixColorSelector: new PrefixColorSelector(['green', 'blue', 'auto']), | ||
expectedColors: [ | ||
// custom colors | ||
'green', | ||
'blue', | ||
|
||
// picks auto colors, not repeating green and blue colors and variants initially | ||
'magenta', | ||
|
||
// picks auto colors | ||
'greenBright', | ||
'blueBright', | ||
'green', | ||
'blue', | ||
'magenta', | ||
], | ||
}); | ||
}); | ||
|
||
it('does not repeat consecutive colors when last prefixColor is auto', () => { | ||
const prefixColorSelector = new PrefixColorSelector(['auto']); | ||
|
||
// pick auto colors over 2 sets | ||
const expectedColors: string[] = [ | ||
...PrefixColorSelector.ACCEPTABLE_CONSOLE_COLORS, | ||
...PrefixColorSelector.ACCEPTABLE_CONSOLE_COLORS, | ||
]; | ||
|
||
expectedColors.reduce((previousColor, currentExpectedColor) => { | ||
const actualSelectedColor = prefixColorSelector.getNextColor(); | ||
expect(actualSelectedColor).not.toBe(previousColor); // no consecutive colors | ||
expect(actualSelectedColor).toBe(currentExpectedColor); // expected color | ||
return actualSelectedColor; | ||
}, ''); | ||
}); | ||
|
||
it('handles when more individual auto prefixColors exist than acceptable console colors', () => { | ||
// pick auto colors over 2 sets | ||
const expectedColors: string[] = [ | ||
...PrefixColorSelector.ACCEPTABLE_CONSOLE_COLORS, | ||
...PrefixColorSelector.ACCEPTABLE_CONSOLE_COLORS, | ||
]; | ||
|
||
const prefixColorSelector = new PrefixColorSelector(expectedColors.map(() => 'auto')); | ||
|
||
expectedColors.reduce((previousColor, currentExpectedColor) => { | ||
const actualSelectedColor = prefixColorSelector.getNextColor(); | ||
expect(actualSelectedColor).not.toBe(previousColor); // no consecutive colors | ||
expect(actualSelectedColor).toBe(currentExpectedColor); // expected color | ||
return actualSelectedColor; | ||
}, ''); | ||
}); | ||
}); | ||
|
||
let previousColor; | ||
let selectedColor: string; | ||
Array(prefixColorSelector.ACCEPTABLE_CONSOLE_COLORS.length * 2).forEach((_, index) => { | ||
previousColor = selectedColor; | ||
selectedColor = prefixColorSelector.getNextColor(index); | ||
expect(selectedColor).not.toBe(previousColor); | ||
describe('PrefixColorSelector#ACCEPTABLE_CONSOLE_COLORS', () => { | ||
it('has more than 1 auto color defined', () => { | ||
// ! code assumes this always has more than one entry, so make sure | ||
expect(PrefixColorSelector.ACCEPTABLE_CONSOLE_COLORS.length).toBeGreaterThan(1); | ||
}); | ||
}); |
Oops, something went wrong.