<commit_before>//
//  EncoderSpec.swift
//  Tyro
//
//  Created by <NAME> on 11/19/15.
//  Copyright © 2015 TypeLift. All rights reserved.
//

import XCTest
import Swiftz
@testable import Tyro

class EncoderSpec : XCTestCase {
    func testEncodeArray() {
        let array = ["a", "b", "c"]
        let result = JSONEncoder.encoder.encodeEither(array)
        XCTAssertNotNil(result)
        XCTAssert(result.right == JSONValue.Array([.String("a"), .String("b"), .String("c")]))
        let jsonString = result.right!.toJSONString()!
        print("testEncodeArray: \(jsonString)")
        XCTAssertEqual("[\"a\",\"b\",\"c\"]", jsonString)
    }
    
    func testEncodeObject() {
        let object = ["key": "value"]
        let result = JSONEncoder.encoder.encodeEither(object)
        XCTAssertNotNil(result)
        XCTAssert(result.right == JSONValue.Object(["key": .String("value")]))
        let jsonString = result.right!.toJSONString()!
        print("testEncodeObject: \(jsonString)")
        XCTAssertEqual("{\"key\":\"value\"}", jsonString)
    }
    
    func testEncodeString() {
        let string = "a string"
        let result = JSONEncoder.encoder.encodeEither(string)
        XCTAssertNotNil(result)
        XCTAssert(result.right == .String("a string"))
    }
    
    func testEncodeNumber() {
        let number = 10
        let result = JSONEncoder.encoder.encodeEither(number)
        XCTAssertNotNil(result)
        XCTAssert(result.right == .Number(number))
    }
    
    func testEncodeEmbeddedObjects() {
        let object = ["number": 42, "array": [1, 2, 3], "object": ["strings": ["1", "2", "3"], "pi": 3.14159], "string": "hello"]
        let result = JSONEncoder.encoder.encodeEither(object)
        XCTAssertNotNil(result)
        
        switch result.right! {
        case .Object(let v):
            XCTAssert(result.right == .Object(v))
            XCTAssert(v["number"] == .Number(42))
            XCTAssert(v["string"] == .String("hello"))
            XCTAssert(v["array"] == .Array([.Number(1), .Number(2), .Number(3)]))
            XCTAssert(v["object"] == .Object(["strings": .Array([.String("1"), .String("2"), .String("3")]), "pi": .Number(3.14159)]))
            let jsonString = result.right!.toJSONString()!
            print("testEncodeEmbeddedObjects: \(jsonString)")
            let expectedJson = "{\"array\":[1,2,3],\"object\":{\"strings\":[\"1\",\"2\",\"3\"],\"pi\":3.14159},\"number\":42,\"string\":\"hello\"}"
            XCTAssertEqual(expectedJson, jsonString)
        default:
            XCTFail("Could not encode to JSONValue.Object")
        }
    }
    
    func testEncodeError() {<commit_msg>Fixed test to ensure decoded json string matches original value.<commit_after>//
//  EncoderSpec.swift
//  Tyro
//
//  Created by <NAME> on 11/19/15.
//  Copyright © 2015 TypeLift. All rights reserved.
//

import XCTest
import Swiftz
@testable import Tyro

class EncoderSpec : XCTestCase {
    func testEncodeArray() {
        let array = ["a", "b", "c"]
        let result = JSONEncoder.encoder.encodeEither(array)
        XCTAssertNotNil(result)
        XCTAssert(result.right == JSONValue.Array([.String("a"), .String("b"), .String("c")]))
        let jsonString = result.right!.toJSONString()!
        print("testEncodeArray: \(jsonString)")
        XCTAssertEqual("[\"a\",\"b\",\"c\"]", jsonString)
    }
    
    func testEncodeObject() {
        let object = ["key": "value"]
        let result = JSONEncoder.encoder.encodeEither(object)
        XCTAssertNotNil(result)
        XCTAssert(result.right == JSONValue.Object(["key": .String("value")]))
        let jsonString = result.right!.toJSONString()!
        print("testEncodeObject: \(jsonString)")
        XCTAssertEqual("{\"key\":\"value\"}", jsonString)
    }
    
    func testEncodeString() {
        let string = "a string"
        let result = JSONEncoder.encoder.encodeEither(string)
        XCTAssertNotNil(result)
        XCTAssert(result.right == .String("a string"))
    }
    
    func testEncodeNumber() {
        let number = 10
        let result = JSONEncoder.encoder.encodeEither(number)
        XCTAssertNotNil(result)
        XCTAssert(result.right == .Number(number))
    }
    
    func testEncodeEmbeddedObjects() {
        let object = ["number": 42, "array": [1, 2, 3], "object": ["strings": ["1", "2", "3"], "pi": 3.14159], "string": "hello"]
        let result = JSONEncoder.encoder.encodeEither(object)
        XCTAssertNotNil(result)
        
        switch result.right! {
        case .Object(let v):
            XCTAssert(result.right == .Object(v))
            XCTAssert(v["number"] == .Number(42))
            XCTAssert(v["string"] == .String("hello"))
            XCTAssert(v["array"] == .Array([.Number(1), .Number(2), .Number(3)]))
            XCTAssert(v["object"] == .Object(["strings": .Array([.String("1"), .String("2"), .String("3")]), "pi": .Number(3.14159)]))
            let jsonString = result.right!.toJSONString()!
            
            // Ensure the decoded json string matches the original encoded value
            let decodedValueFromJson = JSONValue.decodeEither(jsonString).right!
            XCTAssertEqual(decodedValueFromJson, result.right!)
        default:
            XCTFail("Could not encode to JSONValue.Object")
        }
    }
    
    func testEncodeError() {
{'fraction_non_alphanumeric': 0.16912726593160157, 'fraction_numerical': 0.017566809942066903, 'mean_word_length': 2.606469002695418, 'pattern_counts': {'":': 28, '<': 5, '<?xml version=': 0, '>': 5, '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': '4908878', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1618, 'n_words': 325}