Permalink
Browse files

Added more tests and tidied up qtest.js a bit

  • Loading branch information...
pdl committed Mar 24, 2013
1 parent f37a1e6 commit b20549015ff7b2f653b4b5ec495994bf8aca37a4
Showing with 61 additions and 33 deletions.
  1. +60 −32 html/qtest.js
  2. +1 −1 js/bex.document.js
View
@@ -1,29 +1,78 @@
+/*
+
+ qtest.js - test suite for BEX
+
+ Depends on qunit. Requires a div#qunit and a div#testcanvas
+*/
+
+// Make sure nothing is obviously wrong
test( "Check QUnit is working ok", function() {
equal( 2+2, 4, "equal: 2+4=4" );
deepEqual( [{}], [{}], "deepEqual: [{}] = [{}]" );
});
+
test( "Check we have jQuery and a test canvas", function() {
ok( $('#testcanvas'), "$('#testcanvas') returns true" );
});
+// Now define some helper functions for setup and teardown of test cases.
+
+// escapeXml - does what it says on the tin
+function escapeXml (s) {
+ // Adapted from https://gist.github.com/panzi/1857360
+ var XML_CHAR_MAP = {
+ '<': '&lt;',
+ '>': '&gt;',
+ '&': '&amp;',
+ '"': '&quot;',
+ "'": '&apos;'
+ };
+
+ return s.replace(/[<>&"']/g, function (ch) {
+ return XML_CHAR_MAP[ch];
+ });
+}
+
+// clear_canvas
+function clear_canvas() {
+ $('#testcanvas').empty();
+}
+
+function setup_scope(xml) {
+ clear_canvas();
+ $('#testcanvas').append(
+ '<textarea name="" id="bexme" cols="30" rows="10">'
+ + escapeXml(xml)
+ +'</textarea>');
+ $('#bexme').bexdocument();
+}
+
+function roundtrip(xml, reason){
+ setup_scope(xml);
+ equal ( $('#bexme').bexdocument('xml'), xml, "Roundtripped ok. "+ reason );
+}
+function manipulate(xml,callback,result,reason){
+ callback(setup_scope(xml));
+ equal ( $('#bexme').bexdocument('xml'), result, reason );
+}
+
+var noop = function(){};
+
+// The test suite proper begins here
+
test( "Check we can setup a simple document", function() {
var xml = '<root/>'
setup_scope(xml);
ok ( $('#bexme'), "$('#bexme') returns true" );
});
-function roundtrip(xml, reason){
- setup_scope(xml);
- equal ( $('#bexme').bexdocument('xml'), xml, "Roundtripped ok. "+ reason );
-}
test( "Check we can roundtrip with some simple documents", function() {
roundtrip ('<root/>', 'Empty root.');
roundtrip ('<root>Test</root>', 'Text only.');
roundtrip ('<root> </root>', 'Whitespace only.');
roundtrip ('<root> </root>', 'Multiple whitespace.');
roundtrip ('<root>&lt;</root>', 'Named Entity.');
-// roundtrip ('<root>&#x3c;</root>', 'Numerical Entity.'); // shouldn't roundtrip but should resolve to &lt; - test later.
roundtrip ('<root> Test </root>', 'Whitespace and text.');
roundtrip ('<root><test/></root>', 'Element.');
roundtrip ('<root><test/><case/></root>', 'Elements.');
@@ -33,6 +82,7 @@ test( "Check we can roundtrip with some simple documents", function() {
roundtrip ('<root>Before <test/> and After</root>', 'Mixed content with whitespace.');
roundtrip ('<root>Before <test>a <case/> </test> and After</root>', 'Mixed content with more whitespace and nesting.');
});
+
test( "Check we can roundtrip with comments", function() {
roundtrip ('<root><!--Test--></root>', 'Simple text comment.');
roundtrip ('<root><!-- --></root>', 'Whitespace only comment.');
@@ -43,6 +93,7 @@ test( "Check we can roundtrip with comments", function() {
roundtrip ('<root>t<!-- ... -->est</root>', 'Mix comments with text.');
roundtrip ('<root><unit>t<!-- ... -->est<case/></unit></root>', 'Mix comments with elements.');
});
+
test( "Check we can roundtrip with CDATA sections", function() {
roundtrip ('<root><![CDATA[Test]]></root>', 'Simple text CDATA.');
roundtrip ('<root><![CDATA[ ]]></root>', 'Whitespace only CDATA.');
@@ -53,31 +104,8 @@ test( "Check we can roundtrip with CDATA sections", function() {
roundtrip ('<root><unit>t<![CDATA[ ... ]]>est<case/></unit></root>', 'Mix CDATA with elements.');
});
-
-function escapeXml (s) {
- // Adapted from https://gist.github.com/panzi/1857360
- var XML_CHAR_MAP = {
- '<': '&lt;',
- '>': '&gt;',
- '&': '&amp;',
- '"': '&quot;',
- "'": '&apos;'
- };
-
- return s.replace(/[<>&"']/g, function (ch) {
- return XML_CHAR_MAP[ch];
- });
-}
-
-function clear_canvas() {
- $('#testcanvas').empty();
-}
+test( "Some almost-roundtrip scenarios", function() {
+ manipulate ('<root>&#x3c;</root>', noop,'<root>&lt;</root>', 'Entity #x3c should not roundtrip but should resolve to less-than.');
+ manipulate ('<root><test></test></root>', noop,'<root><test/></root>', 'Empty elements should come out with self-closing tag, irrespective of the input document');
+});
-function setup_scope(xml) {
- clear_canvas();
- $('#testcanvas').append(
- '<textarea name="" id="bexme" cols="30" rows="10">'
- + escapeXml(xml)
- +'</textarea>');
- $('#bexme').bexdocument();
-}
View
@@ -78,7 +78,7 @@
return source;
},
xmlpreview: function(){
- return this.xml(); // TODO: Add a callback so that users can insert their own XSLT.
+ return this.xml(); // TODO: Add a callback so that users can insert their own XSLT. BUT isn't this its own callback?
},
tree: function(){
var xml = $(this.element).val();

0 comments on commit b205490

Please sign in to comment.