<commit_before>//
//  URLCacheProvider_Tests.swift
//  DRNet
//
//  Created by <NAME> on 08/11/14.
//  Copyright (c) 2014 Darrarski. All rights reserved.
//

#if os(iOS)
    import UIKit
#elseif os(OS_X)
    import Cocoa
#endif

import XCTest
import DRNet

class URLCacheProvider_Tests: XCTestCase {
    
    func testInit() {
        let provider = DRNet.URLCacheProvider(memoryCapacity: .Limit(bytes: 1024), diskCapacity: .Limit(bytes: 2048), diskPath: "TestPath")
        
        switch provider.memoryCapacity {
        case .Limit(bytes: 1024): ()
        default:
            XCTFail("Invalid memory capacity")
        }
        
        switch provider.diskCapacity {
        case .Limit(bytes: 2048): ()
        default:
            XCTFail("Invalid disk capacity")
        }
        
        XCTAssert(provider.diskPath == "TestPath", "Invalid disk path")
    }
    
}<commit_msg>URLCacheProvider_Tests - added basic tests for saving and fetching from memory and disk<commit_after>//
//  URLCacheProvider_Tests.swift
//  DRNet
//
//  Created by <NAME> on 08/11/14.
//  Copyright (c) 2014 Darrarski. All rights reserved.
//

#if os(iOS)
    import UIKit
#elseif os(OS_X)
    import Cocoa
#endif

import XCTest
import DRNet

class URLCacheProvider_Tests: XCTestCase {
    
    func testInit() {
        let provider = DRNet.URLCacheProvider(memoryCapacity: .Limit(bytes: 1024), diskCapacity: .Limit(bytes: 2048), diskPath: "TestPath")
        
        switch provider.memoryCapacity {
        case .Limit(bytes: 1024): ()
        default:
            XCTFail("Invalid memory capacity")
        }
        
        switch provider.diskCapacity {
        case .Limit(bytes: 2048): ()
        default:
            XCTFail("Invalid disk capacity")
        }
        
        XCTAssert(provider.diskPath == "TestPath", "Invalid disk path")
    }
    
    func testSaveAndFetchFromMemory() {
        testSaveAndFetch(DRNet.URLCacheProvider(memoryCapacity: .Limit(bytes: 10 * 1024), diskCapacity: .Disable, diskPath: "Test"))
    }
    
    func testSaveAndFetchFromDisk() {
        testSaveAndFetch(DRNet.URLCacheProvider(memoryCapacity: .Disable, diskCapacity: .Limit(bytes: 10 * 1024), diskPath: "Test"))
    }
    
    private func testSaveAndFetch(provider: DRNet.URLCacheProvider) {
        let request = DRNet.Request(method: .GET, url: NSURL(string: "http://test")!, headers: nil, parameters: nil)
        let responseDataString = "ResponseTest"
        let responseData = responseDataString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
        let response = DRNet.Response(URLResponse: nil, data: responseData, error: nil, forURLRequest: request.toNSURLRequest())
        
        provider.saveResponse(response, forRequest: request)
        
        let expectProviderReturnsCachedResponse = expectationWithDescription("provider returns cached response")
        
        provider.responseForRequest(request, completion: { (cachedResponse) -> Void in
            if let cachedResponseData = cachedResponse.data {
                if let cachedResponseDataString = NSString(data: cachedResponseData, encoding: NSUTF8StringEncoding) {
                    XCTAssert(cachedResponseDataString == responseDataString, "cached response data string differs from response data string")
                }
                else {
                    XCTFail("cached response data cannot be converted to string")
                }
            }
            else {
                XCTFail("cached response data is empty")
            }
            
            expectProviderReturnsCachedResponse.fulfill()
        })
        
        waitForExpectationsWithTimeout(3, handler: { (error) -> Void in })
    }
    
}
{'fraction_non_alphanumeric': 0.09646302250803858, 'fraction_numerical': 0.01795284030010718, 'mean_word_length': 2.4564814814814815, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4033931', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1017, 'n_words': 254}