Skip to content

Commit

Permalink
AO3-6625 Test more spacing and use more succinct code
Browse files Browse the repository at this point in the history
  • Loading branch information
sarken committed Oct 24, 2023
1 parent 9d572e1 commit 871131f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
9 changes: 3 additions & 6 deletions app/models/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
56 changes: 56 additions & 0 deletions spec/models/download_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 871131f

Please sign in to comment.