<commit_before>import Quick
import Moya
import RxSwift
import Nimble

#if os(iOS) || os(watchOS) || os(tvOS)
    private func ImageJPEGRepresentation(_ image: ImageType, _ compression: CGFloat) -> Data? {
        return UIImageJPEGRepresentation(image, compression)
    }
#elseif os(OSX)
    private func ImageJPEGRepresentation(image: ImageType, _ compression: CGFloat) -> Data? {
        var imageRect: CGRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
        let imageRep = NSBitmapImageRep(cgImage: image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil)!)
        return imageRep.representation(using: .JPEG, properties:[:])
    }
#endif

// Necessary since Image(named:) doesn't work correctly in the test bundle
private extension ImageType {
    class func testPNGImage(named name: String) -> ImageType {
        class TestClass { }
        let bundle = Bundle(for: type(of: TestClass()))
        let path = bundle.path(forResource: name, ofType: "png")
        return Image(contentsOfFile: path!)!
    }
}

private func observableSendingData(_ data: Data, statusCode: Int = 200) -> Observable<Response> {
    return Observable.just(Response(statusCode: statusCode, data: data, response: nil))
}

class ObservableMoyaSpec: QuickSpec {
    override func spec() {
        describe("status codes filtering") {
            it("filters out unrequested status codes") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 10)
                
                var errored = false
                _ = observable.filterStatusCodes(0...9).subscribe { (event) -> Void in
                    switch event {
                    case .next(let object):
                        fail("called on non-correct status code: \(object)")
                    case .error:
                        errored = true
                    default:
                        break
                    }
                }
                
                expect(errored).to(beTruthy())
            }
            
            it("filters out non-successful status codes") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 404)
                
                var errored = false
                _ = observable.filterSuccessfulStatusCodes().subscribe { (event) -> Void in
                    switch event {
                    case .next(let object):
                        fail("called on non-success status code: \(object)")
                    case .error:
                        errored = true
                    default:
                        break
                    }
                }
                
                expect(errored).to(beTruthy())
            }
            
            it("passes through correct status codes") {
                let data = Data()
                let observable = observableSendingData(data)
                
                var called = false
                _ = observable.filterSuccessfulStatusCodes().subscribe(onNext: { (object) -> Void in
                    called = true
                })
                
                expect(called).to(beTruthy())
            }
            
            it("filters out non-successful status and redirect codes") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 404)
                
                var errored = false
                _ = observable.filterSuccessfulStatusAndRedirectCodes().subscribe { (event) -> Void in
                    switch event {
                    case .next(let object):
                        fail("called on non-success status code: \(object)")
                    case .error:
                        errored = true
                    default:
                        break
                    }
                }
                
                expect(errored).to(beTruthy())
            }
            
            it("passes through correct status codes") {
                let data = Data()
                let observable = observableSendingData(data)
                
                var called = false
                _ = observable.filterSuccessfulStatusAndRedirectCodes().subscribe(onNext: { (object) -> Void in
                    called = true
                })
                
                expect(called).to(beTruthy())
            }
            
            it("passes through correct redirect codes") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 304)
                
                var called = false
                _ = observable.filterSuccessfulStatusAndRedirectCodes().subscribe(onNext: { (object) -> Void in
                    called = true
                })
                
                expect(called).to(beTruthy())
            }
            
            
            it("knows how to filter individual status code") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 42)
                
                var called = false
                _ = observable.filterStatusCode(42).subscribe(onNext: { (object) -> Void in
                    called = true
                })
                
                expect(called).to(beTruthy())
            }
            
            it("filters out different individual status code") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 43)
                
                var errored = false
                _ = observable.filterStatusCode(42).subscribe { (event) -> Void in
                    switch event {
                    case .next(let object):
                        fail("called on non-success status code: \(object)")
                    case .error:
                        errored = true
                    default:
                        break
                    }
                }
                
                expect(errored).to(beTruthy())
            }
        }
        
        describe("image maping") {
            it("maps data representing an image to an image") {
                let image = Image.testPNGImage(named: "testImage")
                let data = ImageJPEGRepresentation(image: image, 0.75)
                let observable = observableSendingData(data!)
                
                var size: CGSize?
                _ = observable.mapImage().subscribe(onNext: { (image) -> Void in
                    size = image?.size<commit_msg>[Tests] Fixes syntax error.<commit_after>import Quick
import Moya
import RxSwift
import Nimble

#if os(iOS) || os(watchOS) || os(tvOS)
    private func ImageJPEGRepresentation(_ image: ImageType, _ compression: CGFloat) -> Data? {
        return UIImageJPEGRepresentation(image, compression)
    }
#elseif os(OSX)
    private func ImageJPEGRepresentation(_ image: ImageType, _ compression: CGFloat) -> Data? {
        var imageRect: CGRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
        let imageRep = NSBitmapImageRep(cgImage: image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil)!)
        return imageRep.representation(using: .JPEG, properties:[:])
    }
#endif

// Necessary since Image(named:) doesn't work correctly in the test bundle
private extension ImageType {
    class func testPNGImage(named name: String) -> ImageType {
        class TestClass { }
        let bundle = Bundle(for: type(of: TestClass()))
        let path = bundle.path(forResource: name, ofType: "png")
        return Image(contentsOfFile: path!)!
    }
}

private func observableSendingData(_ data: Data, statusCode: Int = 200) -> Observable<Response> {
    return Observable.just(Response(statusCode: statusCode, data: data, response: nil))
}

class ObservableMoyaSpec: QuickSpec {
    override func spec() {
        describe("status codes filtering") {
            it("filters out unrequested status codes") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 10)
                
                var errored = false
                _ = observable.filterStatusCodes(0...9).subscribe { (event) -> Void in
                    switch event {
                    case .next(let object):
                        fail("called on non-correct status code: \(object)")
                    case .error:
                        errored = true
                    default:
                        break
                    }
                }
                
                expect(errored).to(beTruthy())
            }
            
            it("filters out non-successful status codes") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 404)
                
                var errored = false
                _ = observable.filterSuccessfulStatusCodes().subscribe { (event) -> Void in
                    switch event {
                    case .next(let object):
                        fail("called on non-success status code: \(object)")
                    case .error:
                        errored = true
                    default:
                        break
                    }
                }
                
                expect(errored).to(beTruthy())
            }
            
            it("passes through correct status codes") {
                let data = Data()
                let observable = observableSendingData(data)
                
                var called = false
                _ = observable.filterSuccessfulStatusCodes().subscribe(onNext: { (object) -> Void in
                    called = true
                })
                
                expect(called).to(beTruthy())
            }
            
            it("filters out non-successful status and redirect codes") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 404)
                
                var errored = false
                _ = observable.filterSuccessfulStatusAndRedirectCodes().subscribe { (event) -> Void in
                    switch event {
                    case .next(let object):
                        fail("called on non-success status code: \(object)")
                    case .error:
                        errored = true
                    default:
                        break
                    }
                }
                
                expect(errored).to(beTruthy())
            }
            
            it("passes through correct status codes") {
                let data = Data()
                let observable = observableSendingData(data)
                
                var called = false
                _ = observable.filterSuccessfulStatusAndRedirectCodes().subscribe(onNext: { (object) -> Void in
                    called = true
                })
                
                expect(called).to(beTruthy())
            }
            
            it("passes through correct redirect codes") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 304)
                
                var called = false
                _ = observable.filterSuccessfulStatusAndRedirectCodes().subscribe(onNext: { (object) -> Void in
                    called = true
                })
                
                expect(called).to(beTruthy())
            }
            
            
            it("knows how to filter individual status code") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 42)
                
                var called = false
                _ = observable.filterStatusCode(42).subscribe(onNext: { (object) -> Void in
                    called = true
                })
                
                expect(called).to(beTruthy())
            }
            
            it("filters out different individual status code") {
                let data = Data()
                let observable = observableSendingData(data, statusCode: 43)
                
                var errored = false
                _ = observable.filterStatusCode(42).subscribe { (event) -> Void in
                    switch event {
                    case .next(let object):
                        fail("called on non-success status code: \(object)")
                    case .error:
                        errored = true
                    default:
                        break
                    }
                }
                
                expect(errored).to(beTruthy())
            }
        }
        
        describe("image maping") {
            it("maps data representing an image to an image") {
                let image = Image.testPNGImage(named: "testImage")
                let data = ImageJPEGRepresentation(image, 0.75)
                let observable = observableSendingData(data!)
                
                var size: CGSize?
                _ = observable.mapImage().subscribe(onNext: { (image) -> Void in
                    size = image?.size
{'fraction_non_alphanumeric': 0.08338967771016452, 'fraction_numerical': 0.004357298474945534, 'mean_word_length': 1.2407002188183807, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '5090840', 'n_tokens_mistral': 3285, 'n_tokens_neox': 2967, 'n_words': 788}