| Module | TaliaUtil::TestHelpers |
| In: |
lib/talia_util/test_helpers.rb
|
Test helper mixin for unit tests
Assert the given object is a boolean.
# File lib/talia_util/test_helpers.rb, line 108
108: def assert_boolean(object)
109: assert_kind_of_classes(object, TrueClass, FalseClass)
110: end
Assert the given collection is empty.
# File lib/talia_util/test_helpers.rb, line 87
87: def assert_empty(condition, message = nil)
88: assert condition.empty?, message
89: end
Assert the given element is included into the given collection.
# File lib/talia_util/test_helpers.rb, line 97
97: def assert_included(collection, element, message = nil)
98: assert collection.include?(element), message
99: end
Assert the given object is instance of one of those classes.
# File lib/talia_util/test_helpers.rb, line 102
102: def assert_kind_of_classes(object, *classes)
103: assert_included(classes, object.class,
104: "#{object} should be instance of one of those classes: #{classes.to_sentence}")
105: end
Assert the given condition is false
# File lib/talia_util/test_helpers.rb, line 81
81: def assert_not(condition, message = nil)
82: assert !condition, message
83: end
Assert the given collection is not empty.
# File lib/talia_util/test_helpers.rb, line 92
92: def assert_not_empty(condition, message = nil)
93: assert_not condition.empty?, message
94: end
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.
# File lib/talia_util/test_helpers.rb, line 21
21: def assert_property(property, *values)
22: assert_kind_of(TaliaCore::SemanticCollectionWrapper, property) # Just to be sure
23: assert_equal(values.size, property.size, "Expected #{values.size} values instead of #{property.size}.")
24: value_str = values.inject("Expected:\n") { |str, value| str << "#{value.inspect} (#{value.class.name})\n" }
25: property.each do |value|
26: 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}")
27: end
28: end
Assert the source for the given uri exists.
# File lib/talia_util/test_helpers.rb, line 113
113: def assert_source_exist(uri, message = nil)
114: assert TaliaCore::Source.exists?(uri), message
115: end
Test the types of an element. Asserts if the source has the same types as given in the types argument(s)
# File lib/talia_util/test_helpers.rb, line 8
8: def assert_types(source, *types)
9: assert_kind_of(TaliaCore::Source, source) # Just to be sure
10: type_list = ""
11: source.types.each { |type| type_list << "- Type: #{type.local_name}\n" }
12: assert_equal(types.size, source.types.size, "Type size mismatch: Source has #{source.types.size} instead of #{types.size}.\n#{type_list}")
13: types.each { |type| assert(source.types.include?(type), "#{source.uri.local_name} should have type #{type}\n#{type_list}") }
14: rdf_types = source.my_rdf[N::RDF.type].collect { |type| type.uri.to_s }
15: types.each { |type| assert(rdf_types.include?(type.to_s), "#{source.uri.local_name} should have RDF type #{type}\n#{rdf_types}")}
16: end
Creates a source for the given uri
# File lib/talia_util/test_helpers.rb, line 76
76: def create_source(uri)
77: TaliaCore::Source.create!(uri)
78: end
Creates a dummy Source and saves it
# File lib/talia_util/test_helpers.rb, line 31
31: def make_dummy_source(uri, *types)
32: src = TaliaCore::Source.new(uri)
33: src.primary_source = true
34: types.each do |t|
35: TaliaCore::ActiveSource.new(t).save! unless(TaliaCore::ActiveSource.exists?(:uri => t.to_s))
36: src.types << t
37: end
38: src.save!
39: return src
40: end
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.
# File lib/talia_util/test_helpers.rb, line 63
63: def setup_once(variable, &block)
64: variable = variable.to_sym
65: value = self.class.obj_cache[variable]
66: unless(value)
67: value = block.call
68: self.class.obj_cache[variable] = value
69: end
70: assit_not_nil(value)
71: value ||= false # We can't have a nil value (will cause the block to re-run)
72: instance_variable_set("@#{variable}""@#{variable}", value)
73: end