Skip to content

Instantly share code, notes, and snippets.

@danott
Created March 18, 2017 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danott/8be41b71a445807d5a08966cf16816ae to your computer and use it in GitHub Desktop.
Save danott/8be41b71a445807d5a08966cf16816ae to your computer and use it in GitHub Desktop.
TDD'ing a Riddle
require "minitest/autorun"
def yes
true
end
def no
false
end
class Prince
attr_reader :tell_the_truth
def initialize(tell_the_truth:)
@tell_the_truth = Array(tell_the_truth)
end
def handle_a_yes_no_statement(yes_or_no)
handler = tell_the_truth.sample
yes_or_no ? handler : !handler
end
def deterministic?
tell_the_truth.count == 1
end
end
def ask(askee, about:)
askee
end
class MarriageTest < MiniTest::Test
def test_all_the_questions
youngest = Prince.new(tell_the_truth: no)
middle = Prince.new(tell_the_truth: [yes, no])
oldest = Prince.new(tell_the_truth: [yes])
assert_predicate ask(youngest, about: [middle, oldest]), :deterministic?
assert_predicate ask(youngest, about: [oldest, middle]), :deterministic?
assert_predicate ask(middle, about: [youngest, oldest]), :deterministic?
assert_predicate ask(middle, about: [oldest, youngest]), :deterministic?
assert_predicate ask(oldest, about: [youngest, middle]), :deterministic?
assert_predicate ask(oldest, about: [middle, youngest]), :deterministic?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment