mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-25 17:30:59 +00:00
Test bad inputs
This commit is contained in:
parent
816eb78064
commit
803f0b52cc
2 changed files with 20 additions and 4 deletions
|
@ -36,11 +36,19 @@ defmodule LiveBeats.MP3Stat do
|
||||||
def parse(path) do
|
def parse(path) do
|
||||||
{tag_info, rest} = parse_tag(File.read!(path))
|
{tag_info, rest} = parse_tag(File.read!(path))
|
||||||
duration = parse_frame(rest, 0, 0, 0)
|
duration = parse_frame(rest, 0, 0, 0)
|
||||||
|
|
||||||
|
case duration do
|
||||||
|
duration when is_float(duration) and duration > 0 ->
|
||||||
title = Enum.at(tag_info["TIT2"] || [], 0)
|
title = Enum.at(tag_info["TIT2"] || [], 0)
|
||||||
artist = Enum.at(tag_info["TPE1"] || [], 0)
|
artist = Enum.at(tag_info["TPE1"] || [], 0)
|
||||||
|
seconds = round(duration)
|
||||||
|
|
||||||
{:ok,
|
{:ok,
|
||||||
%MP3Stat{duration: round(duration), path: path, tags: tag_info, title: title, artist: artist}}
|
%MP3Stat{duration: seconds, path: path, tags: tag_info, title: title, artist: artist}}
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
{:error, :bad_file}
|
||||||
|
end
|
||||||
rescue
|
rescue
|
||||||
_ -> {:error, :bad_file}
|
_ -> {:error, :bad_file}
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,4 +9,12 @@ defmodule LiveBeats.MP3StatTest do
|
||||||
assert stat.title == "Silence"
|
assert stat.title == "Silence"
|
||||||
assert stat.artist == "Anon"
|
assert stat.artist == "Anon"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "parse/1 with invalid mp3" do
|
||||||
|
assert {:error, :bad_file} = MP3Stat.parse("mix.exs")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "parse/1 with missing file" do
|
||||||
|
assert {:error, :bad_file} = MP3Stat.parse("lsfjslkfjslkfjs")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue