def test_add_variant_at_instantiation(self): mock_backend = asp_module.ASPBackend(Mock(), None, Mock()) a = asp_module.SpecializedFunction("foo", mock_backend, Mock(), ["foo_1"], ["void foo_1(){return;}"]) self.assertEqual(len(a.variant_funcs), 1) self.assertTrue(mock_backend.module.add_to_module.called)
def test_add_variant(self): mock_backend = asp_module.ASPBackend(Mock(), None, Mock()) a = asp_module.SpecializedFunction("foo", mock_backend, Mock()) a.add_variant("foo_1", "void foo_1(){return;}") self.assertEqual(a.variant_names[0], "foo_1") self.assertEqual(len(a.variant_funcs), 1) # also check to make sure the backend added the function self.assertTrue(mock_backend.module.add_to_module.called) self.assertRaises(Exception, a.add_variant, "foo_1", None)
def test_call(self): # this is a complicated situation. we want the backend to have a fake # module, and that fake module should return a fake compiled module. # we'll cheat by just returning itself. mock_backend_module = Mock() mock_backend_module.compile.return_value = mock_backend_module mock_backend = asp_module.ASPBackend(mock_backend_module, None, Mock()) mock_db = Mock() mock_db.get.return_value = [] a = asp_module.SpecializedFunction("foo", mock_backend, mock_db) a.add_variant("foo_1", "void foo_1(){return;}") # test a call a() # it should call foo() on the backend module self.assertTrue(mock_backend_module.foo_1.called)
def test_pick_next_variant(self): mock_db = Mock() mock_db.get.return_value = [] a = asp_module.SpecializedFunction("foo", Mock(), mock_db) a.add_variant("foo_1", "void foo_1(){return;}") a.add_variant("foo_2", "void foo_2(){}") self.assertEqual(a.pick_next_variant(), "foo_1") # now if one has run mock_db.get.return_value = [[None, "foo_1", None, None]] self.assertEqual(a.pick_next_variant(), "foo_2") # now if both have run mock_db.get.return_value = [[None, "foo_1", None, 1.0], [None, "foo_2", None, 2.0]] self.assertEqual(a.pick_next_variant(), "foo_1")
def test_calling_with_multiple_variants(self): # this is a complicated situation. we want the backend to have a fake # module, and that fake module should return a fake compiled module. # we'll cheat by just returning itself. mock_backend_module = Mock() mock_backend_module.compile.return_value = mock_backend_module mock_backend = asp_module.ASPBackend(mock_backend_module, None, Mock()) mock_db = Mock() mock_db.get.return_value = [] a = asp_module.SpecializedFunction("foo", mock_backend, mock_db) a.add_variant("foo_1", "void foo_1(){return;}") a.add_variant("foo_2", "void foo_2(){}") # test 2 calls a() # ensure the second one sees that foo_1 was called the first time mock_db.get.return_value = [["foo", "foo_1", None, None]] a() # it should call both variants on the backend module self.assertTrue(mock_backend_module.foo_1.called) self.assertTrue(mock_backend_module.foo_2.called)
def test_creating(self): a = asp_module.SpecializedFunction("foo", None, Mock())