Skip to content

Instantly share code, notes, and snippets.

@russmatney
Created September 29, 2013 00:40
Show Gist options
  • Save russmatney/6748127 to your computer and use it in GitHub Desktop.
Save russmatney/6748127 to your computer and use it in GitHub Desktop.
Basic CoffeeScript + AngularJS Pig-latin filter + unit tests. Only moves first letter at the moment. AngularJS filters are my favorite TDD.
'use strict';
angular.module('pig-latin', [])
.filter 'pig-latin', () ->
# input is handed into the filter
(input) ->
# equals itself or ''
input ||= ''
if input.length > 0
# splits string based on char
firstChar = input.slice(0, 1)
theRest = input.slice(1)
input = theRest + firstChar + "a"
# returns input
input
'use strict'
describe 'Pig Latin Filter', () ->
# initialize a new instance of the filter before each test
filter = {}
# load the filter's module
beforeEach module('pig-latin')
# give our local object the magical injection
beforeEach inject ($filter) ->
filter = $filter 'pig-latin'
# pretty existential, huh?
it 'should have a filter', () ->
expect(filter).not.toBe(null)
# base case, handles no input
it 'should return an empty string when given null', () ->
output = filter()
expect(output).toBe('')
# simple test
it 'should set "bozo" to "ozoba"', ->
output = filter("bozo")
expect(output).toBe("ozoba")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment