Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refacored and backfilled tests, removed some lenses we weren't using #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/fedora_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def create(data)

def orm_to_hash
if @orm_to_hash.nil?
aggregate_lens = attributes_as_lenses.inject({}) do |acc, (name, path)|
lens = path.inject {|outer, inner| Lenses.compose(outer, inner)}
aggregate_lens = attributes_as_lenses.reduce({}) do |acc, (name, path)|
lens = path.reduce {|outer, inner| Lenses.compose(outer, inner)}
acc.merge(name => lens)
end
@orm_to_hash = Lenses.orm_to_hash(aggregate_lens)
Expand Down
20 changes: 16 additions & 4 deletions lib/fedora_lens/lens.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
require 'active_support/core_ext/hash'

module FedoraLens
class Lens < ActiveSupport::HashWithIndifferentAccess
class Lens
attr_reader(:options)

def initialize(*options)
@options = options
end

def get(source)
self[:get].call(source)
raise NotImplementedError.new
end

def put(source, value)
self[:put].call(source, value)
raise NotImplementedError.new
end

def create(value)
self[:create].call(value)
raise NotImplementedError.new
end

def ==(other_lens)
self.class == other_lens.class && options == other_lens.options
end
end
end
89 changes: 50 additions & 39 deletions lib/fedora_lens/lens_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,36 @@ module LensTests
# See page 6 of the manual for the Harmony language for a description
# on how lenses work
# http://www.seas.upenn.edu/~harmony/manual.pdf

class LensLawError < StandardError
def initialize(actual, expected)
@actual = actual
@expected = expected
end
def message(desc)
"Lens must be well-behaved (#{desc})
expected: #{@expected.inspect}
got: #{@actual.inspect}"
end
end
class GetPutError < LensLawError
def message
super("GetPut: lens.put(source, lens.get(source)) == source")
end
end
class PutGetError < LensLawError
def message
super("PutGet: lens.get(lens.put(source, value)) == value")
end
end
class CreateGetError < LensLawError
def message
super("CreateGet: lens.get(lens.create(value)) == value")
end
end

# @example testing a lens that converts xml to a dom and back
# test_lens(lens, Nokogiri::XML("<a/>"), Nokogiri::XML("<b/>") do |v|
# check_laws(lens, Nokogiri::XML("<a/>"), Nokogiri::XML("<b/>") do |v|
# v.to_xml
# end
# @param [lens]
Expand All @@ -13,48 +41,31 @@ module LensTests
# the value to test with (when calling put)
# @param [source]
# the source document this lens operates on
# @yield [converter]
# a block that converts the value from the lens to something that can be
# compared with #== (defaults to the identity function)
# @yieldparam [value or source]
# a value that will be compared
def test_lens(lens, source, value, &block)
test_lens_get_put(lens, source, &block)
test_lens_put_get(lens, source, value, &block)
test_lens_create_get(lens, value, &block)
end

def test_lens_get_put(lens, source)
it "is well-behaved (GetPut: put(source, get(source)) == source)" do
converted = lens.put(source, lens.get(source))
if block_given?
yield(converted).should eq yield(source)
else
converted.should eq source
end
# @yield [actual, expected]
# a block that returns true if the supplied values are equal
def check_laws(lens, source, value, &block)
if block_given?
equality_test = block
else
equality_test = lambda { |a, b| a == b }
end
end

def test_lens_put_get(lens, source, value)
it "is well-behaved (PutGet: get(put(source, value)) == value)" do
converted = lens.get(lens.put(source, value))
if block_given?
yield(converted).should eq yield(value)
else
converted.should eq value
end
end
actual, expected = check_get_put(lens, source)
raise GetPutError.new(actual, expected) unless equality_test.call(actual, expected)
actual, expected = check_put_get(lens, source, value)
raise PutGetError.new(actual, expected) unless equality_test.call(actual, expected)
actual, expected = check_create_get(lens, value)
raise CreateGetError.new(actual, expected) unless equality_test.call(actual, expected)
end

def test_lens_create_get(lens, value)
it "is well-behaved (CreateGet: get(create(value)) == value)" do
created = lens.get(lens.create(value))
if block_given?
yield(created).should eq yield(value)
else
created.should eq value
end
end
def check_get_put(lens, source)
[lens.put(source, lens.get(source)), source]
end
def check_put_get(lens, source, value)
[lens.get(lens.put(source, value)), value]
end
def check_create_get(lens, value)
[lens.get(lens.create(value)), value]
end
end
end
Loading