<commit_before>//
//  BartlebyTestCase.swift
//  BartlebyKit
//
//  Created by <NAME> on 29/11/2016.
//
//

import XCTest
import BartlebyKit

/// A base test case with document and file oriented facilities
public class BartlebyTestCase: XCTestCase {

    static var documents=[BartlebyDocument]()

    static var createdURI=[URL]()

    public static func urlByAppending(path:String)->URL{
        return URL(fileURLWithPath: Bartleby.getSearchPath(FileManager.SearchPathDirectory.desktopDirectory)!+"/"+path)
    }

    public static var document:BartlebyDocument{
        return documents.first ?? newDocument()
    }

    public static func newDocument()->BartlebyDocument{
        let document=BartlebyDocument()
        document.configureSchema()
        Bartleby.sharedInstance.declare(document)
        let group=AsyncGroup()
        group.enter()
        let documentURL=urlByAppending(path:"testDocument\(documents.count+1).document")
        var catched:Error?=nil
        document.save(to:documentURL , ofType: "", for: NSSaveOperationType.saveOperation, completionHandler: { error in
            catched=error
            group.leave()
        })
        group.wait()
        if let error = catched{
            assertionFailure("Setup Precondition failed \(error)")
        }else{
            BartlebyTestCase.createdURI.append(documentURL)<commit_msg>Fixes a unit test bug when creating a newDocument (The sugar must be defined before to save a document)<commit_after>//
//  BartlebyTestCase.swift
//  BartlebyKit
//
//  Created by <NAME> on 29/11/2016.
//
//

import XCTest
import BartlebyKit

/// A base test case with document and file oriented facilities
public class BartlebyTestCase: XCTestCase {

    static var documents=[BartlebyDocument]()

    static var createdURI=[URL]()

    public static func urlByAppending(path:String)->URL{
        return URL(fileURLWithPath: Bartleby.getSearchPath(FileManager.SearchPathDirectory.desktopDirectory)!+"/"+path)
    }

    public static var document:BartlebyDocument{
        return documents.first ?? newDocument()
    }

    public static func newDocument()->BartlebyDocument{
        let document=BartlebyDocument()
        document.configureSchema()
        document.metadata.sugar=Bartleby.randomStringWithLength(1024)
        Bartleby.sharedInstance.declare(document)
        let group=AsyncGroup()
        group.enter()
        let documentURL=urlByAppending(path:"testDocument\(documents.count+1).document")
        var catched:Error?=nil
        document.save(to:documentURL , ofType: "", for: NSSaveOperationType.saveOperation, completionHandler: { error in
            catched=error
            group.leave()
        })
        group.wait()
        if let error = catched{
            assertionFailure("Setup Precondition failed \(error)")
        }else{
            BartlebyTestCase.createdURI.append(documentURL)
{'fraction_non_alphanumeric': 0.09965397923875433, 'fraction_numerical': 0.007612456747404845, 'mean_word_length': 3.112375533428165, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5932923', 'n_tokens_mistral': 831, 'n_tokens_neox': 797, 'n_words': 180}