Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Fedorov waterlink

View GitHub Profile
@waterlink
waterlink / devise.rb
Created Mar 16, 2014
Rspec Devise helper with support for routing :authenticated and :unauthenticated tests
View devise.rb
# spec/support/devise.rb
module Devise::RoutingTestHelpers
attr_accessor :request
def initialize(*args)
@request ||= Hashie::Mash.new env: {}
super(*args)
end
View FetcherTwo.kt
describe("FetcherTwo") {
lateinit var asyncQueue: AsyncQueue
lateinit var fetcher: FetcherTwo
val client = Client()
beforeEachTest {
asyncQueue = AsyncQueue()
fetcher = FetcherTwo(asyncQueue, client)
}
View Game.kt
fun play(first: Throw, second: Throw): Winner {
if (first == second) {
return TIE
}
return when (first vs second) {
// classic rules
SCISSORS beats PAPER,
PAPER beats ROCK,
ROCK beats SCISSORS -> FIRST_PLAYER
View Game.kt
return when (first to second) {
// classic rules
SCISSORS to PAPER,
PAPER to ROCK,
ROCK to SCISSORS -> FIRST_PLAYER
// additional modern rules
ROCK to LIZARD,
LIZARD to SPOCK,
SPOCK to SCISSORS,
View Game.kt
fun play(first: Throw, second: Throw): Winner {
if (first == second) {
return TIE
}
return when (first to second) {
SCISSORS to PAPER,
PAPER to ROCK,
ROCK to SCISSORS -> FIRST_PLAYER
View Game.kt
val firstWins = when (first to second) {
SCISSORS to PAPER,
PAPER to ROCK,
ROCK to SCISSORS -> true
else -> false
}
View GameRules.kt
private fun rules(first: Throw,
second: Throw,
block: GameRules.() -> Boolean) =
GameRules(first, second).run(block)
class GameRules(private val first: Throw,
private val second: Throw) {
infix fun Throw.beats(other: Throw) =
first == this && second == other
}
View Game.kt
val firstWins = rules(first, second) {
SCISSORS beats PAPER ||
PAPER beats ROCK ||
ROCK beats SCISSORS
}
if (firstWins) {
return FIRST_PLAYER
}
View Game.kt
val scissorsBeatPaper = first == SCISSORS && second == PAPER
val paperBeatsRock = first == PAPER && second == ROCK
val rockBeatsScissors = first == ROCK && second == SCISSORS
if (scissorsBeatPaper || paperBeatsRock || rockBeatsScissors) {
return FIRST_PLAYER
}
View Game.kt
if (first == SCISSORS && second != ROCK ||
first == PAPER && second != SCISSORS ||
first == ROCK && second != PAPER) {
return FIRST_PLAYER
}