From 335fb45de605016dc41bec9b2563e9cd497c009c Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Thu, 12 Mar 2020 23:47:14 +0000 Subject: [PATCH] Add spec for `util.cr` --- spec/util_spec.cr | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 spec/util_spec.cr diff --git a/spec/util_spec.cr b/spec/util_spec.cr new file mode 100644 index 0000000..b2f2c2c --- /dev/null +++ b/spec/util_spec.cr @@ -0,0 +1,28 @@ +require "./spec_helper" + +describe "compare_alphanumerically" do + it "sorts filenames with leading zeros correctly" do + ary = ["010.jpg", "001.jpg", "002.png"] + ary.sort! {|a, b| + compare_alphanumerically a, b + } + ary.should eq ["001.jpg", "002.png", "010.jpg"] + end + + it "sorts filenames without leading zeros correctly" do + ary = ["10.jpg", "1.jpg", "0.png", "0100.jpg"] + ary.sort! {|a, b| + compare_alphanumerically a, b + } + ary.should eq ["0.png", "1.jpg", "10.jpg", "0100.jpg"] + end + + # https://ux.stackexchange.com/a/95441 + it "sorts like the stack exchange post" do + ary = ["2", "12", "200000", "1000000", "a", "a12", "b2", "text2", + "text2a", "text2a2", "text2a12", "text2ab", "text12", "text12a"] + ary.reverse.sort {|a, b| + compare_alphanumerically a, b + }.should eq ary + end +end