def test_toNumber(self): self.assertEqual(Functions.toNumber('1'), 1) self.assertEqual(Functions.toNumber('0'), 0) self.assertEqual(Functions.toNumber('-1'), -1) self.assertEqual(Functions.toNumber('1.1'), 1.1) self.assertEqual(Functions.toNumber(1.1), 1.1)
def test_toInt(self): self.assertEqual(Functions.toInt('1'), 1) self.assertEqual(Functions.toInt('0'), 0) self.assertEqual(Functions.toInt('-1'), -1) self.assertEqual(Functions.toInt('1.1'), None) self.assertEqual(Functions.toInt(1.1), 1)
def applyOperation(value, operation, args, location): """ Applies an operation to a value with some extra arguments. :param value: a valid JSON value :param operation: str name of the operation to apply (from the tokenizer) :param args: [str] argument tokens :return: a valid JSON value """ function = Functions.functions.get(operation) if function is None: #Is it a simple integer index? index = Functions.toInt(operation) if index is not None: return value[index] #Or perhaps it's a selector function? .abc.def if operation[0] == '.': if len(args) == 0: return Utility.extractPath(value, operation[1:]) else: raise SyntaxError( 'selector %s has arguments in "%s" (did you mean to do an operation?)' % (operation[0], location)) #Nothing found -- error! raise NameError('cannot find operation %s in "%s"' % (operation, location)) return function(value, *args)
def test_toBool(self): self.assertEqual(Functions.toBool('True'), True) self.assertEqual(Functions.toBool('true'), True) self.assertEqual(Functions.toBool('False'), False) self.assertEqual(Functions.toBool('false'), False) self.assertEqual(Functions.toBool('t'), False) self.assertEqual(Functions.toBool('y'), False)