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

Instance Method Details

#assert_boolean(object) ⇒ Object

Assert the given object is a boolean.



107
108
109
# File 'lib/talia_util/test_helpers.rb', line 107

def assert_boolean(object)
  assert_kind_of_classes(object, TrueClass, FalseClass)
end

#assert_empty(condition, message = nil) ⇒ Object

Assert the given collection is empty.



86
87
88
# File 'lib/talia_util/test_helpers.rb', line 86

def assert_empty(condition, message = nil)
  assert condition.empty?, message
end

#assert_included(collection, element, message = nil) ⇒ Object

Assert the given element is included into the given collection.



96
97
98
# File 'lib/talia_util/test_helpers.rb', line 96

def assert_included(collection, element, message = nil)
  assert collection.include?(element), message
end

#assert_kind_of_classes(object, *classes) ⇒ Object

Assert the given object is instance of one of those classes.



101
102
103
104
# File 'lib/talia_util/test_helpers.rb', line 101

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



80
81
82
# File 'lib/talia_util/test_helpers.rb', line 80

def assert_not(condition, message = nil)
  assert !condition, message
end

#assert_not_empty(condition, message = nil) ⇒ Object

Assert the given collection is not empty.



91
92
93
# File 'lib/talia_util/test_helpers.rb', line 91

def assert_not_empty(condition, message = nil)
  assert_not condition.empty?, message
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
# 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}.")
  property.each do |value|
    assert(values.detect { |val| val.respond_to?(:uri) ? (val.uri.to_s == value.uri.to_s) : (value == val) }, "Found unexpected value #{value}. Value is a #{value.class}\nExpected:\n#{values.join("\n")}")
  end
end

#assert_source_exist(uri, message = nil) ⇒ Object Also known as: assert_source_exists

Assert the source for the given uri exists.



112
113
114
# File 'lib/talia_util/test_helpers.rb', line 112

def assert_source_exist(uri, message = nil)
  assert TaliaCore::Source.exists?(uri), message
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



75
76
77
# File 'lib/talia_util/test_helpers.rb', line 75

def create_source(uri)
  TaliaCore::Source.create!(uri)
end

#make_dummy_source(uri, *types) ⇒ Object

Creates a dummy Source and saves it



30
31
32
33
34
35
36
37
38
39
# File 'lib/talia_util/test_helpers.rb', line 30

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.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/talia_util/test_helpers.rb', line 62

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