提交 a0d6015a 编写于 作者: J John Hampton

Added data model and tests

上级 27f9a9b0
......@@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */
37154FE2222CBCFE006731A8 /* RatingControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37154FE1222CBCFE006731A8 /* RatingControl.swift */; };
37154FE4222F1EF7006731A8 /* Meal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37154FE3222F1EF7006731A8 /* Meal.swift */; };
3775361422234238005EE5FE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3775361322234238005EE5FE /* AppDelegate.swift */; };
3775361622234238005EE5FE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3775361522234238005EE5FE /* ViewController.swift */; };
3775361922234238005EE5FE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3775361722234238005EE5FE /* Main.storyboard */; };
......@@ -36,6 +37,7 @@
/* Begin PBXFileReference section */
37154FE1222CBCFE006731A8 /* RatingControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RatingControl.swift; sourceTree = "<group>"; };
37154FE3222F1EF7006731A8 /* Meal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Meal.swift; sourceTree = "<group>"; };
3775361022234238005EE5FE /* iOSTemplate.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = iOSTemplate.app; sourceTree = BUILT_PRODUCTS_DIR; };
3775361322234238005EE5FE /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3775361522234238005EE5FE /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
......@@ -101,6 +103,7 @@
children = (
3775361322234238005EE5FE /* AppDelegate.swift */,
3775361522234238005EE5FE /* ViewController.swift */,
37154FE3222F1EF7006731A8 /* Meal.swift */,
3775361722234238005EE5FE /* Main.storyboard */,
37154FE1222CBCFE006731A8 /* RatingControl.swift */,
3775361A22234238005EE5FE /* Assets.xcassets */,
......@@ -258,6 +261,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
37154FE4222F1EF7006731A8 /* Meal.swift in Sources */,
3775361622234238005EE5FE /* ViewController.swift in Sources */,
37154FE2222CBCFE006731A8 /* RatingControl.swift in Sources */,
3775361422234238005EE5FE /* AppDelegate.swift in Sources */,
......
//
// Meal.swift
// iOSTemplate
//
// Created by John Hampton on 3/5/19.
//
import UIKit
class Meal {
// MARK: Properties
var name: String
var photo: UIImage?
var rating: Int
// MARK: Initialization
init?(name: String, photo: UIImage?, rating: Int) {
// The name must not be empty
guard !name.isEmpty else {
return nil
}
// The rating must be between 0 and 5 inclusively
guard (rating >= 0) && (rating <= 5) else {
return nil
}
// Initialize stored properties.
self.name = name
self.photo = photo
self.rating = rating
}
}
......@@ -10,24 +10,31 @@ import XCTest
class iOSTemplateTests: XCTestCase {
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
// MARK: Meal Class Tests
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
// Confirm that the Meal initializer returns a Meal object when passed valid parameters.
func testMealInitializationSucceeds() {
// Zero rating
let zeroRatingMeal = Meal.init(name: "Zero", photo: nil, rating: 0)
XCTAssertNotNil(zeroRatingMeal)
// Highest positive rating
let positiveRatingMeal = Meal.init(name: "Positive", photo: nil, rating: 5)
XCTAssertNotNil(positiveRatingMeal)
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Confirm that the Meal initialier returns nil when passed a negative rating or an empty name.
func testMealInitializationFails() {
// Negative rating
let negativeRatingMeal = Meal.init(name: "Negative", photo: nil, rating: -1)
XCTAssertNil(negativeRatingMeal)
// Empty String
let emptyStringMeal = Meal.init(name: "", photo: nil, rating: 0)
XCTAssertNil(emptyStringMeal)
// Rating exceeds maximum
let largeRatingMeal = Meal.init(name: "Large", photo: nil, rating: 6)
XCTAssertNil(largeRatingMeal)
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册