From 09432b2d1277b8d0cffe00e79c24d077e780e316 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Mon, 17 Sep 2012 01:04:28 +0200 Subject: [PATCH] Initial download tests for extension and unique filename --- midori/midori-download.vala | 6 ++-- tests/download.vala | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tests/download.vala diff --git a/midori/midori-download.vala b/midori/midori-download.vala index 64d6ddfd..32d9464b 100644 --- a/midori/midori-download.vala +++ b/midori/midori-download.vala @@ -228,7 +228,7 @@ namespace Midori { return filename; } - public static string? get_extension_for_uri (string uri, out string basename) { + public static string? get_extension_for_uri (string uri, out string basename = null) { if (&basename != null) basename = null; /* Find the last slash and the last period *after* the last slash. */ @@ -248,11 +248,11 @@ namespace Midori { } public string get_unique_filename (string filename) { - if (Posix.access (filename, Posix.F_OK) != 0) { + if (Posix.access (filename, Posix.F_OK) == 0) { string basename; string? extension = get_extension_for_uri (filename, out basename); string? new_filename = null; - int i = -1; + int i = 0; do { new_filename = "%s-%d%s".printf (basename, i++, extension ?? ""); } while (Posix.access (new_filename, Posix.F_OK) == 0); diff --git a/tests/download.vala b/tests/download.vala new file mode 100644 index 00000000..9bdbc027 --- /dev/null +++ b/tests/download.vala @@ -0,0 +1,62 @@ +/* + Copyright (C) 2012 Christian Dywan + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + See the file COPYING for the full license text. +*/ + +namespace Katze { + extern static string assert_str_equal (string input, string result, string expected); +} + +struct TestCase { + public string data; + public string? expected; +} + +const TestCase[] filenames = { + { "/tmp/midori-user/tumblr123.jpg", ".jpg" }, + { "https://green.cat/8019B6/a.b/500.jpg", ".jpg" }, + { "http://example.com/file.png", ".png" } +}; + +static void download_extension () { + foreach (var filename in filenames) { + string? result = Midori.Download.get_extension_for_uri (filename.data); + Katze.assert_str_equal (filename.data, result, filename.expected); + } +} + +static void download_unique () { + string folder = DirUtils.make_tmp ("cacheXXXXXX"); + string filename = Path.build_path (Path.DIR_SEPARATOR_S, folder, "foo.png"); + string unique = Midori.Download.get_unique_filename (filename); + Katze.assert_str_equal (folder, unique, filename); + FileUtils.set_contents (filename, "12345"); + unique = Midori.Download.get_unique_filename (filename); + filename = Path.build_path (Path.DIR_SEPARATOR_S, folder, "foo-0.png"); + Katze.assert_str_equal (folder, unique, filename); + FileUtils.set_contents (filename, "12345"); + unique = Midori.Download.get_unique_filename (filename); + filename = Path.build_path (Path.DIR_SEPARATOR_S, folder, "foo-1.png"); + Katze.assert_str_equal (folder, unique, filename); + + filename = Path.build_path (Path.DIR_SEPARATOR_S, folder, "foo-9.png"); + FileUtils.set_contents (filename, "12345"); + unique = Midori.Download.get_unique_filename (filename); + filename = Path.build_path (Path.DIR_SEPARATOR_S, folder, "foo-10.png"); + Katze.assert_str_equal (folder, unique, filename); + DirUtils.remove (folder); +} + +void main (string[] args) { + Test.init (ref args); + Test.add_func ("/download/extension", download_extension); + Test.add_func ("/download/unique", download_unique); + Test.run (); +} + -- 2.39.5