-
Notifications
You must be signed in to change notification settings - Fork 0
/
input-output.exs
49 lines (38 loc) · 993 Bytes
/
input-output.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
defmodule CowInterrogator do
def get_name do
IO.gets("What is your name? ")
|> String.trim
end
def get_cow_lover do
IO.getn("Do you like cows? [y|n] ", 1)
end
def interrogate do
name = get_name()
case String.downcase(get_cow_lover()) do
"y" ->
IO.puts "Great! Here's a cow for you #{name}"
IO.puts cow_art()
"n" ->
IO.puts "That's a shame, #{name}."
_ ->
IO.puts "You should have entered 'y' or 'n'."
end
end
def cow_art do
path = Path.expand("support/cow.txt", __DIR__)
case File.read(path) do
{:ok, art} -> art
{:error, _} -> IO.puts "Error: cow.txt file not found"; System.halt(1)
end
end
ExUnit.start
defmodule InputOutPutTest do
use ExUnit.Case
import String
test "checks if cow art returns string from support/cow.txt" do
art = CowInterrogator.cow_art()
assert trim(art) |> first == "("
end
end
end
CowInterrogator.interrogate