Skip to content

Commit

Permalink
Extend PackageName with value object interface
Browse files Browse the repository at this point in the history
We should be able to compare two package names, and since they don't
have an identity, it makes sense to treat them as values.

One assumption I made here is that we'd want to be able to compare
non-PackageName objects' string values with PackageName object string
values (i.e. `PackageName.new("string") == "string"`)

Co-authored-by: Mattt <mattt@github.com>
  • Loading branch information
landongrindheim and mattt committed Apr 18, 2022
1 parent 525fbcf commit 997cade
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
19 changes: 12 additions & 7 deletions npm_and_yarn/lib/dependabot/npm_and_yarn/package_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@ def to_s
end

def <=>(other)
to_s <=> other.to_s
to_s.casecmp(other.to_s)
end

def types_package
def eql?(other)
to_s.eql?(other.to_s)
end

def types_package_name
return self if types_package?

if scoped?
"@types/#{@scope}__#{@name}"
else
"@types/#{@name}"
end
@types_package_name ||=
if scoped?
self.class.new("@types/#{@scope}__#{@name}")
else
self.class.new("@types/#{@name}")
end
end

private
Expand Down
54 changes: 46 additions & 8 deletions npm_and_yarn/spec/dependabot/npm_and_yarn/package_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,65 @@
end
end

describe "#types_package" do
describe "#types_package_name" do
it "returns the corresponding types package name" do
lodash = "lodash"
lodash_types = "@types/lodash"

types_package = described_class.new(lodash).types_package
types_package_name = described_class.new(lodash).types_package_name

expect(types_package).to eq(lodash_types)
expect(types_package_name.to_s).to eq(lodash_types)
end

it "returns self if it is already a types package" do
stereo_types = "@types/stereo"

types_package = described_class.new(stereo_types).types_package
types_package_name = described_class.new(stereo_types).types_package_name

expect(types_package.to_s).to eq(stereo_types)
expect(types_package_name.to_s).to eq(stereo_types)
end

context "when given a scoped dependency name" do
it "returns the corresponding scoped types package name" do
babel_core = "@babel/core"
babel_core_types = "@types/babel__core"

types_package = described_class.new(babel_core).types_package
types_package_name = described_class.new(babel_core).types_package_name

expect(types_package).to eq(babel_core_types)
expect(types_package_name.to_s).to eq(babel_core_types)
end
end
end

describe "#eql?" do
it "compares the string representation of the package name" do
package = described_class.new("package")
package_again = described_class.new("package")

equality_check = package.eql?(package_again)

expect(equality_check).to be true
end

it "returns true for equivalent package names" do
react = described_class.new("react")
react_again = described_class.new("react")

equality_check = react.eql?(react_again)

expect(equality_check).to be true
end

it "returns false for non-equivalent package names" do
react = described_class.new("react")
vue = described_class.new("vue")

equality_check = react.eql?(vue)

expect(equality_check).to be false
end
end

describe "#<=>" do
it "provides affordances for sorting/comparison" do
first = described_class.new("first")
Expand All @@ -74,10 +103,19 @@
expect([third, second, first].sort).to eq([first, second, third])
end

it "ignores case" do
package_name_string = "jquery"
all_caps = described_class.new(package_name_string.upcase)
all_lower = described_class.new(package_name_string.downcase)

expect(all_lower <=> all_caps).to be_zero
end

it "allows for comparison with types packages" do
library = described_class.new("my-library")

expect([library, library.types_package].sort).to eq([library.types_package, library])
expect([library, library.types_package_name].sort).
to eq([library.types_package_name, library])
end
end
end

0 comments on commit 997cade

Please sign in to comment.