123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- //
- // AutoTest.swift
- // KdanAuto
- //
- // Created by 朱东勇 on 2022/11/25.
- //
- import Foundation
- import AppKit
- class AutoTest : NSObject, AutoTestProtocal {
- var reportString : NSMutableAttributedString? = nil
-
- public var _status : AutoTestStatus = .Normal
-
- var _fileType : String = "RTF"
-
- class func autoTestFor(_ fileType:NSString ,type:NSString) -> AutoTest? {
- let fileTypes = testTypeInfo[fileType] as! NSArray
-
- let clsname = "KdanAuto"//Bundle.main.infoDictionary! ["CFBundleExecutable"]
-
- for item in fileTypes {
- let cItem = item as! NSDictionary
-
- let cType = cItem["Type"] as! NSString
- if (cType.isEqual(to: type)) {
- let className = String((clsname )+"."+(cItem["Class"] as! String)) as! String
- let cl = NSClassFromString(className) as! AutoTest.Type
-
- let object = cl.shared()
- object?._fileType = fileType as! String
-
- return object
- }
- }
-
- let object = AutoTest.shared()
-
- object?._fileType = fileType as String
-
- return object
- }
-
- static var sharedInstance = AutoTest()
- class func shared() -> AutoTest? {
- return sharedInstance
- }
-
- func fileType() -> String {
- return _fileType
- }
-
- func type() -> String {
- return "Others"
- }
-
- func name() -> String {
- return "未指定类型对照测试"
- }
-
- func keys() -> NSArray {
- return ["快照对比"]
- }
-
- func selectedKeys() -> NSArray {
- let userDefaults = UserDefaults.standard
-
- let key = self.fileType() + "." + self.type() + ".selectedKeys"
-
- if userDefaults.value(forKey: key) != nil {
- return userDefaults.value(forKey: key) as! NSArray
- }
-
- self.setSelectedKeys(self.keys())
-
- return self.keys()
- }
-
- func setSelectedKeys(_ keys: NSArray) {
- let userDefaults = UserDefaults.standard
-
- let key = self.fileType() + "." + self.type() + ".selectedKeys"
-
- userDefaults.setValue(keys, forKey: key)
- userDefaults.synchronize()
- }
-
- func status() -> AutoTestStatus {
- return _status
- }
-
- func setStatus(_ status:AutoTestStatus) {
- _status = status
- }
-
- // Auto Test
- func autoTest() {
-
- }
-
- func autoCheck() {
-
- }
-
-
- func testReport() -> NSAttributedString? {
- return reportString
- }
-
- /// Path
- func originFilePath() -> String {
- return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kOriginPathComponent)")
- }
-
- func resultFilePath() -> String {
- return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kResultPathComponent)")
- }
-
- func checkFilePath() -> String {
- return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kCheckPathComponent)")
- }
-
- // check File Exist
- func isOriginFileExist() -> Bool {
- if nil != self.originFilePath() {
- return FileManager.default.fileExists(atPath: self.originFilePath())
- }
-
- return false
- }
-
- func isResultFileExist() -> Bool {
- if nil != self.resultFilePath() {
- return FileManager.default.fileExists(atPath: self.resultFilePath())
- }
-
- return false
- }
-
- func isCheckFileExist() -> Bool {
- if nil != self.checkFilePath() {
- return FileManager.default.fileExists(atPath: self.checkFilePath())
- }
-
- return false
- }
-
- }
- extension AutoTest {
- func stringToImage(_ string:String) ->NSImage? {
- let length = Int(string.lengthOfBytes(using: .utf8)/2)
-
- var bytes = malloc(length)!
-
- for i in 0..<length {
- let index = i
- let subString = String(NSString(string: string).substring(with: NSMakeRange(Int(index * 2), 2)))
- bytes.storeBytes(of: UInt8(hexForString(subString)), as: UInt8.self)
- }
-
- var data = Data.init(bytes: bytes, count: length)
- let image = NSImage.init(data: data)
-
- return image
- }
-
- func hexForString(_ string:String) -> UInt8 {
- let chars = string.utf8CString
-
- if (string.lengthOfBytes(using: .utf8) >= 2) {
- return UInt8(intvalueForChar(chars[0]) * 16 + intvalueForChar(chars[1]))
- }
-
- return UInt8(0)
- }
-
- func intvalueForChar(_ char:CChar) -> UInt8 {
- let iValue = char
-
- // 0 ~ 9s
- if iValue >= 48 && iValue <= 57 {
- return UInt8(iValue - 48)
- }else if iValue >= 65 && iValue <= 70 {
- return UInt8(iValue - 65 + 10)
- }else if iValue >= 97 && iValue <= 102 {
- return UInt8(iValue - 97 + 10)
- }
-
- return UInt8(0)
- }
- }
|