Skip to content

Instantly share code, notes, and snippets.

@markryall
Created August 28, 2015 12:51
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 markryall/6da5f8a52eb0d311f489 to your computer and use it in GitHub Desktop.
Save markryall/6da5f8a52eb0d311f489 to your computer and use it in GitHub Desktop.
defmodule Madlib do
@default_io :erlang.group_leader
def main(_, device \\ @default_io) do
noun = prompt device, "noun"
verb = prompt device, "verb"
adjective = prompt device, "adjective"
adverb = prompt device, "adverb"
sentence = assemble(noun: noun, verb: verb, adjective: adjective, adverb: adverb)
IO.puts(device, sentence)
end
def prompt(device \\ @default_io, message) do
IO.gets(device, "Enter a #{message}: ")
|> String.strip
end
def assemble noun: noun, verb: verb, adjective: adjective, adverb: adverb do
"Do you #{verb} your #{adjective} #{noun} #{adverb}?"
end
end
defmodule MadlibTest do
use ExUnit.Case
test "prompt asks me for stuff?" do
{:ok, pid} = StringIO.open("something I entered\n", capture_prompt: true)
assert Madlib.prompt(pid, "prompt") == "something I entered"
{input, output} = StringIO.contents(pid)
assert input == ""
assert output == "Enter a prompt: "
end
test "assemble sentence" do
assert "Do you verb your adjective noun adverb" ==
Madlib.assemble noun: "noun", verb: "verb", adjective: "adjective", adverb: "adverb"
end
test "main asks for many things" do
input = """
dog
walk
blue
quickly
"""
{:ok, pid} = StringIO.open(input)
Madlib.main [], pid
{input, output} = StringIO.contents(pid)
assert input == ""
assert output == "Do you walk your blue dog quickly?\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment