2010-12-29 00:48:54 -05:00
|
|
|
# Helpers
|
|
|
|
# -------
|
|
|
|
|
|
|
|
# pull the helpers from `CoffeeScript.helpers` into local variables
|
2015-02-07 15:50:41 -05:00
|
|
|
{starts, ends, repeat, compact, count, merge, extend, flatten, del, baseFileName} = CoffeeScript.helpers
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# `starts`
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "the `starts` helper tests if a string starts with another string", ->
|
|
|
|
ok starts('01234', '012')
|
|
|
|
ok not starts('01234', '123')
|
|
|
|
|
|
|
|
test "the `starts` helper can take an optional offset", ->
|
|
|
|
ok starts('01234', '34', 3)
|
|
|
|
ok not starts('01234', '01', 1)
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# `ends`
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "the `ends` helper tests if a string ends with another string", ->
|
|
|
|
ok ends('01234', '234')
|
|
|
|
ok not ends('01234', '012')
|
|
|
|
|
|
|
|
test "the `ends` helper can take an optional offset", ->
|
|
|
|
ok ends('01234', '012', 2)
|
|
|
|
ok not ends('01234', '234', 6)
|
|
|
|
|
|
|
|
|
2013-02-26 12:41:01 -05:00
|
|
|
# `repeat`
|
|
|
|
|
|
|
|
test "the `repeat` helper concatenates a given number of times", ->
|
|
|
|
eq 'asdasdasd', repeat('asd', 3)
|
|
|
|
|
|
|
|
test "`repeat`ing a string 0 times always returns the empty string", ->
|
|
|
|
eq '', repeat('whatever', 0)
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# `compact`
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "the `compact` helper removes falsey values from an array, preserves truthy ones", ->
|
|
|
|
allValues = [1, 0, false, obj={}, [], '', ' ', -1, null, undefined, true]
|
|
|
|
truthyValues = [1, obj, [], ' ', -1, true]
|
|
|
|
arrayEq truthyValues, compact(allValues)
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# `count`
|
2010-12-29 00:48:54 -05:00
|
|
|
|
2014-08-26 21:34:45 -04:00
|
|
|
test "the `count` helper counts the number of occurrences of a string in another string", ->
|
2010-12-29 00:48:54 -05:00
|
|
|
eq 1/0, count('abc', '')
|
|
|
|
eq 0, count('abc', 'z')
|
|
|
|
eq 1, count('abc', 'a')
|
|
|
|
eq 1, count('abc', 'b')
|
|
|
|
eq 2, count('abcdc', 'c')
|
|
|
|
eq 2, count('abcdabcd','abc')
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# `merge`
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "the `merge` helper makes a new object with all properties of the objects given as its arguments", ->
|
|
|
|
ary = [0, 1, 2, 3, 4]
|
|
|
|
obj = {}
|
|
|
|
merged = merge obj, ary
|
|
|
|
ok merged isnt obj
|
|
|
|
ok merged isnt ary
|
|
|
|
for own key, val of ary
|
|
|
|
eq val, merged[key]
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# `extend`
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "the `extend` helper performs a shallow copy", ->
|
|
|
|
ary = [0, 1, 2, 3]
|
|
|
|
obj = {}
|
|
|
|
# should return the object being extended
|
|
|
|
eq obj, extend(obj, ary)
|
|
|
|
# should copy the other object's properties as well (obviously)
|
|
|
|
eq 2, obj[2]
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# `flatten`
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "the `flatten` helper flattens an array", ->
|
|
|
|
success = yes
|
|
|
|
(success and= typeof n is 'number') for n in flatten [0, [[[1]], 2], 3, [4]]
|
|
|
|
ok success
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# `del`
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "the `del` helper deletes a property from an object and returns the deleted value", ->
|
|
|
|
obj = [0, 1, 2]
|
|
|
|
eq 1, del(obj, 1)
|
|
|
|
ok 1 not of obj
|
|
|
|
|
|
|
|
|
2013-03-06 16:41:34 -05:00
|
|
|
# `baseFileName`
|
|
|
|
|
|
|
|
test "the `baseFileName` helper returns the file name to write to", ->
|
|
|
|
ext = '.js'
|
|
|
|
sourceToCompiled =
|
2013-03-06 23:23:36 -05:00
|
|
|
'.coffee': ext
|
2013-03-06 16:41:34 -05:00
|
|
|
'a.coffee': 'a' + ext
|
|
|
|
'b.coffee': 'b' + ext
|
2013-03-06 23:15:01 -05:00
|
|
|
'coffee.coffee': 'coffee' + ext
|
|
|
|
|
2013-03-06 23:23:36 -05:00
|
|
|
'.litcoffee': ext
|
2013-03-06 16:41:34 -05:00
|
|
|
'a.litcoffee': 'a' + ext
|
|
|
|
'b.litcoffee': 'b' + ext
|
2013-03-06 23:15:01 -05:00
|
|
|
'coffee.litcoffee': 'coffee' + ext
|
|
|
|
|
2013-03-06 23:23:36 -05:00
|
|
|
'.lit': ext
|
2013-03-06 16:41:34 -05:00
|
|
|
'a.lit': 'a' + ext
|
|
|
|
'b.lit': 'b' + ext
|
2013-03-06 23:15:01 -05:00
|
|
|
'coffee.lit': 'coffee' + ext
|
|
|
|
|
2013-03-06 23:23:36 -05:00
|
|
|
'.coffee.md': ext
|
2013-03-06 16:41:34 -05:00
|
|
|
'a.coffee.md': 'a' + ext
|
|
|
|
'b.coffee.md': 'b' + ext
|
2013-03-06 23:15:01 -05:00
|
|
|
'coffee.coffee.md': 'coffee' + ext
|
2013-03-06 16:41:34 -05:00
|
|
|
|
|
|
|
for sourceFileName, expectedFileName of sourceToCompiled
|
|
|
|
name = baseFileName sourceFileName, yes
|
|
|
|
filename = name + ext
|
|
|
|
eq filename, expectedFileName
|