class Tree
Represents a tree structure of test contexts.
Definitions
def initialize(context)
Initialize a new Tree.
Signature
-
parameter
contextObject The root context.
Implementation
def initialize(context)
@context = context
end
def traverse(current = @context, &block)
Traverse the tree, yielding each context.
Signature
-
parameter
currentObject The current context (defaults to root).
-
yields
{|context| ...} Each context in the tree.
-
returns
Hash A hash representation of the tree.
Implementation
def traverse(current = @context, &block)
node = {}
node[:self] = yield(current)
if children = current.children # and children.any?
node[:children] = children.values.map do |context|
self.traverse(context, &block)
end
end
return node
end
def to_json(options = nil)
Convert the tree to JSON.
Signature
-
parameter
optionsHash, nil Options to pass to JSON.generate.
-
returns
String A JSON representation of the tree.
Implementation
def to_json(options = nil)
traverse do |context|
[context.identity.to_s, context.description.to_s, context.leaf?]
end.to_json(options)
end