Skip to content

Commit

Permalink
Added: doctests for File (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Kapkov authored Jun 19, 2018
1 parent c3a4cf5 commit 5366812
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 61 deletions.
49 changes: 36 additions & 13 deletions lib/faker/datetime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Faker.DateTime do
#=> std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2016,
#=> zone_abbr: "UTC"}
"""
@spec backward(integer) :: DateTime.t
@spec backward(integer) :: DateTime.t()
def backward(days) do
forward(-days)
end
Expand All @@ -30,7 +30,7 @@ defmodule Faker.DateTime do
#=> std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2016,
#=> zone_abbr: "UTC"}
"""
@spec forward(integer) :: DateTime.t
@spec forward(integer) :: DateTime.t()
def forward(days) do
sign = if days < 0, do: -1, else: 1

Expand All @@ -53,33 +53,56 @@ defmodule Faker.DateTime do
#=> std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2016,
#=> zone_abbr: "UTC"}
"""
@spec between(Date.t | NaiveDateTime.t | DateTime.t, Date.t | NaiveDateTime.t | DateTime.t) :: DateTime.t
@spec between(
Date.t() | NaiveDateTime.t() | DateTime.t(),
Date.t() | NaiveDateTime.t() | DateTime.t()
) :: DateTime.t()
def between(%Date{} = from, %Date{} = to) do
between(date_to_datetime(from), date_to_datetime(to))
end

def between(%NaiveDateTime{} = from, %NaiveDateTime{} = to) do
between(naivedatetime_to_datetime(from), naivedatetime_to_datetime(to))
end

def between(from, to) do
unix_between(to_timestamp(from), to_timestamp(to))
end

# private

@spec date_to_datetime(Date.t) :: DateTime.t
@spec date_to_datetime(Date.t()) :: DateTime.t()
defp date_to_datetime(date) do
%DateTime{calendar: Calendar.ISO, day: date.day, hour: 0, minute: 0,
month: date.month, second: 0, time_zone: "Etc/UTC",
utc_offset: 0, std_offset: 0, year: date.year, zone_abbr: "UTC"}
%DateTime{
calendar: Calendar.ISO,
day: date.day,
hour: 0,
minute: 0,
month: date.month,
second: 0,
time_zone: "Etc/UTC",
utc_offset: 0,
std_offset: 0,
year: date.year,
zone_abbr: "UTC"
}
end

@spec naivedatetime_to_datetime(NaiveDateTime.t) :: DateTime.t
@spec naivedatetime_to_datetime(NaiveDateTime.t()) :: DateTime.t()
defp naivedatetime_to_datetime(naivedatetime) do
%DateTime{calendar: naivedatetime.calendar, day: naivedatetime.day,
hour: naivedatetime.hour, minute: naivedatetime.minute,
month: naivedatetime.month, second: naivedatetime.second,
time_zone: "Etc/UTC", utc_offset: 0, std_offset: 0,
year: naivedatetime.year, zone_abbr: "UTC"}
%DateTime{
calendar: naivedatetime.calendar,
day: naivedatetime.day,
hour: naivedatetime.hour,
minute: naivedatetime.minute,
month: naivedatetime.month,
second: naivedatetime.second,
time_zone: "Etc/UTC",
utc_offset: 0,
std_offset: 0,
year: naivedatetime.year,
zone_abbr: "UTC"
}
end

defp to_timestamp(datetime) do
Expand Down
82 changes: 62 additions & 20 deletions lib/faker/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ defmodule Faker.File do
}

@mimes %{
application: ~w(application/atom+xml application/ecmascript application/EDI-X12 application/EDIFACT application/json application/javascript application/ogg application/pdf application/postscript application/rdf+xml application/rss+xml application/soap+xml application/font-woff application/xhtml+xml application/xml-dtd application/xop+xml application/zip application/gzip),
audio: ~w(audio/basic audio/L24 audio/mp4 audio/mpeg audio/ogg audio/vorbis audio/vnd.rn-realaudio audio/vnd.wave audio/webm),
image: ~w(image/gif image/jpeg image/pjpeg image/png image/svg+xml image/tiff image/vnd.microsoft.icon),
application:
~w(application/atom+xml application/ecmascript application/EDI-X12 application/EDIFACT application/json application/javascript application/ogg application/pdf application/postscript application/rdf+xml application/rss+xml application/soap+xml application/font-woff application/xhtml+xml application/xml-dtd application/xop+xml application/zip application/gzip),
audio:
~w(audio/basic audio/L24 audio/mp4 audio/mpeg audio/ogg audio/vorbis audio/vnd.rn-realaudio audio/vnd.wave audio/webm),
image:
~w(image/gif image/jpeg image/pjpeg image/png image/svg+xml image/tiff image/vnd.microsoft.icon),
message: ~w(message/http message/imdn+xml message/partial message/rfc822),
model: ~w(model/example model/iges model/mesh model/vrml model/x3d+binary model/x3d+vrml model/x3d+xml),
multipart: ~w(multipart/mixed multipart/alternative multipart/related multipart/form-data multipart/signed multipart/encrypted),
model:
~w(model/example model/iges model/mesh model/vrml model/x3d+binary model/x3d+vrml model/x3d+xml),
multipart:
~w(multipart/mixed multipart/alternative multipart/related multipart/form-data multipart/signed multipart/encrypted),
text: ~w(text/cmd text/css text/csv text/html text/javascript text/plain text/vcard text/xml),
video: ~w(video/mpeg video/mp4 video/ogg video/quicktime video/webm video/x-matroska video/x-ms-wmv video/x-flv)
video:
~w(video/mpeg video/mp4 video/ogg video/quicktime video/webm video/x-matroska video/x-ms-wmv video/x-flv)
}

@categories_extensions Map.keys(@extensions)
Expand All @@ -33,9 +39,15 @@ defmodule Faker.File do
## Examples
iex> Faker.File.file_extension()
#=> "mp3"
"wav"
iex> Faker.File.file_extension()
"wav"
iex> Faker.File.file_extension()
"doc"
iex> Faker.File.file_extension()
"mov"
"""
@spec file_extension() :: String.t
@spec file_extension() :: String.t()
def file_extension do
@categories_extensions
|> pick
Expand All @@ -49,9 +61,15 @@ defmodule Faker.File do
## Examples
iex> Faker.File.file_extension(:video)
#=> "webm"
"mov"
iex> Faker.File.file_extension(:image)
"tiff"
iex> Faker.File.file_extension(:audio)
"flac"
iex> Faker.File.file_extension(:office)
"xls"
"""
@spec file_extension(:atom) :: String.t
@spec file_extension(:atom) :: String.t()
def file_extension(category) do
category
|> get_extensions_from_category()
Expand All @@ -64,11 +82,17 @@ defmodule Faker.File do
## Examples
iex> Faker.File.file_name()
#=> "voluptaes.jpg"
"aliquam.jpg"
iex> Faker.File.file_name()
"deleniti.doc"
iex> Faker.File.file_name()
"qui.jpg"
iex> Faker.File.file_name()
"quibusdam.csv"
"""
@spec file_name() :: String.t
@spec file_name() :: String.t()
def file_name do
Lorem.word <> "." <> file_extension()
Lorem.word() <> "." <> file_extension()
end

@doc """
Expand All @@ -78,11 +102,17 @@ defmodule Faker.File do
## Examples
iex> Faker.File.file_name(:text)
#=> "reo.json"
"aliquam.txt"
iex> Faker.File.file_name(:video)
"sint.mp4"
iex> Faker.File.file_name(:image)
"consequatur.bmp"
iex> Faker.File.file_name(:audio)
"qui.wav"
"""
@spec file_name(:atom) :: String.t
@spec file_name(:atom) :: String.t()
def file_name(category) do
Lorem.word <> "." <> file_extension(category)
Lorem.word() <> "." <> file_extension(category)
end

@doc """
Expand All @@ -91,9 +121,15 @@ defmodule Faker.File do
## Examples
iex> Faker.File.mime_type()
#=> "application/atom+xml"
"text/css"
iex> Faker.File.mime_type()
"message/http"
iex> Faker.File.mime_type()
"application/ogg"
iex> Faker.File.mime_type()
"model/x3d+xml"
"""
@spec mime_type :: String.t
@spec mime_type :: String.t()
def mime_type do
@categories_mimes
|> pick
Expand All @@ -108,9 +144,15 @@ defmodule Faker.File do
## Examples
iex> Faker.File.mime_type(:image)
#=> "image/gif"
"image/vnd.microsoft.icon"
iex> Faker.File.mime_type(:audio)
"audio/mp4"
iex> Faker.File.mime_type(:application)
"application/xop+xml"
iex> Faker.File.mime_type(:video)
"video/mpeg"
"""
@spec mime_type(:atom) :: String.t
@spec mime_type(:atom) :: String.t()
def mime_type(category) do
category
|> get_mimes_from_category()
Expand Down
29 changes: 2 additions & 27 deletions test/faker/file_test.exs
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
defmodule FileTest do
defmodule Faker.FileTest do
use ExUnit.Case, async: true

test "file_extension/0" do
assert is_binary(Faker.File.file_extension)
end

test "file_extension/1" do
assert is_binary(Faker.File.file_extension(:audio))
end

test "mime_type/0" do
assert is_binary(Faker.File.mime_type)
end

test "mime_type/1" do
assert is_binary(Faker.File.mime_type(:video))
end

test "file_name/0" do
assert is_binary(Faker.File.file_name)
assert String.contains?(Faker.File.file_name, ".")
end

test "file_name/1" do
assert is_binary(Faker.File.file_name(:office))
assert String.contains?(Faker.File.file_name(:office), ".")
end

doctest Faker.File
end
2 changes: 1 addition & 1 deletion test/faker/gov/us_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Gov.UsTest do
defmodule Faker.Gov.UsTest do
use ExUnit.Case, async: true

import Faker.Gov.Us
Expand Down

0 comments on commit 5366812

Please sign in to comment.