<commit_before>class Source
  attr_accessor :children, :parent

  def initialize(parent=:root)
    @children = []
    @parent = parent
  end

  def addChild(child)
    child.parent = self
    self.children << child
  end

  alias :<<  :addChild

  def addChildren(children)
    children.each do |child|
      addChild(child)
    end
  end

  def pre_walk(&block)
    values = [ block.call(self) ]
    children.each do |chld| 
      values += chld.pre_walk(&block) 
    end
    values
  end

  def post_walk(&block)
    values = []
    children.each do |chld| 
      values += chld.post_walk(&block) 
    end
    values += [ block.call(self) ]
    values
  end
end

class RoxxDefinition < Source
end

class Renderer
end

describe Source do
  context "#children" do
    it "is initialized with an array" do
      subject.children.should == []
    end
  end
  context "#parent" do
    it "is initialized with symbol :root" do
      subject.parent.should == :root
    end
  end
  context "#addChild" do
    let(:added_source) {Source.new}
    let(:root) do
      subject.addChild(added_source)    
      subject
    end
    it "adds a child" do
      root.children[0].should == added_source
    end
    it "sets the parent" do
      root.children[0].parent.should == root
    end
  end

  context "#<<" do
    it "adds children" do
      root = Source.new
      root << ( added_source = Source.new )

      root.children.first.should == added_source 
    end
  end

  context "#addChildren" do
    it "should call addChild number of times" do
      subject.should_receive(:addChild).exactly(3).times
      subject.addChildren([stub,stub,stub])
    end
  end

  context "walking the tree" do
    def generate_sources count, parent=nil
      Array.new(count) { Source.new(parent) }
    end
    let(:children0) { generate_sources(2) }
    let(:children1) { generate_sources(2) }
    let(:root) do
      root = Source.new()
      root.addChildren( children0 )
      root.children[0].addChildren( children1 )
      root
    end

    context "#pre_walk" do
      it "traverses the tree head first" do
        values = root.pre_walk do |chld|
          chld.object_id
        end
        values.should == ( [root] + 
                             [ children0[0] ] + 
                                 children1 + 
                             children0[1..-1] ).map(&:object_id)
      end
    end

    context "#post_walk" do
      it "traverses the tree head last" do
        values = root.post_walk do |chld|
          chld.object_id
        end
        values.should == ( children1 + children0 + [root] ).map(&:object_id)
      end
    end

  end
end

describe RoxxDefinition do
  it "has children" do
    subject.children.should == []
  end
end

describe Renderer do
end<commit_msg>rubified method calls and abstracted to composable<commit_after>class Composable
  attr_accessor :children, :parent

  def initialize(parent=:root)
    @children = []
    @parent = parent
  end

  def add_child(child)
    child.parent = self
    self.children << child
  end

  alias :<<  :add_child

  def add_children(children)
    children.each do |child|
      add_child(child)
    end
  end

  def pre_walk(&block)
    values = [ block.call(self) ]
    children.each do |chld| 
      values += chld.pre_walk(&block) 
    end
    values
  end

  def post_walk(&block)
    values = []
    children.each do |chld| 
      values += chld.post_walk(&block) 
    end
    values += [ block.call(self) ]
    values
  end
end


class Sound < Composable
end

class AudioMix < Composable
end

class Renderer
end

describe Composable do
  context "#children" do
    it "is initialized with an array" do
      subject.children.should == []
    end
  end
  context "#parent" do
    it "is initialized with symbol :root" do
      subject.parent.should == :root
    end
  end
  context "#add_child" do
    let(:added_source) {described_class.new}
    let(:root) do
      subject.add_child(added_source)    
      subject
    end
    it "adds a child" do
      root.children[0].should == added_source
    end
    it "sets the parent" do
      root.children[0].parent.should == root
    end
  end

  context "#<<" do
    it "adds a child" do
      root = described_class.new
      root << ( added_source = described_class.new )

      root.children.first.should == added_source 
    end
  end

  context "#add_children" do
    it "should call add_child number of times" do
      subject.should_receive(:add_child).exactly(3).times
      subject.add_children([stub,stub,stub])
    end
  end

  context "walking the tree" do
    def generate_sources count, parent=nil
      Array.new(count) { described_class.new(parent) }
    end
    let(:children0) { generate_sources(2) }
    let(:children1) { generate_sources(2) }
    let(:root) do
      root = described_class.new()
      root.add_children( children0 )
      root.children[0].add_children( children1 )
      root
    end

    context "#pre_walk" do
      it "traverses the tree head first" do
        values = root.pre_walk do |chld|
          chld.object_id
        end
        values.should == ( [root] + 
                             [ children0[0] ] + 
                                 children1 + 
                             children0[1..-1] ).map(&:object_id)
      end
    end

    context "#post_walk" do
      it "traverses the tree head last" do
        values = root.post_walk do |chld|
          chld.object_id
        end
        values.should == ( children1 + children0 + [root] ).map(&:object_id)
      end
    end

  end
end

describe AudioMix do
  it "has children" do
    subject.children.should == []
  end
end

describe Renderer do
end
{'fraction_non_alphanumeric': 0.09788359788359788, 'fraction_numerical': 0.006349206349206349, 'mean_word_length': 2.1913337084974676, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5860387', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1768, 'n_words': 484}