<commit_before>import XCTest
import Tailor

class Event: Mappable {
  var name: String = ""

  required init(_ map: [String : Any]) {
    self.name <- map.property("name")
  }
}

class PushEvent: Event {
  var SHA: String = ""

  required init(_ map: [String : Any]) {
    super.init(map)

    self.SHA <- map.property("sha")
  }
}

class IssueEvent: Event {
  var number: Int = 0

  required init(_ map: [String : Any]) {
    super.init(map)

    self.number <- map.property("number")
  }
}

extension Event: HierarchyType {
  static func cluster(_ map: [String : Any]) -> AnyObject {
    let kinds: [String: Event.Type] = [
      "push": PushEvent.self,
      "issue": IssueEvent.self
    ]

    if let kind = map["type"] as? String, let type = kinds[kind] {
      return type.init(map)
    } else {
      return self.init(map)
    }
  }
}

class Notification: Mappable {
  var events: [Event] = []

  required init(_ map: [String : Any]) {
    self.events <- map.relationsHierarchically("events")
  }
}

class TestHierarchyType: XCTestCase {
  func testHierarchyType() {
    let json = [
      "events": [
        [
          "type": "push",
          "name": "Update README",
          "sha": "a8037a7bda800c51ee1aae557729a9b16e8e57fe"
        ],
        [
          "type": "issue",
          "name": "Add HierarchyType",
          "number": 3
        ]
      ]
    ]

    let notification = Notification(json as [String : Any])

    let push = notification.events[0] as! PushEvent
    XCTAssertEqual(push.name, "Update README")
    XCTAssertEqual(push.SHA, "a8037a7bda800c51ee1aae557729a9b16e8e57fe")

    let issue = notification.events[1] as! IssueEvent
    XCTAssertEqual(issue.name, "Add HierarchyType")
    XCTAssertEqual(issue.number, 3)
  }
}<commit_msg>Hierarchy Type<commit_after>import Quick
import Nimble
import Tailor

class Event: Mappable {
  var name: String = ""

  required init(_ map: [String : Any]) {
    self.name <- map.property("name")
  }
}

class PushEvent: Event {
  var SHA: String = ""

  required init(_ map: [String : Any]) {
    super.init(map)

    self.SHA <- map.property("sha")
  }
}

class IssueEvent: Event {
  var number: Int = 0

  required init(_ map: [String : Any]) {
    super.init(map)

    self.number <- map.property("number")
  }
}

extension Event: HierarchyType {
  static func cluster(_ map: [String : Any]) -> AnyObject {
    let kinds: [String: Event.Type] = [
      "push": PushEvent.self,
      "issue": IssueEvent.self
    ]

    if let kind = map["type"] as? String, let type = kinds[kind] {
      return type.init(map)
    } else {
      return self.init(map)
    }
  }
}

class Notification: Mappable {
  var events: [Event] = []

  required init(_ map: [String : Any]) {
    self.events <- map.relationsHierarchically("events")
  }
}

class TestHierarchyType: QuickSpec {
    override func spec() {

        describe("hierarchy type") {

            var json: [String : Any]!
            var notification: Notification!
            var issue: IssueEvent!
            var push: PushEvent!

            beforeEach {
                json = [
                    "events": [
                        [
                            "type": "push",
                            "name": "Update README",
                            "sha": "a8037a7bda800c51ee1aae557729a9b16e8e57fe"
                        ],
                        [
                            "type": "issue",
                            "name": "Add HierarchyType",
                            "number": 3
                        ]
                    ]
                ]
                notification = Notification(json as [String : Any])
                push = notification.events[0] as! PushEvent
                issue = notification.events[1] as! IssueEvent
            }

            context("push") {

                it("has the correct push name") {
                    expect(push.name).to(equal("Update README"))
                }

                it("has the correct push SHA") {
                    expect(push.SHA).to(equal("a8037a7bda800c51ee1aae557729a9b16e8e57fe"))
                }

            }

            context("issue") {

                it("has the correct issue name") {
                    expect(issue.name).to(equal("Add HierarchyType"))
                }

                it("has the correct issue number") {
                    expect(issue.number).to(equal(3))
                }

            }

        }

    }
}
{'fraction_non_alphanumeric': 0.12363067292644757, 'fraction_numerical': 0.022803487592219986, 'mean_word_length': 1.7549261083743843, 'pattern_counts': {'":': 18, '<': 11, '<?xml version=': 0, '>': 5, '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': '515571', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1341, 'n_words': 312}