Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

move tests to nodeunit

simpler unit testing
  • Loading branch information...
commit 5e32b62724ce677e364907efebd07a18d51ec0a9 1 parent 33d7592
@defunctzombie defunctzombie authored
Showing with 974 additions and 1,377 deletions.
  1. +1 −0  .gitignore
  2. +1 −0  Makefile
  3. +5 −1 package.json
  4. +0 −81 spec/helpers.js
  5. +0 −142 spec/spec_element.js
  6. +0 −54 spec/spec_html_parser.js
  7. +0 −25 spec/spec_main.js
  8. +0 −82 spec/spec_node_attributes.js
  9. +0 −27 spec/spec_ref_integrity.js
  10. +0 −137 spec/spec_searching.js
  11. +0 −60 spec/spec_text_node.js
  12. +0 −168 spec/spec_traversal.js
  13. +0 −70 spec/spec_xml_parser.js
  14. +0 −115 spec/spec_xml_sax_parser.js
  15. +0 −227 spec/tacular.js
  16. +50 −43 spec/spec_attribute.js → test/attribute.js
  17. +60 −98 spec/spec_document.js → test/document.js
  18. +139 −0 test/element.js
  19. 0  {spec → test}/fixtures/errors/comment.xml
  20. 0  {spec → test}/fixtures/parser.html
  21. 0  {spec → test}/fixtures/parser.xml
  22. 0  {spec → test}/fixtures/sax_parser.xml
  23. 0  {spec → test}/fixtures/warnings/amp.html
  24. 0  {spec → test}/fixtures/warnings/ent9.xml
  25. +50 −0 test/html_parser.js
  26. +10 −0 test/main.js
  27. +53 −47 spec/spec_namespaces.js → test/namespace.js
  28. +75 −0 test/node_attributes.js
  29. +27 −0 test/ref_integrity.js
  30. +116 −0 test/searching.js
  31. +27 −0 test/text_node.js
  32. +99 −0 test/traversal.js
  33. +67 −0 test/xml_parser.js
  34. +194 −0 test/xml_sax_parser.js
View
1  .gitignore
@@ -1,3 +1,4 @@
.lock-wscript
*.swp
build/
+node_modules/
View
1  Makefile
@@ -15,3 +15,4 @@ clean:
test: $(target)
npm test
+.PHONY: test clean
View
6 package.json
@@ -6,7 +6,7 @@
, "scripts" :
{ "install" : "node-waf configure build"
, "preuninstall" : "node-waf clean"
- , "test" : "node --expose_gc spec/tacular.js"
+ , "test" : "node --expose_gc ./node_modules/nodeunit/bin/nodeunit test"
}
, "repository" :
{ "type" : "git"
@@ -18,4 +18,8 @@
}
, "main" : "./index"
, "engines" : { "node" : ">=0.1.90" }
+, "devDependencies":
+ {
+ "nodeunit": "*"
+ }
}
View
81 spec/helpers.js
@@ -1,81 +0,0 @@
-exports.gc = (typeof gc == 'undefined') ? function() {} : gc;
-
-exports.libxml = require('../index');
-
-exports.clone = function(obj) {
- if(obj == null || typeof(obj) != 'object')
- return obj;
-
- var temp = new obj.constructor();
- for(var key in obj)
- temp[key] = arguments.callee(obj[key]);
-
- return temp;
-};
-
-exports.callbackTest = {
- startDocument: [],
- endDocument: [],
-
- startElementNS: [],
- endElementNS: [],
-
- characters: [],
- cdata: [],
-
- comment: [],
-
- warning: [],
- error: []
-};
-
-exports.callbackControl = {
- startDocument: [
- []
- ],
-
- endDocument: [
- []
- ],
-
- startElementNS: [
- ['error', [], null, null, []],
- ['stream', [['to', '', '', 'example.com'], ['version', '', '', '1.0']], 'stream', 'http://etherx.jabber.org/streams', [[null, 'jabber:client'], ['stream', 'http://etherx.jabber.org/streams']]],
- ['message', [['type', '', '', 'chat'], ['to', '', '', 'n@d'], ['from', '', '', 'n@d/r'], ['id', '', '', 'id1']], null, 'jabber:client', []],
- ['x', [['name', '', '', 'abc & xyz']], null, 'jabber:client', []],
- ['body', [], null, 'jabber:client', []],
- ['html', [], null, 'http://jabber.org/protocol/xhtml-im', [[null, 'http://jabber.org/protocol/xhtml-im']]],
- ['body', [], null, 'http://www.w3.org/1999/xhtml', [[null, 'http://www.w3.org/1999/xhtml']]],
- ['prefixed', [], 'stream', 'http://etherx.jabber.org/streams', []]
- ],
-
- endElementNS: [
- ['x', null, 'jabber:client'],
- ['body', null, 'jabber:client'],
- ['body', null, 'http://www.w3.org/1999/xhtml'],
- ['html', null, 'http://jabber.org/protocol/xhtml-im'],
- ['message', null, 'jabber:client'],
- ['prefixed', 'stream', 'http://etherx.jabber.org/streams'],
- ['stream', 'stream', 'http://etherx.jabber.org/streams']
- ],
-
- characters: [
- ['ABC '], ['&'], ['&'], [' XYZ'], ['exit'], ['exit']
- ],
-
- cdata: [
- [' some cdata ']
- ],
-
- comment: [
- [' comment ']
- ],
-
- warning: [
- ["xmlParsePITarget: invalid name prefix 'xml'\n"]
- ],
-
- error: [
- ["Premature end of data in tag error line 2\n"]
- ]
-};
View
142 spec/spec_element.js
@@ -1,142 +0,0 @@
-with(require('./helpers')) {
-
-describe('An element node', function() {
- var doc = null;
- var elem = null;
-
- beforeEach(function() {
- doc = new libxml.Document();
- elem = doc.node('name1');
- });
-
- it('can be created', function() {
- assert.equal('name1', elem.name());
- });
-
- it('can be created with a callback', function() {
- doc = new libxml.Document();
- var elem = false;
- doc.node('name2', function(n) { elem = n; });
- assert.equal('name2', elem.name());
- });
-
- it('can be created as the document root', function() {
- assert.equal('name1', doc.root().name());
- });
-
- it('can be built by nesting', function() {
- var doc = new libxml.Document();
- var levels = 0;
- doc.node('root', function(n) {
- levels++;
- assert.equal('root', n.name());
- n.node('child', function(n) {
- levels++;
- assert.equal('child', n.name());
- var grandchild = n.node('grandchild', function() { levels++; });
- assert.equal('grandchild', grandchild.name());
- });
- });
- assert.equal(3, levels);
- });
-
- it('can have content assigned after creation', function() {
- assert.equal("", elem.text());
- elem.text('content');
- assert.equal('content', elem.text());
- });
-
- it('can undergo a namechange after creation', function() {
- assert.equal('name1', elem.name());
- elem.name('newname');
- assert.equal('newname', elem.name());
- });
-
- it('can have a child added to it', function() {
- var newChild = new libxml.Element(elem.doc(), 'new-child');
- elem.addChild(newChild);
- assert.ok(doc.get('/name1/new-child'));
- });
-
- it('can describe its path', function() {
- var gchild = null, sibling = null;
- var doc = new libxml.Document(function(n) {
- n.node('root', function(n) {
- n.node('child', function(n) {
- gchild = n.node('grandchild');
- });
- sibling = n.node('child');
- });
- });
- assert.equal('/root/child[1]/grandchild', gchild.path());
- assert.equal('/root/child[2]', sibling.path());
- });
-
- it('knows that its type is "element"', function() {
- assert.ok('element', elem.type());
- });
-
- it('can print itself using #toString', function() {
- assert.equal('<name1/>', elem.toString());
- elem.node('child');
- assert.equal('<name1><child/></name1>', elem.toString());
- });
-
- it('can remove itself from the document', function() {
- var child = elem.node('child');
- assert.ok(doc.get('/name1/child'));
-
- child.remove();
- assert.ok(!doc.get('/name1/child'));
- });
-
- it('can be moved from one place to another', function() {
- var child = elem.node('child');
- assert.ok(doc.get('/name1/child'));
-
- child.remove();
- var name2 = elem.node('name2');
- name2.node(child);
- assert.ok(!doc.get('/name1/child'));
- assert.ok(doc.get('/name1/name2/child'));
- });
-
- it('can add a previous sibling', function() {
- var child1 = elem.node('child1');
- var child2 = elem.node('child2');
- assert.equal(elem.childNodes().length, 2);
- var prevSibling = new libxml.Element(elem.doc(), 'prev-sibling');
- var addedSibling = child2.addPrevSibling(prevSibling);
- var children = elem.childNodes();
- assert.equal(3, children.length);
- assert.equal('prev-sibling', children[1].name());
- });
-
- it('can add a next sibling', function() {
- var child1 = elem.node('child1');
- var child2 = elem.node('child2');
- assert.equal(elem.childNodes().length, 2);
- var nextSibling = new libxml.Element(elem.doc(), 'next-sibling');
- var addedSibling = child1.addNextSibling(nextSibling);
- var children = elem.childNodes();
- assert.equal(3, children.length);
- assert.equal('next-sibling', children[1].name());
- });
-
- it('can be imported to another document', function() {
- var child1 = elem.node('child1');
- doc = child1.doc();
-
- var newdoc = new libxml.Document();
- newdoc.node('newdoc');
- newdoc.root().addChild(child1);
-
- assert.ok(newdoc);
- assert. notEqual(doc, newdoc, true);
- assert.equal('child1', newdoc.root().childNodes()[0].name());
- gc();
- assert.equal(child1, elem.childNodes()[0]); // child1 is the the first child of elem
- });
-});
-
-}
View
54 spec/spec_html_parser.js
@@ -1,54 +0,0 @@
-with(require('./helpers')) {
-
-var recoverableFile = path.dirname(__filename)+'/fixtures/warnings/amp.html';
-var recoverableErrors = [
- { domain: 5,
- code: 23,
- message: "htmlParseEntityRef: expecting ';'\n",
- level: 2,
- line: 12,
- column: 27 },
- { domain: 5,
- code: 68,
- message: "htmlParseEntityRef: no name\n",
- level: 2,
- line: 12,
- column: 38 },
- { domain: 5,
- code: 23,
- message: "htmlParseEntityRef: expecting ';'\n",
- level: 2,
- line: 14,
- column: 4 },
- { domain: 5,
- code: 68,
- message: "htmlParseEntityRef: no name\n",
- level: 2,
- line: 15,
- column: 4 }
-];
-
-describe('Parsing HTML', function() {
- var filename = path.dirname(__filename)+'/fixtures/parser.html';
-
- it('can be done by string', function() {
- var str = fs.readFileSync(filename, 'utf8');
-
- var doc = libxml.parseHtmlString(str);
- assert.equal('html', doc.root().name());
- assert.equal('Test HTML document', doc.get('head/title').text());
- assert.equal('HTML content!', doc.get('body/span').text());
- });
-});
-
-describe('A recoverable parse error when parsing an HTML string', function() {
- var str = fs.readFileSync(recoverableFile, 'utf8');
-
- it('will attach the errors to the document', function() {
- var doc = libxml.parseHtmlString(str);
- assert.equal(4, doc.errors.length);
- assert.deepEqual(recoverableErrors, doc.errors);
- });
-});
-
-}
View
25 spec/spec_main.js
@@ -1,25 +0,0 @@
-with(require('./helpers')) {
-
-describe('libxmljs', function() {
- it('has a version number', function() {
- if (specVerbose) print(libxml.version+" ");
- assert.ok(typeof libxml.version == 'string');
- });
-
- it('knows the libxml version number', function() {
- if (specVerbose) print(libxml.libxml_version+" ");
- assert.ok(typeof libxml.libxml_version == 'string');
- });
-
- it('knows the libxml parser version number', function() {
- if (specVerbose) print(libxml.libxml_parser_version+" ");
- assert.ok(typeof libxml.libxml_parser_version == 'string');
- });
-
- it('knows if libxml debugging is enabled', function() {
- if (specVerbose) print(libxml.libxml_debug_enabled+" ");
- assert.ok(typeof libxml.libxml_debug_enabled == 'boolean');
- });
-});
-
-}
View
82 spec/spec_node_attributes.js
@@ -1,82 +0,0 @@
-with(require('./helpers')) {
-
-describe('A node attribute', function() {
- it('can be read', function() {
- // reading a node is implied during all tests
- var doc = new libxml.Document();
- elem = doc.node('name');
- new libxml.Attribute(elem, 'to', 'wongfoo');
- assert.equal('wongfoo', elem.attr('to').value());
- });
-
- it('will return null when an attr is not found', function() {
- var doc = new libxml.Document();
- elem = doc.node('name');
- assert.equal(null, elem.attr('to'));
- });
-
- it('can be assigned on creation', function() {
- var doc = new libxml.Document();
- var elem = doc.node('name', {to: 'wongfoo'});
- assert.equal('wongfoo', elem.attr('to').value());
- });
-
- it('can be assigned with an object', function() {
- var doc = new libxml.Document();
- elem = doc.node('name');
- elem.attr({to: 'wongfoo'});
- assert.equal('wongfoo', elem.attr('to').value());
- });
-
- it('can be reassigned', function() {
- var doc = new libxml.Document();
- var elem = doc.node('name', {to: 'wongfoo'});
- assert.equal('wongfoo', elem.attr('to').value());
- elem.attr({to: 'julie newmar'});
- assert.equal('julie newmar', elem.attr('to').value());
- });
-
- it('can be retrieved in an array', function() {
- var doc = new libxml.Document();
- var elem = doc.node('root');
-
- assert.deepEqual([], elem.attrs());
-
- elem.attr({foo: 'bar'})
- .attr({bar: 'baz'})
- .attr({baz: 'foo'});
-
- var attrs = [elem.attr('foo'), elem.attr('bar'), elem.attr('baz')];
- var i;
- for (i = 0; i < 3; i++)
- assert.equal(attrs[i], elem.attrs()[i]);
- });
-
- it('can traverse over its siblings', function() {
- var doc = new libxml.Document();
- var elem = doc.node('root')
- .attr({foo: 'bar'})
- .attr({bar: 'baz'})
- .attr({baz: 'foo'});
- assert.equal(elem.attr('baz'), elem.attr('bar').nextSibling());
- assert.equal(elem.attr('foo'), elem.attr('bar').prevSibling());
- assert.equal(null, elem.attr('foo').prevSibling());
- assert.equal(null, elem.attr('baz').nextSibling());
- });
-
- it('can get back to its node', function() {
- var doc = new libxml.Document();
- var elem = doc.node('root')
- .attr({foo: 'bar'});
- assert.equal(elem, elem.attr('foo').parent());
- });
-
- it('can get back to its document', function() {
- var doc = new libxml.Document();
- var elem = doc.node('root')
- .attr({foo: 'bar'});
- assert.equal(doc, elem.attr('foo').doc());
- });
-});
-
-}
View
27 spec/spec_ref_integrity.js
@@ -1,27 +0,0 @@
-with(require('./helpers')) {
-
-describe('Referential Integrity', function() {
-
- it("doesn't crash", function() {
- var doc = new libxml.Document();
- doc.node('root').node('child').node('grandchild').parent().node('child2');
- gc();
- assert.ok(doc, "doc");
- gc();
- assert.ok(doc.root(), "root");
- gc();
- assert.equal("child", doc.root().childNodes()[0].name(), "child name");
- });
-
- it("Keeps references", function() {
- var nodes = libxml.parseXmlString('<root> <child> <grandchildren/> </child> <child2/> </root>').childNodes();
-
- gc();
-
- assert.ok(nodes[0].doc());
- assert.equal("child", nodes[1].name(), "child name");
- });
-
-});
-
-}
View
137 spec/spec_searching.js
@@ -1,137 +0,0 @@
-with(require('./helpers')) {
-
-describe('Finding a node', function() {
- it('can be done with #get', function() {
- var child = null;
- var doc = new libxml.Document(function(n) {
- n.node('root', function(n) { child = n.node('child'); });
- });
- assert.equal(child, doc.get('child'));
- });
-
- it('can be done with #find', function() {
- var children = [];
- var doc = new libxml.Document();
- doc.node('root', function(n) {
- children.push(n.node('child'));
- children.push(n.node('child'));
- });
- var results = doc.find('child');
- assert.equal(2, children.length);
- assert.equal(2, results.length);
- for (child = 0; child < children.length; child++)
- assert.equal(children[child], results[child]);
- });
-
- it('can be nested', function() {
- var grandchild = null;
- var doc = new libxml.Document();
- doc.node('root', function(n) {
- n.node('child', function(n) {
- grandchild = n.node('grandchild');
- });
- });
- assert.equal(grandchild, doc.get('child').get('grandchild'));
- });
-});
-
-describe('Finding a non-prefix namespaced node', function() {
- var uri = 'nsuri';
-
- it('can be done with #get', function() {
- var child = null;
- var doc = new libxml.Document(function(n) {
- n.node('root', function(n) {
- child = n.node('child');
- child.namespace(uri);
- });
- });
-
- assert.equal(child, doc.get('xmlns:child', uri));
- });
-
- it('can be done with #find', function() {
- var children = [];
- var doc = new libxml.Document();
- doc.node('root', function(n) {
- children.push(n.node('child'));
- children.push(n.node('child'));
- });
-
- var ns = children[0].namespace(uri);
- children[1].namespace(ns);
-
- var results = doc.find('xmlns:child', uri);
- assert.equal(2, children.length);
- assert.equal(2, results.length);
- for (child = 0; child < children.length; child++)
- assert.equal(children[child], results[child]);
- });
-
- it('can be nested', function() {
- var grandchild = null;
- var doc = new libxml.Document();
- doc.node('root', function(n) {
- n.node('child', function(n) {
- grandchild = n.node('grandchild');
- grandchild.namespace(uri);
- });
- });
-
- assert.equal(grandchild, doc.get('child').get('xmlns:grandchild', uri));
- });
-});
-
-describe('Finding a prefixed namespaced node', function() {
- var prefix = 'pefname',
- uri = 'nsuri';
-
- var ns_parms = {};
- ns_parms[prefix] = uri;
-
- it('can be done with #get', function() {
- var child = null;
- var doc = new libxml.Document(function(n) {
- n.node('root', function(n) {
- child = n.node('child');
- child.namespace(prefix, uri);
- });
- });
-
- assert.equal(child, doc.get(prefix+':child', ns_parms));
- });
-
- it('can be done with #find', function() {
- var children = [];
- var doc = new libxml.Document();
- doc.node('root', function(n) {
- children.push(n.node('child'));
- children.push(n.node('child'));
- });
-
- var ns = children[0].namespace(prefix, uri);
- children[1].namespace(ns);
-
- var results = doc.find(prefix+':child', ns_parms);
- assert.equal(2, children.length);
- assert.equal(2, results.length);
- for (child = 0; child < children.length; child++)
- assert.equal(children[child], results[child]);
- });
-
- it('can be nested', function() {
- var grandchild = null;
- var doc = new libxml.Document();
- doc.node('root', function(n) {
- n.node('child', function(n) {
- grandchild = n.node('grandchild');
- grandchild.namespace(prefix, uri);
- });
- });
-
- assert.equal(grandchild,
- doc.get('child').get(prefix+':grandchild', ns_parms));
- });
-});
-
-}
View
60 spec/spec_text_node.js
@@ -1,60 +0,0 @@
-with(require('./helpers')) {
-
-describe('Text node', function() {
- var doc = null;
-
- beforeEach(function() {
- doc = libxml.parseXmlString(
- '<?xml version="1.0"?>\
- <root>child</root>\
- ');
- });
-
- it('knows its type is "text"', function() {
- assert.equal('text', doc.child().type());
- });
-
- it('knows its name is "text"', function() {
- assert.equal('text', doc.child().name());
- });
-});
-
-describe('Comment node', function() {
- var doc = null;
-
- beforeEach(function() {
- doc = libxml.parseXmlString(
- '<?xml version="1.0"?>\
- <root><!-- comment --></root>\
- ');
- });
-
- it('knows its type is "comment"', function() {
- assert.equal('comment', doc.child().type());
- });
-
- it('knows its name is "comment"', function() {
- assert.equal('comment', doc.child().name());
- });
-});
-
-describe('CDATA node', function() {
- var doc = null;
-
- beforeEach(function() {
- doc = libxml.parseXmlString(
- '<?xml version="1.0"?>\
- <root><![CDATA[cdata text]]></root>\
- ');
- });
-
- it('knows its type is "cdata"', function() {
- assert.equal('cdata', doc.child().type());
- });
-
- it('has no name', function() {
- assert.equal(undefined, doc.child().name());
- });
-});
-
-}
View
168 spec/spec_traversal.js
@@ -1,168 +0,0 @@
-with(require('./helpers')) {
-
-describe('Document traversal on a built document', function() {
- it('can get to the document from any node', function() {
- var gchild = null;
- var doc = new libxml.Document(function(n) {
- n.node('root', function(n) {
- n.node('child', function(n) {
- gchild = n.node('grandchild');
- });
- });
- });
-
- assert.equal(doc, gchild.doc());
- });
-
- it('can travel down the tree', function() {
- var gchild = null;
- doc = new libxml.Document(function(n) {
- n.node('root', function(n) {
- n.node('child', function(n) {
- gchild = n.node('grandchild');
- });
- });
- });
- // uses the specific and default syntax for returning a child.
- assert.equal(gchild, doc.child(1).child());
- });
-
- it('can travel across siblings', function() {
- var sibling = null;
- doc = new libxml.Document(function(n) {
- n.node('root', function(n) {
- n.node('child');
- sibling = n.node('sibling');
- });
- });
- assert.equal(sibling, doc.child(2));
- });
-
- it('can travel up the tree', function() {
- var gchild = null, child = null;
- var doc = new libxml.Document(function(n) {
- n.node('root', function(n) {
- child = n.node('child', function(n) {
- gchild = n.node('grandchild');
- });
- });
- });
- assert.equal(doc, doc.root().parent());
- assert.equal(child, gchild.parent());
- });
-
- it('will list children', function() {
- var children = [];
- var doc = new libxml.Document(function(n) {
- n.node('root', function(n) {
- children.push(n.node('child'));
- children.push(n.node('sibling1'));
- children.push(n.node('sibling2'));
- });
- });
- var i;
- assert.equal(children.length, doc.childNodes().length);
- for (i = 0; i < children.length; i++)
- assert.equal(children[i], doc.child(i+1));
- });
-
- it('can traverse siblings', function() {
- var children = [];
- var doc = new libxml.Document(function(n) {
- n.node('root', function(n) {
- children.push(n.node('prevSibling'));
- children.push(n.node('child'));
- children.push(n.node('nextSibling'));
- });
- });
- assert.equal(children[0], children[1].prevSibling());
- assert.equal(children[2], children[1].nextSibling());
- assert.equal(null, children[0].prevSibling());
- assert.equal(null, children[2].nextSibling());
- });
-});
-
-describe('Document traversal on a parsed document', function() {
- it('can get to the document from any node', function() {
- var doc = libxml.parseXmlString(
- '<?xml version="1.0"?>\
- <root><child><grandchild /></child></root>\
- ');
- assert.equal(doc, doc.child(0).child(1).doc());
- });
-
- it('can travel down the tree', function() {
- var doc = libxml.parseXmlString(
- '<?xml version="1.0"?>\
- <root><child><grandchild /></child></root>\
- ');
- // uses the specific and default syntax for returning a child.
- assert.equal('grandchild', doc.child(1).child().name());
- });
-
- it('can travel across siblings', function() {
- var doc = libxml.parseXmlString(
- '<?xml version="1.0"?>\
- <root><child /><sibling /></root>\
- ');
- assert.equal('sibling', doc.child(2).name());
- });
-
- it('can travel up the tree', function() {
- var gchild = null, child = null;
- var doc = libxml.parseXmlString(
- '<?xml version="1.0"?>\
- <root><child><grandchild /></child></root>\
- ');
- assert.equal(doc, doc.root().parent());
- assert.equal('child', doc.child().child().parent().name());
- });
-
- it('will list children', function() {
- var doc = libxml.parseXmlString(
- '<?xml version="1.0"?>\
- <root><child /><sibling1 /><sibling2 /></root>\
- ');
- var children = ['child', 'sibling1', 'sibling2'];
- var i;
- assert.equal(3, doc.childNodes().length);
- for (i = 0; i < children.length; i++)
- assert.equal(children[i], doc.child(i+1).name());
- });
-
- it('can traverse siblings', function() {
- var doc = libxml.parseXmlString(
- '<?xml version="1.0"?>\
- <root><prevSibling /><child /><nextSibling /></root>\
- ');
-
- var children = ['prevSibling', 'child', 'nextSibling'];
- var child = doc.child(2);
- assert.equal('child', child.name());
- assert.equal(children[0], child.prevSibling().name());
- assert.equal(children[2], child.nextSibling().name());
- assert.equal(null, child.prevSibling().prevSibling());
- assert.equal(null, child.nextSibling().nextSibling());
- });
-
- it('can traverse sibling elements', function() {
- var doc = libxml.parseXmlString(
- '<?xml version="1.0"?>\
- <root>\
- <prevSibling />\
- <child />\
- <nextSibling />\
- </root>\
- ');
-
- var children = ['prevSibling', 'child', 'nextSibling'];
- var child = doc.child(4);
- assert.equal('child', child.name());
- assert.equal(children[0], child.prevElement().name());
- assert.equal(children[2], child.nextElement().name());
- assert.equal(null, child.prevElement().prevElement());
- assert.equal(null, child.nextElement().nextElement());
- });
-});
-
-}
View
70 spec/spec_xml_parser.js
@@ -1,70 +0,0 @@
-with(require('./helpers')) {
-
-describe('Parsing XML', function() {
- var filename = path.dirname(__filename)+'/fixtures/parser.xml';
-
- it('can be done by string', function() {
- var str = fs.readFileSync(filename, 'utf8');
-
- var doc = libxml.parseXmlString(str);
- assert.equal('1.0', doc.version());
- assert.equal('UTF-8', doc.encoding());
- assert.equal('root', doc.root().name());
- assert.equal('child', doc.get('child').name());
- assert.equal('grandchild', doc.get('child').get('grandchild').name());
- assert.equal('with love', doc.get('child/grandchild').text());
- assert.equal('sibling', doc.get('sibling').name());
- assert.equal('with content!', doc.get('sibling').text());
- assert.equal(str, doc.toString());
- });
-});
-
-describe('A fatal parse error when parsing an XML string', function() {
- var filename = path.dirname(__filename)+'/fixtures/errors/comment.xml';
- var str = fs.readFileSync(filename, 'utf8');
- var err = null;
-
- it('will throw an exception', function() {
- try {
- libxml.parseXmlString(str);
- } catch(e) { err = e; }
-
- var errorControl = {
- domain: 1,
- code: 4,
- message: "Start tag expected, '<' not found\n",
- level: 3,
- file: null,
- line: 5,
- str1: null,
- str2: null,
- str3: null,
- int1: null,
- column: 10
- };
- assert.equal(errorControl.code, err.code);
- });
-});
-
-describe('A recoverable parse error when parsing an XML string', function() {
- var filename = path.dirname(__filename)+'/fixtures/warnings/ent9.xml';
- var str = fs.readFileSync(filename, 'utf8');
-
- it('will attach the errors to the document', function() {
- var doc = libxml.parseXmlString(str);
- var err = {
- domain: 3,
- code: 201,
- message: "Namespace prefix prefix on node is not defined\n",
- level: 2,
- line: 1,
- str1: "prefix",
- str2: "node",
- column: 13
- };
- assert.equal(1, doc.errors.length);
- assert.deepEqual(err, doc.errors.shift());
- });
-});
-
-}
View
115 spec/spec_xml_sax_parser.js
@@ -1,115 +0,0 @@
-with(require('./helpers')) {
-
-describe("SAX Push Parser", function() {
- var callbacks = {};
- var filename = path.dirname(__filename)+'/fixtures/sax_parser.xml';
-
- function createParser(parserType) {
- var parser = new libxml[parserType](function(cb) {
- function argsToArray(args) {
- var ary = [];
- var i;
- for (i = 0; i < args.length; i++)
- ary.push(args[i]);
- return ary;
- }
-
- cb.onStartDocument(function() {
- callbacks.startDocument.push(argsToArray(arguments));
- });
-
- cb.onEndDocument(function() {
- callbacks.endDocument.push(argsToArray(arguments));
- });
-
- cb.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {
- // p({e: elem, a: attrs, p: prefix, u: uri, n: namespaces});
- callbacks.startElementNS.push(argsToArray(arguments));
- });
-
- cb.onEndElementNS(function(elem, prefix, uri) {
- callbacks.endElementNS.push(argsToArray(arguments));
- });
-
- cb.onCharacters(function(chars) {
- if (!chars.match(/^[\s\n\r]+$/))
- callbacks.characters.push(argsToArray(arguments));
- });
-
- cb.onCdata(function(cdata) {
- callbacks.cdata.push(argsToArray(arguments));
- });
-
- cb.onComment(function(msg) {
- callbacks.comment.push(argsToArray(arguments));
- });
-
- cb.onWarning(function(msg) {
- callbacks.warning.push(argsToArray(arguments));
- });
-
- cb.onError(function(msg) {
- callbacks.error.push(argsToArray(arguments));
- });
- });
- return parser;
- }
-
- beforeEach(function() {
- callbacks = clone(callbackTest);
- });
-
- it('will properly parse a regular string', function() {
- var str = fs.readFileSync(filename, 'utf8');
- var parser = createParser('SaxParser');
- parser.parseString(str);
- var control = JSON.stringify(callbackControl);
- var test = JSON.stringify(callbacks);
- assert.equal(control, test);
- });
-
- it('will properly parse a string chunk by chunk', function() {
- var str_ary = fs.readFileSync(filename, 'utf8').split("\n");
- var parser = createParser('SaxPushParser');
- var i;
- for (i = 0; i < str_ary.length; i++)
- parser.push(str_ary[i], (i+1 == str_ary.length));
-
- var control = clone(callbackControl);
- control.error = [["Extra content at the end of the document\n"]];
- assert.equal(JSON.stringify(control), JSON.stringify(callbacks));
- });
-
- it('will properly parse a file', function() {
- var parser = createParser('SaxParser');
- parser.parseFile(filename);
-
- var control = clone(callbackControl);
- assert.deepEqual(control, callbacks);
- });
-
- it('can can be reused as a string parser', function() {
- var str = fs.readFileSync(filename, 'utf8');
- var parser = createParser('SaxParser');
-
- for (var i=0; i<10; i++) {
- gc();
- parser.parseString(str);
- }
-
- assert.ok(true);
- });
-
- it('can can be reused as a file parser', function() {
- var parser = createParser('SaxParser');
-
- for (var i=0; i<10; i++) {
- gc();
- parser.parseFile(filename);
- }
-
- assert.ok(true);
- });
-});
-
-}
View
227 spec/tacular.js
@@ -1,227 +0,0 @@
-/*
- * spec/tacular
- *
- * A minimal spec library for node.js for testing both syncronous and asyncronous code.
- *
- * To run:
- *
- * 1. Place this file in your project's spec directory.
- * 2. Run with "node spec/tacular.js" to test all files beginning with the word "spec".
- *
- * Options:
- *
- * "=verbose": Use verbose mode showing description, tests, and status for each spec.
- *
- * Synopsis:
- *
- * var foo = require("./../lib/foo"); // the file you are specing
- *
- * describe("A foo", function() {
- * beforeEach(function() {
- * // setup goes here
- * });
- *
- * afterEach(function() {
- * // teardown goes here
- * });
- *
- * // Supply a function that takes no arguments for a syncronous test
- * it("should do X", function() {
- * // place code and assertions here
- * // assert module is available
- assert.ok(true);
- * });
- *
- * // Supply a function that takes one argument, a promise, to
- * // use an async test
- * it("should fail async", function(promise){
- * promise.addCallback(function(){
- * assert.ok(false);
- * })
- * setTimeout(function(){ promise.emitSuccess() }, 500);
- * });
- *
- * });
- *
- * Copyright 2009, Jon Crosby, MIT Licensed
- *
- */
-
-(function() {
-with(require('sys')) {
- if (typeof gc == 'undefined') var gc = function() {}
- if (process.argv) { process.ARGV = process.argv; }
-
- var assert = require('assert');
- var path = require('path');
- var fs = require('fs');
- var specCount = 0;
- var specStack = [];
- var specFailures = [];
- var specVerbose = process.argv.join(";").match(/;(--verbose|-v)/);
-
- var describe = function(name, func) {
- specStack.push(name);
- if (specVerbose) print(name);
- specBeforeEach = specAfterEach = function() {};
- func();
- if (specVerbose) print("\n\n");
- specStack.pop();
- gc();
- };
-
- var it = function(name, func) {
- specCount++;
- specStack.push(name);
- if (specVerbose) print("\n "+name+" : ");
- specBeforeEach();
- try {
- if ( func.length == 0 ){
- func();
- } else {
- var promise = new process.Promise();
- func(promise);
- promise.addErrback(function(e){ throw e });
- promise.wait();
- }
- }
- catch(e) { if (e != 'fail') specError(e); }
- specStack.pop();
- specAfterEach();
- gc();
- };
-
- var specError = function(message) {
- print(specVerbose ? "Error ("+message+") " : "E");
- specFailures.push(specStack.join(" ") + "\n" + message);
- };
-
- var specFail = function(message) {
- print(specVerbose ? "Fail " : "F");
- specFailures.push(specStack.join(" ") + "\n" + message);
- };
-
- var specPass = function() {
- print(specVerbose ? "Pass " : ".");
- };
-
- var inspectObject = function(obj) {
- if(obj === null) return "null";
- var elements = [], orderedProperties = [], i;
- for(property in obj) {
- orderedProperties.push(property);
- }
- orderedProperties = orderedProperties.sort();
- for(i = 0; i < orderedProperties.length; i++) {
- elements.push([orderedProperties[i], inspect(obj[orderedProperties[i]])].join(":"));
- }
- return "{" + elements.join(",") + "}";
- };
-
- var inspectArray = function(arr) {
- var elements = [];
- if(arr.length == 0) return "[]";
- for(var i = 0; i < arr.length; i++) {
- elements.push(inspect(arr[i]));
- }
- return "[" + elements.join(",") + "]";
- };
-
- var inspectDate = function(date) {
- return date.toString();
- };
-
- var inspectString = function(str) {
- return '"' + str + '"';
- };
-
- var inspectBoolean = function(bool) {
- return bool;
- };
-
- var inspectNumber = function(num) {
- return num;
- };
-
- var inspect = function(element) {
- switch(typeof(element)) {
- case "object":
- if(element instanceof Array) {
- return inspectArray(element);
- } else if (element instanceof Date) {
- return inspectDate(element);
- } else {
- return inspectObject(element);
- }
- break;
- case "string":
- return inspectString(element);
- break;
- case "number":
- return inspectNumber(element);
- break;
- case "boolean":
- return inspectBoolean(element);
- break;
- default:
- if(element === undefined) {
- return "undefined";
- } else {
- return element.toString();
- }
- }
- };
-
- var beforeEach = function(func) {
- specBeforeEach = func;
- };
-
- var afterEach = function(func) {
- specAfterEach = func;
- };
-
- var summarize = function() {
- var plural = function(word, isPlural) {
- return word + (isPlural ? "s" : "");
- };
-
- var specSummary = function() {
- return [specCount, plural("example", specCount != 1)].join(" ");
- };
-
- var failSummary = function() {
- return [specFailures.length, plural("failure", specFailures.length != 1)].join(" ");
- };
-
- var summary = function() {
- return [specSummary(), failSummary()].join(", ");
- };
-
- if(specFailures.length) {
- var i;
- for(i = 1; i <= specFailures.length; i++) {
- puts("" + i + ")");
- puts(specFailures[i-1]);
- puts("");
- }
- }
- puts(summary());
- };
-
- var specDirectory = path.dirname(__filename);
- var files = fs.readdirSync(specDirectory);
- var i;
- for(i = 0; i < files.length; i++) {
- var file = files[i];
- if(file.match(/^spec/)) {
- if (specVerbose) print(file+"\n");
- var content = fs.readFileSync(specDirectory + "/" + file, "utf8");
- eval(content);
- }
- }
- puts("\n");
- process.addListener("exit", function () {
- summarize();
- });
-}
-})();
View
93 spec/spec_attribute.js → test/attribute.js
@@ -1,73 +1,80 @@
-with(require('./helpers')) {
+var libxml = require('../index');
-describe('Attribute', function() {
- var doc = null;
- var node = null;
- beforeEach(function() {
- doc = new libxml.parseXmlString(
+var body = "<?xml version='1.0' encoding='UTF-8'?>\n" +
+ "<root><node attr-one-key='attr-one-value' attr-two-key='attr-two-value' attr-three-key='attr-three-value' /></root>";
+
+module.exports.new = function(assert) {
+ var doc = libxml.parseXmlString(body);
+ var node = doc.get('node');
+
+ // add new attribute to the node
+ node.attr({'new-attr-key': 'new-attr-value'});
+ assert.equal('new-attr-value', node.attr('new-attr-key').value());
+ assert.done();
+};
+
+module.exports.create_with_namespace = function(assert) {
+ var doc = new libxml.parseXmlString(
"<?xml version='1.0' encoding='UTF-8'?>\n"+
"<root><node attr-one-key='attr-one-value' attr-two-key='attr-two-value' attr-three-key='attr-three-value' /></root>"
);
- node = doc.get('node');
- });
-
- it('can be created without a namespace', function() {
- var attr = new libxml.Attribute(node, 'new-attr-key', 'new-attr-value');
- assert.ok(attr);
- assert.equal(attr.value(), node.attr('new-attr-key').value());
- });
+ var node = doc.get('node');
- it('can be created with a namespace', function() {
var ns = new libxml.Namespace(node, 'ns-prefix', 'ns-uri');
var attr = new libxml.Attribute(node, 'new-attr-key', 'new-attr-value', ns);
+ var attr = node.attr('new-attr-key');
assert.ok(attr);
assert.equal(ns.prefix(), attr.namespace().prefix());
assert.equal(ns.href(), attr.namespace().href());
- });
+ assert.done();
+};
- it('knows its name', function() {
- assert.equal('attr-one-key', node.attr('attr-one-key').name());
- });
+module.exports.getters = function(assert) {
+ var doc = libxml.parseXmlString(body);
+ var node = doc.get('node');
- it('knows its value', function() {
+ assert.equal('attr-one-key', node.attr('attr-one-key').name());
assert.equal('attr-one-value', node.attr('attr-one-key').value());
- });
-
- it('knows its node', function() {
assert.equal('node', node.attr('attr-one-key').node().name());
- });
+ assert.ok('attribute', node.attr('attr-two-key').type());
+
+ // siblings
+ assert.equal('attr-one-key', node.attr('attr-two-key').prevSibling().name());
+ assert.equal('attr-three-key', node.attr('attr-two-key').nextSibling().name());
+
+ assert.done();
+};
+
+module.exports.setters = function(assert) {
+ var doc = libxml.parseXmlString(body);
+ var node = doc.get('node');
- it('can change its value', function() {
node.attr('attr-one-key').value('new-value');
assert.equal(node.attr('attr-one-key').value(), 'new-value');
- });
+ assert.done();
+};
- it('knows its previous sibling', function() {
- assert.equal('attr-one-key', node.attr('attr-two-key').prevSibling().name());
- });
+/*
+module.exports.namespace = function(assert) {
+ var doc = libxml.parseXmlString(body);
+ var node = doc.get('node');
- it('knows its next sibling', function() {
- assert.equal('attr-three-key', node.attr('attr-two-key').nextSibling().name());
- });
-
- it('can set and retrieve its namespace', function() {
var ns = new libxml.Namespace(node, 'ns-prefix', 'ns-uri');
var attr = node.attr('attr-one-key');
attr.namespace(ns);
assert.equal(ns.prefix(), attr.namespace().prefix());
assert.equal(ns.href(), attr.namespace().href());
- });
+ assert.done();
+};
+*/
- it('knows that its type is "attribute"', function() {
- assert.ok('attribute', node.attr('attr-two-key').type());
- });
+module.exports.remove = function(assert) {
+ var doc = libxml.parseXmlString(body);
+ var node = doc.get('node');
- it('can remove itself', function() {
var attr = node.attr('attr-one-key');
assert.ok(node.attr('attr-one-key'));
attr.remove();
assert.ok(!node.attr('attr-one-key'));
- });
-});
-
-}
+ assert.done();
+};
View
158 spec/spec_document.js → test/document.js
@@ -1,148 +1,113 @@
-with(require('./helpers')) {
+var libxml = require('../index');
-describe('A new document', function() {
- it('can be instantiated with nothing', function() {
- var doc = new libxml.Document();
+module.exports.blank = function(assert) {
+ var doc = libxml.Document();
assert.ok(doc);
assert.equal('1.0', doc.version());
- assert.equal(null, doc.encoding());
- });
+ //assert.equal('utf8', doc.encoding());
+ assert.done();
+};
- it('can be instantiated with a version', function() {
- var doc = new libxml.Document('2.0');
+module.exports.version = function(assert) {
+ var doc = libxml.Document('2.0');
assert.ok(doc);
assert.equal('2.0', doc.version());
- assert.equal(null, doc.encoding());
- });
+ //assert.equal('utf8', doc.encoding());
+ assert.done();
+};
- it('can be instantiated with a version and encoding', function() {
- var doc = new libxml.Document('2.0', 'UTF-8');
+module.exports.full = function(assert) {
+ var doc = libxml.Document('2.0', 'UTF-8');
assert.ok(doc);
assert.equal('2.0', doc.version());
assert.equal('UTF-8', doc.encoding());
- });
+ assert.done();
+};
- it('can be instantiated with a callback', function() {
- var doc = null;
- new libxml.Document(function(d) { doc = d; });
- assert.ok(doc);
- assert.equal('1.0', doc.version());
- assert.equal(null, doc.encoding());
- });
-
- it('can be instantiated with a version and a callback', function() {
- var doc = null;
- new libxml.Document('2.0', function(d) { doc = d; });
- assert.ok(doc);
- assert.equal('2.0', doc.version());
- assert.equal(null, doc.encoding());
- });
-
- it('can be instantiated with a version, encoding and callback', function() {
- var doc = null;
- new libxml.Document('2.0', 'UTF-8', function(d) { doc = d; });
- assert.ok(doc);
- assert.equal('2.0', doc.version());
- assert.equal('UTF-8', doc.encoding());
- });
-
- it('is created with a null root node', function() {
- var doc = new libxml.Document();
+module.exports.null_root = function(assert) {
+ var doc = libxml.Document();
assert.equal(null, doc.root());
- });
+ assert.done();
+};
- it('can have a root node attached to it', function() {
- var doc = new libxml.Document();
+module.exports.new_root = function(assert) {
+ var doc = libxml.Document();
var root = doc.node('root');
assert.equal('root', root.name());
assert.equal(root, doc.root());
- });
- it('can return an arbitrary child of the root node', function() {
- var doc = new libxml.Document();
- doc.node('root').node('child-one').parent().node('child-two');
+ root.node('child').parent().node('child');
+ assert.equal(doc.root().name(), doc.get('/root').name());
+ assert.done();
+};
+
+module.exports.one_child = function(assert) {
+ var doc = libxml.Document();
+ var root = doc.node('root').node('child-one').parent().node('child-two');
assert.equal('child-one', doc.child(1).name());
assert.equal('child-two', doc.child(2).name());
- });
+ assert.done();
+};
- it('can return an array of children of the root node', function() {
- var doc = new libxml.Document();
+module.exports.root_children = function(assert) {
+ var doc = libxml.Document();
doc.node('root').node('child-one').parent().node('child-two');
assert.equal('child-one', doc.childNodes()[0].name());
assert.equal('child-two', doc.childNodes()[1].name());
- });
-
- it('returns itself when asked', function() {
- var doc = new libxml.Document();
- assert.equal(doc, doc.document());
- });
+ assert.done();
+};
- it('can under go an encoding change', function() {
- var doc = new libxml.Document();
- assert.ok(doc);
- assert.equal(null, doc.encoding());
- doc.encoding('UTF-8');
- assert.equal('UTF-8', doc.encoding());
- });
-
- it('can find a set by xpath', function() {
- var doc = new libxml.Document();
+module.exports.xpath = function(assert) {
+ var doc = libxml.Document();
doc.node('root').node('child').parent().node('child');
assert.equal(2, doc.find('child').length);
- });
+ assert.done();
+};
- it('can get a child by xpath', function() {
- var doc = new libxml.Document();
+module.exports.xpath_child = function(assert) {
+ var doc = libxml.Document();
doc.node('root').node('child-one').parent().node('child-two');
assert.equal('child-one', doc.get('child-one').name());
assert.equal('child-two', doc.get('child-two').name());
- });
+ assert.done();
+};
- it('knows its root node', function() {
- var doc = new libxml.Document();
- doc.node('root').node('child').parent().node('child');
- assert.equal(doc.root().name(), doc.get('/root').name());
- });
-
- it('can be output as a string', function() {
+module.exports.toString = function(assert) {
var control = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<root><child to="wongfoo"><grandchild from="julie numar">with love</grandchild></child><sibling>with content!</sibling></root>',
''
].join("\n");
- var doc = new libxml.Document(function(n) {
- n.node('root', function(n) {
- n.node('child', {to: 'wongfoo'}, null, function(n) {
- n.node('grandchild', {from: 'julie numar'}, 'with love');
- });
- n.node('sibling', null, 'with content!');
- });
- });
+ var doc = libxml.Document();
+ var root = doc.node('root');
+ root.node('child').attr({to: 'wongfoo'})
+ .node('grandchild', 'with love').attr({from: 'julie numar'})
+ root.node('sibling', 'with content!');
assert.equal(control, doc.toString());
- });
+ assert.done();
+};
- it('can add child nodes', function() {
+module.exports.add_child_nodes = function(assert) {
var gchild = '';
var doc1_string = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<root><child to="wongfoo"><grandchild from="julie numar">with love</grandchild></child><sibling>with content!</sibling></root>',
- ''
].join("\n");
var doc2_string = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<root><child to="wongfoo"></child><sibling>with content!</sibling></root>',
- ''
].join("\n");
var doc1 = libxml.parseXmlString(doc1_string);
var doc2 = libxml.parseXmlString(doc2_string);
doc2.child(0).addChild(doc1.child(0).child(0));
assert.equal(doc1.toString(), doc2.toString());
- });
+ assert.done();
+};
- it('can add a cloned node', function() {
+module.exports.cloned_node = function(assert) {
var gchild_string = '<grandchild from="julie numar">with love</grandchild>';
var doc1_string = [
'<?xml version="1.0" encoding="UTF-8"?>',
@@ -158,9 +123,9 @@ describe('A new document', function() {
var doc1 = libxml.parseXmlString(doc1_string);
var doc2 = libxml.parseXmlString(doc2_string);
-
+
var gchild = doc1.child(0).child(0); //the element to operate on
-
+
doc2.child(0).addChild(gchild); // add gchild clone to doc2, implicit clone
assert.equal(doc1.toString(), doc2.toString()); // both documents should be the same
@@ -168,11 +133,8 @@ describe('A new document', function() {
assert. notEqual(gchild, doc2.child(0).child(0)); // these nodes should be different (cloned)
gchild.remove();
-
- assert.equal(doc2_string, doc1.toString()); //doc1 should be the same as doc2 str
+ assert.equal(doc2_string, doc1.toString()); //doc1 should be the same as doc2 str
assert.equal(doc1_string, doc2.toString()); //doc2 should be the same as doc1 str
- });
-});
-
-}
+ assert.done();
+};
View
139 test/element.js
@@ -0,0 +1,139 @@
+var libxml = require('../index');
+
+module.exports.new = function(assert) {
+ var doc = libxml.Document();
+ var elem = libxml.Element(doc, 'name1');
+ doc.root(elem);
+ assert.equal('name1', elem.name());
+ assert.equal('name1', doc.root().name());
+ assert.done();
+};
+
+module.exports.setters = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name1');
+
+ // change content
+ assert.equal('', elem.text());
+ elem.text('content');
+ assert.equal('content', elem.text());
+
+ // change name
+ assert.equal('name1', elem.name());
+ elem.name('newname');
+ assert.equal('newname', elem.name());
+ assert.done();
+};
+
+module.exports.getters = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name1');
+
+ assert.equal('element', elem.type());
+ //TODO assert.equal(doc, elem.doc());
+ assert.done();
+};
+
+module.exports.remove = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name1');
+ var child = elem.node('child');
+ assert.ok(doc.get('/name1/child'));
+
+ child.remove();
+ assert.ok(!doc.get('/name1/child'));
+
+ assert.done();
+};
+
+module.exports.toString = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name1');
+ assert.equal('<name1/>', elem.toString());
+ elem.node('child');
+ assert.equal('<name1><child/></name1>', elem.toString());
+ assert.done();
+};
+
+module.exports.path = function(assert) {
+ var gchild = null, sibling = null;
+ var doc = libxml.Document();
+ var root = doc.node('root');
+ var gchild = root.node('child').node('grandchild');
+ var sibling = root.node('child');
+ assert.equal('/root/child[1]/grandchild', gchild.path());
+ assert.equal('/root/child[2]', sibling.path());
+ assert.done();
+};
+
+module.exports.move = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name1');
+ var child = elem.node('child');
+ assert.ok(doc.get('/name1/child'));
+
+ child.remove();
+ var name2 = elem.node('name2');
+ name2.addChild(child);
+ assert.ok(!doc.get('/name1/child'));
+ assert.ok(doc.get('/name1/name2/child'));
+ assert.done();
+};
+
+module.exports.addChild = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name1');
+ var newChild = libxml.Element(doc, 'new-child');
+ elem.addChild(newChild);
+ assert.ok(doc.get('/name1/new-child'));
+ assert.done();
+};
+
+module.exports.add_prev_sibling = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name1');
+
+ var child1 = elem.node('child1');
+ var child2 = elem.node('child2');
+ assert.equal(elem.childNodes().length, 2);
+ var prevSibling = libxml.Element(doc, 'prev-sibling');
+ var addedSibling = child2.addPrevSibling(prevSibling);
+ var children = elem.childNodes();
+ assert.equal(3, children.length);
+ assert.equal('prev-sibling', children[1].name());
+ assert.done();
+};
+
+module.exports.add_next_sibling = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name1');
+
+ var child1 = elem.node('child1');
+ var child2 = elem.node('child2');
+ assert.equal(elem.childNodes().length, 2);
+ var nextSibling = libxml.Element(elem.doc(), 'next-sibling');
+ var addedSibling = child1.addNextSibling(nextSibling);
+ var children = elem.childNodes();
+ assert.equal(3, children.length);
+ assert.equal('next-sibling', children[1].name());
+ assert.done();
+};
+
+module.exports.import = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name1');
+
+ var child1 = elem.node('child1');
+ doc = child1.doc();
+
+ var newdoc = libxml.Document();
+ newdoc.node('newdoc');
+
+ newdoc.root().addChild(child1);
+
+ assert.ok(newdoc);
+ assert.notEqual(doc, newdoc, true);
+ assert.equal('child1', newdoc.root().childNodes()[0].name());
+ assert.equal(child1, elem.childNodes()[0]); // child1 is the the first child of elem
+ assert.done();
+};
View
0  spec/fixtures/errors/comment.xml → test/fixtures/errors/comment.xml
File renamed without changes
View
0  spec/fixtures/parser.html → test/fixtures/parser.html
File renamed without changes
View
0  spec/fixtures/parser.xml → test/fixtures/parser.xml
File renamed without changes
View
0  spec/fixtures/sax_parser.xml → test/fixtures/sax_parser.xml
File renamed without changes
View
0  spec/fixtures/warnings/amp.html → test/fixtures/warnings/amp.html
File renamed without changes
View
0  spec/fixtures/warnings/ent9.xml → test/fixtures/warnings/ent9.xml
File renamed without changes
View
50 test/html_parser.js
@@ -0,0 +1,50 @@
+var fs = require('fs');
+var libxml = require('../index');
+
+module.exports.parse = function(assert) {
+ var filename = __dirname + '/fixtures/parser.html';
+ var str = fs.readFileSync(filename, 'utf8');
+
+ var doc = libxml.parseHtmlString(str);
+ assert.equal('html', doc.root().name());
+ assert.equal('Test HTML document', doc.get('head/title').text());
+ assert.equal('HTML content!', doc.get('body/span').text());
+ assert.done();
+};
+
+module.exports.recoverable_parse = function(assert) {
+ var recoverableFile = __dirname +'/fixtures/warnings/amp.html';
+ var str = fs.readFileSync(recoverableFile, 'utf8');
+ var recoverableErrors = [
+ { domain: 5,
+ code: 23,
+ message: "htmlParseEntityRef: expecting ';'\n",
+ level: 2,
+ line: 12,
+ column: 27 },
+ { domain: 5,
+ code: 68,
+ message: "htmlParseEntityRef: no name\n",
+ level: 2,
+ line: 12,
+ column: 38 },
+ { domain: 5,
+ code: 23,
+ message: "htmlParseEntityRef: expecting ';'\n",
+ level: 2,
+ line: 14,
+ column: 4 },
+ { domain: 5,
+ code: 68,
+ message: "htmlParseEntityRef: no name\n",
+ level: 2,
+ line: 15,
+ column: 4 }
+ ];
+
+ var doc = libxml.parseHtmlString(str);
+ assert.equal(4, doc.errors.length);
+ assert.deepEqual(recoverableErrors, doc.errors);
+ assert.done();
+};
+
View
10 test/main.js
@@ -0,0 +1,10 @@
+var libxml = require('../index');
+
+module.exports.constants = function(assert) {
+ assert.ok(typeof libxml.version == 'string');
+ assert.ok(typeof libxml.libxml_version == 'string');
+ assert.ok(typeof libxml.libxml_parser_version == 'string');
+ assert.ok(typeof libxml.libxml_debug_enabled == 'boolean');
+ assert.done();
+};
+
View
100 spec/spec_namespaces.js → test/namespace.js
@@ -1,92 +1,98 @@
-with(require('./helpers')) {
+var libxml = require('../index');
-describe('A namespace', function() {
- it('can be created and not set', function() {
- var doc = new libxml.Document();
+module.exports.create = function(assert) {
+ var doc = libxml.Document();
var elem = doc.node('name1');
var ns = elem.defineNamespace('http://my-namespace.com');
+ assert.ok(ns);
assert.equal(null, elem.namespace());
assert.equal(null, ns.prefix());
assert.equal('http://my-namespace.com', ns.href());
- });
+ assert.done();
+};
- it('can be created with a prefix and not set', function() {
- var doc = new libxml.Document();
+// assign namespace to a node
+module.exports.set = function(assert) {
+ var doc = libxml.Document();
var elem = doc.node('name1');
- var ns = elem.defineNamespace('pref', 'http://my-namespace.com');
- assert.equal(null, elem.namespace());
- assert.equal('pref', ns.prefix());
- assert.equal('http://my-namespace.com', ns.href());
- });
- it('can be created and set', function() {
- var doc = new libxml.Document();
- var elem = doc.node('name1');
+ // this will set a namespace on the node
var ns = elem.namespace('http://my-namespace.com');
- assert.ok(ns == elem.namespace());
+ assert.ok(ns);
+ assert.equal(ns, elem.namespace());
assert.equal(null, elem.namespace().prefix());
assert.equal('http://my-namespace.com', elem.namespace().href());
- });
+ assert.done();
+};
- it('can be created with a prefix and set', function() {
- var doc = new libxml.Document();
+
+module.exports.with_prefix = function(assert) {
+ var doc = libxml.Document();
var elem = doc.node('name1');
- var ns = elem.namespace('pref', 'http://my-namespace.com');
- assert.ok(ns == elem.namespace());
+ var ns = elem.defineNamespace('pref', 'http://my-namespace.com');
+ assert.equal(null, elem.namespace());
+ assert.equal('pref', ns.prefix());
+ assert.equal('http://my-namespace.com', ns.href());
+
+ // this should detect existing namespace object
+ var ns2 = elem.namespace('pref', 'http://my-namespace.com');
+ assert.ok(ns2);
+ assert.equal(ns, ns2);
+ assert.equal(ns, elem.namespace());
assert.equal('pref', elem.namespace().prefix());
assert.equal('http://my-namespace.com', elem.namespace().href());
- });
+ assert.done();
+};
- it('without a prefix can be applied through parsing', function() {
- var doc = libxml.parseXmlString('<?xml version="1.0" encoding="UTF-8"?>\
- <name1 xmlns="http://my-namespace.com"/>');
+module.exports.from_parsing = function(assert) {
+ var doc = libxml.parseXmlString('<?xml version="1.0" encoding="UTF-8"?>' +
+ '<name1 xmlns="http://my-namespace.com"/>');
var elem = doc.root();
assert.ok(elem.namespace());
assert.equal(null, elem.namespace().prefix());
assert.equal('http://my-namespace.com', elem.namespace().href());
- });
- it('with a prefix wont be applied through parsing', function() {
- var doc = libxml.parseXmlString('<?xml version="1.0" encoding="UTF-8"?>\
- <name1 xmlns:pref="http://my-namespace.com"/>');
+ // no prefix from parsing
+ var doc = libxml.parseXmlString('<?xml version="1.0" encoding="UTF-8"?>' +
+ '<name1 xmlns:pref="http://my-namespace.com"/>');
var elem = doc.root();
assert.ok(!elem.namespace());
- });
- it('with a prefix can be applied through parsing', function() {
- var doc = libxml.parseXmlString('<?xml version="1.0" encoding="UTF-8"?>\
- <pref:name1 xmlns:pref="http://my-namespace.com"/>');
+ var doc = libxml.parseXmlString('<?xml version="1.0" encoding="UTF-8"?>' +
+ '<pref:name1 xmlns:pref="http://my-namespace.com"/>');
var elem = doc.root();
assert.ok(elem.namespace());
assert.equal('pref', elem.namespace().prefix());
assert.equal('http://my-namespace.com', elem.namespace().href());
- });
- it('will use an existing namespace with only a url', function() {
- var doc = new libxml.Document();
+ assert.done();
+};
+
+module.exports.existing = function(assert) {
+ var doc = libxml.Document();
var elem = doc.node('name1');
var ns = elem.defineNamespace('http://my-namespace.com');
elem.namespace('http://my-namespace.com');
- assert.ok(ns == elem.namespace());
- });
+ assert.ok(ns);
+ assert.equal(ns, elem.namespace());
- it('will use an existing namespace with a prefix', function() {
- var doc = new libxml.Document();
+ var doc = libxml.Document();
var elem = doc.node('name1');
var ns = elem.defineNamespace('pref', 'http://my-namespace.com');
elem.namespace('pref', 'http://my-namespace.com');
- assert.ok(ns == elem.namespace());
- });
+ assert.ok(ns);
+ assert.equal(ns, elem.namespace());
+ assert.done();
+};
- it('can be pulled off a node', function() {
- var doc = new libxml.Document();
+module.exports.remove = function(assert) {
+ var doc = libxml.Document();
var elem = doc.node('name1');
var ns = elem.namespace('http://my-namespace.com');
assert.ok(ns);
assert.ok(ns == elem.namespace());
elem.namespace(null);
assert.ok(!elem.namespace());
- });
-});
+ assert.done();
+};
-}
View
75 test/node_attributes.js
@@ -0,0 +1,75 @@
+var libxml = require('../index');
+
+module.exports.basic = function(assert) {
+ // reading a node is implied during all tests
+ var doc = libxml.Document();
+ var elem = doc.node('name').attr({to: 'wongfoo'});
+ assert.equal('wongfoo', elem.attr('to').value());
+ assert.done();
+};
+
+module.exports.null = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name');
+ assert.equal(null, elem.attr('to'));
+ assert.done();
+};
+
+module.exports.assign_object = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name');
+ elem.attr({to: 'wongfoo'});
+ assert.equal('wongfoo', elem.attr('to').value());
+ assert.done();
+};
+
+module.exports.change = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('name').attr({to: 'wongfoo'});
+ assert.equal('wongfoo', elem.attr('to').value());
+ elem.attr({to: 'julie newmar'});
+ assert.equal('julie newmar', elem.attr('to').value());
+ assert.done();
+};
+
+module.exports.attrs = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('root');
+
+ assert.deepEqual([], elem.attrs());
+
+ elem.attr({foo: 'bar'})
+ .attr({bar: 'baz'})
+ .attr({baz: 'foo'});
+
+ var attrs = [elem.attr('foo'), elem.attr('bar'), elem.attr('baz')];
+ for (var i=0; i < 3; ++i) {
+ assert.equal(attrs[i], elem.attrs()[i]);
+ }
+ assert.done();
+};
+
+module.exports.siblings = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('root')
+ .attr({foo: 'bar'})
+ .attr({bar: 'baz'})
+ .attr({baz: 'foo'});
+ assert.equal(elem.attr('baz'), elem.attr('bar').nextSibling());
+ assert.equal(elem.attr('foo'), elem.attr('bar').prevSibling());
+ assert.equal(null, elem.attr('foo').prevSibling());
+ assert.equal(null, elem.attr('baz').nextSibling());
+ assert.done();
+};
+
+module.exports.getters = function(assert) {
+ var doc = libxml.Document();
+ var elem = doc.node('root').attr({foo: 'bar'});
+
+ // get node
+ assert.equal(elem, elem.attr('foo').parent());
+ // get document
+ assert.equal(doc, elem.attr('foo').doc());
+
+ assert.done();
+};
View
27 test/ref_integrity.js
@@ -0,0 +1,27 @@
+var libxml = require('../index');
+
+if (!global.gc) {
+ throw new Error('must run with --expose_gc for ref integrity tests');
+}
+
+module.exports.gc = function(assert) {
+ var doc = libxml.Document();
+ doc.node('root').node('child').node('grandchild').parent().node('child2');
+ gc();
+ assert.ok(doc, "doc");
+ gc();
+ assert.ok(doc.root(), "root");
+ gc();
+ assert.equal("child", doc.root().childNodes()[0].name(), "child name");
+ assert.done();
+};
+
+module.exports.references = function(assert) {
+ var nodes = libxml.parseXmlString('<root> <child> <grandchildren/> </child> <child2/></root>').childNodes();
+
+ gc();
+
+ assert.ok(nodes[0].doc());
+ assert.equal("child", nodes[1].name());
+ assert.done();
+};
View
116 test/searching.js
@@ -0,0 +1,116 @@
+var libxml = require('../index');
+
+module.exports.get = function(assert) {
+ var doc = libxml.Document();
+ var root = doc.node('root');
+ var child = root.node('child');
+ var grandchild = child.node('grandchild');
+
+ // on document
+ assert.equal(child, doc.get('child'));
+
+ // nested
+ assert.equal(grandchild, doc.get('child').get('grandchild'));
+ assert.done();
+};
+
+module.exports.find = function(assert) {
+ var children = [];
+ var doc = libxml.Document();
+ var root = doc.node('root');
+ children.push(root.node('child'));
+ children.push(root.node('child'));
+
+ var results = doc.find('child');
+ assert.equal(2, children.length);
+ assert.equal(2, results.length);
+
+ for (var child = 0; child < children.length; ++child) {
+ assert.equal(children[child], results[child]);
+ }
+
+ assert.done();
+};
+
+var uri = 'nsuri';
+var prefix = 'pefname';
+
+// non prefixed namespaces
+module.exports.namespace = {
+ get: function(assert) {
+ var doc = libxml.Document();
+ var root = doc.node('root');
+ var child = root.node('child');
+ var grandchild = child.node('grandchild');
+ grandchild.namespace(uri);
+
+ // on document
+ assert.equal(child, doc.get('child'));
+
+ // nested
+ assert.equal(grandchild, doc.get('child').get('xmlns:grandchild', uri));
+ assert.done();
+ },
+ find: function(assert) {
+ var children = [];
+ var doc = libxml.Document();
+ var root = doc.node('root');
+ children.push(root.node('child'));
+ children.push(root.node('child'));
+
+ var ns = children[0].namespace(uri);
+ children[1].namespace(ns);
+
+ var results = doc.find('xmlns:child', uri);
+ assert.equal(2, children.length);
+ assert.equal(2, results.length);
+ for (var child = 0; child < children.length; ++child) {
+ assert.equal(children[child], results[child]);
+ }
+ assert.done();
+ }
+};
+
+module.exports.prefixed_namespace = {
+ get: function(assert) {
+ var doc = libxml.Document();
+ var root = doc.node('root');
+ var child = root.node('child');
+ var grandchild = child.node('grandchild');
+ grandchild.namespace(prefix, uri);
+
+ // on document
+ assert.equal(child, doc.get('child'));
+
+ var ns_params = {
+ pefname: uri
+ };
+
+ // nested
+ assert.equal(grandchild, doc.get('child').get('pefname:grandchild', ns_params));
+ assert.done();
+ },
+ find: function(assert) {
+ var children = [];
+ var doc = libxml.Document();
+ var root = doc.node('root');
+ children.push(root.node('child'));
+ children.push(root.node('child'));
+
+ var ns = children[0].namespace(prefix, uri);
+ children[1].namespace(ns);
+
+ var ns_params = {
+ pefname: uri
+ };
+
+ var results = doc.find('pefname:child', ns_params);
+ assert.equal(2, children.length);
+ assert.equal(2, results.length);
+ for (var child = 0; child < children.length; ++child) {
+ assert.equal(children[child], results[child]);
+ }
+ assert.done();
+ }
+};
+
View
27 test/text_node.js
@@ -0,0 +1,27 @@
+var libxml = require('../index');
+
+module.exports.text = function(assert) {
+ var doc = libxml.parseXmlString('<?xml version="1.0"?><root>child</root>');
+ assert.equal('text', doc.child(0).type());
+ assert.equal('text', doc.child(0).name());
+ assert.done();
+};
+
+module.exports.comment = function(assert) {
+ var doc = libxml.parseXmlString('<?xml version="1.0"?>' +
+ '<root><!-- comment --></root>');
+ assert.equal('comment', doc.child(0).type());
+ assert.equal('comment', doc.child(0).name());
+ assert.done();
+};
+
+module.exports.cdata = function(assert) {
+
+ var doc = libxml.parseXmlString('<?xml version="1.0"?>' +
+ '<root><![CDATA[cdata text]]></root>');
+
+ assert.equal('cdata', doc.child(0).type());
+ assert.equal(undefined, doc.child(0).name());
+ assert.done();
+};
+
View
99 test/traversal.js
@@ -0,0 +1,99 @@
+var libxml = require('../index');
+
+module.exports.built = function(assert) {
+ var doc = libxml.Document();
+ var child = doc.node('root').node('child');
+ var sibling = doc.root().node('sibling');