<commit_before>//
//  CalendarServiceTests.swift
//  Eurofurence
//
//  Created by ShezHsky on 05/07/2017.
//  Copyright © 2017 Eurofurence. All rights reserved.
//

@testable import Eurofurence
import XCTest

protocol CalendarPort {
    
    var isAuthorizedForEventsAccess: Bool { get }

    func requestAccessToEvents(completionHandler: (Bool) -> Void)
    func makeEvent() -> CalendarEvent
    func save(event: CalendarEvent)

}

protocol CalendarEvent {
    
}

class CapturingCalendarPort: CalendarPort {
    
    var isAuthorizedForEventsAccess: Bool {
        get {
            return false
        }
    }

    private(set) var wasAskedForEventsPermissions = false
    func requestAccessToEvents(completionHandler: (Bool) -> Void) {
        wasAskedForEventsPermissions = true
    }
    
    private(set) var didMakeEvent = false
    var createdEvent: CalendarEvent = StubCalendarEvent()
    func makeEvent() -> CalendarEvent {
        didMakeEvent = true
        return createdEvent
    }
    
    private(set) var savedEvent: CalendarEvent?
    func save(event: CalendarEvent) {
        savedEvent = event
    }

}

class StubCalendarEvent: CalendarEvent {
    
}

class AuthorizedCalendarPort: CapturingCalendarPort {
    
    override var isAuthorizedForEventsAccess: Bool {
        get {
            return true
        }
    }
    
}

class UnauthorizedCalendarPort: CapturingCalendarPort {
    
    override var isAuthorizedForEventsAccess: Bool {
        get {
            return false
        }
    }
    
}

class PermissionGrantingCalendarPort: CapturingCalendarPort {
    
    override func requestAccessToEvents(completionHandler: (Bool) -> Void) {
        super.requestAccessToEvents(completionHandler: completionHandler)
        completionHandler(true)
    }
    
}

class PermissionDenyingCalendarPort: CapturingCalendarPort {
    
    override func requestAccessToEvents(completionHandler: (Bool) -> Void) {
        super.requestAccessToEvents(completionHandler: completionHandler)
        completionHandler(false)
    }
    
}

class CalendarService {

    private let calendar: CalendarPort

    init(calendar: CalendarPort) {
        self.calendar = calendar
    }

    func add(event: Any) {
        if calendar.isAuthorizedForEventsAccess {
            let calendarEvent = calendar.makeEvent()
            calendar.save(event: calendarEvent)
        }
        else {
            calendar.requestAccessToEvents() { authorized in
                if authorized {
                    _ = calendar.makeEvent()
                }
            }
        }
    }

}

class CalendarServiceTests: XCTestCase {
    
    func testAddingEventShouldRequestEventsPermissionsFromTheCalendar() {
        let capturingCalendar = CapturingCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)

        XCTAssertTrue(capturingCalendar.wasAskedForEventsPermissions)
    }

    func testEventsPermissionsShouldNotBeRequestedUntilAttemptingToAddEvent() {
        let capturingCalendar = CapturingCalendarPort()
        _ = CalendarService(calendar: capturingCalendar)

        XCTAssertFalse(capturingCalendar.wasAskedForEventsPermissions)
    }
    
    func testEventsPermissionsShouldNotBeRequestedWhenAddingEventWhenTheCalendarIndicatesWeAreAlreadyAuthorized() {
        let capturingCalendar = AuthorizedCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)
        
        XCTAssertFalse(capturingCalendar.wasAskedForEventsPermissions)
    }
    
    func testHavingEventsPermissionsWhenAddingEventShouldRequestCreationOfCalendarEvent() {
        let capturingCalendar = AuthorizedCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)
        
        XCTAssertTrue(capturingCalendar.didMakeEvent)
    }
    
    func testHavingEventsPermissionsWhenAddingEventShouldTellTheCalendarToSaveItsCreatedEvent() {
        let capturingCalendar = AuthorizedCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        let stubbedCalendarEvent = StubCalendarEvent()
        capturingCalendar.createdEvent = stubbedCalendarEvent
        service.add(event: event)
        
        XCTAssertTrue(stubbedCalendarEvent === capturingCalendar.savedEvent as? StubCalendarEvent)
    }
    
    func testNotHavingEventsPermissionsShouldNotMakeEventFromCalendar() {
        let capturingCalendar = UnauthorizedCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)
        
        XCTAssertFalse(capturingCalendar.didMakeEvent)
    }
    
    func testRequestingPermissionWhenAddingEventThenBeingGrantedItShouldMakeTheEvent() {
        let capturingCalendar = PermissionGrantingCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)
        
        XCTAssertTrue(capturingCalendar.didMakeEvent)
    }
    
    func testRequestingPermissionWhenAddingEventThenBeingDeniedItShouldNotMakeTheEvent() {
        let capturingCalendar = PermissionDenyingCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)
        
        XCTAssertFalse(capturingCalendar.didMakeEvent)
    }
    
}<commit_msg>Save inserted events into as permissions granted<commit_after>//
//  CalendarServiceTests.swift
//  Eurofurence
//
//  Created by ShezHsky on 05/07/2017.
//  Copyright © 2017 Eurofurence. All rights reserved.
//

@testable import Eurofurence
import XCTest

protocol CalendarPort {
    
    var isAuthorizedForEventsAccess: Bool { get }

    func requestAccessToEvents(completionHandler: (Bool) -> Void)
    func makeEvent() -> CalendarEvent
    func save(event: CalendarEvent)

}

protocol CalendarEvent {
    
}

class CapturingCalendarPort: CalendarPort {
    
    var isAuthorizedForEventsAccess: Bool {
        get {
            return false
        }
    }

    private(set) var wasAskedForEventsPermissions = false
    func requestAccessToEvents(completionHandler: (Bool) -> Void) {
        wasAskedForEventsPermissions = true
    }
    
    private(set) var didMakeEvent = false
    var createdEvent: CalendarEvent = StubCalendarEvent()
    func makeEvent() -> CalendarEvent {
        didMakeEvent = true
        return createdEvent
    }
    
    private(set) var savedEvent: CalendarEvent?
    func save(event: CalendarEvent) {
        savedEvent = event
    }

}

class StubCalendarEvent: CalendarEvent {
    
}

class AuthorizedCalendarPort: CapturingCalendarPort {
    
    override var isAuthorizedForEventsAccess: Bool {
        get {
            return true
        }
    }
    
}

class UnauthorizedCalendarPort: CapturingCalendarPort {
    
    override var isAuthorizedForEventsAccess: Bool {
        get {
            return false
        }
    }
    
}

class PermissionGrantingCalendarPort: CapturingCalendarPort {
    
    override func requestAccessToEvents(completionHandler: (Bool) -> Void) {
        super.requestAccessToEvents(completionHandler: completionHandler)
        completionHandler(true)
    }
    
}

class PermissionDenyingCalendarPort: CapturingCalendarPort {
    
    override func requestAccessToEvents(completionHandler: (Bool) -> Void) {
        super.requestAccessToEvents(completionHandler: completionHandler)
        completionHandler(false)
    }
    
}

class CalendarService {

    private let calendar: CalendarPort

    init(calendar: CalendarPort) {
        self.calendar = calendar
    }

    func add(event: Any) {
        if calendar.isAuthorizedForEventsAccess {
            makeAndInsertEvent(event)
        }
        else {
            calendar.requestAccessToEvents() { authorized in
                if authorized {
                    makeAndInsertEvent(event)
                }
            }
        }
    }
    
    private func makeAndInsertEvent(_ event: Any) {
        let calendarEvent = calendar.makeEvent()
        calendar.save(event: calendarEvent)
    }

}

class CalendarServiceTests: XCTestCase {
    
    func testAddingEventShouldRequestEventsPermissionsFromTheCalendar() {
        let capturingCalendar = CapturingCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)

        XCTAssertTrue(capturingCalendar.wasAskedForEventsPermissions)
    }

    func testEventsPermissionsShouldNotBeRequestedUntilAttemptingToAddEvent() {
        let capturingCalendar = CapturingCalendarPort()
        _ = CalendarService(calendar: capturingCalendar)

        XCTAssertFalse(capturingCalendar.wasAskedForEventsPermissions)
    }
    
    func testEventsPermissionsShouldNotBeRequestedWhenAddingEventWhenTheCalendarIndicatesWeAreAlreadyAuthorized() {
        let capturingCalendar = AuthorizedCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)
        
        XCTAssertFalse(capturingCalendar.wasAskedForEventsPermissions)
    }
    
    func testHavingEventsPermissionsWhenAddingEventShouldRequestCreationOfCalendarEvent() {
        let capturingCalendar = AuthorizedCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)
        
        XCTAssertTrue(capturingCalendar.didMakeEvent)
    }
    
    func testHavingEventsPermissionsWhenAddingEventShouldTellTheCalendarToSaveItsCreatedEvent() {
        let capturingCalendar = AuthorizedCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        let stubbedCalendarEvent = StubCalendarEvent()
        capturingCalendar.createdEvent = stubbedCalendarEvent
        service.add(event: event)
        
        XCTAssertTrue(stubbedCalendarEvent === capturingCalendar.savedEvent as? StubCalendarEvent)
    }
    
    func testNotHavingEventsPermissionsShouldNotMakeEventFromCalendar() {
        let capturingCalendar = UnauthorizedCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)
        
        XCTAssertFalse(capturingCalendar.didMakeEvent)
    }
    
    func testRequestingPermissionWhenAddingEventThenBeingGrantedItShouldMakeTheEvent() {
        let capturingCalendar = PermissionGrantingCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)
        
        XCTAssertTrue(capturingCalendar.didMakeEvent)
    }
    
    func testRequestingPermissionWhenAddingEventThenBeingDeniedItShouldNotMakeTheEvent() {
        let capturingCalendar = PermissionDenyingCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        service.add(event: event)
        
        XCTAssertFalse(capturingCalendar.didMakeEvent)
    }
    
    func testRequestingPermissionWhenAddingEventThenBeingGrantedItShouldSaveTheCreatedEvent() {
        let capturingCalendar = PermissionGrantingCalendarPort()
        let service = CalendarService(calendar: capturingCalendar)
        let event = Event()
        let stubbedCalendarEvent = StubCalendarEvent()
        capturingCalendar.createdEvent = stubbedCalendarEvent
        service.add(event: event)
        
        XCTAssertTrue(stubbedCalendarEvent === capturingCalendar.savedEvent as? StubCalendarEvent)
    }
    
}
{'fraction_non_alphanumeric': 0.06358479383625434, 'fraction_numerical': 0.002032004064008128, 'mean_word_length': 2.949180875961217, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '3006298', 'n_tokens_mistral': 3147, 'n_tokens_neox': 2927, 'n_words': 589}