diff options
author | James Lal <james@lightsofapollo.com> | 2012-06-19 14:01:01 -0700 |
---|---|---|
committer | James Lal <james@lightsofapollo.com> | 2012-06-19 14:01:01 -0700 |
commit | 936e76b8945c6f310ad893905e08728715620e38 (patch) | |
tree | 39c969cb8739e26e3c4f628b50f1afffa8330dcc /test/webcals | |
parent | f1ae9e6f2a4503b9c5ab55abb13d95cbd9ee753c (diff) | |
download | jsCalDAV-936e76b8945c6f310ad893905e08728715620e38.tar.gz |
v1 sax parser
Diffstat (limited to 'test/webcals')
-rw-r--r-- | test/webcals/ics_test.js | 6 | ||||
-rw-r--r-- | test/webcals/sax/base_test.js | 94 | ||||
-rw-r--r-- | test/webcals/sax/propstat_test.js | 27 | ||||
-rw-r--r-- | test/webcals/sax_test.js | 387 |
4 files changed, 418 insertions, 96 deletions
diff --git a/test/webcals/ics_test.js b/test/webcals/ics_test.js index 904bbf9..af1192e 100644 --- a/test/webcals/ics_test.js +++ b/test/webcals/ics_test.js @@ -5,11 +5,7 @@ var fs = require('fs'), suite('webcals/ics', function() { test('intiailizer', function() { - ics(data, function(data) { - console.log(data.vevent[0]); - }, function() { - console.log('y'); - }) + assert.ok(ics); }); }); diff --git a/test/webcals/sax/base_test.js b/test/webcals/sax/base_test.js new file mode 100644 index 0000000..9ad0bdc --- /dev/null +++ b/test/webcals/sax/base_test.js @@ -0,0 +1,94 @@ +requireLib('sax'); +requireLib('sax/base'); + +suite('webcals/sax/base', function() { + + var data, + subject, + parser, + Parse, + Base, + handler; + + + suiteSetup(function() { + Parse = Webcals.require('sax'); + Base = Webcals.require('sax/base'); + }); + + setup(function() { + //we omit the option to pass base parser + //because we are the base parser + subject = new Parse(); + }); + + test('#create', function() { + function childText() {} + + var Child = Base.create({ + ontext: childText + }); + + assert.equal(Child.ontext, childText); + assert.equal(Child.tagField, Base.tagField); + + assert.isTrue( + Base.isPrototypeOf(Child), + 'should have base in proto chain' + ); + + assert.equal(Child._super, Base); + + var ChildChild = Child.create(); + + assert.isTrue( + Child.isPrototypeOf(ChildChild), + 'should have child in childchild protochain' + ); + + assert.isTrue( + Base.isPrototypeOf(ChildChild), + 'should have base in childchild protochain' + ); + + assert.equal(ChildChild._super, Child); + + }); + + suite('base parser', function() { + var xml; + + defineSample('xml/simple.xml', function(data) { + xml = data; + }); + + //base baser does not + //care about attrs at this point + expected = { + simple: { + a: [ + { value: 'Foo' }, + { value: 'Foo\n bar' } + ], + div: { + span: { + value: 'span' + } + } + } + }; + + test('simple tree', function(done) { + subject.once('complete', function(data) { + assert.deepEqual( + data, expected, + "expected \n '" + JSON.stringify(data) + "'\n to equal \n '" + + JSON.stringify(expected) + '\n"' + ); + done(); + }); + subject.write(xml).close(); + }); + }); + +}); diff --git a/test/webcals/sax/propstat_test.js b/test/webcals/sax/propstat_test.js deleted file mode 100644 index 56e65fd..0000000 --- a/test/webcals/sax/propstat_test.js +++ /dev/null @@ -1,27 +0,0 @@ -suite('webcals/sax/propstat', function() { - var stat = requireLib('sax/propstat'), - sax = requireLib('sax'), - subject; - - var expected = { - status: 200, - 'principal-URL': '/calendar/dav/calmozilla1@gmail.com/user/', - 'resource-type': [ - 'principal', - 'collection' - ] - }; - - test('propstat success', function(done) { - var parser = sax(); - - console.log(parser.on); - stat(parser, function(err, result) { - console.log(result); - done(); - }); - - parser.write(loadSample('xml/propstat-success.xml')).close(); - }); - -}); diff --git a/test/webcals/sax_test.js b/test/webcals/sax_test.js index 312990c..71f6571 100644 --- a/test/webcals/sax_test.js +++ b/test/webcals/sax_test.js @@ -1,94 +1,353 @@ -var xml = requireLib('sax'); +requireLib('sax'); +requireLib('sax/base'); -suite('sax test', function() { +suite('webcals/sax', function() { var data, - subject; + subject, + SAX, + Base, + handler; - test('existance', function() { - return; - var parser = new xml(); + // you should not use instances + // for handlers this is only + // to make testing easier. + function TestHander() { + this.text = []; + this.opentag = []; + this.closetag = []; + this.error = []; + this.complete = []; + this.end = []; - var StatusHandler = { - tag: 'DAV:/status', + var events = [ + 'ontext', 'onclosetag', + 'onopentag', 'onerror', + 'oncomplete', 'onend' + ]; + } - onopentag: function(data) { - }, + TestHander.prototype = { - ontext: function(data) { - this.current.status = data.match(/([0-9]{3,3})/)[1]; - }, + ontext: function(data, handler) { + handler.text.push(data); + }, - onclosetag: function(data) { - this.restoreParser(); - } - }; + onclosetag: function(data, handler) { + handler.closetag.push(data); + }, - var ResourceTypeHandler = { - tag: 'DAV:/resourcetype', + onopentag: function(data, handler) { + handler.opentag.push(data); + }, - onopentag: function(data) { - this.tagStack.push(data.tagSpec); + onerror: function(data, handler) { + handler.error.push(data); + }, - if (data.local === 'resourcetype') { - this.current.resourceTypes = []; - } else { - this.current.resourceTypes.push(data.local); - } - }, + oncomplete: function(data, handler) { + handler.complete.push(data); + }, - onclosetag: function(data) { - this.checkStackForHandler(true); - this.tagStack.pop(); - } - }; + onend: function(data) { + handler.end.push(data); + } + }; - var TextOnlyHandler = { - tag: 'DAV:/href', + function firesHandler(type, data) { + var len = handler[type].length; + var event = handler[type][len - 1]; - onopentag: function(data) { - }, + assert.deepEqual(event, data); + } - ontext: function(data) { - this.current.href = data; - }, + suiteSetup(function() { + SAX = Webcals.require('sax'); + Base = Webcals.require('sax/base'); + }); - onclosetag: function(data) { - this.restoreParser(); - } - }; + setup(function() { + handler = new TestHander(); + subject = new SAX(handler); + }); + + test('initializer', function() { + assert.equal(subject.handler, handler); + assert.deepEqual(subject.stack, []); + assert.deepEqual(subject.handles, {}); + assert.deepEqual(subject._handlerStack, []); + assert.deepEqual(subject.tagStack, []); + assert.ok(subject._parse); + }); + + suite('#setHandler', function() { + + setup(function() { + subject.setHandler(handler, false); + }); - var ResponseHandler = { - tag: 'DAV:/response', - handles: { - 'DAV:/status': StatusHandler, - 'DAV:/resourcetype': ResourceTypeHandler, - 'DAV:/href': TextOnlyHandler, - 'DAV:/getetag': TextOnlyHandler - }, + test('set without store', function() { + assert.equal(subject.handler, handler); + + assert.equal( + subject._handlerStack.length, + 0, + 'should not save original' + ); + }); + + test('set/store', function() { + var uniq = {}; + + subject.setHandler(uniq, true); + + assert.equal(subject.handler, uniq); + assert.equal(subject._handlerStack[0], handler); + }); + + }); + + test('#restoreHandler', function() { + var uniq = {}; + subject.setHandler(uniq, true); + subject.restoreHandler(); + + assert.equal(subject.handler, handler); + }); - onclosetag: function(data) { - this.checkStackForHandler(true); - this.onclosetag(data); - }, + test('#registerHandler', function() { + var uniq = {}; - oncomplete: function() { - this.emit('response', this.current, this); - } + subject.registerHandler('a/foo', uniq); + assert.equal(subject.handles['a/foo'], uniq); + }); + test('#write', function() { + var called, uniq = {}; + subject._parse.write = function() { + return uniq; }; - parser.addHandler(ResponseHandler); + assert.equal(subject.write(), uniq); + }); + + test('#closed', function() { + assert.isFalse(subject.closed, 'should not be closed'); + + subject._parse.closed = true; + assert.isTrue( + subject.closed, + 'should be closed now that parser is.' + ); + }); + + suite('#getHandler', function() { + test('handler not found', function() { + assert.isFalse(subject.getHandler('foo')); + }); + + test('handler found', function() { + var uniq; + + subject.registerHandler('foo', uniq); + + var handler = subject.getHandler('foo'); + assert.equal(uniq, handler); + }); + + test('handler found but is current', function() { + var uniq = {}; + subject.registerHandler('foo', uniq); + subject.setHandler(uniq); + + assert.isFalse(subject.getHandler('foo')); + }); + }); + + suite('#onopentag', function() { + + test('basic event', function() { + var obj = { + local: 'foo', + uri: 'bar' + }; + + subject.onopentag(obj); + assert.equal(subject.currentTag, obj); + assert.equal(obj.tagSpec, 'bar/foo'); + assert.deepEqual( + subject.tagStack, + [{ tag: 'bar/foo' }] + ); + + firesHandler('opentag', obj); + }); + }); + + suite('handler stacks', function() { + var newHandler; - parser.on('response', function(data, context) { - console.log(JSON.stringify(data), '\n\n'); + setup(function() { + newHandler = new TestHander(); + subject.registerHandler('a/a', newHandler); + subject.onopentag({ + local: 'a', + uri: 'a' + }); }); - parser.once('complete', function(data, parser) { - console.log(JSON.stringify(data)); + test('switch to new handler', function() { + assert.equal(subject.handler, newHandler); + assert.deepEqual( + subject.tagStack, [ + { tag: 'a/a', handler: newHandler } + ] + ); + }); + + test('pop to original handler', function() { + subject.onclosetag('a/a'); + assert.equal(subject.tagStack.length, 0, 'should clear tagStack'); + assert.equal(subject.handler, handler, 'should reset handler'); + assert.equal( + newHandler.complete.length, 1, + 'should fire complete event on new handler' + ); + }); + + }); + + test('#onclosetag', function() { + var obj = { local: 'a', uri: 'b' }; + + subject.onopentag(obj); + assert.equal(subject.tagStack.length, 1); + + subject.onclosetag('a:b'); + assert.equal(subject.tagStack.length, 0); + + firesHandler('closetag', 'a:b'); + }); + + test('#ontext', function() { + subject.ontext('foo'); + firesHandler('text', 'foo'); + }); + + test('#onerror', function() { + subject.onerror('foo'); + firesHandler('error', 'foo'); + }); + + test('#onend', function() { + subject.onend(); + assert.ok(handler.end); + assert.equal(handler.end[0], subject.root); + }); + + suite('complex mutli-handler', function() { + var xml, + expected, + events; + + var ResponseHandler; + var TextOnlyHandler; + + defineSample('xml/complex-tree.xml', function(data) { + xml = data; + }); + + suiteSetup(function() { + TextOnlyHandler = Base.create({ + name: 'text', + + //don't add text only elements + //to the stack as objects + onopentag: function() {}, + onclosetag: function() {}, + + //add the value to the parent + //value where key is local tag name + //and value is the text. + ontext: function(data) { + var handler = this.handler; + this.current[this.currentTag[handler.tagField]] = data; + } + }); + + ResponseHandler = Base.create({ + name: 'response', + + handles: { + 'DAV:/href': TextOnlyHandler, + 'DAV:/status': TextOnlyHandler, + 'DAV:/getetag': TextOnlyHandler + }, + + oncomplete: function() { + events.push(this.current); + } + }); + }); + + setup(function() { + //use real handlers + subject.setHandler(Base); + }); + + test('complex result', function() { + var result, + expectedEvent, + expectedResult; + + events = []; + + expectedEvent = { + href: 'uri', + propstat: [ + { + status: '400', + prop: { + current: {} + } + }, + { + status: '200', + prop: { + next: {} + } + } + ] + }; + + expectedResult = { + complex: { + response: [{}, expectedEvent] + } + }; + + subject.registerHandler('DAV:/response', ResponseHandler); + + subject.on('response', function(data) { + events.push(data); + }); + + subject.once('complete', function(data) { + result = data; + }); + + subject.write(xml).close(); + + assert.ok(events[0]); + assert.equal(events.length, 2); + + assert.ok(result); + + assert.deepEqual(events[0], {}); + assert.deepEqual(events[1], expectedEvent); + assert.deepEqual(result, expectedResult); }); - parser.write(data).close(); }); }); |