Liquid Object: Unit test for entering liquid containers

The tests handles the entrance of liquid objects into the container. Will look to simplify the script in the next commit.
liquid_container
Mark 2016-02-20 20:14:19 +01:00
parent d0cf26490e
commit 9e46d752a9
3 changed files with 99 additions and 4 deletions

View File

@ -48,14 +48,19 @@ func CannotEnter(object into)
// Enters liquid containers only
if (into->~IsLiquidContainer() || into->~IsLiquidPump())
{
for (var liquid in FindObjects(Find_Func("IsLiquid"), Find_Container(into)))
if (!into->~LiquidContainerAccepts(IsLiquid()))
{
if (MergeWith(liquid))
return true; // Cannot enter
}
for (var other_liquid in FindObjects(Find_Func("IsLiquid"), Find_Container(into)))
{
if (MergeWith(other_liquid))
{
return true; // Cannot enter
}
}
return false; // Enter this object
}
else
@ -157,7 +162,8 @@ func MergeWith(object liquid_object)
{
if (WildcardMatch(IsLiquid(), liquid_object->~IsLiquid()))
{
liquid_object->DoLiquidAmount(GetLiquidAmount());
var transferred = liquid_object->PutLiquid(IsLiquid(), GetLiquidAmount(), this);
DoLiquidAmount(-transferred);
return true;
}
return false;
@ -247,6 +253,11 @@ func PutLiquid(string liquid_name, int amount, object source)
{
FatalError(Format("You can insert positive amounts of liquid only, got %d", amount));
}
if (Contained() && Contained()->~IsLiquidContainer())
{
return Contained()->PutLiquid(liquid_name, amount, source);
}
if (IsLiquid() == liquid_name)
{
@ -277,6 +288,12 @@ func RemoveLiquid(string liquid_name, int amount, object destination)
{
FatalError(Format("You can remove positive amounts of liquid only, got %d", amount));
}
if (Contained() && Contained()->~IsLiquidContainer())
{
return Contained()->RemoveLiquid(liquid_name, amount, destination);
}
// default parameters if nothing is provided: the current material and level
liquid_name = liquid_name ?? IsLiquid();

View File

@ -220,3 +220,13 @@ func SetLiquidContainer(string liquid_name, int amount)
SetLiquidFillLevel(amount);
}
func Collection2(object item)
{
if (item->~IsLiquid() && !GetLiquidItem())
{
SetLiquidItem(item);
}
_inherited(...);
}

View File

@ -702,3 +702,71 @@ global func Test11_Execute()
return passed;
}
global func Test12_OnStart(int plr){ return true;}
global func Test12_OnFinished(){ return; }
global func Test12_Execute()
{
Log("Test the behaviour of liquid objects entering liquid containers");
var container = CreateObject(Barrel);
var liquid = CreateObject(Liquid_Water);
// can fill empty barrel with the liquid
liquid->SetLiquidAmount(100);
liquid->Enter(container);
var passed = true;
var returned = liquid->Contained();
var test = (returned == container); passed &= test;
Log("- Liquid can enter empty barrel: %v", test);
returned = container->GetLiquidFillLevel();
test = (100 == returned); passed &= test;
Log("- Barrel contains %d units, expected %d: %v", returned, 100, test);
// can fill barrel with more liquid, liquid object gets removed
liquid = CreateObject(Liquid_Water);
liquid->SetLiquidAmount(100);
liquid->Enter(container);
var returned = liquid;
var test = (returned == nil); passed &= test;
Log("- Liquid can enter filled barrel, liquid got removed: %v", test);
returned = container->GetLiquidFillLevel();
test = (200 == returned); passed &= test;
Log("- Barrel contains %d units, expected %d: %v", returned, 200, test);
// cannot fill in more than the allowed amount
liquid = CreateObject(Liquid_Water);
liquid->SetLiquidAmount(200);
liquid->Enter(container);
var returned = liquid->Contained();
var test = (returned == nil); passed &= test;
Log("- Liquid cannot enter filled barrel if the capacity is exceeded: %v", test);
returned = container->GetLiquidFillLevel();
test = (300 == returned); passed &= test;
Log("- Barrel does increase fill level, up to the allowed amount, contains %d units, expected %d: %v", returned, 300, test);
liquid->RemoveObject();
Log("- Retting liquid amount to 200");
container->SetLiquidFillLevel(200);
// cannot fill in a different liquid
liquid = CreateObject(Liquid_Oil);
liquid->SetLiquidAmount(50);
liquid->Enter(container);
var returned = liquid->Contained();
var test = (returned == nil); passed &= test;
Log("- Liquid cannot enter filled barrel of a different liquid type: %v", test);
returned = container->GetLiquidFillLevel();
test = (200 == returned); passed &= test;
Log("- Barrel does not increase fill level, contains %d units, expected %d: %v", returned, 200, test);
liquid->RemoveObject();
container->RemoveObject();
return passed;
}