class Campa::Core::Test

Searches functions with prefix test_ or test- (case insenstive) in a given context, invoke those and store their results in a Data Structure with the given form:

(
  (success, (test-one, test-two)),
  (failures, (test-three, test-four))
)

In this example we are considering that functions test-one and test-two returned true and functions test-three and test-four returned false.

Constants

TEST_REGEXP

Attributes

evaler[R]

Public Class Methods

new() click to toggle source
# File lib/campa/core/test.rb, line 16
def initialize
  @evaler = Campa::Evaler.new
end

Public Instance Methods

call(*tests, env:) click to toggle source

Execute functions named test-* or test_* (case insentive) and collect the results.

The param tests can be used to filter specific tests to be executed. For a context where functions test-great-one, test-great-two and test-awful exists, if we want to execute only the great ones, we could do:

@example

test = Test.new
test.call("great", env: ctx)

@param tests [Array<String>] if given will be used to “filter”

functions by name

@param env [Context] will be used to search functions

named test-* or test_* (case insentive) and also
to execute those functions
# File lib/campa/core/test.rb, line 38
def call(*tests, env:)
  summary = execute_all(tests, env)
  List.new(
    List.new(Symbol.new("success"), List.new(*summary[:success])),
    List.new(Symbol.new("failures"), List.new(*summary[:failures]))
  )
end

Private Instance Methods

add_to_summary(summary, sym, func, env) click to toggle source
# File lib/campa/core/test.rb, line 70
def add_to_summary(summary, sym, func, env)
  type = safely_execute(func, env) ? :success : :failures
  summary[type] << sym
end
execute_all(tests, env) click to toggle source
# File lib/campa/core/test.rb, line 52
def execute_all(tests, env)
  { success: [], failures: [] }.tap do |summary|
    env
      .bindings
      .select { |(sym, object)| test_func?(sym, object) }
      .select { |(sym, _)| included?(tests, sym) }
      .each { |(sym, fn)| add_to_summary(summary, sym, fn, env) }
  end
end
included?(tests, sym) click to toggle source
# File lib/campa/core/test.rb, line 66
def included?(tests, sym)
  tests.empty? || tests.include?(TEST_REGEXP.match(sym.label)[2])
end
safely_execute(func, env) click to toggle source
# File lib/campa/core/test.rb, line 75
def safely_execute(func, env)
  func.call(env: env)
rescue StandardError
  false
end
test_func?(sym, object) click to toggle source
# File lib/campa/core/test.rb, line 62
def test_func?(sym, object)
  TEST_REGEXP.match?(sym.label) && object.respond_to?(:call)
end