[Pkg-javascript-commits] [node-expat] 75/371: test.js: port to vows

Jonas Smedegaard dr at jones.dk
Sun Feb 28 09:59:47 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository node-expat.

commit 0b78f48e0e467f802aa2f84edbb76341557da76b
Author: Astro <astro at spaceboyz.net>
Date:   Tue Jun 14 02:20:06 2011 +0200

    test.js: port to vows
---
 package.json |   1 +
 test.js      | 288 +++++++++++++++++++++++++++++++----------------------------
 2 files changed, 150 insertions(+), 139 deletions(-)

diff --git a/package.json b/package.json
index 325619e..44ec480 100644
--- a/package.json
+++ b/package.json
@@ -7,6 +7,7 @@
 	      ,"test": "node test.js"
 	     }
 ,"dependencies": []
+,"devDependencies": ["vows"]
 ,"repositories": [{ "type": "git"
 		   ,"path": "git://github.com/astro/node-expat.git"
 		  }]
diff --git a/test.js b/test.js
index 94ead47..00b70dc 100644
--- a/test.js
+++ b/test.js
@@ -1,6 +1,8 @@
 var sys = require('sys');
 var expat = require('./build/default/node-expat');
 var Buffer = require('buffer').Buffer;
+var vows = require('vows');
+var assert = require('assert');
 
 function collapseTexts(evs) {
     var r = [];
@@ -20,11 +22,8 @@ function collapseTexts(evs) {
     return r;
 }
 
-var tests = 0, iterations = 0, fails = 0;
 function expect(s, evs_expected) {
-    tests++;
     for(var step = s.length; step > 0; step--) {
-	iterations++;
 	var evs_received = [];
 	var p = new expat.Parser("UTF-8");
 	//p.setEncoding("UTF-8");
@@ -67,21 +66,104 @@ function expect(s, evs_expected) {
 
 	var expected = JSON.stringify(evs_expected);
 	var received = JSON.stringify(collapseTexts(evs_received));
-	if (expected != received) {
-	    fails++;
-	    sys.puts("Fail for: " + s + " (step=" + step + ")");
-	    sys.puts("Expected: " + expected);
-	    sys.puts("Received: " + received);
-	    return;  // don't try with smaller step size
-	}
+	assert.equal(received, expected);
     }
 }
 
-function testStopResume(cb)
-{
-	var p = new expat.Parser("UTF-8");
-	
-	var input = '\
+vows.describe('node-expat').addBatch({
+    'single element': {
+	'simple': function() {
+	    expect("<r/>",
+		[['startElement', 'r', {}],
+		['endElement', 'r']]);
+	},
+	'single element with attribute': function() {
+	    expect("<r foo='bar'/>",
+		[['startElement', 'r', {foo: 'bar'}],
+		['endElement', 'r']]);
+	},
+	'single elemeht with differently quoted attributes': function() {
+	    expect("<r foo='bar' baz=\"quux\" test=\"tset\"/>",
+		[['startElement', 'r', {foo: 'bar', baz: 'quux', test: 'tset'}],
+		['endElement', 'r']]);
+	},
+	'single element with namespaces': function() {
+	    expect("<r xmlns='http://localhost/' xmlns:x=\"http://example.com/\"></r>",
+		[['startElement', 'r', {xmlns: 'http://localhost/', 'xmlns:x': 'http://example.com/'}],
+		['endElement', 'r']]);
+	},
+	'single element with text content': function() {
+	    expect("<r>foo</r>",
+		[['startElement', 'r', {}],
+		['text', "foo"],
+		['endElement', 'r']]);
+	},
+	'single element with text content and line break': function() {
+	    expect("<r>foo\nbar</r>",
+		[['startElement', 'r', {}],
+		['text', "foo\nbar"],
+		['endElement', 'r']]);
+	},
+	'single element with CDATA content': function() {
+	    expect("<r><![CDATA[<greeting>Hello, world!</greeting>]]></r>",
+		[['startElement', 'r', {}],
+		['text', "<greeting>Hello, world!</greeting>"],
+		['endElement', 'r']]);
+	},
+	'single element with entity text': function() {
+	    expect("<r>foo&bar</r>",
+		[['startElement', 'r', {}],
+		['text', "foo&bar"],
+		['endElement', 'r']]);
+	},
+	'single element with umlaut text': function() {
+	    expect("<r>ß</r>",
+		[['startElement', 'r', {}],
+		['text', "ß"],
+		['endElement', 'r']]);
+	},
+	'from buffer': function() {
+	    expect(new Buffer('<foo>bar</foo>'),
+		[['startElement', 'foo', {}],
+		['text', 'bar'],
+		['endElement', 'foo']]);
+	}
+    },
+    'processing instruction': {
+	'with parameters': function() {
+	    expect("<?i like xml?>",
+		[['processingInstruction', 'i', 'like xml']]);
+	},
+	'simple': function() {
+	    expect("<?dragons?>",
+		[['processingInstruction', 'dragons', '']]);
+	},
+	'XML declaration with encoding': function() {
+	    expect("<?xml version='1.0' encoding='UTF-8'?>",
+		[['xmlDecl', '1.0', 'UTF-8', true]]);
+	},
+	'XML declaration': function() {
+	    expect("<?xml version='1.0'?>",
+		[['xmlDecl', '1.0', null, true]]);
+	}
+    },
+    'comment': {
+	'simple': function() {
+	    expect("<!-- no comment -->",
+		[['comment', ' no comment ']]);
+	}
+    },
+    'error': {
+	'tag name starting with ampersand': function() {
+	    expect("<&", [['error']]);
+	}
+    },
+    'stop and resume': {
+	topic: function() {
+	    var cb = this.callback;
+	    var p = new expat.Parser("UTF-8");
+
+	    var input = '\
 		<wrap> \
 			<short /> \
 			<short></short> \
@@ -89,135 +171,63 @@ function testStopResume(cb)
 			<short /> \
 			<long>foo</long> \
 		</wrap>';
-	
-	var expected = ['wrap', 'short', 'short', 'long', 'short', 'long'];
-	var received = [];
-	
-	var tolerance = 10/100;
-	var expectedRuntime = 1000;
-	var start = new Date();
-	
-	p.addListener('startElement', function(name, attrs) {
+
+	    var expected = ['wrap', 'short', 'short', 'long', 'short', 'long'];
+	    var received = [];
+
+	    var tolerance = 10/100;
+	    var expectedRuntime = 1000;
+	    var start = new Date();
+
+	    p.addListener('startElement', function(name, attrs) {
 		received.push(name);
-		
+
 		// suspend parser for 1/2 second
 		if(name == 'long') {
-			p.stop();
-			
-			setTimeout(function() {
-				p.resume();
-			}, 500);
+		    p.stop();
+
+		    setTimeout(function() {
+			p.resume();
+		    }, 500);
 		}
-	})
-	
-	p.addListener('endElement', function(name) {
+	    });
+
+	    p.addListener('endElement', function(name) {
 		// finished parsing
 		if(name == 'wrap') {
-			// test elements received (count. naming, order)
-			if(JSON.stringify(expected) != JSON.stringify(received)) {
-				sys.puts("Failed Stop/Resume test");
-				sys.puts("Expected: " + expected);
-				sys.puts("Received: " + received);
-				return cb(false);
-			}
-			
-			// test timing (+-5%)
-			var now = new Date();
-			var diff = now.getTime() - start.getTime();
-			var max = expectedRuntime + expectedRuntime * tolerance, 
-				min = expectedRuntime - expectedRuntime * tolerance;
-			
-			if(diff > max) {
-				sys.puts("Failed Stop/Resume test");
-				sys.puts("Expected Runtime < " + max);
-				sys.puts("Taken Runtime: " + diff);
-				return cb(false);
-			}
-			
-			if(diff < min) {
-				sys.puts("Failed Stop/Resume test");
-				sys.puts("Expected Runtime > " + min);
-				sys.puts("Taken Runtime: " + diff);
-				return cb(false);
-			}
-			
-			return cb(true);
-		}
-	});
-	
-	if(!p.parse(input)) {
-		sys.puts("Failed Stop/Resume test: parse returned error: "+p.getError());
-		return cb(false);
-	}
-}
+		    // test elements received (count. naming, order)
+		    if(JSON.stringify(expected) != JSON.stringify(received)) {
+			sys.puts("Failed Stop/Resume test");
+			sys.puts("Expected: " + expected);
+			sys.puts("Received: " + received);
+			return cb(false);
+		    }
 
-expect("<r/>",
-       [['startElement', 'r', {}],
-	['endElement', 'r']]);
-expect("<r foo='bar'/>",
-       [['startElement', 'r', {foo: 'bar'}],
-	['endElement', 'r']]);
-expect("<r foo='bar' baz=\"quux\" test=\"tset\"/>",
-       [['startElement', 'r', {foo: 'bar', baz: 'quux', test: 'tset'}],
-	['endElement', 'r']]);
-expect("<r xmlns='http://localhost/' xmlns:x=\"http://example.com/\"></r>",
-       [['startElement', 'r', {xmlns: 'http://localhost/', 'xmlns:x': 'http://example.com/'}],
-	['endElement', 'r']]);
-expect("<r>foo</r>",
-       [['startElement', 'r', {}],
-	['text', "foo"],
-	['endElement', 'r']]);
-expect("<r>foo\nbar</r>",
-       [['startElement', 'r', {}],
-	['text', "foo\nbar"],
-	['endElement', 'r']]);
-expect("<r><![CDATA[<greeting>Hello, world!</greeting>]]></r>",
-       [['startElement', 'r', {}],
-	['startCdata'],
-	['text', "<greeting>Hello, world!</greeting>"],
-	['endCdata'],
-	['endElement', 'r']]);
-expect("<r>foo&bar</r>",
-       [['startElement', 'r', {}],
-	['text', "foo&bar"],
-	['endElement', 'r']]);
-expect("<r>ß</r>",
-       [['startElement', 'r', {}],
-	['text', "ß"],
-	['endElement', 'r']]);
-expect("<?i like xml?>",
-       [['processingInstruction', 'i', 'like xml']]);
-expect("<?dragons?>",
-       [['processingInstruction', 'dragons', '']]);
-expect("<!-- no comment -->",
-       [['comment', ' no comment ']]);
-expect("<&", [['error']]);
-expect("<?xml version='1.0' encoding='UTF-8'?>",
-       [['xmlDecl', '1.0', 'UTF-8', true]]);
-expect("<?xml version='1.0'?>",
-       [['xmlDecl', '1.0', null, true]]);
-expect(new Buffer('<foo>bar</foo>'),
-       [['startElement', 'foo', {}],
-	['text', 'bar'],
-	['endElement', 'foo']]);
-expect(new Buffer('<foo><![CDATA[bar]]></foo>'),
-       [['startElement', 'foo', {}],
-	['startCdata'],
-	['text', 'bar'],
-	['endCdata'],
-	['endElement', 'foo']]);
-expect('<!DOCTYPE b [<!ELEMENT b (#PCDATA)>' +
-       '<!ENTITY l0 "ha"><!ENTITY l1 "&l0;&l0;"><!ENTITY l2 "&l1;&l1;">' +
-       ']><b>&l2;</b>',
-       [["entityDecl","l0",false,"ha",null,null,null,null],
-	["entityDecl","l1",false,"&l0;&l0;",null,null,null,null],
-	["entityDecl","l2",false,"&l1;&l1;",null,null,null,null],
-	["startElement","b",{}],["text","hahahaha"],["endElement","b"]]);
-
-sys.puts("Ran "+tests+" tests with "+iterations+" iterations: "+fails+" failures.");
-
-sys.puts("Starting Stop/Resume Test (wait for result...)");
-testStopResume(function(success) {
-	sys.puts("Stop/Resume Test "+(success ? 'succeeded' : 'failed'));
-});
+		    // test timing (+-5%)
+		    var now = new Date();
+		    var diff = now.getTime() - start.getTime();
+		    var max = expectedRuntime + expectedRuntime * tolerance,
+			min = expectedRuntime - expectedRuntime * tolerance;
 
+		    if(diff > max) {
+			sys.puts("Failed Stop/Resume test");
+			sys.puts("Expected Runtime < " + max);
+			sys.puts("Taken Runtime: " + diff);
+			return cb(false);
+		    }
+
+		    if(diff < min) {
+			sys.puts("Failed Stop/Resume test");
+			sys.puts("Expected Runtime > " + min);
+			sys.puts("Taken Runtime: " + diff);
+			return cb(false);
+		    }
+
+		    return cb(true);
+		}
+	    });
+
+	    assert.ok(p.parse(input));
+	}
+    }
+}).run();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-expat.git



More information about the Pkg-javascript-commits mailing list