From 8105d68102d91d99e19e85c4fb41b04c09547235 Mon Sep 17 00:00:00 2001 From: Samuel Owen Date: Thu, 21 Dec 2023 23:55:33 +0000 Subject: [PATCH] Pass test: "opens file and writes each line to a list of strings" (Remember that filepaths in Elixir projects are from the root and are not relative to the source code reading the file) --- lib/day1/day1.ex | 9 +++++++++ lib/day1/day1_test.exs | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/day1/day1.ex b/lib/day1/day1.ex index 0e8d119..3518b2f 100644 --- a/lib/day1/day1.ex +++ b/lib/day1/day1.ex @@ -30,4 +30,13 @@ defmodule Day1 do {List.first(numbers), List.last(numbers)} end + + def parse_file(file_path, return_type) do + case return_type do + :string -> + file_path + |> File.read!() + |> String.split("\n", trim: true) + end + end end diff --git a/lib/day1/day1_test.exs b/lib/day1/day1_test.exs index fcdf272..323b9c2 100644 --- a/lib/day1/day1_test.exs +++ b/lib/day1/day1_test.exs @@ -7,7 +7,7 @@ defmodule Day1Test do end test "opens file and writes each line to a list of strings" do - assert Day1.parse_file("test/day1_test.txt", :string) == [ + assert Day1.parse_file("./lib/day1/mocks/lines_of_text.txt", :string) == [ "first line of the file", "second line of the file", "third line of the file"