From 871131f15aa08cc5da42c4f202a1c6f52f778716 Mon Sep 17 00:00:00 2001 From: Sarken Date: Tue, 24 Oct 2023 06:03:17 -0400 Subject: [PATCH] AO3-6625 Test more spacing and use more succinct code --- app/models/download.rb | 9 ++---- spec/models/download_spec.rb | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/app/models/download.rb b/app/models/download.rb index 095f4d3dc89..d7d2c799f4c 100644 --- a/app/models/download.rb +++ b/app/models/download.rb @@ -54,12 +54,9 @@ def file_type_from_mime(mime) # The base name of the file (e.g., "War_and_Peace") def file_name name = clean(work.title) - extender = "Work_#{work.id}" - if name.length.zero? - name = extender - elsif name.length < 3 - name += "_#{extender}" - end + # If the file name is 1-2 characters, append "_Work_#{work.id}". + # If the file name is blank, name the file "Work_#{work.id}". + name = [name, "Work_#{work.id}"].compact_blank.join("_") if name.length < 3 name.strip end diff --git a/spec/models/download_spec.rb b/spec/models/download_spec.rb index 3f39b748587..25c200b27c5 100644 --- a/spec/models/download_spec.rb +++ b/spec/models/download_spec.rb @@ -36,6 +36,62 @@ expect(Download.new(work).file_name).to eq("Emjoi_Yay") end + it "strips leading space" do + work.title = " Blank Space Baby" + expect(Download.new(work).file_name).to eq("Blank_Space_Baby") + end + + it "strips trailing space" do + work.title = "Write your name: " + expect(Download.new(work).file_name).to eq("Write_your_name") + end + + it "replaces multiple spaces with single underscore" do + work.title = "Space Opera" + expect(Download.new(work).file_name).to eq("Space_Opera") + end + + it "replaces unicode space with underscores" do + work.title = "No-break Space" + expect(Download.new(work).file_name).to eq("No-break_Space") + + work.title = "En Quad Space" + expect(Download.new(work).file_name).to eq("En_Quad_Space") + + work.title = "Em Quad Space" + expect(Download.new(work).file_name).to eq("Em_Quad_Space") + + work.title = "En Space" + expect(Download.new(work).file_name).to eq("En_Space") + + work.title = "Em Space" + expect(Download.new(work).file_name).to eq("Em_Space") + + work.title = "3 Per Em Space" + expect(Download.new(work).file_name).to eq("3_Per_Em_Space") + + work.title = "4 Per Em Space" + expect(Download.new(work).file_name).to eq("4_Per_Em_Space") + + work.title = "6 Per Em Space" + expect(Download.new(work).file_name).to eq("6_Per_Em_Space") + + work.title = "Figure Space" + expect(Download.new(work).file_name).to eq("Figure_Space") + + work.title = "Punctuation Space" + expect(Download.new(work).file_name).to eq("Punctuation_Space") + + work.title = "Thin Space" + expect(Download.new(work).file_name).to eq("Thin_Space") + + work.title = "Hair Space" + expect(Download.new(work).file_name).to eq("Hair_Space") + + work.title = "Narrow No-Break Space" + expect(Download.new(work).file_name).to eq("Narrow_No-Break_Space") + end + it "appends work ID if too short" do work.id = 999_999 work.title = "Uh"