1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-08-15 08:00:31 +03:00

Merge pull request #702 from f3dm76/fix/test-references

Fix test references
This commit is contained in:
Yuri Strot 2020-06-16 16:40:56 +07:00 committed by GitHub
commit e7fd368b28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
139 changed files with 222 additions and 61 deletions

View File

@ -5,6 +5,22 @@
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "57FCD26B1D76EA4600CC0FB6"
BuildableName = "Macaw.framework"
BlueprintName = "Macaw"
ReferencedContainer = "container:Macaw.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
@ -23,8 +39,6 @@
</BuildableReference>
</TestableReference>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
@ -36,8 +50,6 @@
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
@ -45,6 +57,15 @@
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "57FCD26B1D76EA4600CC0FB6"
BuildableName = "Macaw.framework"
BlueprintName = "Macaw"
ReferencedContainer = "container:Macaw.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">

View File

@ -20,13 +20,13 @@ class MacawSVGTests: XCTestCase {
private let testFolderName = "MacawTestOutputData"
private let shouldComparePNGImages = true
private let multipleTestsWillRun = false
private let shouldSaveFaildedTestImage = false
private let shouldSaveFailedTestImage = false
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
super.setUp()
if shouldSaveFaildedTestImage {
if shouldSaveFailedTestImage {
setupTestFolderDirectory()
}
}
@ -36,6 +36,21 @@ class MacawSVGTests: XCTestCase {
super.tearDown()
}
func compareResults(nodeContent: String?, referenceContent: String?) {
guard let nodeContent = nodeContent else {
XCTFail("nodeContent is empty")
return
}
guard let referenceContent = referenceContent else {
XCTFail("referenceContent is empty")
return
}
if nodeContent != referenceContent {
XCTFail("nodeContent is not equal to referenceContent" + TestUtils.prettyFirstDifferenceBetweenStrings(s1: nodeContent, s2: referenceContent))
}
}
func validate(node: Node, referenceFile: String) {
let bundle = Bundle(for: type(of: TestUtils()))
@ -43,7 +58,7 @@ class MacawSVGTests: XCTestCase {
if let path = bundle.path(forResource: referenceFile, ofType: "reference") {
let clipReferenceContent = try String.init(contentsOfFile: path).trimmingCharacters(in: .newlines)
let result = SVGSerializer.serialize(node: node)
XCTAssertEqual(result, clipReferenceContent)
compareResults(nodeContent: result, referenceContent: clipReferenceContent)
} else {
XCTFail("No file \(referenceFile)")
}
@ -192,13 +207,10 @@ class MacawSVGTests: XCTestCase {
let bundle = Bundle(for: type(of: TestUtils()))
do {
if let path = bundle.path(forResource: referenceFile, ofType: "reference") {
let referenceContent = try String(contentsOfFile: path)
let nodeContent = String(data: getJSONData(node: node), encoding: String.Encoding.utf8)
if nodeContent != referenceContent {
XCTFail("nodeContent is not equal to referenceContent")
}
compareResults(nodeContent: nodeContent, referenceContent: referenceContent)
let nativeImage = getImage(from: referenceFile)
@ -261,9 +273,9 @@ class MacawSVGTests: XCTestCase {
if referenceContentData != nodeContentData {
var failInfo = "referenceContentData is not equal to nodeContentData"
var failInfo = "referenceImageData is not equal to nodeImageData"
if shouldSaveFaildedTestImage {
if shouldSaveFailedTestImage {
let _ = saveImage(image: referenceImage, fileName: referenceFile + "_reference")
let _ = saveImage(image: nodeImage, fileName: referenceFile + "_incorrect")
@ -314,7 +326,7 @@ class MacawSVGTests: XCTestCase {
}
func writeToFile(string: String, fileName: String) -> URL? {
guard let directory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) as NSURL else {
guard let directory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) as NSURL else {
return .none
}
do {

View File

@ -25,4 +25,133 @@ class TestUtils {
return result
}
class func prettyFirstDifferenceBetweenStrings(s1: String, s2: String) -> String {
return prettyFirstDifferenceBetweenNSStrings(s1: s1 as NSString, s2: s2 as NSString) as String
}
}
/// Find first differing character between two strings
///
/// :param: s1 First String
/// :param: s2 Second String
///
/// :returns: .DifferenceAtIndex(i) or .NoDifference
fileprivate func firstDifferenceBetweenStrings(s1: NSString, s2: NSString) -> FirstDifferenceResult {
let len1 = s1.length
let len2 = s2.length
let lenMin = min(len1, len2)
for i in 0..<lenMin {
if s1.character(at: i) != s2.character(at: i) {
return .DifferenceAtIndex(i)
}
}
if len1 < len2 {
return .DifferenceAtIndex(len1)
}
if len2 < len1 {
return .DifferenceAtIndex(len2)
}
return .NoDifference
}
/// Create a formatted String representation of difference between strings
///
/// :param: s1 First string
/// :param: s2 Second string
///
/// :returns: a string, possibly containing significant whitespace and newlines
fileprivate func prettyFirstDifferenceBetweenNSStrings(s1: NSString, s2: NSString) -> NSString {
let firstDifferenceResult = firstDifferenceBetweenStrings(s1: s1, s2: s2)
return prettyDescriptionOfFirstDifferenceResult(firstDifferenceResult: firstDifferenceResult, s1: s1, s2: s2)
}
/// Create a formatted String representation of a FirstDifferenceResult for two strings
///
/// :param: firstDifferenceResult FirstDifferenceResult
/// :param: s1 First string used in generation of firstDifferenceResult
/// :param: s2 Second string used in generation of firstDifferenceResult
///
/// :returns: a printable string, possibly containing significant whitespace and newlines
fileprivate func prettyDescriptionOfFirstDifferenceResult(firstDifferenceResult: FirstDifferenceResult, s1: NSString, s2: NSString) -> NSString {
func diffString(index: Int, s1: NSString, s2: NSString) -> NSString {
let markerArrow = "\u{2b06}" // ""
let ellipsis = "\u{2026}" // ""
/// Given a string and a range, return a string representing that substring.
///
/// If the range starts at a position other than 0, an ellipsis
/// will be included at the beginning.
///
/// If the range ends before the actual end of the string,
/// an ellipsis is added at the end.
func windowSubstring(s: NSString, range: NSRange) -> String {
let validRange = NSMakeRange(range.location, min(range.length, s.length - range.location))
let substring = s.substring(with: validRange)
let prefix = range.location > 0 ? ellipsis : ""
let suffix = (s.length - range.location > range.length) ? ellipsis : ""
return "\(prefix)\(substring)\(suffix)"
}
// Show this many characters before and after the first difference
let windowPrefixLength = 10
let windowSuffixLength = 10
let windowLength = windowPrefixLength + 1 + windowSuffixLength
let windowIndex = max(index - windowPrefixLength, 0)
let windowRange = NSMakeRange(windowIndex, windowLength)
let sub1 = windowSubstring(s: s1, range: windowRange)
let sub2 = windowSubstring(s: s2, range: windowRange)
let markerPosition = min(windowSuffixLength, index) + (windowIndex > 0 ? 1 : 0)
let markerPrefix = String(repeating: " ", count: markerPosition)
let markerLine = "\(markerPrefix)\(markerArrow)"
return "Difference at index \(index):\n\(sub1)\n\(sub2)\n\(markerLine)" as NSString
}
switch firstDifferenceResult {
case .NoDifference: return "No difference"
case .DifferenceAtIndex(let index): return diffString(index: index, s1: s1, s2: s2)
}
}
/// Result type for firstDifferenceBetweenStrings()
public enum FirstDifferenceResult {
/// Strings are identical
case NoDifference
/// Strings differ at the specified index.
///
/// This could mean that characters at the specified index are different,
/// or that one string is longer than the other
case DifferenceAtIndex(Int)
}
extension FirstDifferenceResult: CustomStringConvertible {
/// Textual representation of a FirstDifferenceResult
public var description: String {
switch self {
case .NoDifference:
return "NoDifference"
case .DifferenceAtIndex(let index):
return "DifferenceAtIndex(\(index))"
}
}
/// Textual representation of a FirstDifferenceResult for debugging purposes
public var debugDescription: String {
return self.description
}
}

Some files were not shown because too many files have changed in this diff Show More