Created
March 18, 2017 20:16
-
-
Save danott/8be41b71a445807d5a08966cf16816ae to your computer and use it in GitHub Desktop.
TDD'ing a Riddle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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