<commit_before>//
//  SwiftSVGTests.swift
//  SwiftSVGTests
//
//  Created by <NAME> on 02/10/2015.
//  Copyright © 2015 No. All rights reserved.
//

import XCTest
@testable import SwiftSVG

public enum TestError: ErrorType {
    case invalidFilePath
    case noContentInFile(String)
    case invalidXML
    case invalidSVG
    case invalidJSONObject
}

func makeURLFromNamedFile(namedFile: String, fileExtension: String) throws -> NSURL {
    let testBundle = NSBundle(forClass: SwiftSVGTests.self)
    guard let url = testBundle.URLForResource(namedFile, withExtension:fileExtension) else {
        throw TestError.invalidFilePath
    }
    
    return url
}

func svgSourceFromNamedFile(namedFile: String) throws -> String {
    let textDrawingURL = try makeURLFromNamedFile(namedFile, fileExtension: "svg")
    var encoding = NSStringEncoding()
    guard let source = try? String(contentsOfURL: textDrawingURL, usedEncoding: &encoding) else {
        throw TestError.noContentInFile(textDrawingURL.path!)
    }
    return source
}

func xmlDocumentFromNamedSVGFile(namedFile: String) throws -> NSXMLDocument {
    let source = try svgSourceFromNamedFile(namedFile)
    guard let xmlDocument = try? NSXMLDocument(XMLString: source, options: 0) else {
        throw TestError.invalidXML
    }
    return xmlDocument
}

class SwiftSVGTests: XCTestCase {
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    func testPaperPlane() {
        var optionalSVGDocument: SVGDocument?
        do {
            let xmlDocument = try xmlDocumentFromNamedSVGFile("paperplane")
            let processor = SVGProcessor()
            optionalSVGDocument = try processor.processXMLDocument(xmlDocument)
        }
        catch let error {
            XCTAssert(false, "Failed to create SVGDocument: \(error)")
            return
        }

        guard let svgDocument = optionalSVGDocument else {
            XCTAssert(false, "optionalSVGDocument should not be .None")
            return
        }
        
        XCTAssert(svgDocument.children.count == 3, "Number of children of paper plane document should be 3")
        // print("viewBox: \(svgDocument.viewBox)")
        XCTAssert(svgDocument.viewBox! == CGRect(x: 0, y: 0, width: 500, height: 260), "viewBox should be 300x200 at 0,0")
        XCTAssert(svgDocument.version!.majorVersion == 1, "SVG majorVersion should be 1")
        XCTAssert(svgDocument.version!.minorVersion == 1, "SVG minorVersion should be 1")
        XCTAssert(svgDocument.drawFill == true, "Draw fill should be true")
        XCTAssert(svgDocument.display == true, "Display should be true")
        print("Child0: \(svgDocument.children[0])")
        // XCTAssert(svgDocument.children[0] is SVGSimpleText, "Only document child element should be a SVGSimpleText")
        
    }
    
    func testSimpleText() {
        var optionalSVGDocument: SVGDocument?
        do {
            let xmlDocument = try xmlDocumentFromNamedSVGFile("TextDrawing")
            let processor = SVGProcessor()
            optionalSVGDocument = try processor.processXMLDocument(xmlDocument)
        }
        catch let error {
            XCTAssert(false, "Failed to create SVGDocument: \(error)")
            return
        }

        guard let svgDocument = optionalSVGDocument else {
            XCTAssert(false, "optionalSVGDocument should not be .None")
            return
        }
        <commit_msg>Made paper plane gradient tests more thorough<commit_after>//
//  SwiftSVGTests.swift
//  SwiftSVGTests
//
//  Created by <NAME> on 02/10/2015.
//  Copyright © 2015 No. All rights reserved.
//

import XCTest
@testable import SwiftSVG

public enum TestError: ErrorType {
    case invalidFilePath
    case noContentInFile(String)
    case invalidXML
    case invalidSVG
    case invalidJSONObject
}

func makeURLFromNamedFile(namedFile: String, fileExtension: String) throws -> NSURL {
    let testBundle = NSBundle(forClass: SwiftSVGTests.self)
    guard let url = testBundle.URLForResource(namedFile, withExtension:fileExtension) else {
        throw TestError.invalidFilePath
    }
    
    return url
}

func svgSourceFromNamedFile(namedFile: String) throws -> String {
    let textDrawingURL = try makeURLFromNamedFile(namedFile, fileExtension: "svg")
    var encoding = NSStringEncoding()
    guard let source = try? String(contentsOfURL: textDrawingURL, usedEncoding: &encoding) else {
        throw TestError.noContentInFile(textDrawingURL.path!)
    }
    return source
}

func xmlDocumentFromNamedSVGFile(namedFile: String) throws -> NSXMLDocument {
    let source = try svgSourceFromNamedFile(namedFile)
    guard let xmlDocument = try? NSXMLDocument(XMLString: source, options: 0) else {
        throw TestError.invalidXML
    }
    return xmlDocument
}

class SwiftSVGTests: XCTestCase {
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    func testPaperPlane() {
        var optionalSVGDocument: SVGDocument?
        do {
            let xmlDocument = try xmlDocumentFromNamedSVGFile("paperplane")
            let processor = SVGProcessor()
            optionalSVGDocument = try processor.processXMLDocument(xmlDocument)
        }
        catch let error {
            XCTAssert(false, "Failed to create SVGDocument: \(error)")
            return
        }

        guard let svgDocument = optionalSVGDocument else {
            XCTAssert(false, "optionalSVGDocument should not be .None")
            return
        }
        
        XCTAssert(svgDocument.children.count == 3, "Number of children of paper plane document should be 3")
        // print("viewBox: \(svgDocument.viewBox)")
        XCTAssert(svgDocument.viewBox! == CGRect(x: 0, y: 0, width: 500, height: 260), "viewBox should be 300x200 at 0,0")
        XCTAssert(svgDocument.version!.majorVersion == 1, "SVG majorVersion should be 1")
        XCTAssert(svgDocument.version!.minorVersion == 1, "SVG minorVersion should be 1")
        XCTAssert(svgDocument.drawFill == true, "Draw fill should be true")
        XCTAssert(svgDocument.display == true, "Display should be true")
        XCTAssert(svgDocument.children[0] is SVGGroup, "Child 0 of svgDocument should be a group")
        let group0 = svgDocument.children[0] as! SVGGroup
        XCTAssert(group0.children[0] is SVGGroup, "Child 0 of Group 0 should be a group")
        let group00 = group0.children[0] as! SVGGroup
        XCTAssert(group00.children[0] is SVGPolygon, "Child 0 of Group 0 should be a polygon")
        let polygon = group00.children[0]
        guard let svgGradient = polygon.gradientFill else {
            XCTAssert(false, "No gradient fill as part of polygon")
            return
        }
        let coalescedGradient = svgGradient.coalesceLinearGradientInheritance()
        print("CoalescedGradient: \(coalescedGradient)")
        print("start point: \(coalescedGradient.startPoint)")
        print("end point: \(coalescedGradient.endPoint)")
        guard let stops = coalescedGradient.stops else {
            XCTAssert(false, "Stops array should exist")
            return
        }
        XCTAssert(stops.count == 2, "Number of gradient stops should be 2")
        XCTAssert(stops[0].offset == 0.0, "Start offset should be 0.0")
        XCTAssert(stops[0].opacity == 1.0, "Start opacity should be 1.0")
        // print("Child0 of group00: \(group00.children[0])")
        // XCTAssert(svgDocument.children[0] is SVGSimpleText, "Only document child element should be a SVGSimpleText")
        
    }
    
    func testSimpleText() {
        var optionalSVGDocument: SVGDocument?
        do {
            let xmlDocument = try xmlDocumentFromNamedSVGFile("TextDrawing")
            let processor = SVGProcessor()
            optionalSVGDocument = try processor.processXMLDocument(xmlDocument)
        }
        catch let error {
            XCTAssert(false, "Failed to create SVGDocument: \(error)")
            return
        }

        guard let svgDocument = optionalSVGDocument else {
            XCTAssert(false, "optionalSVGDocument should not be .None")
            return
        }
        
{'fraction_non_alphanumeric': 0.08432208270936528, 'fraction_numerical': 0.012901739430941136, 'mean_word_length': 2.7213887698242605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1189820', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2382, 'n_words': 707}