mercredi 17 février 2021

Method testing a Calculator

I have this calculator that i made in UWP. It can add, multiply, subtract, do divisions, convert Celsius to Fahrenheit and vice versa.

My problem is that i need a way to test it so that i can add, sub, divide, multiply in one test method. And also test so that i can take a Celsius value and convert it to Fahrenheit, all in a test method.

I can change it so that some of my methods become public instead of private, but i don't know how i should go about testing some of these?

My testmethods are at the top of the code.

using NUnit.Framework;
using System;
using System.Data;
using System.Reflection;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace Calc
{
    /// <summary>
    /// A calculator, namespace start. 
    /// </summary>
    /// 

    
    /// Public start, mainpage. 
    public sealed partial class MainPage : Page
    {


       
        /// Input for Celcius/Farh.
        string input;


        /// <summary>
        /// Testing celcius / F.
        /// </summary>
        [TestMethod]
         private void TestDifferentScales()
          {
          
          }


        /// <summary>
        /// Test method - testing all functions regarding mathematical equations!
        /// </summary>
        [TestMethod()]
        public void AdditionTest()
        {
            
           
            
        }




        /// <summary>
        /// Start main program!
        /// </summary>
        public MainPage()
        {
            //Startar allt.
            this.InitializeComponent();
        }


        /// <summary>
        /// Button for number 1.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {

            textBox.Text += '1';
            input += "1";
          
        }

        /// <summary>
        /// Button for number 2.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            textBox.Text += '2';
            input += "2";

        }


        /// <summary>
        /// Button for number 3.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            textBox.Text += '3';
            input += "3";
        }


        /// <summary>
        /// Button for number 4.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            textBox.Text += '4';
            input += "4";

        }


        /// <summary>
        /// Button for number 5.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            textBox.Text += '5';
            input += "5";
        }



        /// <summary>
        /// Button for number 6.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            textBox.Text += '6';
            input += "6";

        }



        /// <summary>
        /// Button for number 7.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            textBox.Text += '7';
            input += "7";

        }



        /// <summary>
        /// Button for number 8.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            textBox.Text += '8';
            input += "8";
        }



        /// <summary>
        /// Button for number 9.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_8(object sender, RoutedEventArgs e)
        {
            textBox.Text += '9';
            input += "9";

        }



        /// <summary>
        /// Button for numbers 42 "Marcus".
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_9(object sender, RoutedEventArgs e)
        {
            textBox.Text += "42";
            input += "42";
        }



        /// <summary>
        /// Button for number 0.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_10(object sender, RoutedEventArgs e)
        {
            textBox.Text += '0';
            input += "0";
        }



        /// <summary>
        /// Button for multiplication of equation "F = M*A".
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_11(object sender, RoutedEventArgs e)
        {

            /* F = M * A 
             * Denna ekvation är simpel och man lär sig den redan i början utav Fysik 1.
             * Ekvationen är inte specifik, så som tex "F = M * G" då gravitationen kan bli ett "fixed" värde som 
             * går mellan 9,802 och 9,820.
             * Anledningen till varför denna är så simpel är för att jag ville bevisa att det gick att ha många komplikaitoner 
             * i ett knapp tryck. Så denna skulle lika gärna kunnat vara knappen för multiplikation.
             */
            textBox.Text += '*';

        }



        /// <summary>
        /// Button for addition.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickPlus(object sender, RoutedEventArgs e)
        {

            /* "Exceptions without Execeptions"
             * Här försökte jag att gå runt utan att behöva använda en 
             * try/catch eller Exception inför min "data table" på min "=" knapp.
             * Men, som sagt, det är ingen skillnad här och på min knapp för "F = M * A".
             * Det finns ju klara skillander, jag har försökt att se till så att jag går tillbaka ett
             * steg ifall det finns flera matematiska karaktärer. 
             * 
             * MEN: Det skapar problem då jag fortfarande ville kunna köra "6--1 = 7" utan att krasha allt.
             * 
             */
            var last = textBox.Text;
            if (last[last.Length - 1] != '+' || last[last.Length - 1] != '-' || last[last.Length - 1] != '*' || last[last.Length - 1] != '/')
            {
                textBox.Text += '+';
            }
            else
            {
                textBox.Text = last.Remove(last.Length - 1, 1);
                textBox.Text += '+';
            }
        }


        /// <summary>
        /// Button for subtraction. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickMinus(object sender, RoutedEventArgs e)
        {
            var last = textBox.Text;
            if (last[last.Length - 1] != '+' || last[last.Length - 1] != '-' || last[last.Length - 1] != '*' || last[last.Length - 1] != '/')
            {
                textBox.Text += '-';
            }
            else
            {
                textBox.Text = last.Remove(last.Length - 1, 1);
                textBox.Text += '-';
            }
        }



        /// <summary>
        /// Button for multiplication.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickTimes(object sender, RoutedEventArgs e)
        {
            var last = textBox.Text;
            if (last[last.Length - 1] != '+' || last[last.Length - 1] != '-' || last[last.Length - 1] != '*' || last[last.Length - 1] != '/')
            {
                textBox.Text += '*';
            }
            else
            {
                textBox.Text = last.Remove(last.Length - 1, 1);
                textBox.Text += '*';
            }
        }
        

        /// <summary>
        /// Button for division. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickDEv(object sender, RoutedEventArgs e)
        {
            var last = textBox.Text;
            if (last[last.Length - 1] != '+' || last[last.Length - 1] != '-' || last[last.Length - 1] != '*' || last[last.Length - 1] != '/')
            {
                textBox.Text += '/';
            }
            else
            {
                textBox.Text = last.Remove(last.Length - 1, 1);
                textBox.Text += '/';
            }
        }


        /// <summary>
        /// Button for "Equal", the equal button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickEqual(object sender, RoutedEventArgs e)
        {
            /* Exception.
             * Jag prövade att unit testa här med en thread timer för att kunna 
             * skriva ett meddelande i boxen typ "Error: Please try again" 
             * och sen rensa allt, men det gav error, det funkade men jag ville
             * inte ha något error med så jag tog bort den från min "catch".
             * 
             */
            try
            {
             object q = new DataTable().Compute(textBox.Text, null);
                        textBox.Text = q.ToString();
            }
            catch
            {
             
                /*
                 * Här återställer jag bara allt.  
                 */
           
                textBox.Text = string.Empty;
                input = string.Empty;
            }
           
        }



        /// <summary>
        /// Button for reset of calcuator.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickReset(object sender, RoutedEventArgs e)
        {

            /*
             * Åter ställer här på "ClickReset" också.
             */
            textBox.Text = string.Empty;
            input = string.Empty;

        }

        /// <summary>
        /// Button from  F to C.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_fc_(object sender, RoutedEventArgs e)
        {

            /*
             * Här så var jag tvungen att gå fram och tillbaka med att 
             * konvertera saker från och till int och string. 
             * Multiplicerade och addade 32 eller tog bort 32 för att
             * få rätt värden i slutet. 
             */
            int value = int.Parse(input);
            int after = value - 32; 
            int a = (int)(decimal.Divide(after, (decimal)1.8));
            string answer = a.ToString();
            textBox.Text = string.Empty;
            textBox.Text = answer;
        }


        /// <summary>
        /// Button from C to F.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Button_Click_cf_(object sender, RoutedEventArgs e)
        {
            int value = int.Parse(input);
            int a = (int)(decimal.Multiply(value, (decimal)1.8) + 32);
            string answer = a.ToString();
            textBox.Text = string.Empty;
            textBox.Text = answer; 
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire