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.



975
976
977
978
979
980
981
982
983
984
# File 'lib/oktest.rb', line 975

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.



986
987
988
# File 'lib/oktest.rb', line 986

def context_class
  @context_class
end

#fixturesObject (readonly)

Returns the value of attribute fixtures.



986
987
988
# File 'lib/oktest.rb', line 986

def fixtures
  @fixtures
end

#hooksObject (readonly)

Returns the value of attribute hooks.



986
987
988
# File 'lib/oktest.rb', line 986

def hooks
  @hooks
end

#parentObject (readonly)

Returns the value of attribute parent.



986
987
988
# File 'lib/oktest.rb', line 986

def parent
  @parent
end

#tagObject (readonly)

Returns the value of attribute tag.



986
987
988
# File 'lib/oktest.rb', line 986

def tag
  @tag
end

Instance Method Details

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



1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
# File 'lib/oktest.rb', line 1074

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



990
991
992
993
994
995
# File 'lib/oktest.rb', line 990

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

#clear_childrenObject



1020
1021
1022
1023
1024
1025
# File 'lib/oktest.rb', line 1020

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

#each_child(&b) ⇒ Object



1002
1003
1004
1005
1006
1007
1008
1009
# File 'lib/oktest.rb', line 1002

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



1058
1059
1060
1061
# File 'lib/oktest.rb', line 1058

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

#get_hook_block(key) ⇒ Object



1069
1070
1071
1072
# File 'lib/oktest.rb', line 1069

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

#has_child?Boolean

Returns:

  • (Boolean)


997
998
999
1000
# File 'lib/oktest.rb', line 997

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

#new_context_objectObject



1040
1041
1042
1043
1044
1045
1046
1047
# File 'lib/oktest.rb', line 1040

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



1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/oktest.rb', line 1049

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



1063
1064
1065
1066
1067
# File 'lib/oktest.rb', line 1063

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

#remove_child_at(index) ⇒ Object



1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/oktest.rb', line 1011

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



1035
1036
1037
1038
# File 'lib/oktest.rb', line 1035

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

#topic?Boolean

Returns:

  • (Boolean)


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

def topic?; false; end


1027
1028
1029
1030
1031
1032
1033
# File 'lib/oktest.rb', line 1027

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