def testDifferentStrings(self): # Checking escaping of strings problematic_strings = [ "control", "new\nline", "", "single'quote", 'double"quote', r"one\slash", r"two\\slash", u"unicode eng", u"unicode \u05e2\u05d1\u05e8\u05d9\u05ea" ] for s in problematic_strings: js_value = DataTable.SingleValueToJS(s, "string") if isinstance(js_value, unicode): js_value = "u%s" % js_value self.assertEquals(s, eval(js_value)) js_value = DataTable.SingleValueToJS(("str", s), "string")[1] if isinstance(js_value, unicode): js_value = "u%s" % js_value self.assertEquals(s, eval(js_value))
def testSingleValueToJS(self): # We first check that given an unknown type it raises exception self.assertRaises(DataTableException, DataTable.SingleValueToJS, 1, "no_such_type") # If we give a type which does not match the value, we expect it to fail self.assertRaises(DataTableException, DataTable.SingleValueToJS, "a", "number") self.assertRaises(DataTableException, DataTable.SingleValueToJS, "b", "timeofday") self.assertRaises(DataTableException, DataTable.SingleValueToJS, 10, "date") # A tuple for value and formatted value should be of length 2 self.assertRaises(DataTableException, DataTable.SingleValueToJS, (5, "5$", "6$"), "string") # Some good examples from all the different types self.assertEqual("true", DataTable.SingleValueToJS(True, "boolean")) self.assertEqual("false", DataTable.SingleValueToJS(False, "boolean")) self.assertEqual("true", DataTable.SingleValueToJS(1, "boolean")) self.assertEqual("null", DataTable.SingleValueToJS(None, "boolean")) self.assertEqual(("false", "'a'"), DataTable.SingleValueToJS((False, "a"), "boolean")) self.assertEqual("1", DataTable.SingleValueToJS(1, "number")) self.assertEqual("1.0", DataTable.SingleValueToJS(1., "number")) self.assertEqual("-5", DataTable.SingleValueToJS(-5, "number")) self.assertEqual("null", DataTable.SingleValueToJS(None, "number")) self.assertEqual(("5", "'5$'"), DataTable.SingleValueToJS((5, "5$"), "number")) self.assertEqual("'-5'", DataTable.SingleValueToJS(-5, "string")) self.assertEqual("'abc'", DataTable.SingleValueToJS("abc", "string")) self.assertEqual("null", DataTable.SingleValueToJS(None, "string")) self.assertEqual("new Date(2010,0,2)", DataTable.SingleValueToJS(date(2010, 1, 2), "date")) self.assertEqual( "new Date(2001,1,3)", DataTable.SingleValueToJS(datetime(2001, 2, 3, 4, 5, 6), "date")) self.assertEqual("null", DataTable.SingleValueToJS(None, "date")) self.assertEqual( "[10,11,12]", DataTable.SingleValueToJS(time(10, 11, 12), "timeofday")) self.assertEqual( "[3,4,5]", DataTable.SingleValueToJS(datetime(2010, 1, 2, 3, 4, 5), "timeofday")) self.assertEqual("null", DataTable.SingleValueToJS(None, "timeofday")) self.assertEqual( "new Date(2001,1,3,4,5,6)", DataTable.SingleValueToJS(datetime(2001, 2, 3, 4, 5, 6), "datetime")) self.assertEqual("null", DataTable.SingleValueToJS(None, "datetime")) self.assertEqual(("null", "'none'"), DataTable.SingleValueToJS((None, "none"), "string"))