Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
iss #22: hashable AccountSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
maizy committed Aug 15, 2014
1 parent 6b7ca20 commit bdb313f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/hedgehog/models/Sources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ class Sources(configuration: Configuration) {
class AccountSettings(
val account: Account,
val includePrivateRepos: Boolean = false)
{

def canEqual(other: Any): Boolean = other.isInstanceOf[AccountSettings]

override def equals(other: Any): Boolean = other match {
case that: AccountSettings =>
(that canEqual this) &&
account == that.account &&
includePrivateRepos == that.includePrivateRepos
case _ => false
}

override def hashCode(): Int =
Seq(account, includePrivateRepos).map(_.hashCode).foldLeft(0)((a, b) => 31 * a + b)
}


object AccountSettings {
Expand Down
23 changes: 23 additions & 0 deletions scripts/console.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// load this script in `play console` via
// :load scripts/console.scala
val app = new play.core.StaticApplication(new java.io.File("."))
import play.api.Play.current
val conf = current.configuration

import hedgehog.models._
val userAccount = GithubUser("user", Some("http://example.com/u.png"))
val orgAccount = GithubOrg("org", Some("http://example.com/o.png"))

val userAccountSettings = new AccountSettings(userAccount, includePrivateRepos = false)
val orgAccountSettings = new AccountSettings(orgAccount, includePrivateRepos = true)
val accountSettings = userAccountSettings :: orgAccountSettings :: Nil

val userRepoA = new Repo("a", userAccount, Some("some description a"))
val userRepoB = new Repo("b", userAccount, Some("some description b"))
val userRepos = userRepoA :: userRepoB :: Nil

val orgRepoZ = new Repo("z", orgAccount, Some("some description z"), isPrivate = Some(true))
val orgRepoY = new Repo("y", userAccount, Some("some description y"), isPrivate = Some(false))
val orgRepos = orgRepoZ :: orgRepoY :: Nil

val repos = userRepos ++ userRepos
27 changes: 27 additions & 0 deletions test/models/AccountSettingsSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test

import org.scalatest._

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

import hedgehog.models.{GithubUser, AccountSettings}

/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2014
* See LICENSE.txt for details.
*/
@RunWith(classOf[JUnitRunner])
class AccountSettingsSuite extends FunSuite with Matchers {

test("hash equals for the same content") {
val user1 = GithubUser("user")
val user2 = GithubUser("user")
assert(user1.hashCode === user2.hashCode)
val accountSettings1 = new AccountSettings(user1, true)
val accountSettings2 = new AccountSettings(user2, true)
assert(accountSettings1.hashCode === accountSettings2.hashCode)
val accountSettings3 = new AccountSettings(user1, false)
assert(accountSettings1.hashCode !== accountSettings3.hashCode)
}
}

0 comments on commit bdb313f

Please sign in to comment.