<commit_before>//
//  BNRSwiftJSONTests.swift
//  BNRSwiftJSONTests
//
//  Created by <NAME> on 3/25/15.
//  Copyright (c) 2015 BigNerdRanch. All rights reserved.
//

import UIKit
import XCTest
import BNRSwiftJSON
import Swift

class BNRSwiftJSONTests: XCTestCase {
    
    func testThatJSONValueCanCreateInstanceWithData() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        switch json {
        case .Success(let b):
            XCTAssert(true, "There should be a `JSONValue` in the `box`.")
        case .Failure(let error):
            XCTAssert(false, "There should be no error in parsing the sample JSON data.")
        }
    }
    
    func testThatJSONValueCanCreatePeople() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let peopleArray = json["people"].array
        switch peopleArray {
        case .Success(let people):
            for person in people.value {
                let per = Person.createWithJSONValue(person)
                switch per {
                case .Success(let p):
                    XCTAssertTrue(p.value.name != "", "People should have names.")
                case .Failure(let error):
                    XCTAssert(false, "There should be no `error`.")
                }
            }
        case .Failure(let error):
            XCTAssert(false, "There should be no error.")
        }
    }
    
    func testThatCollectResultsCanCreateArrayOfPeople() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let peopleArray = json.bind { $0["people"] }.array.bind { collectResults(map($0, Person.createWithJSONValue)) }
        switch peopleArray {
        case .Success(let box):
            box.value.map { XCTAssertTrue($0.name != "", "There should be a name.") }
        case .Failure(let error):
            XCTAssert(false, "There should be no failure.")
        }
    }

    func testThatSplitResultsCanCreateArrayOfPeople() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let peopleArray = json.bind { $0["people"] }.array
        switch peopleArray {
        case .Success(let people):
            let results = splitResults(people.value.map { Person.createWithJSONValue($0) })
            XCTAssertTrue(results.successes.count != 0, "There should be successes.")
            XCTAssertTrue(results.failures.count == 0, "There should be no failures.")
        case .Failure(let error):
            XCTAssert(false, "There should be no error.")
        }
    }
    
    func testSplitResultCanGatherPeopleInSuccesses() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let people = splitResult(json.bind { $0["people"].array }, Person.createWithJSONValue)
        XCTAssertTrue(people.successes.count > 0, "There should be people in `successes`.")
        XCTAssertTrue(people.failures.count == 0, "There should be no errors in `failures`.")
    }
    
    func testSplitResultCanGatherErrorsInFailures() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let peopl = splitResult(json.bind { $0["peopl"] }.array, Person.createWithJSONValue)
        XCTAssertTrue(peopl.successes.count == 0, "There should be no people in `successes`.")
        XCTAssertTrue(peopl.failures.count > 0, "There should be errors in `failures`.")
    }
    
    func testThatSubscriptingJSONValueWorksForTopLevelObject() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let success = json["success"].bool
        switch success {
        case .Success(let s):
            XCTAssertTrue(s.value == true, "There should be `success`.")
        case .Failure(let error):
            XCTAssert(false, "There should be no error: \(error.localizedFailureReason)")
        }
    }
    
    func testThatJSONNullMatchesNullValue() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let key = json["key"]
        switch key {
        case .Success(let value):
            switch value {
            case .JSONNull:
                XCTAssert(true, "`value` should be `.JSONNull`.")
            default:<commit_msg>Cleans up splitResults and splitResult.<commit_after>//
//  BNRSwiftJSONTests.swift
//  BNRSwiftJSONTests
//
//  Created by <NAME> on 3/25/15.
//  Copyright (c) 2015 BigNerdRanch. All rights reserved.
//

import UIKit
import XCTest
import BNRSwiftJSON
import Swift

class BNRSwiftJSONTests: XCTestCase {
    
    func testThatJSONValueCanCreateInstanceWithData() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        switch json {
        case .Success(let b):
            XCTAssert(true, "There should be a `JSONValue` in the `box`.")
        case .Failure(let error):
            XCTAssert(false, "There should be no error in parsing the sample JSON data.")
        }
    }
    
    func testThatJSONValueCanCreatePeople() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let peopleArray = json["people"].array
        switch peopleArray {
        case .Success(let people):
            for person in people.value {
                let per = Person.createWithJSONValue(person)
                switch per {
                case .Success(let p):
                    XCTAssertTrue(p.value.name != "", "People should have names.")
                case .Failure(let error):
                    XCTAssert(false, "There should be no `error`.")
                }
            }
        case .Failure(let error):
            XCTAssert(false, "There should be no error.")
        }
    }
    
    func testThatCollectResultsCanCreateArrayOfPeople() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let peopleArray = json.bind { $0["people"] }.array.bind { collectResults(map($0, Person.createWithJSONValue)) }
        switch peopleArray {
        case .Success(let box):
            box.value.map { XCTAssertTrue($0.name != "", "There should be a name.") }
        case .Failure(let error):
            XCTAssert(false, "There should be no failure.")
        }
    }

    func testThatSplitResultsCanCreateArrayOfPeople() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let peopleArray = json.bind { $0["people"] }.array
        switch peopleArray {
        case .Success(let people):
            let results = splitResults(people.value.map { Person.createWithJSONValue($0) })
            XCTAssertTrue(results.successes.count != 0, "There should be successes.")
            XCTAssertTrue(results.failures.count == 0, "There should be no failures.")
        case .Failure(let error):
            XCTAssert(false, "There should be no error.")
        }
    }
    
    func testSplitResultCanGatherPeopleInSuccesses() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let people = splitResult(json["people"].array, Person.createWithJSONValue)
        XCTAssertTrue(people.successes.count > 0, "There should be people in `successes`.")
        XCTAssertTrue(people.failures.count == 0, "There should be no errors in `failures`.")
    }
    
    func testSplitResultCanGatherErrorsInFailures() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let peopl = splitResult(json["peopl"].array, Person.createWithJSONValue)
        XCTAssertTrue(peopl.successes.count == 0, "There should be no people in `successes`.")
        XCTAssertTrue(peopl.failures.count > 0, "There should be errors in `failures`.")
    }
    
    func testThatSubscriptingJSONValueWorksForTopLevelObject() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let success = json["success"].bool
        switch success {
        case .Success(let s):
            XCTAssertTrue(s.value == true, "There should be `success`.")
        case .Failure(let error):
            XCTAssert(false, "There should be no error: \(error.localizedFailureReason)")
        }
    }
    
    func testThatJSONNullMatchesNullValue() {
        let data = createData()
        let json = JSONValue.createJSONValueFrom(data!)
        let key = json["key"]
        switch key {
        case .Success(let value):
            switch value {
            case .JSONNull:
                XCTAssert(true, "`value` should be `.JSONNull`.")
            default:
{'fraction_non_alphanumeric': 0.10440324449594438, 'fraction_numerical': 0.004866743916570104, 'mean_word_length': 2.279255319148936, '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': '6642736', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2367, 'n_words': 626}