Permalink
Browse files

Add specs for getFunctions and getClasses.

Add getObjects method for documented objects.
Remove unused test/requirejs/*.coffee files.
  • Loading branch information...
1 parent 12c6c1c commit 2f9437213824c4367ba37eeb51b30de7e7bf9f10 @dmartinzar dmartinzar committed Aug 15, 2011
Showing with 109 additions and 34 deletions.
  1. +11 −0 lib/parsers.js
  2. +4 −0 src/parsers.coffee
  3. +0 −17 test/requirejs/class.coffee
  4. +0 −17 test/requirejs/define.coffee
  5. +94 −0 test/requirejsspec.coffee
View
@@ -337,6 +337,17 @@
}
return _results;
};
+ RequireJSParser.prototype.getObjects = function(nodes) {
+ var n, _i, _len, _results;
+ _results = [];
+ for (_i = 0, _len = nodes.length; _i < _len; _i++) {
+ n = nodes[_i];
+ if (n.type === 'Assign' && getAttr(n, 'value.base').type === 'Obj') {
+ _results.push(n);
+ }
+ }
+ return _results;
+ };
RequireJSParser.prototype.getFunctions = function(nodes) {
var n, _i, _len, _results;
_results = [];
View
@@ -255,6 +255,10 @@ exports.RequireJSParser = class RequireJSParser extends BaseParser
return (n for n in nodes when n.type == 'Class' \
or n.type == 'Assign' and n.value.type == 'Class')
+ getObjects: (nodes) ->
+ return (n for n in nodes when n.type == 'Assign' \
+ and getAttr(n, 'value.base').type == 'Obj')
+
getFunctions: (nodes) ->
return (n for n in nodes \
when n.type == 'Assign' and n.value.type == 'Code')
@@ -1,17 +0,0 @@
-###
-# Single class definition #
-
-Test documentation for a module containing a class.
-###
-
-class Test
- ### Documentation for Test ###
-
- constructor: ->
- ### constructor documentation ###
-
- method1: ->
- ### method1 documentation ###
-
- method2: ->
- ### method2 documentation ###
@@ -1,17 +0,0 @@
-###
-# Single class inside a define() call #
-###
-
-define ['mod1', __dirname + '/mod2'], (mod1, mod2) ->
-
- class Test
- ### Documentation for Test ###
-
- constructor: ->
- ### constructor documentation ###
-
- method1: ->
- ### method1 documentation ###
-
- method2: ->
- ### method2 documentation ###
@@ -337,18 +337,112 @@ describe 'RequireJSParser', ->
describe 'provides a getClasses method and', ->
it 'handles top-level classes', ->
+ script = """
+ class Test
+ constructor: ->
+ method1: ->
+ method2: ->
+ """
+ nodes = parser.getNodes(script)
+ classes = parser.getClasses(nodes)
+ expect(classes.length).toBe(1)
+ expect(classes[0].variable.base.value).toBe('Test')
+
it 'handles classes inside of define', ->
+ script = """
+ define ['mod'], (arg) ->
+ class Test
+ constructor: ->
+ method1: ->
+ method2: ->
+ """
+ nodes = parser.getNodes(script)
+ classes = parser.getClasses(nodes)
+ expect(classes.length).toBe(1)
+ expect(classes[0].variable.base.value).toBe('Test')
+
it 'handles classes inside of require', ->
+ script = """
+ require ['mod'], (arg) ->
+ class Test
+ constructor: ->
+ method1: ->
+ method2: ->
+ """
+ nodes = parser.getNodes(script)
+ classes = parser.getClasses(nodes)
+ expect(classes.length).toBe(1)
+ expect(classes[0].variable.base.value).toBe('Test')
describe 'provides a getObjects method and', ->
it 'handles top-level objects', ->
+ script = """
+ test1 =
+ val: true
+
+ test2 = {val: false}
+ """
+ nodes = parser.getNodes(script)
+ objs = parser.getObjects(nodes)
+ expect(objs.length).toBe(2)
+ expect(objs[0].variable.base.value).toBe('test1')
+ expect(objs[1].variable.base.value).toBe('test2')
+
it 'handles objects inside of define', ->
+ script = """
+ define ['mod'], (arg) ->
+ test1 =
+ val: true
+
+ test2 = {val: false}
+ """
+ nodes = parser.getNodes(script)
+ objs = parser.getObjects(nodes)
+ expect(objs.length).toBe(2)
+ expect(objs[0].variable.base.value).toBe('test1')
+ expect(objs[1].variable.base.value).toBe('test2')
+
it 'handles objects inside of require', ->
+ script = """
+ require ['mod'], (arg) ->
+ test1 =
+ val: true
+
+ test2 = {val: false}
+ """
+ nodes = parser.getNodes(script)
+ objs = parser.getObjects(nodes)
+ expect(objs.length).toBe(2)
+ expect(objs[0].variable.base.value).toBe('test1')
+ expect(objs[1].variable.base.value).toBe('test2')
describe 'provides a getFunctions method and', ->
it 'handles top-level functions', ->
+ script = "test = ->"
+ nodes = parser.getNodes(script)
+ funcs = parser.getFunctions(nodes)
+ expect(funcs.length).toBe(1)
+ expect(funcs[0].variable.base.value).toBe('test')
+
it 'handles functions inside of define', ->
+ script = """
+ define ['mod'], (arg) ->
+ test = ->
+ """
+ nodes = parser.getNodes(script)
+ funcs = parser.getFunctions(nodes)
+ expect(funcs.length).toBe(1)
+ expect(funcs[0].variable.base.value).toBe('test')
+
it 'handles functions inside of require', ->
+ script = """
+ require ['mod'], (arg) ->
+ test = ->
+ """
+ nodes = parser.getNodes(script)
+ funcs = parser.getFunctions(nodes)
+ expect(funcs.length).toBe(1)
+ expect(funcs[0].variable.base.value).toBe('test')

0 comments on commit 2f94372

Please sign in to comment.