<commit_before>//
//  SwiftDocsTests.swift
//  SourceKitten
//
//  Created by <NAME> on 2015-01-03.
//  Copyright (c) 2015 SourceKitten. All rights reserved.
//

import Foundation
import SourceKittenFramework
import XCTest

func compareJSONStringWithFixturesName(name: String, jsonString: CustomStringConvertible, rootDirectory: String = fixturesDirectory) {
    func jsonValue(jsonString: String) -> AnyObject {
        let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!
        let result = try! NSJSONSerialization.JSONObjectWithData(data, options: [])
        return (result as? NSDictionary) ?? (result as! NSArray)
    }

    // Strip out fixtures directory since it's dependent on the test machine's setup
    let escapedFixturesDirectory = rootDirectory.stringByReplacingOccurrencesOfString("/", withString: "\\/")
    let jsonString = String(jsonString).stringByReplacingOccurrencesOfString(escapedFixturesDirectory, withString: "")

    // Strip out any other absolute paths after that, since it's also dependent on the test machine's setup
    let absolutePathRegex = try! NSRegularExpression(pattern: "\"key\\.filepath\" : \"\\\\/[^\\\n]+", options: [])
    let actualContent = absolutePathRegex.stringByReplacingMatchesInString(jsonString, options: [], range: NSRange(location: 0, length: jsonString.utf16.count), withTemplate: "\"key\\.filepath\" : \"\",")

    let expectedFile = File(path: fixturesDirectory + name + ".json")!

    let overwrite = false
    if overwrite && actualContent != expectedFile.contents {
        _ = try? actualContent.dataUsingEncoding(NSUTF8StringEncoding)?.writeToFile(expectedFile.path!, options: .AtomicWrite)
        return
    }

    let actualValue = jsonValue(actualContent)
    let expectedValue = jsonValue(expectedFile.contents)
    let message = "output should match expected fixture"
    func AssertEqual<T: Equatable>(firstValue: T, _ secondValue: T) {
        if firstValue != secondValue {
            XCTFail(message)
            print("actual:\n\(actualContent)\nexpected:\n\(expectedFile.contents)")
        }
    }
    if let firstValue = actualValue as? NSDictionary, secondValue = expectedValue as? NSDictionary {
        AssertEqual(firstValue, secondValue)
    } else if let firstValue = actualValue as? NSArray, secondValue = expectedValue as? NSArray {
        AssertEqual(firstValue, secondValue)
    } else {
        XCTFail("output didn't match fixture type")
    }
}

func compareDocsWithFixturesName(name: String) {
    let swiftFilePath = fixturesDirectory + name + ".swift"
    let docs = SwiftDocs(file: File(path: swiftFilePath)!, arguments: ["-j4", swiftFilePath])!
    compareJSONStringWithFixturesName(name, jsonString: docs)
}

class SwiftDocsTests: XCTestCase {

    func testSubscript() {
        compareDocsWithFixturesName("Subscript")
    }

    func testBicycle() {
        compareDocsWithFixturesName("Bicycle")
    }

    func testParseFullXMLDocs() {<commit_msg>use NSObject instead of generics<commit_after>//
//  SwiftDocsTests.swift
//  SourceKitten
//
//  Created by <NAME> on 2015-01-03.
//  Copyright (c) 2015 SourceKitten. All rights reserved.
//

import Foundation
import SourceKittenFramework
import XCTest

func compareJSONStringWithFixturesName(name: String, jsonString: CustomStringConvertible, rootDirectory: String = fixturesDirectory) {
    func jsonValue(jsonString: String) -> AnyObject {
        let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!
        let result = try! NSJSONSerialization.JSONObjectWithData(data, options: [])
        return (result as? NSDictionary) ?? (result as! NSArray)
    }

    // Strip out fixtures directory since it's dependent on the test machine's setup
    let escapedFixturesDirectory = rootDirectory.stringByReplacingOccurrencesOfString("/", withString: "\\/")
    let jsonString = String(jsonString).stringByReplacingOccurrencesOfString(escapedFixturesDirectory, withString: "")

    // Strip out any other absolute paths after that, since it's also dependent on the test machine's setup
    let absolutePathRegex = try! NSRegularExpression(pattern: "\"key\\.filepath\" : \"\\\\/[^\\\n]+", options: [])
    let actualContent = absolutePathRegex.stringByReplacingMatchesInString(jsonString, options: [], range: NSRange(location: 0, length: jsonString.utf16.count), withTemplate: "\"key\\.filepath\" : \"\",")

    let expectedFile = File(path: fixturesDirectory + name + ".json")!

    let overwrite = false
    if overwrite && actualContent != expectedFile.contents {
        _ = try? actualContent.dataUsingEncoding(NSUTF8StringEncoding)?.writeToFile(expectedFile.path!, options: .AtomicWrite)
        return
    }

    let actualValue = jsonValue(actualContent)
    let expectedValue = jsonValue(expectedFile.contents)
    let message = "output should match expected fixture"
    func AssertEqual(firstValue: NSObject, _ secondValue: NSObject) {
        if firstValue != secondValue {
            XCTFail(message)
            print("actual:\n\(actualContent)\nexpected:\n\(expectedFile.contents)")
        }
    }
    if let firstValue = actualValue as? NSDictionary, secondValue = expectedValue as? NSDictionary {
        AssertEqual(firstValue, secondValue)
    } else if let firstValue = actualValue as? NSArray, secondValue = expectedValue as? NSArray {
        AssertEqual(firstValue, secondValue)
    } else {
        XCTFail("output didn't match fixture type")
    }
}

func compareDocsWithFixturesName(name: String) {
    let swiftFilePath = fixturesDirectory + name + ".swift"
    let docs = SwiftDocs(file: File(path: swiftFilePath)!, arguments: ["-j4", swiftFilePath])!
    compareJSONStringWithFixturesName(name, jsonString: docs)
}

class SwiftDocsTests: XCTestCase {

    func testSubscript() {
        compareDocsWithFixturesName("Subscript")
    }

    func testBicycle() {
        compareDocsWithFixturesName("Bicycle")
    }

    func testParseFullXMLDocs() {
{'fraction_non_alphanumeric': 0.10704083347336582, 'fraction_numerical': 0.006049403461603092, 'mean_word_length': 3.951747088186356, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3624190', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1631, 'n_words': 415}