AutoTest.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // AutoTest.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/25.
  6. //
  7. import Foundation
  8. import AppKit
  9. class AutoTest : NSObject, AutoTestProtocal {
  10. var reportString : NSMutableAttributedString? = nil
  11. public var _status : AutoTestStatus = .Normal
  12. var _fileType : String = "RTF"
  13. var _type : String = "Others"
  14. class func autoTestFor(_ fileType:NSString ,type:NSString) -> AutoTest? {
  15. let fileTypes = testTypeInfo[fileType] as! NSArray
  16. let clsname = "KdanAuto"//Bundle.main.infoDictionary! ["CFBundleExecutable"]
  17. for item in fileTypes {
  18. let cItem = item as! NSDictionary
  19. let cType = cItem["Type"] as! NSString
  20. if (cType.isEqual(to: type)) {
  21. let className = String((clsname )+"."+(cItem["Class"] as! String)) as! String
  22. let cl = NSClassFromString(className) as! AutoTest.Type
  23. let object = cl.shared()
  24. object?._fileType = fileType as! String
  25. object?._type = String(cType)
  26. return object
  27. }
  28. }
  29. let object = AutoTest.shared()
  30. object?._fileType = fileType as String
  31. object?._type = String(cType)
  32. return object
  33. }
  34. static var sharedInstance = AutoTest()
  35. class func shared() -> AutoTest? {
  36. return sharedInstance
  37. }
  38. func fileType() -> String {
  39. return _fileType
  40. }
  41. func type() -> String {
  42. return _type
  43. }
  44. func name() -> String {
  45. return "未指定类型对照测试"
  46. }
  47. func keys() -> NSArray {
  48. return ["快照对比"]
  49. }
  50. func selectedKeys() -> NSArray {
  51. let userDefaults = UserDefaults.standard
  52. let key = self.fileType() + "." + self.type() + ".selectedKeys"
  53. if userDefaults.value(forKey: key) != nil {
  54. return userDefaults.value(forKey: key) as! NSArray
  55. }
  56. self.setSelectedKeys(self.keys())
  57. return self.keys()
  58. }
  59. func setSelectedKeys(_ keys: NSArray) {
  60. let userDefaults = UserDefaults.standard
  61. let key = self.fileType() + "." + self.type() + ".selectedKeys"
  62. userDefaults.setValue(keys, forKey: key)
  63. userDefaults.synchronize()
  64. }
  65. func status() -> AutoTestStatus {
  66. return _status
  67. }
  68. func setStatus(_ status:AutoTestStatus) {
  69. _status = status
  70. }
  71. // Auto Test
  72. func autoTest() {
  73. }
  74. func testReport() -> NSAttributedString? {
  75. return reportString
  76. }
  77. /// Path
  78. func originFileDirectory() -> String {
  79. return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kOriginPathComponent)")
  80. }
  81. func resultFileDirectory() -> String {
  82. return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kResultPathComponent)")
  83. }
  84. func checkFileDirectory() -> String {
  85. return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kCheckPathComponent)")
  86. }
  87. // check File Exist
  88. func hasOriginFileExist() -> Bool {
  89. if nil != self.originFileDirectory() {
  90. return (FileManager.default.fileExists(atPath: self.originFileDirectory()) &&
  91. FileManager.default.subpaths(atPath: self.originFileDirectory())!.count > 0)
  92. }
  93. return false
  94. }
  95. func hasResultFileExist() -> Bool {
  96. if nil != self.resultFileDirectory() {
  97. return (FileManager.default.fileExists(atPath: self.resultFileDirectory()) &&
  98. FileManager.default.subpaths(atPath: self.resultFileDirectory())!.count > 0)
  99. }
  100. return false
  101. }
  102. }
  103. extension AutoTest {
  104. func stringToImage(_ string:String) ->NSImage? {
  105. let length = Int(string.lengthOfBytes(using: .utf8)/2)
  106. var bytes = malloc(length)!
  107. for i in 0..<length {
  108. let index = i
  109. let subString = String(NSString(string: string).substring(with: NSMakeRange(Int(index * 2), 2)))
  110. bytes.storeBytes(of: UInt8(hexForString(subString)), as: UInt8.self)
  111. }
  112. var data = Data.init(bytes: bytes, count: length)
  113. let image = NSImage.init(data: data)
  114. return image
  115. }
  116. func hexForString(_ string:String) -> UInt8 {
  117. let chars = string.utf8CString
  118. if (string.lengthOfBytes(using: .utf8) >= 2) {
  119. return UInt8(intvalueForChar(chars[0]) * 16 + intvalueForChar(chars[1]))
  120. }
  121. return UInt8(0)
  122. }
  123. func intvalueForChar(_ char:CChar) -> UInt8 {
  124. let iValue = char
  125. // 0 ~ 9s
  126. if iValue >= 48 && iValue <= 57 {
  127. return UInt8(iValue - 48)
  128. }else if iValue >= 65 && iValue <= 70 {
  129. return UInt8(iValue - 65 + 10)
  130. }else if iValue >= 97 && iValue <= 102 {
  131. return UInt8(iValue - 97 + 10)
  132. }
  133. return UInt8(0)
  134. }
  135. }