Skip to main content

Hey All I have a section of a script that is pretty simple and works but I am wondering the best way to rewrite it to be testable.

I could set up an individual test to make sure the correct pref string is returned but wondering if there is a less breakable way

public class PreferenceHelper
{

public Stringstring getItemPreferenceByNumber(int num)
{
    switch (num)
    {
        case 0:
            return Constant.Tree_Bark_Upgrade_One_Unlocked;
        case 1:
            return Constant.Tree_Bark_Upgrade_Two_Unlocked;
        case 2:
            return Constant.Tree_Bark_Upgrade_Three_Unlocked;
        case 3:
            return Constant.Extra_Coconut_Upgrade_One_Unlocked;
        case 4:
            return Constant.Extra_Coconut_Upgrade_Two_Unlocked;
        case 5:
            return Constant.Extra_Coconut_Upgrade_Three_Unlocked;
        case 6:
            return Constant.Grandpaps_Cane_Unlocked;
        case 7:
            return Constant.Bamboo_Staff_Unlocked;
        case 8:
            return Constant.Whackin_Bat_Unlocked;
        case 9:
            return Constant.Whackin_Mallet_Unlocked;
        case 10:
            return Constant.Whackin_Golf_Club_Unlocked;
        case 11:
            return Constant.Whackin_Hammer_Unlocked;
        case 12:
            return Constant.Sap_Bomb_Unlocked;
        case 13:
            return Constant.Crabby_Bomb_Unlocked;
        case 14:
            return Constant.Boombox_Unlocked;
        case 15:
            return Constant.CraboPolt_Unlocked;
        case 16:
            return Constant.Inflatable_Palm_Tree_Unlocked;
        case 17:
            return Constant.Big_Bomb_Unlocked;
        default:
            return "";
    }
  }
}

Hey All I have a section of script that is pretty simple and works but I am wondering the best way to rewrite it to be testable.

I could set up individual test to make sure the correct pref string is returned but wondering if there is a less breakable way

public class PreferenceHelper
{

public String getItemPreferenceByNumber(int num)
{
    switch (num)
    {
        case 0:
            return Constant.Tree_Bark_Upgrade_One_Unlocked;
        case 1:
            return Constant.Tree_Bark_Upgrade_Two_Unlocked;
        case 2:
            return Constant.Tree_Bark_Upgrade_Three_Unlocked;
        case 3:
            return Constant.Extra_Coconut_Upgrade_One_Unlocked;
        case 4:
            return Constant.Extra_Coconut_Upgrade_Two_Unlocked;
        case 5:
            return Constant.Extra_Coconut_Upgrade_Three_Unlocked;
        case 6:
            return Constant.Grandpaps_Cane_Unlocked;
        case 7:
            return Constant.Bamboo_Staff_Unlocked;
        case 8:
            return Constant.Whackin_Bat_Unlocked;
        case 9:
            return Constant.Whackin_Mallet_Unlocked;
        case 10:
            return Constant.Whackin_Golf_Club_Unlocked;
        case 11:
            return Constant.Whackin_Hammer_Unlocked;
        case 12:
            return Constant.Sap_Bomb_Unlocked;
        case 13:
            return Constant.Crabby_Bomb_Unlocked;
        case 14:
            return Constant.Boombox_Unlocked;
        case 15:
            return Constant.CraboPolt_Unlocked;
        case 16:
            return Constant.Inflatable_Palm_Tree_Unlocked;
        case 17:
            return Constant.Big_Bomb_Unlocked;
        default:
            return "";
    }
  }
}

I have a section of a script that is pretty simple and works but I am wondering the best way to rewrite it to be testable.

I could set up an individual test to make sure the correct pref string is returned but wondering if there is a less breakable way

public class PreferenceHelper
{

public string getItemPreferenceByNumber(int num)
{
    switch (num)
    {
        case 0:
            return Constant.Tree_Bark_Upgrade_One_Unlocked;
        case 1:
            return Constant.Tree_Bark_Upgrade_Two_Unlocked;
        case 2:
            return Constant.Tree_Bark_Upgrade_Three_Unlocked;
        case 3:
            return Constant.Extra_Coconut_Upgrade_One_Unlocked;
        case 4:
            return Constant.Extra_Coconut_Upgrade_Two_Unlocked;
        case 5:
            return Constant.Extra_Coconut_Upgrade_Three_Unlocked;
        case 6:
            return Constant.Grandpaps_Cane_Unlocked;
        case 7:
            return Constant.Bamboo_Staff_Unlocked;
        case 8:
            return Constant.Whackin_Bat_Unlocked;
        case 9:
            return Constant.Whackin_Mallet_Unlocked;
        case 10:
            return Constant.Whackin_Golf_Club_Unlocked;
        case 11:
            return Constant.Whackin_Hammer_Unlocked;
        case 12:
            return Constant.Sap_Bomb_Unlocked;
        case 13:
            return Constant.Crabby_Bomb_Unlocked;
        case 14:
            return Constant.Boombox_Unlocked;
        case 15:
            return Constant.CraboPolt_Unlocked;
        case 16:
            return Constant.Inflatable_Palm_Tree_Unlocked;
        case 17:
            return Constant.Big_Bomb_Unlocked;
        default:
            return "";
    }
  }
}
added 2 characters in body
Source Link
    [UnityTest]
    public IEnumerator UserGetsTreeBarkUpgrade()
    {
        int num = 0;
        String pref = _preferenceHelper.getItemPreferenceByNumber(num);
        Assert.EqualsAreEqual(pref, Constant.Tree_Bark_Upgrade_One_Unlocked);
        yield return null;
    }
    [UnityTest]
    public IEnumerator UserGetsTreeBarkUpgrade()
    {
        int num = 0;
        String pref = _preferenceHelper.getItemPreferenceByNumber(num);
        Assert.Equals(pref, Constant.Tree_Bark_Upgrade_One_Unlocked);
        yield return null;
    }
    [UnityTest]
    public IEnumerator UserGetsTreeBarkUpgrade()
    {
        int num = 0;
        String pref = _preferenceHelper.getItemPreferenceByNumber(num);
        Assert.AreEqual(pref, Constant.Tree_Bark_Upgrade_One_Unlocked);
        yield return null;
    }
Source Link

Testing player preferences in unity

Hey All I have a section of script that is pretty simple and works but I am wondering the best way to rewrite it to be testable.

        var stats = other.gameObject.GetComponent<ItemStats>();
        var itemReturn = other.gameObject.GetComponent<ReturnItemToPositionIfDroppedOrStolen>();
        
        var prefrenceName = "";
        
        switch (stats.itemNumber)
        {
            case 0:
                prefrenceName = Constant.Tree_Bark_Upgrade_One_Unlocked;
                break;
            case 1:
                prefrenceName = Constant.Tree_Bark_Upgrade_Two_Unlocked;
                break;
            case 2:
                prefrenceName = Constant.Tree_Bark_Upgrade_Three_Unlocked;
                break;
            case 3:
                prefrenceName = Constant.Extra_Coconut_Upgrade_One_Unlocked;
                break;
            case 4:
                prefrenceName = Constant.Extra_Coconut_Upgrade_Two_Unlocked;
                break;
            case 5:
                prefrenceName = Constant.Extra_Coconut_Upgrade_Three_Unlocked;
                break;
            case 6:
                prefrenceName = Constant.Grandpaps_Cane_Unlocked;
                break;
            case 7:
                prefrenceName = Constant.Whackin_Bat_Unlocked;
                break;
            case 8:
                prefrenceName = Constant.Whackin_Golf_Club_Unlocked;
                break;
            case 9:
                prefrenceName = Constant.Bamboo_Staff_Unlocked;
                break;
            case 10:
                prefrenceName = Constant.Whackin_Mallet_Unlocked;
                break;
            case 11:
                prefrenceName = Constant.Whackin_Hammer_Unlocked;
                break;
            case 12:
                prefrenceName = Constant.Crabby_Bomb_Unlocked;
                break;
            case 13:
                prefrenceName = Constant.CraboPolt_Unlocked;
                break;
            case 14:
                prefrenceName = Constant.Big_Bomb_Unlocked;
                break;
            case 15:
                prefrenceName = Constant.Sap_Bomb_Unlocked;
                break;
            case 16:
                prefrenceName = Constant.Boombox_Unlocked;
                break;
            case 17:
                prefrenceName = Constant.Inflatable_Palm_Tree_Unlocked;
                break;
        }

        var hasItem = PlayerPrefs.GetInt(prefrenceName, 0);

I could set up individual test to make sure the correct pref string is returned but wondering if there is a less breakable way

public class PreferenceHelper
{

public String getItemPreferenceByNumber(int num)
{
    switch (num)
    {
        case 0:
            return Constant.Tree_Bark_Upgrade_One_Unlocked;
        case 1:
            return Constant.Tree_Bark_Upgrade_Two_Unlocked;
        case 2:
            return Constant.Tree_Bark_Upgrade_Three_Unlocked;
        case 3:
            return Constant.Extra_Coconut_Upgrade_One_Unlocked;
        case 4:
            return Constant.Extra_Coconut_Upgrade_Two_Unlocked;
        case 5:
            return Constant.Extra_Coconut_Upgrade_Three_Unlocked;
        case 6:
            return Constant.Grandpaps_Cane_Unlocked;
        case 7:
            return Constant.Bamboo_Staff_Unlocked;
        case 8:
            return Constant.Whackin_Bat_Unlocked;
        case 9:
            return Constant.Whackin_Mallet_Unlocked;
        case 10:
            return Constant.Whackin_Golf_Club_Unlocked;
        case 11:
            return Constant.Whackin_Hammer_Unlocked;
        case 12:
            return Constant.Sap_Bomb_Unlocked;
        case 13:
            return Constant.Crabby_Bomb_Unlocked;
        case 14:
            return Constant.Boombox_Unlocked;
        case 15:
            return Constant.CraboPolt_Unlocked;
        case 16:
            return Constant.Inflatable_Palm_Tree_Unlocked;
        case 17:
            return Constant.Big_Bomb_Unlocked;
        default:
            return "";
    }
  }
}

Test :

    [UnityTest]
    public IEnumerator UserGetsTreeBarkUpgrade()
    {
        int num = 0;
        String pref = _preferenceHelper.getItemPreferenceByNumber(num);
        Assert.Equals(pref, Constant.Tree_Bark_Upgrade_One_Unlocked);
        yield return null;
    }