<commit_before>/**
 * Copyright IBM Corporation 2015
 *
 * 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.
 **/

import XCTest
@testable import WatsonDeveloperCloud

class SpeechToTextTests: XCTestCase {

    var service: SpeechToText!
    let timeout: NSTimeInterval = 30.0

    override func setUp() {
        super.setUp()
        self.continueAfterFailure = false

        // identify credentials file
        let bundle = NSBundle(forClass: self.dynamicType)
        guard let credentialsURL = bundle.pathForResource("Credentials", ofType: "plist") else {
            XCTFail("Unable to locate credentials file.")
            return
        }

        // load credentials file
        let dict = NSDictionary(contentsOfFile: credentialsURL)
        guard let credentials = dict as? Dictionary<String, String> else {
            XCTFail("Unable to read credentials file.")
            return
        }

        // read SpeechToText username
        guard let user = credentials["SpeechToTextUsername"] else {
            XCTFail("Unable to read Speech to Text username.")
            return
        }

        // read SpeechToText password
        guard let password = credentials["SpeechToTextPassword"] else {
            XCTFail("Unable to read Speech to Text password.")
            return
        }

        // instantiate the service
        service = SpeechToText(username: user, password: password)
    }

    // Wait for an expectation to be fulfilled.
    func waitForExpectation() {
        waitForExpectationsWithTimeout(timeout) { error in
            XCTAssertNil(error, "Timeout.")
        }
    }

    func testTranscribeWAV() {
        transcribe("SpeechSample", withExtension: "wav", format: .WAV)
    }

    func transcribe(filename: String, withExtension: String, format: AudioMediaType) {
        let description = "Testing transcribe with pre-recorded file."
        let expectation = expectationWithDescription(description)

        let bundle = NSBundle(forClass: self.dynamicType)
        guard let url = bundle.URLForResource(filename, withExtension: withExtension) else {
            XCTFail("Unable to locate \(filename).\(withExtension).")
            return
        }
        guard let audioData = NSData(contentsOfURL: url) else {
            XCTFail("Unable to read \(filename).\(withExtension).")
            return
        }

        let settings = SpeechToTextSettings(contentType: format)
        service.transcribe(audioData, settings: settings) { results in
            print(results)
            // TODO: verify results...
            // expectation.fulfill()
        }
        waitForExpectation()
    }
}<commit_msg>Fail Speech to Text test if an error is produced<commit_after>/**
 * Copyright IBM Corporation 2015
 *
 * 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.
 **/

import XCTest
@testable import WatsonDeveloperCloud

class SpeechToTextTests: XCTestCase {

    var service: SpeechToText!
    let timeout: NSTimeInterval = 30.0

    override func setUp() {
        super.setUp()
        self.continueAfterFailure = false

        // identify credentials file
        let bundle = NSBundle(forClass: self.dynamicType)
        guard let credentialsURL = bundle.pathForResource("Credentials", ofType: "plist") else {
            XCTFail("Unable to locate credentials file.")
            return
        }

        // load credentials file
        let dict = NSDictionary(contentsOfFile: credentialsURL)
        guard let credentials = dict as? Dictionary<String, String> else {
            XCTFail("Unable to read credentials file.")
            return
        }

        // read SpeechToText username
        guard let user = credentials["SpeechToTextUsername"] else {
            XCTFail("Unable to read Speech to Text username.")
            return
        }

        // read SpeechToText password
        guard let password = credentials["SpeechToTextPassword"] else {
            XCTFail("Unable to read Speech to Text password.")
            return
        }

        // instantiate the service
        service = SpeechToText(username: user, password: password)
    }

    // Wait for an expectation to be fulfilled.
    func waitForExpectation() {
        waitForExpectationsWithTimeout(timeout) { error in
            XCTAssertNil(error, "Timeout.")
        }
    }

    func testTranscribeWAV() {
        transcribe("SpeechSample", withExtension: "wav", format: .WAV)
    }

    func transcribe(filename: String, withExtension: String, format: AudioMediaType) {
        let description = "Testing transcribe with pre-recorded file."
        let expectation = expectationWithDescription(description)

        let bundle = NSBundle(forClass: self.dynamicType)
        guard let url = bundle.URLForResource(filename, withExtension: withExtension) else {
            XCTFail("Unable to locate \(filename).\(withExtension).")
            return
        }
        guard let audioData = NSData(contentsOfURL: url) else {
            XCTFail("Unable to read \(filename).\(withExtension).")
            return
        }

        func failure(error: NSError) {
            print(error)
            XCTFail()
        }

        let settings = SpeechToTextSettings(contentType: format)
        service.transcribe(audioData, settings: settings, failure: failure) { results in
            print(results)
            // TODO: verify results...
            // expectation.fulfill()
        }
        waitForExpectation()
    }
}
{'fraction_non_alphanumeric': 0.08204573547589616, 'fraction_numerical': 0.003399258343634116, 'mean_word_length': 2.4577991452991452, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5759638', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1563, 'n_words': 546}