Brush with spec
parent
10ebba7755
commit
ed6d1146da
@ -0,0 +1,23 @@
|
||||
class AsciiIo.Brush
|
||||
@cache: {}
|
||||
|
||||
@clearCache: ->
|
||||
@cache = {}
|
||||
|
||||
@create: (options) ->
|
||||
options ||= {}
|
||||
|
||||
key = "#{options.fg}_#{options.bg}_#{options.bright}_#{options.underline}"
|
||||
brush = @cache[key]
|
||||
|
||||
if not brush
|
||||
brush = new AsciiIo.Brush(options)
|
||||
@cache[key] = brush
|
||||
|
||||
brush
|
||||
|
||||
constructor: (options) ->
|
||||
@fg = options.fg
|
||||
@bg = options.bg
|
||||
@bright = options.bright
|
||||
@underline = options.underline
|
@ -0,0 +1,26 @@
|
||||
describe AsciiIo.Brush, ->
|
||||
|
||||
describe '.clearCache', ->
|
||||
|
||||
it 'resets cache hash', ->
|
||||
AsciiIo.Brush.create()
|
||||
expect(_(AsciiIo.Brush.cache).keys().length > 0).toBeTruthy()
|
||||
AsciiIo.Brush.clearCache()
|
||||
expect(_(AsciiIo.Brush.cache).keys().length).toEqual(0)
|
||||
|
||||
describe '.create', ->
|
||||
|
||||
beforeEach ->
|
||||
AsciiIo.Brush.clearCache()
|
||||
|
||||
it 'returns new brush instance if not cached', ->
|
||||
brush = AsciiIo.Brush.create({ fg: 1})
|
||||
expect(brush instanceof AsciiIo.Brush).toBeTruthy()
|
||||
|
||||
it 'returns existing brush instance if cached', ->
|
||||
brush = AsciiIo.Brush.create({ fg: 1})
|
||||
otherBrush = AsciiIo.Brush.create({ fg: 1, bg: 100 })
|
||||
anotherBrush = AsciiIo.Brush.create({ fg: 1})
|
||||
|
||||
expect(_(AsciiIo.Brush.cache).keys().length).toEqual(2)
|
||||
expect(brush is anotherBrush).toBeTruthy()
|
Loading…
Reference in New Issue