Module: TaliaUtil::TestHelpers
- Included in:
- Test::Unit::TestCase
- Defined in:
- lib/talia_util/test_helpers.rb
Overview
Test helper mixin for unit tests
Instance Method Summary collapse
-
#assert_boolean(object) ⇒ Object
Assert the given object is a boolean.
-
#assert_empty(condition, message = nil) ⇒ Object
Assert the given collection is empty.
-
#assert_included(collection, element, message = nil) ⇒ Object
Assert the given element is included into the given collection.
-
#assert_kind_of_classes(object, *classes) ⇒ Object
Assert the given object is instance of one of those classes.
-
#assert_not(condition, message = nil) ⇒ Object
(also: #assert_false)
Assert the given condition is false.
-
#assert_not_empty(condition, message = nil) ⇒ Object
Assert the given collection is not empty.
-
#assert_property(property, *values) ⇒ Object
Checks if the given property has the values given to this assertion.
-
#assert_source_exist(uri, message = nil) ⇒ Object
(also: #assert_source_exists)
Assert the source for the given uri exists.
-
#assert_types(source, *types) ⇒ Object
Test the types of an element.
-
#create_source(uri) ⇒ Object
Creates a source for the given uri.
-
#make_dummy_source(uri, *types) ⇒ Object
Creates a dummy Source and saves it.
-
#setup_once(variable, &block) ⇒ Object
Helper to create a variable only once.
Instance Method Details
#assert_boolean(object) ⇒ Object
Assert the given object is a boolean.
108 109 110 |
# File 'lib/talia_util/test_helpers.rb', line 108 def assert_boolean(object) assert_kind_of_classes(object, TrueClass, FalseClass) end |
#assert_empty(condition, message = nil) ⇒ Object
Assert the given collection is empty.
87 88 89 |
# File 'lib/talia_util/test_helpers.rb', line 87 def assert_empty(condition, = nil) assert condition.empty?, end |
#assert_included(collection, element, message = nil) ⇒ Object
Assert the given element is included into the given collection.
97 98 99 |
# File 'lib/talia_util/test_helpers.rb', line 97 def assert_included(collection, element, = nil) assert collection.include?(element), end |
#assert_kind_of_classes(object, *classes) ⇒ Object
Assert the given object is instance of one of those classes.
102 103 104 105 |
# File 'lib/talia_util/test_helpers.rb', line 102 def assert_kind_of_classes(object, *classes) assert_included(classes, object.class, "#{object} should be instance of one of those classes: #{classes.to_sentence}") end |
#assert_not(condition, message = nil) ⇒ Object Also known as: assert_false
Assert the given condition is false
81 82 83 |
# File 'lib/talia_util/test_helpers.rb', line 81 def assert_not(condition, = nil) assert !condition, end |
#assert_not_empty(condition, message = nil) ⇒ Object
Assert the given collection is not empty.
92 93 94 |
# File 'lib/talia_util/test_helpers.rb', line 92 def assert_not_empty(condition, = nil) assert_not condition.empty?, end |
#assert_property(property, *values) ⇒ Object
Checks if the given property has the values given to this assertion. If a value is an N::URI, this will assert if the property refers to the Source given by the URI.
21 22 23 24 25 26 27 28 |
# File 'lib/talia_util/test_helpers.rb', line 21 def assert_property(property, *values) assert_kind_of(TaliaCore::SemanticCollectionWrapper, property) # Just to be sure assert_equal(values.size, property.size, "Expected #{values.size} values instead of #{property.size}.") value_str = values.inject("Expected:\n") { |str, value| str << "#{value.inspect} (#{value.class.name})\n" } property.each do |value| assert(values.detect { |val| val.respond_to?(:uri) ? (value.respond_to?(:uri) && (val.uri.to_s == value.uri.to_s)) : (value == val) }, "Found unexpected value #{value.inspect} (#{value.class.name})\n#{value_str}") end end |
#assert_source_exist(uri, message = nil) ⇒ Object Also known as: assert_source_exists
Assert the source for the given uri exists.
113 114 115 |
# File 'lib/talia_util/test_helpers.rb', line 113 def assert_source_exist(uri, = nil) assert TaliaCore::Source.exists?(uri), end |
#assert_types(source, *types) ⇒ Object
Test the types of an element. Asserts if the source has the same types as given in the types argument(s)
8 9 10 11 12 13 14 15 16 |
# File 'lib/talia_util/test_helpers.rb', line 8 def assert_types(source, *types) assert_kind_of(TaliaCore::Source, source) # Just to be sure type_list = "" source.types.each { |type| type_list << "- Type: #{type.local_name}\n" } assert_equal(types.size, source.types.size, "Type size mismatch: Source has #{source.types.size} instead of #{types.size}.\n#{type_list}") types.each { |type| assert(source.types.include?(type), "#{source.uri.local_name} should have type #{type}\n#{type_list}") } rdf_types = source.my_rdf[N::RDF.type].collect { |type| type.uri.to_s } types.each { |type| assert(rdf_types.include?(type.to_s), "#{source.uri.local_name} should have RDF type #{type}\n#{rdf_types}")} end |
#create_source(uri) ⇒ Object
Creates a source for the given uri
76 77 78 |
# File 'lib/talia_util/test_helpers.rb', line 76 def create_source(uri) TaliaCore::Source.create!(uri) end |
#make_dummy_source(uri, *types) ⇒ Object
Creates a dummy Source and saves it
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/talia_util/test_helpers.rb', line 31 def make_dummy_source(uri, *types) src = TaliaCore::Source.new(uri) src.primary_source = true types.each do |t| TaliaCore::ActiveSource.new(t).save! unless(TaliaCore::ActiveSource.exists?(:uri => t.to_s)) src.types << t end src.save! return src end |
#setup_once(variable, &block) ⇒ Object
Helper to create a variable only once. This should be used from the setup method, and will assign the given block to the given class variable.
The block will only be executed once, after that the value for the class variable will be retrieved from a cache.
This is a workaround because the Ruby test framework doesn’t provide a setup_once method or something like this, and in fact re-creates the Test case object for every single test (go figure). It would be worth switching to RSpec just for this, but it’s a heap of work so… the test framework has just the braindead “fixtures” mechanism…
The thing is that’s a good practice to have reasonably fine-grained tests, and you often have objects that are re-used often, are read-only for all the tests and expensive to create. So you basically want to create them only once.
This thing is less than perfect, but it should work for now. Basically it assumes that all tests for a TestCase will be run in a row, and the setup method will execute before the first test and that no other tests will execute before all tests of the TestCase are executed.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/talia_util/test_helpers.rb', line 63 def setup_once(variable, &block) variable = variable.to_sym value = self.class.obj_cache[variable] unless(value) value = block.call self.class.obj_cache[variable] = value end assit_not_nil(value) value ||= false # We can't have a nil value (will cause the block to re-run) instance_variable_set(:"@#{variable}", value) end |