<commit_before>import Swish
import Quick
import Nimble
import Result

class APIClientSpec: QuickSpec {
  override func spec() {
    describe("APIClient") {
      context("when the status code is in the 200 range") {
        describe("performRequest(completionHandler:") {
          it("passes the request's request object to the request performer") {
            let request = FakeRequest()
            let performer = FakeRequestPerformer(
              responseData: .JSON([:])
            )
            let client = APIClient(requestPerformer: performer)

            client.performRequest(request) { _ in }

            expect(performer.passedRequest).to(equal(request.build()))
          }

          describe("parsing") {
            context("when the parsing operation is successful") {
              it("returns the parsed object to the completion block") {
                let request = FakeRequest()
                let performer = FakeRequestPerformer(
                  responseData: .JSON(["text": "hello world"])
                )

                let client = APIClient(requestPerformer: performer)
                var result: Result<String, NSError>?

                client.performRequest(request) { result = $0 }

                expect(result?.value).toEventually(equal("hello world"))
              }
            }

            context("when the parsing operation fails") {
              it("returns a failure state to the completion block") {
                let request = FakeRequest()
                let performer = FakeRequestPerformer(
                  responseData: .JSON( ["foo": "bar"])
                )

                let client = APIClient(requestPerformer: performer)
                var message: NSString?

                client.performRequest(request) {
                  message = $0.error?.userInfo[NSLocalizedDescriptionKey] as? NSString
                }

                expect(message).toEventually(equal("MissingKey(text)"))
              }
            }

            context("when the data is empty") {
              it("returns a null JSON object") {
                let request = FakeEmptyDataRequest()
                let performer = FakeRequestPerformer(
                  responseData: .Data(NSData())
                )

                let client = APIClient(requestPerformer: performer)
                var result: Result<EmptyResponse, NSError>?

                client.performRequest(request) { result = $0 }

                expect(result?.value).toEventually(beVoid())
              }
            }
          }

          describe("completionHandler") {
            it("performs on the main thread") {
              let request = FakeRequest()
              let performer = FakeRequestPerformer(
                responseData: .JSON([:])
              )

              let client = APIClient(requestPerformer: performer)
              var isMainThread = false
<commit_msg>Re-add test for if the data is `.None`<commit_after>import Swish
import Quick
import Nimble
import Result

class APIClientSpec: QuickSpec {
  override func spec() {
    describe("APIClient") {
      context("when the status code is in the 200 range") {
        describe("performRequest(completionHandler:") {
          it("passes the request's request object to the request performer") {
            let request = FakeRequest()
            let performer = FakeRequestPerformer(
              responseData: .JSON([:])
            )
            let client = APIClient(requestPerformer: performer)

            client.performRequest(request) { _ in }

            expect(performer.passedRequest).to(equal(request.build()))
          }

          describe("parsing") {
            context("when the parsing operation is successful") {
              it("returns the parsed object to the completion block") {
                let request = FakeRequest()
                let performer = FakeRequestPerformer(
                  responseData: .JSON(["text": "hello world"])
                )

                let client = APIClient(requestPerformer: performer)
                var result: Result<String, NSError>?

                client.performRequest(request) { result = $0 }

                expect(result?.value).toEventually(equal("hello world"))
              }
            }

            context("when the parsing operation fails") {
              it("returns a failure state to the completion block") {
                let request = FakeRequest()
                let performer = FakeRequestPerformer(
                  responseData: .JSON( ["foo": "bar"])
                )

                let client = APIClient(requestPerformer: performer)
                var message: NSString?

                client.performRequest(request) {
                  message = $0.error?.userInfo[NSLocalizedDescriptionKey] as? NSString
                }

                expect(message).toEventually(equal("MissingKey(text)"))
              }
            }

            context("when the data is empty") {
              it("returns a null JSON object") {
                let request = FakeEmptyDataRequest()
                let performer = FakeRequestPerformer(
                  responseData: .Data(NSData())
                )

                let client = APIClient(requestPerformer: performer)
                var result: Result<EmptyResponse, NSError>?

                client.performRequest(request) { result = $0 }

                expect(result?.value).toEventually(beVoid())
              }
            }

            context("when the data is .None") {
              it("returns a null JSON object") {
                let request = FakeEmptyDataRequest()
                let performer = FakeRequestPerformer(
                  responseData: .Data(.None)
                )

                let client = APIClient(requestPerformer: performer)
                var result: Result<EmptyResponse, NSError>?

                client.performRequest(request) { result = $0 }

                expect(result?.value).toEventually(beVoid())
              }
            }
          }

          describe("completionHandler") {
            it("performs on the main thread") {
              let request = FakeRequest()
              let performer = FakeRequestPerformer(
                responseData: .JSON([:])
              )

              let client = APIClient(requestPerformer: performer)
              var isMainThread = false

{'fraction_non_alphanumeric': 0.08996270975761342, 'fraction_numerical': 0.0020198881292728403, 'mean_word_length': 1.6348751534997954, 'pattern_counts': {'":': 4, '<': 8, '<?xml version=': 0, '>': 8, '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': '7197037', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1463, 'n_words': 385}