<commit_before>//
//  BrowserDelegateTests.swift
//  SwiftNetService
//
//  Created by <NAME> on 11/3/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import XCTest
import ReactiveCocoa

@testable import SwiftNetService

class ServicePublicationDelegate : NSObject, NSNetServiceDelegate {
    var expectation : XCTestExpectation
    
    init(expectation : XCTestExpectation) {
        self.expectation = expectation
    }

    @objc func netService(sender: NSNetService, didNotPublish errorDict: [String : NSNumber]) {
        XCTFail("Failed to publish service: \(errorForErrorDictionary(errorDict))")
    }

    @objc func netServiceDidPublish(sender: NSNetService) {
        self.expectation.fulfill()
        NSLog("Did publish service: \(sender)")
    }
    
    @objc func netServiceDidStop(sender: NSNetService) {
        NSLog("Did stop publishing service: \(sender)")
    }

    @objc func netServiceWillPublish(sender: NSNetService) {
        NSLog("Will publish service: \(sender)")
    }

}

class TestService {
    var UUID : String
    var type : String
    var service : NSNetService
    var publicationDelegate : ServicePublicationDelegate?
    var browser : NSNetServiceBrowser?
    var discoveryDelegate : BrowserDelegate?
    
    init(port : Int32) {
        self.UUID = NSUUID().UUIDString
        self.type = "_\(self.UUID)._tcp"
        self.service = NSNetService(domain: "local", type: self.type, name: self.UUID, port: port)
    }
    
    func publishAndFulfillExpectation(expectation: XCTestExpectation) {
        self.publicationDelegate = ServicePublicationDelegate(expectation: expectation)
        self.service.delegate = self.publicationDelegate
        self.service.publish()
    }

    func stopAndFulfillExpectation(expectation: XCTestExpectation) {
        self.publicationDelegate = ServicePublicationDelegate(expectation: expectation)
        self.service.delegate = self.publicationDelegate
        self.service.stop()
    }
    
    func discoverAndFulfillExpectation(expectation: XCTestExpectation) {
        self.browser = NSNetServiceBrowser()
        self.discoveryDelegate = BrowserDelegate()
        self.browser!.delegate = self.discoveryDelegate
        self.discoveryDelegate?.servicesSignal.observeNext({ (services: [NSNetService]) -> () in
            if let theService = services.filter({ $0.name == self.UUID }).first {
                NSLog("Did discover service: \(theService)")
                expectation.fulfill()
            }
        })
        self.browser!.searchForServicesOfType(self.type, inDomain: "local")
    }
}


class BrowserDelegateTests: XCTestCase {
    
    override func setUp() {
        super.setUp()
    }
    
    override func tearDown() {
        super.tearDown()
    }
    
    func testBrowserDelegateDiscovery() {
        // We're going to publish a net service and see if we find it.
        let myTestService = TestService(port: 2015)
        let expectation = self.expectationWithDescription("published")
        myTestService.publishAndFulfillExpectation(expectation)
        self.waitForExpectationsWithTimeout(2.5, handler: nil)
        // Getting this far means that the service was created and published successfully.
        
        // Next, let's start up a browser and try to find the service.
        let expectation2 = self.expectationWithDescription("found")
        myTestService.discoverAndFulfillExpectation(expectation2)
        self.waitForExpectationsWithTimeout(2.5, handler: nil)
        // Passing means that the service was successfully discovered.
    }
   
    
}<commit_msg>Adds some tests for multiple services.<commit_after>//
//  BrowserDelegateTests.swift
//  SwiftNetService
//
//  Created by <NAME> on 11/3/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import XCTest
import ReactiveCocoa

@testable import SwiftNetService

class ServicePublicationDelegate : NSObject, NSNetServiceDelegate {

    var expectation : XCTestExpectation
    
    init(expectation : XCTestExpectation) {
        self.expectation = expectation
    }

    @objc func netService(sender: NSNetService, didNotPublish errorDict: [String : NSNumber]) {
        XCTFail("Failed to publish service: \(errorForErrorDictionary(errorDict))")
    }

    @objc func netServiceDidPublish(sender: NSNetService) {
        self.expectation.fulfill()
        NSLog("Did publish service: \(sender)")
    }
    
    @objc func netServiceDidStop(sender: NSNetService) {
        NSLog("Did stop publishing service: \(sender)")
    }

    @objc func netServiceWillPublish(sender: NSNetService) {
        NSLog("Will publish service: \(sender)")
    }

}

class TestService {
    var UUID : String
    var type : String
    var service : NSNetService
    var publicationDelegate : ServicePublicationDelegate?
    var browser : NSNetServiceBrowser?
    var discoveryDelegate : BrowserDelegate?
    
    init(port : Int32) {
        self.UUID = NSUUID().UUIDString
        self.type = "_\(self.UUID)._tcp"
        self.service = NSNetService(domain: "local", type: self.type, name: self.UUID, port: port)
    }
    
    func publishAndFulfillExpectation(expectation: XCTestExpectation) {
        self.publicationDelegate = ServicePublicationDelegate(expectation: expectation)
        self.service.delegate = self.publicationDelegate
        self.service.publish()
    }

    func stopAndFulfillExpectation(expectation: XCTestExpectation) {
        self.publicationDelegate = ServicePublicationDelegate(expectation: expectation)
        self.service.delegate = self.publicationDelegate
        self.service.stop()
    }
    
    func discoverAndFulfillExpectation(expectation: XCTestExpectation) {
        self.browser = NSNetServiceBrowser()
        self.discoveryDelegate = BrowserDelegate()
        self.browser!.delegate = self.discoveryDelegate
        self.discoveryDelegate?.servicesSignal.observeNext({ (services: [NSNetService]) -> () in
            if let theService = services.filter({ $0.name == self.UUID }).first {
                NSLog("Did discover service: \(theService)")
                expectation.fulfill()
            }
        })
        self.browser!.searchForServicesOfType(self.type, inDomain: "local")
    }
}


class BrowserDelegateTests: XCTestCase {
    
    override func setUp() {
        super.setUp()
    }
    
    override func tearDown() {
        super.tearDown()
    }
    
    func testBrowserDelegateDiscovery() {
        // We're going to publish a net service and see if we find it.
        
        let myTestService = TestService(port: 2015)
        let expectation = self.expectationWithDescription("published")
        myTestService.publishAndFulfillExpectation(expectation)
        self.waitForExpectationsWithTimeout(2.5, handler: nil)
        // Getting this far means that the service was created and published successfully.
        
        // Next, let's start up a browser and try to find the service.
        let expectation2 = self.expectationWithDescription("found")
        myTestService.discoverAndFulfillExpectation(expectation2)
        self.waitForExpectationsWithTimeout(2.5, handler: nil)
        // Passing means that the service was successfully discovered.
    }
   
    func testMultipleBrowserDelegateDiscovery() {
        // We're going to publish several net services and see if we find them.
        
        var myTestServices : [TestService] = []
        
        for index in 0...10 {
            myTestServices.append(TestService(port: 2015+index))
        }
        let expectations = myTestServices.map { (service: TestService) -> XCTestExpectation in
            return self.expectationWithDescription("published service \(service.UUID)")
        }
        for index in 0...myTestServices.count-1 {
            myTestServices[index].publishAndFulfillExpectation(expectations[index])
        }
        self.waitForExpectationsWithTimeout(2.5, handler: nil)
        // Getting this far means that the services were created and published successfully.

        // Next, let's start up browsers and try to find the services.
        let expectations2 = myTestServices.map { (service: TestService) -> XCTestExpectation in
            return self.expectationWithDescription("found service \(service.UUID)")
        }
        for index in 0...myTestServices.count-1 {
            myTestServices[index].discoverAndFulfillExpectation(expectations2[index])
        }
        self.waitForExpectationsWithTimeout(2.5, handler: nil)
        // Passing means that the services were successfully discovered.
    }
    
}
{'fraction_non_alphanumeric': 0.09002915451895044, 'fraction_numerical': 0.007113702623906705, 'mean_word_length': 2.856115107913669, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '817403', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2308, 'n_words': 556}