<commit_before>/**
* Copyright IBM Corporation 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

#if os(Linux)
    import Glibc
#else
    import Darwin
#endif

import XCTest
import Foundation
import SwiftyJSON

@testable import CFEnvironment

/**
* Online tool for escaping JSON: http://www.freeformatter.com/javascript-escape.html
* Online tool for removing new lines: http://www.textfixer.com/tools/remove-line-breaks.php
* Online JSON editor: http://jsonviewer.stack.hu/
*/
class Tests : XCTestCase {

  var allTests : [(String, () throws -> Void)] {
    return [
        ("testConvertStringToJSON", testConvertStringToJSON),
        ("testConvertJSONArrayToStringArray", testConvertJSONArrayToStringArray),
        ("testGetApp", testGetApp),
        ("testGetServices", testGetServices),
        ("testGetService", testGetService),
        ("testGetAppEnv", testGetAppEnv),
        ("testGetServiceURL", testGetServiceURL)
    ]
  }

  let options = "{ \"vcap\": { \"application\": { \"limits\": { \"mem\": 128, \"disk\": 1024, \"fds\": 16384 }, \"application_id\": \"e582416a-9771-453f-8df1-7b467f6d78e4\", \"application_version\": \"e5e029d1-4a1a-4004-9f79-655d550183fb\", \"application_name\": \"swift-test\", \"application_uris\": [ \"swift-test.mybluemix.net\" ], \"version\": \"e5e029d1-4a1a-4004-9f79-655d550183fb\", \"name\": \"swift-test\", \"space_name\": \"dev\", \"space_id\": \"b15eb0bb-cbf3-43b6-bfbc-f76d495981e5\", \"uris\": [ \"swift-test.mybluemix.net\" ], \"users\": null, \"instance_id\": \"7d4f24cfba06462ba23d68aaf1d7354a\", \"instance_index\": 0, \"host\": \"0.0.0.0\", \"port\": 61263, \"started_at\": \"2016-03-04 02:43:07 +0000\", \"started_at_timestamp\": 1457059387, \"start\": \"2016-03-04 02:43:07 +0000\", \"state_timestamp\": 1457059387 }, \"services\": { \"cloudantNoSQLDB\": [ { \"name\": \"Cloudant NoSQL DB-kd\", \"label\": \"cloudantNoSQLDB\", \"tags\": [ \"data_management\", \"ibm_created\", \"ibm_dedicated_public\" ], \"plan\": \"Shared\", \"credentials\": { \"username\": \"09ed7c8a-fae8-48ea-affa-0b44b2224ec0-bluemix\", \"password\": \"<PASSWORD>\", \"host\": \"09ed7c8a-fae8-48ea-affa-0b44b2224ec0-bluemix.cloudant.com\", \"port\": 443, \"url\": \"https://09ed7c8a-fae8-48ea-affa-0b44b2224ec0-bluemix:06c19ae06b1915d8a6649df5901eca85e885182421ffa9ef89e14bbc1b76efd4@09ed7c8a-fae8-48ea-affa-0b44b2224ec0-bluemix.cloudant.com\" } } ] } } }"
  var jsonOptions: JSON = [:]

  override func setUp() {
    super.setUp()
    jsonOptions = JSONUtils.convertStringToJSON(options) as JSON!
  }

  override func tearDown() {
    super.tearDown()
    jsonOptions = [:]
  }

  func testConvertStringToJSON() {
    let VCAP_APPLICATION = "{ \"users\": null,  \"instance_id\": \"7d4f24cfba06462ba23d68aaf1d7354a\",  \"instance_index\": 0,  \"host\": \"0.0.0.0\",  \"port\": 61263,  \"started_at\": \"2016-03-04 02:43:07 +0000\",  \"started_at_timestamp\": 1457059387 }"
    if let json = JSONUtils.convertStringToJSON(VCAP_APPLICATION) {
      //print("JSON object is: \(json)")
      //print("Type is \(json["users"].dynamicType)")
      XCTAssertNil(json["users"] as? AnyObject)
      XCTAssertEqual(json["instance_id"], "7d4f24cfba06462ba23d68aaf1d7354a", "instance_id should match.")
      XCTAssertEqual(json["instance_index"], 0, "instance_index should match.")
      XCTAssertEqual(json["host"], "0.0.0.0", "host should match.")<commit_msg>Unfortunately, xctests on OS X is different from the version on linux. Had to add compilation flags to the source code (yuck) around the definitions of the setup() and tearDown() methods.<commit_after>/**
* Copyright IBM Corporation 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

#if os(Linux)
    import Glibc
#else
    import Darwin
#endif

import XCTest
import Foundation
import SwiftyJSON

@testable import CFEnvironment

/**
* Online tool for escaping JSON: http://www.freeformatter.com/javascript-escape.html
* Online tool for removing new lines: http://www.textfixer.com/tools/remove-line-breaks.php
* Online JSON editor: http://jsonviewer.stack.hu/
*/
class Tests : XCTestCase {

  var allTests : [(String, () throws -> Void)] {
    return [
        ("testConvertStringToJSON", testConvertStringToJSON),
        ("testConvertJSONArrayToStringArray", testConvertJSONArrayToStringArray),
        ("testGetApp", testGetApp),
        ("testGetServices", testGetServices),
        ("testGetService", testGetService),
        ("testGetAppEnv", testGetAppEnv),
        ("testGetServiceURL", testGetServiceURL)
    ]
  }

  let options = "{ \"vcap\": { \"application\": { \"limits\": { \"mem\": 128, \"disk\": 1024, \"fds\": 16384 }, \"application_id\": \"e582416a-9771-453f-8df1-7b467f6d78e4\", \"application_version\": \"e5e029d1-4a1a-4004-9f79-655d550183fb\", \"application_name\": \"swift-test\", \"application_uris\": [ \"swift-test.mybluemix.net\" ], \"version\": \"e5e029d1-4a1a-4004-9f79-655d550183fb\", \"name\": \"swift-test\", \"space_name\": \"dev\", \"space_id\": \"b15eb0bb-cbf3-43b6-bfbc-f76d495981e5\", \"uris\": [ \"swift-test.mybluemix.net\" ], \"users\": null, \"instance_id\": \"7d4f24cfba06462ba23d68aaf1d7354a\", \"instance_index\": 0, \"host\": \"0.0.0.0\", \"port\": 61263, \"started_at\": \"2016-03-04 02:43:07 +0000\", \"started_at_timestamp\": 1457059387, \"start\": \"2016-03-04 02:43:07 +0000\", \"state_timestamp\": 1457059387 }, \"services\": { \"cloudantNoSQLDB\": [ { \"name\": \"Cloudant NoSQL DB-kd\", \"label\": \"cloudantNoSQLDB\", \"tags\": [ \"data_management\", \"ibm_created\", \"ibm_dedicated_public\" ], \"plan\": \"Shared\", \"credentials\": { \"username\": \"09ed7c8a-fae8-48ea-affa-0b44b2224ec0-bluemix\", \"password\": \"<PASSWORD>\", \"host\": \"09ed7c8a-fae8-48ea-affa-0b44b2224ec0-bluemix.cloudant.com\", \"port\": 443, \"url\": \"https://09ed7c8a-fae8-48ea-affa-0b44b2224ec0-bluemix:06c19ae06b1915d8a6649df5901eca85e885182421ffa9ef89e14bbc1b76efd4@09ed7c8a-fae8-48ea-affa-0b44b2224ec0-bluemix.cloudant.com\" } } ] } } }"
  var jsonOptions: JSON = [:]

#if os(Linux)
  func setUp() {
    jsonOptions = JSONUtils.convertStringToJSON(options) as JSON!
}

  func tearDown() {
    jsonOptions = [:]
}
#else
  override func setUp() {
    super.setUp()
    jsonOptions = JSONUtils.convertStringToJSON(options) as JSON!
  }

  override func tearDown() {
    super.tearDown()
    jsonOptions = [:]
  }
#endif

  func testConvertStringToJSON() {
    let VCAP_APPLICATION = "{ \"users\": null,  \"instance_id\": \"7d4f24cfba06462ba23d68aaf1d7354a\",  \"instance_index\": 0,  \"host\": \"0.0.0.0\",  \"port\": 61263,  \"started_at\": \"2016-03-04 02:43:07 +0000\",  \"started_at_timestamp\": 1457059387 }"
    if let json = JSONUtils.convertStringToJSON(VCAP_APPLICATION) {
      //print("JSON object is: \(json)")
      //print("Type is \(json["users"].dynamicType)")
      XCTAssertNil(json["users"] as? AnyObject)
      XCTAssertEqual(json["instance_id"], "7d4f24cfba06462ba23d68aaf1d7354a", "instance_id should match.")
      XCTAssertEqual(json["instance_index"], 0, "instance_index should match.")
      XCTAssertEqual(json["host"], "0.0.0.0", "host should match.")
{'fraction_non_alphanumeric': 0.1801456969996296, 'fraction_numerical': 0.09136930485245091, 'mean_word_length': 4.059337913803873, 'pattern_counts': {'":': 86, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 8, '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': '5192757', 'n_tokens_mistral': 3228, 'n_tokens_neox': 2811, 'n_words': 604}