]> jaekl.net Git - quanlib.git/commitdiff
Add unit test for WalkDir
authorChris Jaekl <chris@jaekl.net>
Sat, 22 Jun 2024 01:42:31 +0000 (21:42 -0400)
committerChris Jaekl <chris@jaekl.net>
Sat, 22 Jun 2024 01:42:31 +0000 (21:42 -0400)
test/walk_dir_test.rb [new file with mode: 0644]

diff --git a/test/walk_dir_test.rb b/test/walk_dir_test.rb
new file mode 100644 (file)
index 0000000..e47ef47
--- /dev/null
@@ -0,0 +1,116 @@
+# frozen_string_literal: true
+
+require "test_helper"
+
+require "walk_dir"
+
+class WalkDirTest < Minitest::Test
+  def test_is_duplicate
+    yes = %w(
+      Little_Women_bis.pdf
+      The_Autobiography_of_Benjamin_Franklin_ter.epub
+      Romeo_and_Juliet_quater.pdf
+      As_You_Like_It_quinquies.epub
+    )
+
+    no = %w(
+      Little_Women.pdf
+      The_Autobiography_of_Benjamin_Franklin.epub
+      Romeo_and_Juliet.pdf
+      As_You_Like_It.epub
+    )
+
+    walk_dir = create_walk_dir
+
+    yes.each do |filename|
+      assert_equal true, walk_dir.is_duplicate?(filename)
+    end
+
+    no.each do |filename|
+      assert_equal false, walk_dir.is_duplicate?(filename)
+    end
+  end
+
+  def test_remove_duplicates
+    input = %w(
+      00_nonFiction/BenjaminFranklin/The_Autobiography_of_Benjamin_Franklin.epub
+      00_nonFiction/BenjaminFranklin/The_Autobiography_of_Benjamin_Franklin.jpeg
+      00_nonFiction/BenjaminFranklin/The_Autobiography_of_Benjamin_Franklin.pdf
+      LouisaAlcott/Little_Women.epub
+      FranklinDixon/HB001_The_Tower_Treasure.epub
+      FranklinDixon/HB001_The_Tower_Treasure.jpeg
+      FranklinDixon/HB001_The_Tower_Treasure.pdf
+      FranklinDixon/HB002_The_House_on_the_Cliff.epub
+    )
+
+    expected = %w(
+      00_nonFiction/BenjaminFranklin/The_Autobiography_of_Benjamin_Franklin.epub
+      LouisaAlcott/Little_Women.epub
+      FranklinDixon/HB001_The_Tower_Treasure.epub
+      FranklinDixon/HB002_The_House_on_the_Cliff.epub
+    )
+
+    walk_dir = create_walk_dir
+    actual = walk_dir.remove_duplicates(input)
+
+    assert_equal expected, actual
+  end
+
+  def test_walk
+    root_entries = %w(
+      00_nonFiction
+      LouisaAlcott
+      FranklinDixon
+    )
+
+    Dir.expects(:entries).with("./00_nonFiction").returns(%w(BenjaminFranklin))
+    Dir.expects(:entries).with("./00_nonFiction/BenjaminFranklin").returns(%w(
+      The_Autobiography_of_Benjamin_Franklin.epub
+    ))
+    Dir.expects(:entries).with("./LouisaAlcott").returns(["LW01_Little_Women.epub"])
+    Dir.expects(:entries).with("./FranklinDixon").returns(%w(
+      HB001_The_Tower_Treasure.epub
+      HB002_The_House_on_the_Cliff.epub
+    ))
+
+    %w(
+      ./00_nonFiction
+      ./00_nonFiction/BenjaminFranklin
+      ./LouisaAlcott
+      ./FranklinDixon
+    ).each do |path|
+      File.expects(:directory?).with(path).returns(true).at_least_once
+      File.expects(:symlink?).with(path).returns(false).at_least_once
+    end
+
+    %w(
+      ./00_nonFiction/BenjaminFranklin/The_Autobiography_of_Benjamin_Franklin.epub
+      ./LouisaAlcott/LW01_Little_Women.epub
+      ./FranklinDixon/HB001_The_Tower_Treasure.epub
+      ./FranklinDixon/HB002_The_House_on_the_Cliff.epub
+    ).each do |path|
+      File.expects(:directory?).with(path).returns(false).at_least_once
+    end
+
+    walk_dir = create_walk_dir(".", root_entries)
+    files = walk_dir.instance_variable_get(:@files)
+
+    expected = %w(
+      ./00_nonFiction/BenjaminFranklin/The_Autobiography_of_Benjamin_Franklin.epub
+      ./LouisaAlcott/LW01_Little_Women.epub
+      ./FranklinDixon/HB001_The_Tower_Treasure.epub
+      ./FranklinDixon/HB002_The_House_on_the_Cliff.epub
+    )
+
+    assert_equal expected, files
+  end
+
+  private
+
+  def create_walk_dir(root_path = "/does/not/exist", root_entries = [])
+    config_file = "quanlib.ini"
+    Dir.expects(:entries).with(root_path).returns(root_entries)
+
+    WalkDir.new(config_file, root_path)
+  end
+end