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.



663
664
665
666
667
668
669
670
671
672
# File 'lib/oktest.rb', line 663

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.



674
675
676
# File 'lib/oktest.rb', line 674

def context_class
  @context_class
end

#fixturesObject (readonly)

Returns the value of attribute fixtures.



674
675
676
# File 'lib/oktest.rb', line 674

def fixtures
  @fixtures
end

#hooksObject (readonly)

Returns the value of attribute hooks.



674
675
676
# File 'lib/oktest.rb', line 674

def hooks
  @hooks
end

#parentObject (readonly)

Returns the value of attribute parent.



674
675
676
# File 'lib/oktest.rb', line 674

def parent
  @parent
end

#tagObject (readonly)

Returns the value of attribute tag.



674
675
676
# File 'lib/oktest.rb', line 674

def tag
  @tag
end

Instance Method Details

#_repr(depth = 0, buf = "") ⇒ Object



761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/oktest.rb', line 761

def _repr(depth=0, buf="")
  #; [!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



678
679
680
681
682
683
# File 'lib/oktest.rb', line 678

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

#clear_childrenObject



708
709
710
711
712
713
# File 'lib/oktest.rb', line 708

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

#each_child(&b) ⇒ Object



690
691
692
693
694
695
696
697
# File 'lib/oktest.rb', line 690

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



745
746
747
748
# File 'lib/oktest.rb', line 745

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

#get_hook_block(key) ⇒ Object



756
757
758
759
# File 'lib/oktest.rb', line 756

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

#has_child?Boolean

Returns:

  • (Boolean)


685
686
687
688
# File 'lib/oktest.rb', line 685

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

#new_context_objectObject



728
729
730
731
732
733
734
# File 'lib/oktest.rb', line 728

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

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



736
737
738
739
740
741
742
743
# File 'lib/oktest.rb', line 736

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



750
751
752
753
754
# File 'lib/oktest.rb', line 750

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

#remove_child_at(index) ⇒ Object



699
700
701
702
703
704
705
706
# File 'lib/oktest.rb', line 699

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



723
724
725
726
# File 'lib/oktest.rb', line 723

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

#topic?Boolean

Returns:

  • (Boolean)


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

def topic?; false; end


715
716
717
718
719
720
721
# File 'lib/oktest.rb', line 715

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