AutoTest.swift 5.1 KB

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