<commit_before>//
//  AddTripSceneSpec.swift
//  TripPlanner
//
//  Created by <NAME> on 7/28/15.
//  Copyright © 2015 Make School. All rights reserved.
//

import Quick
import Nimble
import UIKit
import Result

@testable import TripPlanner

class AddTripViewControllerSpec: QuickSpec {
  
  override func spec() {
    
    describe("AddTripViewController") {
      var addTripViewController: AddTripViewController!
      
      beforeEach {
        addTripViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("AddTripViewController") as! AddTripViewController
        // force view to be loaded, so we can check for outlets
        let _ = addTripViewController.view
      }
      
      describe("after loading") {
        it("has outlets set up correctly") {
          expect(addTripViewController.tableView).notTo(beNil())
          expect(addTripViewController.mapView).notTo(beNil())
        }
      }
      
      describe("search bar") {
        it("accesses the LocationSearch when a search term is entered") {
          class LocationSearchMock: LocationSearch {
            var searchedTerm: String?
            
            override func findPlaces(searchString: String, callback: LocationSearchCallback) {
                searchedTerm = searchString
            }
          }
          
          let locationSearchMock = LocationSearchMock()
          addTripViewController.locationSearch = locationSearchMock
          addTripViewController.searchBar(UISearchBar(), textDidChange: "Stuttgart")
          
          expect(locationSearchMock.searchedTerm!).to(equal("Stuttgart"))
        }
        
        it("resets the search results without calling API when textfield is blank") {
          class LocationSearchMock: LocationSearch {
            var called = false
            
            override func findPlaces(searchString: String, callback: LocationSearchCallback) {
              called = true
            }
          }
          
          let locationSearchMock = LocationSearchMock()
          addTripViewController.locationSearch = locationSearchMock
          addTripViewController.searchBar(UISearchBar(), textDidChange: "")
          
          expect(locationSearchMock.called).to(beFalse())
          expect(addTripViewController.locations).to(beEmpty())
        }
      }
      
      describe("handleSearchResult") {
        it("stores the location results upon successful request") {
          let place = Place(description: "", id: "", matchedSubstrings: [], placeId: "1", reference: "", terms: [], types: [])
          let predictions = Predictions(predictions: [place])
          let result: Result<Predictions, Reason> = .Success(predictions)
          
          addTripViewController.handleSearchResult(result)
          
          expect(addTripViewController.locations[0].placeId).to(equal("1"))
        }
        
        it("invokes the error handler upon a failed request") {
          let result: Result<Predictions, Reason> = .Failure(.NoSuccessStatusCode(statusCode: 404))
          
          class MockErrorHandler: DefaultErrorHandler {
            var called = false;
            
            override func displayErrorMessage(message: String) {
              called = true
            }
          }
          
          let mockErrorHandler = MockErrorHandler()
          
          addTripViewController.errorHandler = mockErrorHandler
          addTripViewController.handleSearchResult(result)
          
          expect(mockErrorHandler.called).to(beTrue())
        }
      }

    }
  }
}
<commit_msg>[Adding View] Add spec for table view<commit_after>//
//  AddTripSceneSpec.swift
//  TripPlanner
//
//  Created by <NAME> on 7/28/15.
//  Copyright © 2015 Make School. All rights reserved.
//

import Quick
import Nimble
import UIKit
import Result

@testable import TripPlanner

class AddTripViewControllerSpec: QuickSpec {
  
  override func spec() {
    
    describe("AddTripViewController") {
      var addTripViewController: AddTripViewController!
      
      beforeEach {
        addTripViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("AddTripViewController") as! AddTripViewController
        // force view to be loaded, so we can check for outlets
        let _ = addTripViewController.view
      }
      
      describe("after loading") {
        it("has outlets set up correctly") {
          expect(addTripViewController.tableView).notTo(beNil())
          expect(addTripViewController.mapView).notTo(beNil())
        }
      }
      
      describe("search bar") {
        it("accesses the LocationSearch when a search term is entered") {
          class LocationSearchMock: LocationSearch {
            var searchedTerm: String?
            
            override func findPlaces(searchString: String, callback: LocationSearchCallback) {
                searchedTerm = searchString
            }
          }
          
          let locationSearchMock = LocationSearchMock()
          addTripViewController.locationSearch = locationSearchMock
          addTripViewController.searchBar(UISearchBar(), textDidChange: "Stuttgart")
          
          expect(locationSearchMock.searchedTerm!).to(equal("Stuttgart"))
        }
        
        it("resets the search results without calling API when textfield is blank") {
          class LocationSearchMock: LocationSearch {
            var called = false
            
            override func findPlaces(searchString: String, callback: LocationSearchCallback) {
              called = true
            }
          }
          
          let locationSearchMock = LocationSearchMock()
          addTripViewController.locationSearch = locationSearchMock
          addTripViewController.searchBar(UISearchBar(), textDidChange: "")
          
          expect(locationSearchMock.called).to(beFalse())
          expect(addTripViewController.locations).to(beEmpty())
        }
      }
      
      describe("handleSearchResult") {
        it("stores the location results upon successful request") {
          let place = Place(description: "", id: "", matchedSubstrings: [], placeId: "1", reference: "", terms: [], types: [])
          let predictions = Predictions(predictions: [place])
          let result: Result<Predictions, Reason> = .Success(predictions)
          
          addTripViewController.handleSearchResult(result)
          
          expect(addTripViewController.locations[0].placeId).to(equal("1"))
        }
        
        it("invokes the error handler upon a failed request") {
          let result: Result<Predictions, Reason> = .Failure(.NoSuccessStatusCode(statusCode: 404))
          
          class MockErrorHandler: DefaultErrorHandler {
            var called = false;
            
            override func displayErrorMessage(message: String) {
              called = true
            }
          }
          
          let mockErrorHandler = MockErrorHandler()
          
          addTripViewController.errorHandler = mockErrorHandler
          addTripViewController.handleSearchResult(result)
          
          expect(mockErrorHandler.called).to(beTrue())
        }
      }
      
      describe("tableViewDidSelectRowAtIndexPath") {
        it("requests location details for the selected location") {
          let place = Place(description: "", id: "", matchedSubstrings: [], placeId: "1", reference: "", terms: [], types: [])
          let predictions = Predictions(predictions: [place])
          addTripViewController.locations = predictions.predictions
          
          class MockLocationSearch: LocationSearch {
            var calledWithPlace :Place?
            
            override func detailsForPlace(place: Place, callback: PlacesDetailsCallback) {
              calledWithPlace = place;
            }
          }
          
          let mockLocationSearch = MockLocationSearch()
          addTripViewController.locationSearch = mockLocationSearch
          
          let tableView = addTripViewController.tableView
          addTripViewController.tableView(tableView, didSelectRowAtIndexPath: NSIndexPath(forRow: 0, inSection: 0))
          
          expect(mockLocationSearch.calledWithPlace?.placeId).to(equal("1"))
        }
      }
      
      describe("tableView rows and sections") {
        it("show results from location search") {
          let place = Place(description: "", id: "", matchedSubstrings: [], placeId: "1", reference: "", terms: [], types: [])
          let predictions = Predictions(predictions: [place])
          addTripViewController.locations = predictions.predictions
          
          let rows = addTripViewController.tableView(addTripViewController.tableView, numberOfRowsInSection: 0)
          let cell = addTripViewController.tableView(addTripViewController.tableView, cellForRowAtIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! LocationResultTableViewCell
          
          expect(rows).to(equal(1))
          expect(cell.model).to(equal(place))
        }
      }

    }
  }
}

{'fraction_non_alphanumeric': 0.08731991641922358, 'fraction_numerical': 0.004289013526888816, 'mean_word_length': 2.297316896301668, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4043689', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2199, 'n_words': 495}