<commit_before>//
//  DecodableOperatorsTests.swift
//  Decodable
//
//  Created by FJBelchi on 13/07/15.
//  Copyright © 2015 anviking. All rights reserved.
//

import XCTest
@testable import Decodable

class DecodableOperatorsTests: XCTestCase {

    // MARK: =>
    
    func testDecodeAnyDecodableSuccess() {
        // given
        let key = "key"
        let value = "value"
        let dictionary: NSDictionary = [key: value]
        // when
        let string = try! dictionary => key as String
        // then
        XCTAssertEqual(string, value)
    }
    
    func testDecodeAnyDecodableDictionarySuccess() {
        // given
        let key = "key"
        let value: NSDictionary = [key : "value"]
        let dictionary: NSDictionary = [key: value]
        // when
        let result: NSDictionary = try! dictionary => key as! NSDictionary
        // then
        XCTAssertEqual(result, value)
    }
    
    func testDecodeDictOfArraysSucess() {
        // given
        let key = "key"
        let value: NSDictionary = ["list": [1, 2, 3]]
        let dictionary: NSDictionary = [key: value]
        // when
        let result: [String: [Int]] = try! dictionary => key
        // then
        XCTAssertEqual(result, value)
    }
    
    func testDecodeNestedDictionarySuccess() {
        // given
        let key = "key"
        let value: NSDictionary = ["aKey" : "value"]
        let dictionary: NSDictionary = [key: [key: value]]
        // when
        let result = try! dictionary => key => key
        // then
        XCTAssertEqual(result as? NSDictionary, value)
    }

    func testDecodeNestedDictionaryOptionalSuccess() {
        // given
        let key = "key"
        let value: NSDictionary = ["aKey" : "value"]
        let dictionary: NSDictionary = [key: [key: value]]
        // when
        let result: [String: AnyObject]? = try! dictionary => key => key as! [String : AnyObject]
        // then
        XCTAssertEqual(result, value)
    }
    
    func testDecodeNestedIntSuccess() {
        // given
        let value = 4
        let dictionary: NSDictionary = ["key1": ["key2": ["key3": value]]]
        // when
        let result: Int = try! dictionary => "key1" => "key2" => "key3"
        // then
        XCTAssertEqual(result, value)
    }

    
    func testDecodeNestedDictionaryCastingSuccess() {
        // given
        let key = "key"
        let value: NSDictionary = ["aKey" : "value"]
        let dictionary: NSDictionary = [key: [key: value]]
        // when
        let result = try! dictionary => key => key as! [String: String]
        // then
        XCTAssertEqual(result, value)
    }

    func testDecodeAnyDecodableOptionalSuccess() {
        // given
        let key = "key"
        let value = "value"
        let dictionary: NSDictionary = [key: value]
        // when
        let string = try! dictionary => key as String?
        // then
        XCTAssertEqual(string!, value)
    }
    
    func testDecodeAnyDecodableOptionalNilSuccess() {
        // given
        let key = "key"
        let dictionary: NSDictionary = [key: NSNull()]
        // when
        let string = try! dictionary => key as String?
        // then
        XCTAssertNil(string)
    }
    
    func testDecodeAnyDecodableOptionalTypeMismatchFailure() {
        // given
        let key = "key"
        let dictionary: NSDictionary = [key: 2]
        // when
        do {
            let a = try dictionary => key as String?
            print(a)
            XCTFail()
        } catch let error as TypeMismatchError {
            XCTAssertEqual(String(error.receivedType), "__NSCFNumber")
        } catch let error {
            XCTFail("should not throw \(error)")
        }
    }
    
    
    // MARK: => Errors
    
    /* // 
    func testDecodeNestedDictionaryCastingFailure() {
        // given
        let value: NSDictionary = ["apple" : 2]
        let dictionary: NSDictionary = ["firstKey": ["secondKey": value]]
        // when
        do {
            _ = try dictionary => "firstKey" => "secondKey" as [String: String]
            XCTFail()
        } catch DecodingError.TypeMismatchError(_, Dictionary<String, String>.self, let info) {
            // then
            XCTAssertEqual(info.formattedPath, "firstKey.secondKey")
            XCTAssertEqual(info.object as? NSDictionary, value)
        } catch let error {
            XCTFail("should not throw \(error)")<commit_msg>add a few nested =>? tests<commit_after>//
//  DecodableOperatorsTests.swift
//  Decodable
//
//  Created by FJBelchi on 13/07/15.
//  Copyright © 2015 anviking. All rights reserved.
//

import XCTest
@testable import Decodable

class DecodableOperatorsTests: XCTestCase {

    // MARK: =>
    
    func testDecodeAnyDecodableSuccess() {
        // given
        let key = "key"
        let value = "value"
        let dictionary: NSDictionary = [key: value]
        // when
        let string = try! dictionary => key as String
        // then
        XCTAssertEqual(string, value)
    }
    
    func testDecodeAnyDecodableDictionarySuccess() {
        // given
        let key = "key"
        let value: NSDictionary = [key : "value"]
        let dictionary: NSDictionary = [key: value]
        // when
        let result: NSDictionary = try! dictionary => key as! NSDictionary
        // then
        XCTAssertEqual(result, value)
    }
    
    func testDecodeDictOfArraysSucess() {
        // given
        let key = "key"
        let value: NSDictionary = ["list": [1, 2, 3]]
        let dictionary: NSDictionary = [key: value]
        // when
        let result: [String: [Int]] = try! dictionary => key
        // then
        XCTAssertEqual(result, value)
    }
    
    // MARK: - Nested keys
    
    func testDecodeNestedDictionarySuccess() {
        // given
        let key = "key"
        let value: NSDictionary = ["aKey" : "value"]
        let dictionary: NSDictionary = [key: [key: value]]
        // when
        let result = try! dictionary => key => key
        // then
        XCTAssertEqual(result as? NSDictionary, value)
    }

    func testDecodeNestedDictionaryOptionalSuccess() {
        // given
        let key = "key"
        let value: NSDictionary = ["aKey" : "value"]
        let dictionary: NSDictionary = [key: [key: value]]
        // when
        let result: [String: AnyObject]? = try! dictionary => key => key as! [String : AnyObject]
        // then
        XCTAssertEqual(result, value)
    }
    
    func testDecodeNestedIntSuccess() {
        // given
        let value = 4
        let dictionary: NSDictionary = ["key1": ["key2": ["key3": value]]]
        // when
        let result: Int = try! dictionary => "key1" => "key2" => "key3"
        // then
        XCTAssertEqual(result, value)
    }
    
    func testDecodeNestedDictionaryCastingSuccess() {
        // given
        let key = "key"
        let value: NSDictionary = ["aKey" : "value"]
        let dictionary: NSDictionary = [key: [key: value]]
        // when
        let result = try! dictionary => key => key as! [String: String]
        // then
        XCTAssertEqual(result, value)
    }

    func testDecodeAnyDecodableOptionalSuccess() {
        // given
        let key = "key"
        let value = "value"
        let dictionary: NSDictionary = [key: value]
        // when
        let string = try! dictionary => key as String?
        // then
        XCTAssertEqual(string!, value)
    }
    
    func testDecodeAnyDecodableOptionalNilSuccess() {
        // given
        let key = "key"
        let dictionary: NSDictionary = [key: NSNull()]
        // when
        let string = try! dictionary => key as String?
        // then
        XCTAssertNil(string)
    }
    
    func testDecodeAnyDecodableOptionalTypeMismatchFailure() {
        // given
        let key = "key"
        let dictionary: NSDictionary = [key: 2]
        // when
        do {
            let a = try dictionary => key as String?
            print(a)
            XCTFail()
        } catch let error as TypeMismatchError {
            XCTAssertEqual(String(error.receivedType), "__NSCFNumber")
        } catch let error {
            XCTFail("should not throw \(error)")
        }
    }
    
    // MARK: - Nested =>? operators
    
    // Should throw on typemismatch with correct metadata
    func testDecodeNestedTypeMismatchFailure() {
        let json: NSDictionary = ["user": ["followers": "not_an_integer"]]
        do {
            let _ : Int? = try json =>? "user" => "followers"
            XCTFail("should throw")
        } catch let error as TypeMismatchError {
            XCTAssertEqual(error.formattedPath, "user.followers")
        } catch {
            XCTFail("should not throw \(error)")
        }
    }
    
    // Should currently (though really it shoult not) treat all keys as either optional or non-optional
    func testDecodeNestedTypeReturnNilForSubobjectMissingKey() {
        let json: NSDictionary = ["user": ["something_else": "test"]]
        try! XCTAssertEqual(json =>? "user" => "followers", Optional<Int>.None)
    }
    
    // Sanity check
    func testDecodeNestedTypeSuccess() {
        let json: NSDictionary = ["user": ["followers": 3]]
        try! XCTAssertEqual(json =>? "user" => "followers", 3)
    }
    
    
    // MARK: => Errors
    
    /* // 
    func testDecodeNestedDictionaryCastingFailure() {
        // given
        let value: NSDictionary = ["apple" : 2]
        let dictionary: NSDictionary = ["firstKey": ["secondKey": value]]
        // when
        do {
            _ = try dictionary => "firstKey" => "secondKey" as [String: String]
            XCTFail()
        } catch DecodingError.TypeMismatchError(_, Dictionary<String, String>.self, let info) {
            // then
            XCTAssertEqual(info.formattedPath, "firstKey.secondKey")
            XCTAssertEqual(info.object as? NSDictionary, value)
        } catch let error {
            XCTFail("should not throw \(error)")
{'fraction_non_alphanumeric': 0.11409665129336274, 'fraction_numerical': 0.004611991177060357, 'mean_word_length': 1.8879559930515344, 'pattern_counts': {'":': 18, '<': 6, '<?xml version=': 0, '>': 52, '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': '4720528', 'n_tokens_mistral': 2901, 'n_tokens_neox': 2857, 'n_words': 788}