< Summary

Class:NutritionScaleBehaviour
Assembly:Test
File(s):E:/Unity/Unity Project/VR-Cooking-Demo/Assets/_Scripts/NutritionScaleBehaviour.cs
Covered lines:26
Uncovered lines:26
Coverable lines:52
Total lines:117
Line coverage:50% (26 of 52)
Covered branches:0
Total branches:0
Covered methods:2
Total methods:4
Method coverage:50% (2 of 4)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Start()0%000100%
Update()0%00040%
OnTriggerEnter(...)0%0000%
OnTriggerExit(...)0%0000%

File(s)

E:/Unity/Unity Project/VR-Cooking-Demo/Assets/_Scripts/NutritionScaleBehaviour.cs

#LineLine coverage
 1/*
 2 *
 3 * Code by:
 4 *      Dimitrios Vlachos
 5 *      djv1@student.london.ac.uk
 6 *      dimitri.j.vlachos@gmail.com
 7 *
 8 */
 9
 10using TMPro;
 11using Unity.VisualScripting;
 12using UnityEngine;
 13
 14public class NutritionScaleBehaviour : MonoBehaviour
 15{
 16    [SerializeField] private GameObject obj;
 17    [SerializeField] private FoodItem item;
 18
 19    [Header("Text Displays")]
 20    [SerializeField] private GameObject foodName;
 21    [SerializeField] private GameObject weight;
 22    [SerializeField] private GameObject calories;
 23    [SerializeField] private GameObject protein;
 24    [SerializeField] private GameObject carbohydrates;
 25    [SerializeField] private GameObject sugar;
 26    [SerializeField] private GameObject fiber;
 27    [SerializeField] private GameObject fat;
 28
 29    private TextMeshProUGUI textMeshPro_name_text;
 30    private TextMeshProUGUI textMeshPro_weight_text;
 31    private TextMeshProUGUI textMeshPro_calories_text;
 32    private TextMeshProUGUI textMeshPro_protein_text;
 33    private TextMeshProUGUI textMeshPro_carbohydrates_text;
 34    private TextMeshProUGUI textMeshPro_sugar_text;
 35    private TextMeshProUGUI textMeshPro_fiber_text;
 36    private TextMeshProUGUI textMeshPro_fat_text;
 37
 38    // Start is called before the first frame update
 39    void Start()
 140    {
 141        obj = null;
 142        item = null;
 43
 144        textMeshPro_name_text           = foodName.GetComponent<TextMeshProUGUI>();
 145        textMeshPro_weight_text         = weight.GetComponent<TextMeshProUGUI>();
 146        textMeshPro_calories_text       = calories.GetComponent<TextMeshProUGUI>();
 147        textMeshPro_protein_text        = protein.GetComponent<TextMeshProUGUI>();
 148        textMeshPro_carbohydrates_text  = carbohydrates.GetComponent<TextMeshProUGUI>();
 149        textMeshPro_sugar_text          = sugar.GetComponent<TextMeshProUGUI>();
 150        textMeshPro_fiber_text          = fiber.GetComponent<TextMeshProUGUI>();
 151        textMeshPro_fat_text            = fat.GetComponent<TextMeshProUGUI>();
 52
 53
 54
 155    }
 56
 57    // Update is called once per frame
 58    void Update()
 168259    {
 60        // Guard statement
 168261        if (obj == null)
 168262        {
 168263            item = null;
 168264            textMeshPro_name_text.text          = "";
 65
 168266            textMeshPro_weight_text.text        = "";
 168267            textMeshPro_calories_text.text      = "";
 168268            textMeshPro_protein_text.text       = "";
 168269            textMeshPro_carbohydrates_text.text = "";
 168270            textMeshPro_sugar_text.text         = "";
 168271            textMeshPro_fiber_text.text         = "";
 168272            textMeshPro_fat_text.text           = "";
 168273            return;
 74        }
 075        if (item == null)
 076        {
 077            item = obj.GetComponentInParent<FoodItem>();
 078        }
 079        if (item == null) return;
 80
 81        // Functional code
 82
 83        // Set the food name
 084        textMeshPro_name_text.text = item.type.ToString();
 85
 86        // Get the nutritional values and round them to one decimal place
 087        float weight        =   Mathf.Round(item.weight * 10f) / 10f;
 088        float calories      =   Mathf.Round(item.calories * 10f) / 10f;
 089        float protein       =   Mathf.Round(item.protein * 10f) / 10f;
 090        float carbohydrates =   Mathf.Round(item.carbohydrates * 10f) / 10f;
 091        float sugar         =   Mathf.Round(item.sugar * 10f) / 10f;
 092        float fibre         =   Mathf.Round(item.fibre * 10f) / 10f;
 093        float fat           =   Mathf.Round(item.fat * 10f) / 10f;
 94
 95        // Set the text to the newly rounded values
 096        textMeshPro_weight_text.text        = weight.ToString() + "g";
 097        textMeshPro_calories_text.text      = calories.ToString() + "kcal";
 098        textMeshPro_protein_text.text       = protein.ToString() + "g";
 099        textMeshPro_carbohydrates_text.text = carbohydrates.ToString() + "g";
 0100        textMeshPro_sugar_text.text         = sugar.ToString() + "g";
 0101        textMeshPro_fiber_text.text         = fibre.ToString() + "g";
 0102        textMeshPro_fat_text.text           = fat.ToString() + "g";
 1682103    }
 104
 105    private void OnTriggerEnter(Collider other)
 0106    {
 107        //Debug.Log("Object entered: " + other.name);
 0108        obj = other.gameObject;
 109
 0110    }
 111
 112    private void OnTriggerExit(Collider other)
 0113    {
 114        //Debug.Log("Object exited");
 0115        obj = null;
 0116    }
 117}