<commit_before>//
//  DiskTests.swift
//  DiskTests
//
//  Created by <NAME> on 7/22/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import XCTest
import Disk

class DiskTests: XCTestCase {
    
    // MARK: Helpers
    
    // Convert Error -> String of descriptions
    func convertErrorToString(_ error: Error) -> String {
        return """
        Domain: \((error as NSError).domain)
        Code: \((error as NSError).code)
        Description: \(error.localizedDescription)
        Failure Reason: \((error as NSError).localizedFailureReason ?? "nil")
        Suggestions: \((error as NSError).localizedRecoverySuggestion ?? "nil")\n
        """
    }
    
    // We'll clear out all our directories after each test
    override func tearDown() {
        do {
            try Disk.clear(.documents)
            try Disk.clear(.caches)
            try Disk.clear(.temporary)
        } catch {
            fatalError(convertErrorToString(error))
        }
    }
    
    // MARK: Dummmy data
    
    let messages: [Message] = {
        var array = [Message]()
        for i in 1...10 {
            let element = Message(title: "Message \(i)", body: "...")
            array.append(element)
        }
        return array
    }()
    
    let images = [
        UIImage(named: "Deku", in: Bundle(for: DiskTests.self), compatibleWith: nil)!,
        UIImage(named: "AllMight", in: Bundle(for: DiskTests.self), compatibleWith: nil)!,
        UIImage(named: "Bakugo", in: Bundle(for: DiskTests.self), compatibleWith: nil)!
    ]
    
    lazy var data: [Data] = self.images.map { UIImagePNGRepresentation($0)! }
    
    // MARK: Tests
    
    func testSaveStructs() {
        do {
            // 1 struct
            try Disk.save(messages[0], to: .documents, as: "message.json")
            XCTAssert(Disk.exists("message.json", in: .documents))
            let messageUrl = try Disk.getURL(for: "message.json", in: .documents)
            print("A message was saved as \(messageUrl.absoluteString)")
            let retrievedMessage = try Disk.retrieve("message.json", from: .documents, as: Message.self)
            XCTAssert(messages[0] == retrievedMessage)
            
            // ... in folder hierarchy
            try Disk.save(messages[0], to: .documents, as: "Messages/Bob/message.json")
            XCTAssert(Disk.exists("Messages/Bob/message.json", in: .documents))
            let messageInFolderUrl = try Disk.getURL(for: "Messages/Bob/message.json", in: .documents)
            print("A message was saved as \(messageInFolderUrl.absoluteString)")
            let retrievedMessageInFolder = try Disk.retrieve("Messages/Bob/message.json", from: .documents, as: Message.self)
            XCTAssert(messages[0] == retrievedMessageInFolder)
            
            // Array of structs
            try Disk.save(messages, to: .documents, as: "messages.json")
            XCTAssert(Disk.exists("messages.json", in: .documents))
            let messagesUrl = try Disk.getURL(for: "messages.json", in: .documents)
            print("Messages were saved as \(messagesUrl.absoluteString)")
            let retrievedMessages = try Disk.retrieve("messages.json", from: .documents, as: [Message].self)
            XCTAssert(messages == retrievedMessages)
        } catch {
            fatalError(convertErrorToString(error))
        }
    }
    
    func testAppendStructs() {
        do {
            // Append a single struct to an empty location
            try Disk.append(messages[0], to: "single-message.json", in: .documents)
            let retrievedSingleMessage = try Disk.retrieve("single-message.json", from: .documents, as: Message.self)
            XCTAssert(Disk.exists("single-message.json", in: .documents))
            XCTAssert(retrievedSingleMessage == messages[0])
            
            // Append an array of structs to an empty location
            try Disk.append(messages, to: "multiple-messages.json", in: .documents)
            let retrievedMultipleMessages = try Disk.retrieve("multiple-messages.json", from: .documents, as: [Message].self)
            XCTAssert(Disk.exists("multiple-messages.json", in: .documents))
            XCTAssert(retrievedMultipleMessages == messages)
            
            // Append a single struct to a single struct
            try Disk.save(messages[0], to: .documents, as: "messages.json")
            XCTAssert(Disk.exists("messages.json", in: .documents))
            try Disk.append(messages[1], to: "messages.json", in: .documents)
            let retrievedMessages = try Disk.retrieve("messages.json", from: .documents, as: [Message].self)
            XCTAssert(retrievedMessages[0] == messages[0] && retrievedMessages[1] == messages[1])
            
            // Append an array of structs to a single struct
            try Disk.save(messages[5], to: .caches, as: "one-message.json")
            try Disk.append(messages, to: "one-message.json", in: .caches)
            let retrievedOneMessage = try Disk.retrieve("one-message.json", from: .caches, as: [Message].self)
            XCTAssert(retrievedOneMessage.count == messages.count + 1)
            XCTAssert(retrievedOneMessage[0] == messages[5])
            XCTAssert(retrievedOneMessage.last! == messages.last!)
            <commit_msg>Fixed test for appending struct to empty file<commit_after>//
//  DiskTests.swift
//  DiskTests
//
//  Created by <NAME> on 7/22/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import XCTest
import Disk

class DiskTests: XCTestCase {
    
    // MARK: Helpers
    
    // Convert Error -> String of descriptions
    func convertErrorToString(_ error: Error) -> String {
        return """
        Domain: \((error as NSError).domain)
        Code: \((error as NSError).code)
        Description: \(error.localizedDescription)
        Failure Reason: \((error as NSError).localizedFailureReason ?? "nil")
        Suggestions: \((error as NSError).localizedRecoverySuggestion ?? "nil")\n
        """
    }
    
    // We'll clear out all our directories after each test
    override func tearDown() {
        do {
            try Disk.clear(.documents)
            try Disk.clear(.caches)
            try Disk.clear(.temporary)
        } catch {
            fatalError(convertErrorToString(error))
        }
    }
    
    // MARK: Dummmy data
    
    let messages: [Message] = {
        var array = [Message]()
        for i in 1...10 {
            let element = Message(title: "Message \(i)", body: "...")
            array.append(element)
        }
        return array
    }()
    
    let images = [
        UIImage(named: "Deku", in: Bundle(for: DiskTests.self), compatibleWith: nil)!,
        UIImage(named: "AllMight", in: Bundle(for: DiskTests.self), compatibleWith: nil)!,
        UIImage(named: "Bakugo", in: Bundle(for: DiskTests.self), compatibleWith: nil)!
    ]
    
    lazy var data: [Data] = self.images.map { UIImagePNGRepresentation($0)! }
    
    // MARK: Tests
    
    func testSaveStructs() {
        do {
            // 1 struct
            try Disk.save(messages[0], to: .documents, as: "message.json")
            XCTAssert(Disk.exists("message.json", in: .documents))
            let messageUrl = try Disk.getURL(for: "message.json", in: .documents)
            print("A message was saved as \(messageUrl.absoluteString)")
            let retrievedMessage = try Disk.retrieve("message.json", from: .documents, as: Message.self)
            XCTAssert(messages[0] == retrievedMessage)
            
            // ... in folder hierarchy
            try Disk.save(messages[0], to: .documents, as: "Messages/Bob/message.json")
            XCTAssert(Disk.exists("Messages/Bob/message.json", in: .documents))
            let messageInFolderUrl = try Disk.getURL(for: "Messages/Bob/message.json", in: .documents)
            print("A message was saved as \(messageInFolderUrl.absoluteString)")
            let retrievedMessageInFolder = try Disk.retrieve("Messages/Bob/message.json", from: .documents, as: Message.self)
            XCTAssert(messages[0] == retrievedMessageInFolder)
            
            // Array of structs
            try Disk.save(messages, to: .documents, as: "messages.json")
            XCTAssert(Disk.exists("messages.json", in: .documents))
            let messagesUrl = try Disk.getURL(for: "messages.json", in: .documents)
            print("Messages were saved as \(messagesUrl.absoluteString)")
            let retrievedMessages = try Disk.retrieve("messages.json", from: .documents, as: [Message].self)
            XCTAssert(messages == retrievedMessages)
        } catch {
            fatalError(convertErrorToString(error))
        }
    }
    
    func testAppendStructs() {
        do {
            // Append a single struct to an empty location
            try Disk.append(messages[0], to: "single-message.json", in: .documents)
            let retrievedSingleMessage = try Disk.retrieve("single-message.json", from: .documents, as: [Message].self)
            XCTAssert(Disk.exists("single-message.json", in: .documents))
            XCTAssert(retrievedSingleMessage[0] == messages[0])
            
            // Append an array of structs to an empty location
            try Disk.append(messages, to: "multiple-messages.json", in: .documents)
            let retrievedMultipleMessages = try Disk.retrieve("multiple-messages.json", from: .documents, as: [Message].self)
            XCTAssert(Disk.exists("multiple-messages.json", in: .documents))
            XCTAssert(retrievedMultipleMessages == messages)
            
            // Append a single struct to a single struct
            try Disk.save(messages[0], to: .documents, as: "messages.json")
            XCTAssert(Disk.exists("messages.json", in: .documents))
            try Disk.append(messages[1], to: "messages.json", in: .documents)
            let retrievedMessages = try Disk.retrieve("messages.json", from: .documents, as: [Message].self)
            XCTAssert(retrievedMessages[0] == messages[0] && retrievedMessages[1] == messages[1])
            
            // Append an array of structs to a single struct
            try Disk.save(messages[5], to: .caches, as: "one-message.json")
            try Disk.append(messages, to: "one-message.json", in: .caches)
            let retrievedOneMessage = try Disk.retrieve("one-message.json", from: .caches, as: [Message].self)
            XCTAssert(retrievedOneMessage.count == messages.count + 1)
            XCTAssert(retrievedOneMessage[0] == messages[5])
            XCTAssert(retrievedOneMessage.last! == messages.last!)
            
{'fraction_non_alphanumeric': 0.12642487046632125, 'fraction_numerical': 0.00574658502119642, 'mean_word_length': 2.280593325092707, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6234356', 'n_tokens_mistral': 3135, 'n_tokens_neox': 2928, 'n_words': 741}