Class: Oktest::Node

Inherits:
Item
  • Object
show all
Defined in:
lib/oktest.rb

Direct Known Subclasses

ScopeNode, TopicNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Item

#accept_visitor

Constructor Details

#initialize(parent, tag: nil) ⇒ Node

Returns a new instance of Node.



956
957
958
959
960
961
962
963
964
965
# File 'lib/oktest.rb', line 956

def initialize(parent, tag: nil)
  @parent   = parent
  @tag      = tag
  @children = []
  parent.add_child(self) if parent
  @context_class = Class.new(parent ? parent.context_class : Oktest::Context)
  @context_class.__node = self
  @fixtures = {}       # {name: [[param], block]}
  @hooks    = {}       # {name: block}
end

Instance Attribute Details

#context_classObject (readonly)

Returns the value of attribute context_class.



967
968
969
# File 'lib/oktest.rb', line 967

def context_class
  @context_class
end

#fixturesObject (readonly)

Returns the value of attribute fixtures.



967
968
969
# File 'lib/oktest.rb', line 967

def fixtures
  @fixtures
end

#hooksObject (readonly)

Returns the value of attribute hooks.



967
968
969
# File 'lib/oktest.rb', line 967

def hooks
  @hooks
end

#parentObject (readonly)

Returns the value of attribute parent.



967
968
969
# File 'lib/oktest.rb', line 967

def parent
  @parent
end

#tagObject (readonly)

Returns the value of attribute tag.



967
968
969
# File 'lib/oktest.rb', line 967

def tag
  @tag
end

Instance Method Details

#_repr(depth = 0, buf = String.new) ⇒ Object



1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
# File 'lib/oktest.rb', line 1055

def _repr(depth=0, buf=String.new)
  #; [!bt5j8] builds debug string.
  if depth < 0
    id_str = "%x" % self.object_id
    return "#<#{self.class.name}:0x#{id_str}>"
  end
  indent = "  " * depth
  id_str = "%x" % self.object_id
  buf << "#{indent}- #<#{self.class.name}:0x#{id_str}>\n"
  instance_variables().sort.each do |name|
    next if name.to_s == "@children"
    val = instance_variable_get(name)
    next if val.nil?
    next if val.respond_to?(:empty?) && val.empty?
    if val.respond_to?(:_repr)
      buf << "#{indent}  #{name}: #{val._repr(-1)}\n"
    else
      buf << "#{indent}  #{name}: #{val.inspect}\n"
    end
  end
  @children.each {|child| child._repr(depth+1, buf) }
  return buf
end

#add_child(child) ⇒ Object



971
972
973
974
975
976
# File 'lib/oktest.rb', line 971

def add_child(child)
  #; [!1fyk9] keeps children.
  @children << child
  #; [!w5r6l] returns self.
  self
end

#clear_childrenObject



1001
1002
1003
1004
1005
1006
# File 'lib/oktest.rb', line 1001

def clear_children()
  #; [!o8xfb] removes all children.
  @children.clear()
  #; [!cvaq1] return self.
  self
end

#each_child(&b) ⇒ Object



983
984
985
986
987
988
989
990
# File 'lib/oktest.rb', line 983

def each_child(&b)
  #; [!osoep] returns enumerator if block not given.
  return @children.each unless block_given?()
  #; [!pve8m] yields block for each child.
  @children.each(&b)
  #; [!8z6un] returns nil.
  nil
end

#get_fixture_block(name) ⇒ Object



1039
1040
1041
1042
# File 'lib/oktest.rb', line 1039

def get_fixture_block(name)
  #; [!f0105] returns fixture info.
  return @fixtures[name]
end

#get_hook_block(key) ⇒ Object



1050
1051
1052
1053
# File 'lib/oktest.rb', line 1050

def get_hook_block(key)
  #; [!u3fc6] returns block corresponding to key.
  return @hooks[key]
end

#has_child?Boolean

Returns:

  • (Boolean)


978
979
980
981
# File 'lib/oktest.rb', line 978

def has_child?
  #; [!xb30d] return true when no children, else false.
  return !@children.empty?
end

#new_context_objectObject



1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/oktest.rb', line 1021

def new_context_object()
  #; [!p271z] creates new context object.
  context = @context_class.new()
  #; [!9hbxn] context object has 'ok()' method.
  context.extend SpecHelper
  context.extend UtilHelper
  return context
end

#register_fixture_block(name, location, &block) ⇒ Object



1030
1031
1032
1033
1034
1035
1036
1037
# File 'lib/oktest.rb', line 1030

def register_fixture_block(name, location, &block)
  #; [!5ctsn] registers fixture name, block, and location.
  params = Util.required_param_names_of_block(block)  # [Symbol]
  params = nil if params.empty?
  @fixtures[name] = [block, params, location]
  #; [!hfcvo] returns self.
  self
end

#register_hook_block(key, &block) ⇒ Object



1044
1045
1046
1047
1048
# File 'lib/oktest.rb', line 1044

def register_hook_block(key, &block)
  #; [!zb66o] registers block with key.
  @hooks[key] = block
  self
end

#remove_child_at(index) ⇒ Object



992
993
994
995
996
997
998
999
# File 'lib/oktest.rb', line 992

def remove_child_at(index)
  #; [!hsomo] removes child at index.
  child = @children.delete_at(index)
  #; [!7fhx1] unlinks reference between parent and child.
  child.unlink_parent() if child
  #; [!hiz1b] returns removed child.
  return child
end

#run_block_in_context_class(&block) ⇒ Object



1016
1017
1018
1019
# File 'lib/oktest.rb', line 1016

def run_block_in_context_class(&block)
  #; [!j9qdh] run block in context class.
  @context_class.class_eval(&block)
end

#topic?Boolean

Returns:

  • (Boolean)


969
# File 'lib/oktest.rb', line 969

def topic?; false; end


1008
1009
1010
1011
1012
1013
1014
# File 'lib/oktest.rb', line 1008

def unlink_parent()
  #; [!59m52] clears '@parent' instance variable.
  #; [!qksxv] returns parent object.
  parent = @parent
  @parent = nil
  return parent
end