Skip to content

Commit

Permalink
Merge pull request #183 from iNecas/ellipsis-fix
Browse files Browse the repository at this point in the history
Don't include ellipsis in truncated digest output
  • Loading branch information
damianlegawiec authored Sep 27, 2018
2 parents e306e5c + 2c04d4d commit 19e5b65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
10 changes: 1 addition & 9 deletions lib/deface/digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,8 @@ def digest_class=(klass)
end

def hexdigest(arg)
new.hexdigest(arg)
digest_class.hexdigest(arg)[0...32]
end
end

def initialize(klass = nil)
@digest_class = klass || self.class.digest_class
end

def hexdigest(arg)
@digest_class.hexdigest(arg).truncate(32)
end
end
end
25 changes: 18 additions & 7 deletions spec/deface/digest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@

module Deface
describe Digest do
def with_custom_digest
digest = double("digest")
original_digest = Digest.digest_class
Digest.digest_class = digest
yield digest
ensure
Digest.digest_class = original_digest
end

it "should use MD5 by default" do
expect(Digest.new.hexdigest("123")).to eq "202cb962ac59075b964b07152d234b70"
expect(Digest.hexdigest("123")).to eq "202cb962ac59075b964b07152d234b70"
end

it "should use user-provided digest" do
digest = double("digest")
expect(digest).to receive(:hexdigest).with("to_digest").and_return("digested")
expect(Digest.new(digest).hexdigest("to_digest")).to eq "digested"
with_custom_digest do |digest|
expect(digest).to receive(:hexdigest).with("to_digest").and_return("digested")
expect(Digest.hexdigest("to_digest")).to eq "digested"
end
end

it "should truncate digest to 32 characters" do
digest = double("digest")
expect(digest).to receive(:hexdigest).with("to_digest").and_return("a" * 50)
expect(Digest.new(digest).hexdigest("to_digest").size).to eq 32
with_custom_digest do |digest|
expect(digest).to receive(:hexdigest).with("to_digest").and_return("a" * 50)
expect(Digest.hexdigest("to_digest").size).to eq 32
end
end
end
end
Expand Down

0 comments on commit 19e5b65

Please sign in to comment.