def test_evaluate(self): obj = Pow(Number(2), Number(2)) self.assertEqual(obj.evaluate(), 4) obj = Pow(Number(2.0), String('a')) with self.assertRaises(DslSyntaxError): obj.evaluate()
def test_call_functions_without_pending_call_stack(self): fc = FunctionCall(Name('f'), [Name('x')]) fd = FunctionDef('f', [FunctionArg('a', '')], Name('a'), []) number1 = Number(1234) number2 = Number(2345) ns1 = DslNamespace({ 'f': fd, 'x': number1, }) ns2 = DslNamespace({ 'f': fd, 'x': number2, }) # Call functions with pending call stack. queue = Mock() fc1 = fc.substitute_names(ns1) fc2 = fc.substitute_names(ns2) self.assertNotEqual(fd.create_hash(fc1), fd.create_hash(fc2)) t1 = datetime.datetime(2011, 1, 1) expr = fc1.call_functions(pending_call_stack=queue, present_time=t1) # Check we got a stub. self.assertIsInstance(expr, Stub) self.assertEqual(queue.put.call_count, 1) # Check the call to the stub was queued. first_call = queue.put.mock_calls[0] self.assertEqual(first_call[2]['stub_id'], expr.name) self.assertEqual(first_call[2]['stacked_function_def'], fd) self.assertEqual(first_call[2]['stacked_locals'], {'a': 1234}) # Maybe this should be Number(1234)? self.assertEqual(first_call[2]['present_time'], t1)
def test_substitute_names(self): fc = FunctionCall(Name('f'), [Name('x')]) fd = FunctionDef('f', [], Name('a'), []) ns = DslNamespace({ 'f': fd, 'x': Number(1), }) fc1 = fc.substitute_names(ns) self.assertEqual(fc1.functionDef, fd) self.assertEqual(fc1.callArgExprs[0], Number(1))
def test_evaluate(self): obj = Add(Number(1), Number(1)) self.assertEqual(obj.evaluate(), 2) obj = Add(String('a'), String('b')) self.assertEqual(obj.evaluate(), 'ab') obj = Add(Number(1), String('a')) with self.assertRaises(DslSyntaxError): obj.evaluate()
def test_evaluate(self): obj = Sub(Number(1), Number(1)) self.assertEqual(obj.evaluate(), 0) obj = Sub(Number(1), String('a')) with self.assertRaises(DslSyntaxError): obj.evaluate() obj = Sub(Date('2011-1-2'), Date('2011-1-1')) self.assertEqual(obj.evaluate(), relativedelta(days=1))
def test_value(self): # Integers are ok. obj = Number(1) self.assertEqual(obj.value, 1) # Floats are ok. obj = Number(1.1) self.assertEqual(obj.value, 1.1) # Numpy arrays are ok. obj = Number(array([1, 2])) self.assertEqual(list(obj.value), list(array([1, 2]))) # No args is not ok. with self.assertRaises(DslSyntaxError): Number() # Two args is not ok. with self.assertRaises(DslSyntaxError): Number(1, 1.1) # A list is not ok. with self.assertRaises(DslSyntaxError): Number([1, 1.1]) # A string is not ok. with self.assertRaises(DslSyntaxError): Number('1')
def test_evaluate(self): obj = Min(Number(1), Number(2)) self.assertEqual(obj.evaluate(), 1) obj = Min(Number(1), Number(array([1, 2, 3]))) self.assertEqual(list(obj.evaluate()), list(array([1, 1, 1]))) obj = Min(Number(2), Number(array([1, 2, 3]))) self.assertEqual(list(obj.evaluate()), list(array([1, 2, 2]))) obj = Min(Number(array([3, 2, 1])), Number(array([1, 2, 3]))) self.assertEqual(list(obj.evaluate()), list(array([1, 2, 1])))
def test_evaluate(self): obj = Or([Number(1), Number(1)]) self.assertTrue(obj.evaluate()) obj = Or([Number(1), Number(0)]) self.assertTrue(obj.evaluate()) obj = Or([Number(0), Number(1)]) self.assertTrue(obj.evaluate()) obj = Or([Number(0), Number(0)]) self.assertFalse(obj.evaluate())
def test_substitute(self): # Maybe if Name can take a string, perhaps also other things can? # Maybe the parser should return Python string, numbers etc? # Maybe String and Number etc don't add anything? obj = Name('a') self.assertEqual(obj.name, 'a') obj = Name(String('a')) self.assertEqual(obj.name, 'a') ns = DslNamespace() with self.assertRaises(DslNameError): obj.substitute_names(ns) ns = DslNamespace({'a': 1}) self.assertEqual(obj.substitute_names(ns), Number(1)) ns = DslNamespace({'a': datetime.timedelta(1)}) self.assertEqual(obj.substitute_names(ns), TimeDelta(datetime.timedelta(1))) function_def = FunctionDef('f', [], Number(1), []) ns = DslNamespace({'a': function_def}) self.assertEqual(obj.substitute_names(ns), function_def)
def test_call_functions_with_pending_call_stack(self): fc = FunctionCall(Name('f'), [Name('x')]) fd = FunctionDef('f', [FunctionArg('a', '')], Name('a'), []) namespace = DslNamespace({fd.name: fd}) fd.module_namespace = namespace number = Number(1234) ns = DslNamespace({ 'f': fd, 'x': number, }) # Substitute names. fc1 = fc.substitute_names(ns) self.assertEqual(fc1.functionDef, fd) self.assertEqual(fc1.callArgExprs[0], number) # Call functions. expr = fc1.call_functions() self.assertEqual(expr, number)
def test_evaluate(self): obj = Div(Number(5), Number(2)) self.assertEqual(obj.evaluate(), 2.5) obj = Div(TimeDelta(String('2d')), Number(2)) self.assertEqual(obj.evaluate(), relativedelta(days=1)) obj = Div(Number(5), Number(0)) with self.assertRaises(ZeroDivisionError): obj.evaluate() obj = Div(Number(2.1), String('a')) with self.assertRaises(DslSyntaxError): obj.evaluate()
def test_evaluate(self): obj = Max(Number(1), Number(2)) self.assertEqual(obj.evaluate(), 2) obj = Max(Number(1), Number(array([1, 2, 3]))) self.assertEqual(list(obj.evaluate()), list(array([1, 2, 3]))) obj = Max(Number(2), Number(array([1, 2, 3]))) self.assertEqual(list(obj.evaluate()), list(array([2, 2, 3]))) obj = Max(Number(array([3, 2, 1])), Number(array([1, 2, 3]))) self.assertEqual(list(obj.evaluate()), list(array([3, 2, 3]))) # Swap args. obj = Max(Number(array([1, 2, 3])), Number(1)) self.assertEqual(list(obj.evaluate()), list(array([1, 2, 3]))) obj = Max(Number(array([1, 2, 3])), Number(2)) self.assertEqual(list(obj.evaluate()), list(array([2, 2, 3]))) obj = Max(Number(array([1, 2, 3])), Number(array([3, 2, 1]))) self.assertEqual(list(obj.evaluate()), list(array([3, 2, 3])))
def test_str(self): obj = And([Number(1), Or([Number(2), Number(3)])]) self.assertEqual(str(obj), '(1 and (2 or 3))') # Check the indentation isn't propagated. self.assertEqual(str(obj), '(1 and (2 or 3))')
def test_str(self): obj = Or([Number(1), Number(2), Number(3)]) self.assertEqual(str(obj), '(1 or 2 or 3)')
def test_str(self): obj = And([Number(1), Number(2), Number(3)]) self.assertEqual(str(obj), '(1 and 2 and 3)')
def test_str(self): obj = Number(1) self.assertEqual(str(obj), '1') self.assertEqual(str(Subclass(obj)), 'Subclass(1)')