Created
November 29, 2010 21:23
-
-
Save netzpirat/720646 to your computer and use it in GitHub Desktop.
String.call and Namespaces in CoffeeScript tested with JasmineBDD
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
# | |
# Calls a function that is defined as a String | |
# | |
# 'ws.extranett.subdomain_for'.call('argument1', 'argument2') | |
# | |
# Will call the function subdomain_for bound on the object extranett | |
# with arguments 'argument1' and 'argument2' | |
# | |
String::call = (args...) -> | |
parts = @split('.') | |
func = window | |
for all part in parts | |
obj = func | |
func = func[part] | |
func.apply(obj, args) |
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
describe 'String:', -> | |
describe 'call()', -> | |
describe 'without a namespace', -> | |
beforeEach -> window.test = jasmine.createSpy('test') | |
it 'executes a function from a string', -> | |
'test'.call() | |
expect(test).toHaveBeenCalled() | |
it 'executes a function from a string with an argument', -> | |
'test'.call(1) | |
expect(test).toHaveBeenCalledWith(1) | |
it 'executes a function from a string with multiple arguments', -> | |
'test'.call(1, 2) | |
expect(test).toHaveBeenCalledWith(1, 2) | |
describe 'with a namespace', -> | |
beforeEach -> | |
namespace 'a.b.c' | |
window.a.b.c.test = jasmine.createSpy('test') | |
it 'executes a function from a string', -> | |
'a.b.c.test'.call() | |
expect(window.a.b.c.test).toHaveBeenCalled() | |
it 'executes a function from a string with an argument', -> | |
'a.b.c.test'.call(1) | |
expect(window.a.b.c.test).toHaveBeenCalledWith(1) | |
it 'executes a function from a string with multiple arguments', -> | |
'a.b.c.test'.call(1, 2) | |
expect(window.a.b.c.test).toHaveBeenCalledWith(1, 2) |
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
# | |
# Register a namespace | |
# | |
window.namespace = (space) -> | |
parts = space.split('.') | |
root = window | |
for all part in parts | |
root[part] = new Object() if not root[part]? | |
root = root[part] | |
root |
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
describe 'Window:', -> | |
describe 'namespace()', -> | |
it 'creates a namespace on the window object', -> | |
namespace 'test' | |
expect(window.test).toBeDefined | |
it 'creates a nested namespace on the window object', -> | |
namespace 'test1.test2.test3.test4.test5' | |
expect(window.test1).toBeDefined | |
expect(window.test1.test2).toBeDefined | |
expect(window.test1.test2.test3).toBeDefined | |
expect(window.test1.test2.test3.test4).toBeDefined | |
expect(window.test1.test2.test3.test4.test5).toBeDefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment