[jstest] get back more tests writen during the sprint by vgodard
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 11 Jun 2010 16:07:58 +0200
changeset 5738 74bd8123fb07
parent 5736 375819ec7d43
child 5739 aaf9f5ea1405
[jstest] get back more tests writen during the sprint by vgodard
web/test/jstests/test_datetime.html
web/test/jstests/test_datetime.js
web/test/jstests/test_htmlhelpers.js
web/test/jstests/test_utils.html
web/test/jstests/test_utils.js
web/test/jstests/utils.js
--- a/web/test/jstests/test_datetime.html	Fri Jun 11 14:59:08 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,18 +0,0 @@
-<html>
-  <head>
-    <script type="text/javascript" src="../../data/jquery.js"></script>
-    <script src="../../data/cubicweb.js" type="text/javascript"></script>
-    <script src="../../data/cubicweb.compat.js" type="text/javascript"></script>
-    <script type="text/javascript" src="qunit.js"></script>
-    <link rel="stylesheet" type="text/css" media="all" href="qunit.css" />
-    <script src="test_datetime.js" type="text/javascript"></script>
-  </head>
-  <body>
-    <div id="main">
-    </div>
-    <h1 id="qunit-header">QUnit example</h1>
-    <h2 id="qunit-banner"></h2>
-    <h2 id="qunit-userAgent"></h2>
-    <ol id="qunit-tests">
-  </body>
-</html>
--- a/web/test/jstests/test_datetime.js	Fri Jun 11 14:59:08 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-$(document).ready(function() {
-
-  module("datetime tests");
-
-  test("test full datetime", function() {
-      equals(cw.utils.toISOTimestamp(new Date(1986, 3, 18, 10, 30, 0, 0)),
-	     '1986-04-18 10:30:00');
-  });
-
-  test("test only date", function() {
-      equals(cw.utils.toISOTimestamp(new Date(1986, 3, 18)), '1986-04-18 00:00:00');
-  });
-
-  test("test null", function() {
-      equals(cw.utils.toISOTimestamp(null), null);
-  });
-
-
-});
-
--- a/web/test/jstests/test_htmlhelpers.js	Fri Jun 11 14:59:08 2010 +0200
+++ b/web/test/jstests/test_htmlhelpers.js	Fri Jun 11 16:07:58 2010 +0200
@@ -6,7 +6,7 @@
     			'</select>');
       }
     });
-  
+
     test("test first selected", function() {
         $('#theselect').append('<option value="foo">foo</option>' +
     			     '<option selected="selected" value="bar">bar</option>' +
@@ -15,7 +15,7 @@
         var selected = firstSelected(document.getElementById("theselect"));
         equals(selected.value, 'bar');
     });
-  
+
     test("test first selected 2", function() {
         $('#theselect').append('<option value="foo">foo</option>' +
     			     '<option value="bar">bar</option>' +
@@ -24,7 +24,7 @@
         var selected = firstSelected(document.getElementById("theselect"));
         equals(selected, null);
     });
-  
+
     module("visibilty");
     test('toggleVisibility', function() {
         $('#main').append('<div id="foo"></div>');
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/test/jstests/test_utils.html	Fri Jun 11 16:07:58 2010 +0200
@@ -0,0 +1,21 @@
+<html>
+  <head>
+    <script type="text/javascript" src="../../data/jquery.js"></script>
+    <script src="../../data/jquery.corner.js" type="text/javascript"></script>
+    <script src="../../data/cubicweb.js" type="text/javascript"></script>
+    <script src="../../data/cubicweb.python.js" type="text/javascript"></script>
+    <script src="../../data/cubicweb.compat.js" type="text/javascript"></script>
+    <script src="utils.js" type="text/javascript"></script>
+    <script type="text/javascript" src="qunit.js"></script>
+    <link rel="stylesheet" type="text/css" media="all" href="qunit.css" />
+    <script src="test_utils.js" type="text/javascript"></script>
+  </head>
+  <body>
+    <div id="main">
+    </div>
+    <h1 id="qunit-header">QUnit example</h1>
+    <h2 id="qunit-banner"></h2>
+    <h2 id="qunit-userAgent"></h2>
+    <ol id="qunit-tests">
+  </body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/test/jstests/test_utils.js	Fri Jun 11 16:07:58 2010 +0200
@@ -0,0 +1,92 @@
+$(document).ready(function() {
+
+  module("datetime");
+
+  test("test full datetime", function() {
+      equals(cw.utils.toISOTimestamp(new Date(1986, 3, 18, 10, 30, 0, 0)),
+	     '1986-04-18 10:30:00');
+  });
+
+  test("test only date", function() {
+      equals(cw.utils.toISOTimestamp(new Date(1986, 3, 18)), '1986-04-18 00:00:00');
+  });
+
+  test("test null", function() {
+      equals(cw.utils.toISOTimestamp(null), null);
+  });
+
+  module("parsing");
+  test("test basic number parsing", function() {
+      var d = strptime('2008/08/08', '%Y/%m/%d');
+      same(datetuple(d), [2008, 8, 8, 0, 0]);
+      d = strptime('2008/8/8', '%Y/%m/%d');
+      same(datetuple(d), [2008, 8, 8, 0, 0]);
+      d = strptime('8/8/8', '%Y/%m/%d');
+      same(datetuple(d), [8, 8, 8, 0, 0]);
+      d = strptime('0/8/8', '%Y/%m/%d');
+      same(datetuple(d), [0, 8, 8, 0, 0]);
+      d = strptime('-10/8/8', '%Y/%m/%d');
+      same(datetuple(d), [-10, 8, 8, 0, 0]);
+      d = strptime('-35000', '%Y');
+      same(datetuple(d), [-35000, 1, 1, 0, 0]);
+  });
+
+  test("test custom format parsing", function() {
+      var d = strptime('2008-08-08', '%Y-%m-%d');
+      same(datetuple(d), [2008, 8, 8, 0, 0]);
+      d = strptime('2008 - !  08: 08', '%Y - !  %m: %d');
+      same(datetuple(d), [2008, 8, 8, 0, 0]);
+      d = strptime('2008-08-08 12:14', '%Y-%m-%d %H:%M');
+      same(datetuple(d), [2008, 8, 8, 12, 14]);
+      d = strptime('2008-08-08 1:14', '%Y-%m-%d %H:%M');
+      same(datetuple(d), [2008, 8, 8, 1, 14]);
+      d = strptime('2008-08-08 01:14', '%Y-%m-%d %H:%M');
+      same(datetuple(d), [2008, 8, 8, 1, 14]);
+  });
+
+  module("sliceList");
+  test("test slicelist", function() {
+      var list = ['a', 'b', 'c', 'd', 'e', 'f'];
+      same(sliceList(list, 2),  ['c', 'd', 'e', 'f']);
+      same(sliceList(list, 2, -2), ['c', 'd']);
+      same(sliceList(list, -3), ['d', 'e', 'f']);
+      same(sliceList(list, 0, -2), ['a', 'b', 'c', 'd']);
+      same(sliceList(list),  list);
+  });
+
+  module("formContents", {
+    setup: function() {
+      $('#main').append('<form id="test-form"></form>');
+    }
+  });
+  // XXX test fckeditor
+  test("test formContents", function() {
+      $('#test-form').append('<input name="input-text" ' +
+			     'type="text" value="toto" />');
+      $('#test-form').append('<textarea rows="10" cols="30" '+
+			     'name="mytextarea">Hello World!</textarea> ');
+      $('#test-form').append('<input name="choice" type="radio" ' +
+			     'value="yes" />');
+      $('#test-form').append('<input name="choice" type="radio" ' +
+			     'value="no" checked="checked"/>');
+      $('#test-form').append('<input name="check" type="checkbox" ' +
+			     'value="yes" />');
+      $('#test-form').append('<input name="check" type="checkbox" ' +
+			     'value="no" checked="checked"/>');
+      $('#test-form').append('<select id="theselect" name="theselect" ' +
+			     'multiple="multiple" size="2"></select>');
+      $('#theselect').append('<option selected="selected" ' +
+			     'value="foo">foo</option>' +
+  			     '<option value="bar">bar</option>');
+      //Append an unchecked radio input : should not be in formContents list
+      $('#test-form').append('<input name="unchecked-choice" type="radio" ' +
+			     'value="one" />');
+      $('#test-form').append('<input name="unchecked-choice" type="radio" ' +
+			     'value="two"/>');
+      same(formContents($('#test-form')[0]), [
+	['input-text', 'mytextarea', 'choice', 'check', 'theselect'],
+	['toto', 'Hello World!', 'no', 'no', 'foo']
+      ]);
+  });
+});
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/test/jstests/utils.js	Fri Jun 11 16:07:58 2010 +0200
@@ -0,0 +1,29 @@
+function datetuple(d) {
+    return [d.getFullYear(), d.getMonth()+1, d.getDate(),
+	    d.getHours(), d.getMinutes()];
+}
+
+function pprint(obj) {
+    print('{');
+    for(k in obj) {
+	print('  ' + k + ' = ' + obj[k]);
+    }
+    print('}');
+}
+
+function arrayrepr(array) {
+    return '[' + array.join(', ') + ']';
+}
+
+function assertArrayEquals(array1, array2) {
+    if (array1.length != array2.length) {
+	throw new crosscheck.AssertionFailure(array1.join(', ') + ' != ' + array2.join(', '));
+    }
+    for (var i=0; i<array1.length; i++) {
+	if (array1[i] != array2[i]) {
+
+	    throw new crosscheck.AssertionFailure(arrayrepr(array1) + ' and ' + arrayrepr(array2)
+						 + ' differs at index ' + i);
+	}
+    }
+}