如何为计算器创建一些单元测试/测试?

时间:2018-07-16 22:25:04

标签: swift xcode macos

我已经使用swift4在Xcode中创建了一个计算器。这在设计上是非常基础的。我想知道如何为计算器创建测试?我已经使用标签链接了按钮。如何在Xcode中创建测试,我已经知道要去哪里,我只想知道如何在代码中测试某些功能以及可以编写哪些功能来测试我的功能?

这是下面的代码-

import UIKit

class ViewController: UIViewController {

    var inputNum:Double = 0
    var previousNum:Double = 0
    var mathFormula = false
    var operation = 0

    @IBOutlet weak var label: UILabel!

    @IBAction func NumberPanel(_ sender: UIButton)
    {
        //Store inputNum for calculating formula
        if mathFormula == true
        {
            label.text = String(sender.tag-1)
            inputNum = Double(label.text!)!
            mathFormula = false
        }
        else
        {
            label.text = label.text! + String(sender.tag-1)
            inputNum = Double(label.text!)!
        }
    }    
    @IBAction func buttons(_ sender: UIButton)
    {
      if label.text != "" && sender.tag != 11 && sender.tag != 16

      {
        previousNum = Double(label.text!)!
        //divide functionality
        if sender.tag == 12
        {
            label.text = "/"
        }
        //multiplication functionality
        else if sender.tag == 13
        {
            label.text = "x"
        }
        //subtraction functionality
        else if sender.tag == 14
        {
            label.text = "-"
        }
        //addition functionality
        else if sender.tag == 15
        {
            label.text = "+"
        }
        operation = sender.tag
        mathFormula = true;
    }
    else if sender.tag == 16
    {
         if operation == 12
         {
            //calculate division formula
            label.text = String(previousNum / inputNum)
         }
         else if operation == 13
         {
            //calculate multiplication formula
            label.text = String(previousNum * inputNum)
         }
         else if operation == 14
         {
            //calculate subtraction formula
            label.text = String(previousNum - inputNum)
         }
         else if operation == 15
         {
            //calculate addition formula
            label.text = String(previousNum + inputNum)
         }
    }
    else if sender.tag == 11
    {     
        // reset values back to 0
        label.text = ""
        inputNum = 0
        previousNum = 0
        operation = 0
    }
}
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

0 个答案:

没有答案