aboutsummaryrefslogtreecommitdiffstats
path: root/test/webcals/sax_test.js
diff options
context:
space:
mode:
authorJames Lal <james@lightsofapollo.com>2012-06-19 14:01:01 -0700
committerJames Lal <james@lightsofapollo.com>2012-06-19 14:01:01 -0700
commit936e76b8945c6f310ad893905e08728715620e38 (patch)
tree39c969cb8739e26e3c4f628b50f1afffa8330dcc /test/webcals/sax_test.js
parentf1ae9e6f2a4503b9c5ab55abb13d95cbd9ee753c (diff)
downloadjsCalDAV-936e76b8945c6f310ad893905e08728715620e38.tar.gz
v1 sax parser
Diffstat (limited to 'test/webcals/sax_test.js')
-rw-r--r--test/webcals/sax_test.js387
1 files changed, 323 insertions, 64 deletions
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();
});
});