<commit_before>//
//  ExampleTests.swift
//  ExampleTests
//
//  Created by <NAME> on 12/07/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import UIKit
import XCTest

import Example

class ExampleTests: XCTestCase {
	
	var cache: AwesomeCache<NSString> = AwesomeCache<NSString>(name: "awesomeCache")
	
    override func setUp() {
		cache = AwesomeCache<NSString>(name: "awesomeCache")
		cache.removeAllObjects()
		
		super.setUp()
    }
    
    override func tearDown() {
		cache.removeAllObjects()
		
		super.tearDown()
    }
	
	func testGetterAndSetter() {
		let nilObject = cache.objectForKey("unavailable")
		XCTAssertNil(nilObject, "Get nil object")
		
		cache.setObject("AddedString", forKey: "add")
		XCTAssertNotNil(cache.objectForKey("add"), "Get non-nil object")
		XCTAssertEqual("AddedString", cache.objectForKey("add")!, "Get non-nil object")
	}
	
	func testRemoveObject() {
		cache.setObject("AddedString", forKey: "remove")
		XCTAssertNotNil(cache.objectForKey("remove"), "Get non-nil object")
		XCTAssertEqual("AddedString", cache.objectForKey("remove")!, "Get non-nil object")
		
		cache.removeObjectForKey("remove")
		XCTAssertNil(cache.objectForKey("remove"), "Get deleted object")
	}
	
	func testSubscripting() {
		cache["addSubscript"] = "AddedString"
		XCTAssertNotNil(cache["addSubscript"], "Get non-nil object via subscript")
		XCTAssertEqual("AddedString", cache["addSubscript"]!, "Get non-nil object via subscript")
		
		cache["addSubscript"] = nil
		XCTAssertNil(cache["addSubscript"], "Get deleted object via subscript")
	}
	
	func testObjectExpiry() {
		cache.setObject("NeverExpires", forKey: "never", expires: .Never)
		cache.setObject("ExpiresIn2Seconds", forKey: "2Seconds", expires: .Seconds(2))
		cache.setObject("ExpiresAtDate", forKey: "atDate", expires: .Date(NSDate().dateByAddingTimeInterval(4)))
		
		XCTAssertNotNil(cache.objectForKey("never"), "Never expires")
		XCTAssertNotNil(cache.objectForKey("2Seconds"), "Expires in 2 seconds")
		XCTAssertNotNil(cache.objectForKey("atDate"), "Expires in 4 seconds")
		
		sleep(2)
		
		XCTAssertNotNil(cache.objectForKey("never"), "Never expires")
		XCTAssertNil(cache.objectForKey("2Seconds"), "Expires in 2 seconds")
		XCTAssertNotNil(cache.objectForKey("atDate"), "Expires in 4 seconds")
		
		sleep(2)
		
		XCTAssertNotNil(cache.objectForKey("never"), "Never expires")
		XCTAssertNil(cache.objectForKey("2Seconds"), "Expires in 2 seconds")
		XCTAssertNil(cache.objectForKey("atDate"), "Expires in 3 seconds")
	}
	
	func testCacheBlockExecuted() {
		var executed = false
		
		cache.setObjectForKey("blockExecuted", cacheBlock: { returnBlock in
			executed = true
			returnBlock("AddedString", .Never)
		}, completion: { object, isCached in
			XCTAssertEqual("AddedString", object, "Get cached object")
			
			XCTAssertNotNil(self.cache.objectForKey("blockExecuted"), "Get cached object")
			XCTAssertTrue(executed, "Block was executed")
		})
	}
	
	func testCacheBlockNotExecuted() {
		var executed = false
		
		cache.setObject("AddedString", forKey: "blockNotExecuted")
		
		cache.setObjectForKey("blockNotExecuted", cacheBlock: { returnBlock in
			executed = true
			returnBlock("SometingElse", .Never)
		}, completion: { object, isCached in
			XCTAssertEqual("AddedString", object, "Get cached object")
			
			XCTAssertEqual("AddedString", self.cache.objectForKey("blockNotExecuted")!, "Get cached object")
			XCTAssertNotNil(self.cache.objectForKey("blockNotExecuted"), "Get cached object")
			XCTAssertFalse(executed, "Block was not executed")
		})
	}
}<commit_msg>Add tests for error in cache block<commit_after>//
//  ExampleTests.swift
//  ExampleTests
//
//  Created by <NAME> on 12/07/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import UIKit
import XCTest

import Example

class ExampleTests: XCTestCase {
	
	var cache: AwesomeCache<NSString> = AwesomeCache<NSString>(name: "awesomeCache")
	
    override func setUp() {
		cache = AwesomeCache<NSString>(name: "awesomeCache")
		cache.removeAllObjects()
		
		super.setUp()
    }
    
    override func tearDown() {
		cache.removeAllObjects()
		
		super.tearDown()
    }
	
	func testGetterAndSetter() {
		let nilObject = cache.objectForKey("unavailable")
		XCTAssertNil(nilObject, "Get nil object")
		
		cache.setObject("AddedString", forKey: "add")
		XCTAssertNotNil(cache.objectForKey("add"), "Get non-nil object")
		XCTAssertEqual("AddedString", cache.objectForKey("add")!, "Get non-nil object")
	}
	
	func testRemoveObject() {
		cache.setObject("AddedString", forKey: "remove")
		XCTAssertNotNil(cache.objectForKey("remove"), "Get non-nil object")
		XCTAssertEqual("AddedString", cache.objectForKey("remove")!, "Get non-nil object")
		
		cache.removeObjectForKey("remove")
		XCTAssertNil(cache.objectForKey("remove"), "Get deleted object")
	}
	
	func testSubscripting() {
		cache["addSubscript"] = "AddedString"
		XCTAssertNotNil(cache["addSubscript"], "Get non-nil object via subscript")
		XCTAssertEqual("AddedString", cache["addSubscript"]!, "Get non-nil object via subscript")
		
		cache["addSubscript"] = nil
		XCTAssertNil(cache["addSubscript"], "Get deleted object via subscript")
	}
	
	func testObjectExpiry() {
		cache.setObject("NeverExpires", forKey: "never", expires: .Never)
		cache.setObject("ExpiresIn2Seconds", forKey: "2Seconds", expires: .Seconds(2))
		cache.setObject("ExpiresAtDate", forKey: "atDate", expires: .Date(NSDate().dateByAddingTimeInterval(4)))
		
		XCTAssertNotNil(cache.objectForKey("never"), "Never expires")
		XCTAssertNotNil(cache.objectForKey("2Seconds"), "Expires in 2 seconds")
		XCTAssertNotNil(cache.objectForKey("atDate"), "Expires in 4 seconds")
		
		sleep(2)
		
		XCTAssertNotNil(cache.objectForKey("never"), "Never expires")
		XCTAssertNil(cache.objectForKey("2Seconds"), "Expires in 2 seconds")
		XCTAssertNotNil(cache.objectForKey("atDate"), "Expires in 4 seconds")
		
		sleep(2)
		
		XCTAssertNotNil(cache.objectForKey("never"), "Never expires")
		XCTAssertNil(cache.objectForKey("2Seconds"), "Expires in 2 seconds")
		XCTAssertNil(cache.objectForKey("atDate"), "Expires in 3 seconds")
	}
	
	func testCacheBlockExecuted() {
		var executed = false
		
		cache.setObjectForKey("blockExecuted", cacheBlock: { returnBlock in
			executed = true
			returnBlock("AddedString", .Never, nil)
		}, completion: { object, isLoadedFromCache, error in
			XCTAssertNotNil(object, "Cached object not nil")
			XCTAssertEqual("AddedString", object!, "Get cached object")
			
			XCTAssertNotNil(self.cache.objectForKey("blockExecuted"), "Get cached object")
			XCTAssertTrue(executed, "Block was executed")
			XCTAssertFalse(isLoadedFromCache, "Object was not loaded cached")
			XCTAssertNil(error, "Error is nil")
		})
	}
	
	func testCacheBlockNotExecuted() {
		var executed = false
		
		cache.setObject("AddedString", forKey: "blockNotExecuted")
		
		cache.setObjectForKey("blockNotExecuted", cacheBlock: { returnBlock in
			executed = true
			returnBlock("SometingElse", .Never, nil)
		}, completion: { object, isLoadedFromCache, error in
			XCTAssertNotNil(object, "Cached object not nil")
			XCTAssertEqual("AddedString", object!, "Get cached object")
				
			XCTAssertNotNil(self.cache.objectForKey("blockNotExecuted"), "Get cached object")
			XCTAssertEqual("AddedString", self.cache.objectForKey("blockNotExecuted")!, "Get cached object")

			XCTAssertFalse(executed, "Block was not executed")
			XCTAssertTrue(isLoadedFromCache, "Object was loaded from cached")
			XCTAssertNil(error, "Error is nil")
		})
	}
	
	
	func testCacheBlockError() {
		
		cache.setObjectForKey("blockError", cacheBlock: { returnBlock in
			let error = NSError(domain: "AwesomeCacheErrorDomain", code: 42, userInfo: nil)
			returnBlock(nil, .Never, error)
		}, completion: { object, isLoadedFromCache, error in
			XCTAssertNil(object, "Cached object nil")
			XCTAssertNil(self.cache.objectForKey("blockError"), "Get cached object")
			
			XCTAssertFalse(isLoadedFromCache, "Object was loaded from cached")
			XCTAssertNotNil(error, "Error is nil")
			XCTAssert(error!.domain == "AwesomeCacheErrorDomain", "Error domain")
			XCTAssert(error!.code == 42, "Error code")
		})
		
		
	}
}
{'fraction_non_alphanumeric': 0.13442703232125366, 'fraction_numerical': 0.006611165523996082, 'mean_word_length': 5.288683602771362, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '6110699', 'n_tokens_mistral': 2933, 'n_tokens_neox': 2558, 'n_words': 556}