Add tests for ServerProfile

This commit is contained in:
Rainux Luo
2017-01-07 09:26:32 +08:00
parent 5e06b4b4f0
commit 9dafb4fb79
5 changed files with 64 additions and 3 deletions

View File

@ -60,6 +60,7 @@
C8E42A6C1D4F270A0074C7EA /* UserRulesController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8E42A6A1D4F270A0074C7EA /* UserRulesController.swift */; }; C8E42A6C1D4F270A0074C7EA /* UserRulesController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8E42A6A1D4F270A0074C7EA /* UserRulesController.swift */; };
C8E42A6E1D4F2CAF0074C7EA /* UserRulesController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C8E42A701D4F2CAF0074C7EA /* UserRulesController.xib */; }; C8E42A6E1D4F2CAF0074C7EA /* UserRulesController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C8E42A701D4F2CAF0074C7EA /* UserRulesController.xib */; };
D829DB3F1E20262300FB5E1E /* UtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D829DB3E1E20262300FB5E1E /* UtilsTests.swift */; }; D829DB3F1E20262300FB5E1E /* UtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D829DB3E1E20262300FB5E1E /* UtilsTests.swift */; };
D8E3630B1E2072980027449B /* ServerProfileTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8E3630A1E2072980027449B /* ServerProfileTests.swift */; };
E0E57CCA7EB34B90F9D340F2 /* Pods_ShadowsocksX_NGTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 388120F062D7EB7DD0D8DDCA /* Pods_ShadowsocksX_NGTests.framework */; }; E0E57CCA7EB34B90F9D340F2 /* Pods_ShadowsocksX_NGTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 388120F062D7EB7DD0D8DDCA /* Pods_ShadowsocksX_NGTests.framework */; };
F0809FF1595BE2966343D3C7 /* libPods-proxy_conf_helper.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1E7783AEDB4A3BDDC9FF16AC /* libPods-proxy_conf_helper.a */; }; F0809FF1595BE2966343D3C7 /* libPods-proxy_conf_helper.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1E7783AEDB4A3BDDC9FF16AC /* libPods-proxy_conf_helper.a */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
@ -178,6 +179,7 @@
C8E42A6F1D4F2CAF0074C7EA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/UserRulesController.xib; sourceTree = "<group>"; }; C8E42A6F1D4F2CAF0074C7EA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/UserRulesController.xib; sourceTree = "<group>"; };
C8E42A721D4F2CB10074C7EA /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/UserRulesController.strings"; sourceTree = "<group>"; }; C8E42A721D4F2CB10074C7EA /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/UserRulesController.strings"; sourceTree = "<group>"; };
D829DB3E1E20262300FB5E1E /* UtilsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UtilsTests.swift; sourceTree = "<group>"; }; D829DB3E1E20262300FB5E1E /* UtilsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UtilsTests.swift; sourceTree = "<group>"; };
D8E3630A1E2072980027449B /* ServerProfileTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServerProfileTests.swift; sourceTree = "<group>"; };
E9E9FB3855DA55D0710EE7BD /* Pods-ShadowsocksX-NG.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ShadowsocksX-NG.release.xcconfig"; path = "Pods/Target Support Files/Pods-ShadowsocksX-NG/Pods-ShadowsocksX-NG.release.xcconfig"; sourceTree = "<group>"; }; E9E9FB3855DA55D0710EE7BD /* Pods-ShadowsocksX-NG.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ShadowsocksX-NG.release.xcconfig"; path = "Pods/Target Support Files/Pods-ShadowsocksX-NG/Pods-ShadowsocksX-NG.release.xcconfig"; sourceTree = "<group>"; };
FE3237E9FB24D9B924A0E630 /* Pods-ShadowsocksX-NG.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ShadowsocksX-NG.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ShadowsocksX-NG/Pods-ShadowsocksX-NG.debug.xcconfig"; sourceTree = "<group>"; }; FE3237E9FB24D9B924A0E630 /* Pods-ShadowsocksX-NG.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ShadowsocksX-NG.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ShadowsocksX-NG/Pods-ShadowsocksX-NG.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
@ -311,6 +313,7 @@
9B0BFFF71D0460A70040E62B /* ShadowsocksX-NGTests */ = { 9B0BFFF71D0460A70040E62B /* ShadowsocksX-NGTests */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
D8E3630A1E2072980027449B /* ServerProfileTests.swift */,
D829DB3E1E20262300FB5E1E /* UtilsTests.swift */, D829DB3E1E20262300FB5E1E /* UtilsTests.swift */,
9B0BFFF81D0460A70040E62B /* ShadowsocksX_NGTests.swift */, 9B0BFFF81D0460A70040E62B /* ShadowsocksX_NGTests.swift */,
9B0BFFFA1D0460A70040E62B /* Info.plist */, 9B0BFFFA1D0460A70040E62B /* Info.plist */,
@ -670,6 +673,7 @@
files = ( files = (
9B0BFFF91D0460A70040E62B /* ShadowsocksX_NGTests.swift in Sources */, 9B0BFFF91D0460A70040E62B /* ShadowsocksX_NGTests.swift in Sources */,
D829DB3F1E20262300FB5E1E /* UtilsTests.swift in Sources */, D829DB3F1E20262300FB5E1E /* UtilsTests.swift in Sources */,
D8E3630B1E2072980027449B /* ServerProfileTests.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -123,7 +123,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
for url in urls { for url in urls {
let profielDict = ParseSSURL(url) let profielDict = ParseSSURL(url)
if let profielDict = profielDict { if let profielDict = profielDict {
let profile = ServerProfile.fromDictionary(profielDict as [String : AnyObject]) let profile = ServerProfile.fromDictionary(profielDict)
mgr.profiles.append(profile) mgr.profiles.append(profile)
isChanged = true isChanged = true

View File

@ -28,7 +28,7 @@ class ServerProfile: NSObject {
self.uuid = uuid self.uuid = uuid
} }
static func fromDictionary(_ data:[String:AnyObject]) -> ServerProfile { static func fromDictionary(_ data:[String: Any?]) -> ServerProfile {
let cp = { let cp = {
(profile: ServerProfile) in (profile: ServerProfile) in
profile.serverHost = data["ServerHost"] as! String profile.serverHost = data["ServerHost"] as! String

View File

@ -21,7 +21,7 @@ class ServerProfileManager: NSObject {
let defaults = UserDefaults.standard let defaults = UserDefaults.standard
if let _profiles = defaults.array(forKey: "ServerProfiles") { if let _profiles = defaults.array(forKey: "ServerProfiles") {
for _profile in _profiles { for _profile in _profiles {
let profile = ServerProfile.fromDictionary(_profile as! [String : AnyObject]) let profile = ServerProfile.fromDictionary(_profile as! [String: Any])
profiles.append(profile) profiles.append(profile)
} }
} }

View File

@ -0,0 +1,57 @@
//
// ServerProfileTests.swift
// ShadowsocksX-NG
//
// Created by Rainux Luo on 07/01/2017.
// Copyright © 2017 qiuyuzhou. All rights reserved.
//
import XCTest
@testable import ShadowsocksX_NG
class ServerProfileTests: XCTestCase {
// "ss://aes-256-cfb:password@example.com:8388"
let profileUrl = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OA")
// "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true"
let profileFullUrl = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OD9SZW1hcms9UHJpc20mT1RBPXRydWU")
var profile: ServerProfile!
override func setUp() {
super.setUp()
profile = ServerProfile.fromDictionary(["ServerHost": "example.com",
"ServerPort": 8388,
"Method": "aes-256-cfb",
"Password": "password",
"Remark": "Prism",
"OTA": true])
XCTAssertNotNil(profile)
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testServerProfileURL() {
let parsed = ParseSSURL(profile.URL())
XCTAssertNotNil(parsed)
XCTAssertEqual(parsed?["ServerHost"] as? String, profile.serverHost)
XCTAssertEqual(parsed?["ServerPort"] as? UInt16, profile.serverPort)
XCTAssertEqual(parsed?["Method"] as? String, profile.method)
XCTAssertEqual(parsed?["Password"] as? String, profile.password)
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}