From 43267d53e544554c3cb89b8ac114de277a1d8404 Mon Sep 17 00:00:00 2001 From: lowks Date: Tue, 3 Feb 2015 07:28:44 +0800 Subject: [PATCH] ENH doing mv right using defdelegate and adding some 2 test for it --- lib/radpath.ex | 9 +++++++-- test/radpath_test.exs | 43 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/lib/radpath.ex b/lib/radpath.ex index a5f4689..de4fa2d 100644 --- a/lib/radpath.ex +++ b/lib/radpath.ex @@ -122,7 +122,6 @@ defmodule Radpath do Radpath.rename(source, destination) """ - def mv(source, destination) when is_bitstring(source), do: rename(source, destination) @doc """ @@ -137,12 +136,18 @@ defmodule Radpath do Radpath.rename(source, destination) + or + + Radpath.mv(source, destination) + """ def rename(source, destination) when is_bitstring(source) do F.rename(source, destination) end + defdelegate mv(source,destination), to: __MODULE__, as: :rename + @doc """ Gives you back the relative path: @@ -307,4 +312,4 @@ defmodule Radpath do Path.absname(path_str) <> "/" end -end \ No newline at end of file +end diff --git a/test/radpath_test.exs b/test/radpath_test.exs index 16dae60..46dd978 100644 --- a/test/radpath_test.exs +++ b/test/radpath_test.exs @@ -216,7 +216,7 @@ defmodule RadpathTests.RadpathFacts do end end - facts "Test rename" do + facts "Test rename and mv" do fact "Test rename: Normal Usage" do source_file = "/tmp/hoho.txt" @@ -232,13 +232,46 @@ defmodule RadpathTests.RadpathFacts do end end + fact "Test mv: Normal Usage" do + source_file = "/tmp/hoho.txt" + dest_file = "/tmp/hehe.txt" + File.touch!(source_file) + try do + source_file |> path_exists() + Radpath.mv(source_file, dest_file) + source_file |> ! path_exists() + dest_file |> path_exists() + after + File.rm_rf dest_file + end + end + fact "Test rename: Source file does not exist" do source_file = "/tmp/hoho.txt" dest_file = "/tmp/hehe.txt" - source_file |> ! path_exists() - Radpath.rename(source_file, dest_file) - dest_file |> ! path_exists() + try do + source_file |> ! path_exists() + Radpath.rename(source_file, dest_file) + dest_file |> ! path_exists() + after + File.rm_rf source_file + File.rm_rf dest_file + end end + + fact "Test mv: Source file does not exist" do + source_file = "/tmp/hoho.txt" + dest_file = "/tmp/hehe.txt" + try do + source_file |> ! path_exists() + Radpath.mv(source_file, dest_file) + dest_file |> ! path_exists() + after + File.rm_rf source_file + File.rm_rf dest_file + end + end + end facts "Test relative paths" do @@ -295,4 +328,4 @@ defmodule RadpathTests.RadpathFacts do Radpath.parent_path("/I/am/long/dir") |> "/I/am/long/" end end -end \ No newline at end of file +end