Skip to content

Commit

Permalink
Merge pull request #10 from mlibrary/CP-53-wildcard-helpers
Browse files Browse the repository at this point in the history
CP-53 - Add convenient methods for testing Resource wildcards
  • Loading branch information
malakai97 committed May 8, 2019
2 parents a97d6ca + d48b386 commit 5e6ca46
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/checkpoint/resource.rb
Expand Up @@ -54,6 +54,14 @@ def self.all
AllOfAnyType.new
end

# Test whether this Resource represents all entities of all types.
#
# @see {::all}
# @return [Boolean] true if this is a universal wildcard
def all?
type == ALL && id == ALL
end

# Convert this object to a Resource.
#
# For Checkpoint-supplied Resources, this is an identity operation,
Expand Down Expand Up @@ -117,6 +125,14 @@ def all_of_type
Resource::AllOfType.new(type)
end

# Test whether this is a Resource wildcard of some specific type.
#
# @return [Boolean] true if this Resource has a specific type, but
# has the any/all ID, representing any Resources of that type
def all_of_type?
type != ALL && id == ALL
end

# Check whether two Resources refer to the same entity.
# @param other [Resource] Another Resource to compare with
# @return [Boolean] true when the other Resource's entity is the same as
Expand Down
15 changes: 15 additions & 0 deletions lib/checkpoint/resource/token.rb
Expand Up @@ -37,6 +37,21 @@ def self.all
@all ||= new(Resource::ALL, Resource::ALL).freeze
end

# Test whether this token is for the special "all" Resource.
#
# @return [Boolean] true if this token represents any/all resources
def all?
type == Resource::ALL && id == Resource::ALL
end

# Test whether this token is a wildcard for some specific type.
#
# @return [Boolean] true if this token has a specific type, but
# represents any/all resources of that type
def all_of_type?
type != Resource::ALL && id == Resource::ALL
end

# @return [Token] self; for convenience of taking a Resource or token
def token
self
Expand Down
46 changes: 46 additions & 0 deletions spec/checkpoint/resource/token_spec.rb
Expand Up @@ -65,6 +65,52 @@ module Checkpoint
end
end

describe "#all?" do
context 'for the "all" resource' do
it "is true" do
token = described_class.all
expect(token.all?).to eq true
end
end

context 'for a type wildcard' do
it "is false" do
token = described_class.new('some-type', Checkpoint::Resource::ALL)
expect(token.all?).to eq false
end
end

context 'for a specific resource' do
it "is false" do
token = described_class.new('some-type', 'some-id')
expect(token.all?).to eq false
end
end
end

describe "#all_of_type?" do
context 'for the "all" resource' do
it "is false" do
token = described_class.all
expect(token.all_of_type?).to eq false
end
end

context 'for a type wildcard' do
it "is true" do
token = described_class.new('some-type', Checkpoint::Resource::ALL)
expect(token.all_of_type?).to eq true
end
end

context 'for a specific resource' do
it "is false" do
token = described_class.new('some-type', 'some-id')
expect(token.all_of_type?).to eq false
end
end
end

describe "#eql?" do
it 'considers resources as the same if type and id match' do
res1 = described_class.new('some-type', 'some-id')
Expand Down
52 changes: 52 additions & 0 deletions spec/checkpoint/resource_spec.rb
Expand Up @@ -79,6 +79,58 @@
end
end

describe "#all?" do
let(:entity) { double('Entity', type: 'type', id: 'id') }

context 'for the "all" resource' do
it "is true" do
resource = described_class.all
expect(resource.all?).to eq true
end
end

context 'for a type wildcard' do
it "is false" do
resource = described_class.new(entity).all_of_type
expect(resource.all?).to eq false
end
end

context 'for a specific resource' do
it "is false" do
resource = described_class.new(entity)
expect(resource.all?).to eq false
end
end
end

describe "#all_of_type?" do
context 'for the "all" resource' do
it "is false" do
token = described_class.all
expect(token.all_of_type?).to eq false
end
end

context 'for a type wildcard' do
let(:entity) { double('Entity', type: 'type', id: 'id') }

it "is true" do
token = described_class.new(entity).all_of_type
expect(token.all_of_type?).to eq true
end
end

context 'for a specific resource' do
let(:entity) { double('Entity', type: 'type', id: 'id') }

it "is false" do
token = described_class.new(entity)
expect(token.all_of_type?).to eq false
end
end
end

# @botimer - 2018-02-26: I'm of two minds on this kind of test. There is an
# argument that including some kind of realistic example (say, a Document
# type) helps clarify expected usage. There is also an argument that this is
Expand Down

0 comments on commit 5e6ca46

Please sign in to comment.