Class: Capsium::Package::Testing::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/capsium/package/testing/test_case.rb,
sig/capsium/package/testing/test_case.rbs

Overview

Base class for one test of the Capsium testing YAML DSL (05x-testing). Subclasses implement a test kind and register themselves under their DSL type name (open/closed registry).

Direct Known Subclasses

ConfigTest, DataValidationTest, FileTest, RouteTest

Defined Under Namespace

Classes: DefinitionError, Result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ TestCase

Returns a new instance of TestCase.

Parameters:

  • name: (String name)


21
22
23
# File 'lib/capsium/package/testing/test_case.rb', line 21

def initialize(name:)
  @name = name
end

Instance Attribute Details

#nameString (readonly)

Returns the value of attribute name.

Returns:

  • (String)


19
20
21
# File 'lib/capsium/package/testing/test_case.rb', line 19

def name
  @name
end

Class Method Details

.build(definition) ⇒ TestCase

Builds a test case from a YAML definition hash (string keys).

Parameters:

  • definition (Object)

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/capsium/package/testing/test_case.rb', line 39

def self.build(definition)
  unless definition.is_a?(Hash)
    raise DefinitionError, "test definition is not a mapping: #{definition.inspect}"
  end

  type = definition["type"] ||
         raise(DefinitionError, "test #{definition['name'].inspect} has no type")
  klass = types[type] || raise(DefinitionError, "unknown test type: #{type}")
  klass.from_h(definition)
rescue ArgumentError => e
  raise DefinitionError, "invalid #{type} test #{definition['name'].inspect}: #{e.message}"
end

.from_h(definition) ⇒ TestCase

Parameters:

  • definition (Hash[String, untyped])

Returns:



52
53
54
55
56
# File 'lib/capsium/package/testing/test_case.rb', line 52

def self.from_h(definition)
  attributes = definition.transform_keys(&:to_sym)
  attributes.delete(:type)
  new(**attributes)
end

.register(type, klass) ⇒ void

This method returns an undefined value.

Parameters:

  • type (String)
  • klass (singleton(TestCase))


30
31
32
# File 'lib/capsium/package/testing/test_case.rb', line 30

def self.register(type, klass)
  types[type] = klass
end

.typesHash[String, singleton(TestCase)]

Returns:



34
35
36
# File 'lib/capsium/package/testing/test_case.rb', line 34

def self.types
  @types ||= {}
end

Instance Method Details

#run(context) ⇒ Result

Runs the test against the context and returns a Result.

Parameters:

Returns:



26
27
28
# File 'lib/capsium/package/testing/test_case.rb', line 26

def run(context)
  raise NotImplementedError
end