<commit_before>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe SexpAnalysis::SingleLHSExpression do
  describe '#name' do
    it 'extracts the name from the identifier wrapped by the SingleLHSExpression' do
      tree = Sexp.new([:var_field, [:@ident, "a", [1, 0]]])
      ExpandedIdentifierAnnotation.new.annotate!(tree)
      SingleLHSExpression.new(tree).names.should == ['a']
    end
  end
  
  describe '#assignment_pairs' do
    it 'should simply pair the given values to the LHS node' do
      tree = Sexp.new([:var_field, [:@ident, "$b", [1, 0]]])
      ExpandedIdentifierAnnotation.new.annotate!(tree)
      SingleLHSExpression.new(tree).assignment_pairs(4).should == [[tree, 4]]
      SingleLHSExpression.new(tree).assignment_pairs([1, 2]).should == [[tree, [1, 2]]]
    end
  end
end

describe SexpAnalysis::MultipleLHSExpression do
  describe '#name' do
    it 'extracts the names from a simple set of expressions' do
      input = 'a, b, c, d = nil'
      tree = Sexp.new(Ripper.sexp(input))[1][0][1]
      ExpandedIdentifierAnnotation.new.annotate!(tree)
      MultipleLHSExpression.new(tree).names.should == ['a', 'b', 'c', 'd']
    end
    it 'extracts all the names from the subexpressions' do
      input = 'a, (Z, ((b, $f, j), (i, p)), *d, e), *k, $l = nil'
      tree = Sexp.new(Ripper.sexp(input))[1][0][1]
      ExpandedIdentifierAnnotation.new.annotate!(tree)
      MultipleLHSExpression.new(tree).names.should == ['a', 'Z', 'b', '$f', 'j', 'i', 'p', 'd', 'e', 'k', '$l']
    end
  end
  
  describe '#assignment_pairs' do
    it 'should pair off each constituent element with a value' do
      outputs = Annotations.annotate_inputs(
          [['(stdin)', 'a, b, c, d = 1, 2, [3, 4]']])
      lhs = MultipleLHSExpression.new(outputs[0][1][1][0][1])
      rhs = MultipleRHSExpression.new(outputs[0][1][1][0][2])
      lvals = outputs[0][1][1][0][1]
      lhs.assignment_pairs(rhs.constant_values).should ==
          [[lvals[0], 1], [lvals[1], 2], [lvals[2], [3, 4]], [lvals[3], nil]]
    end
    
    it 'should pair off each constituent element with a value with subassignment' do
      outputs = Annotations.annotate_inputs(
          [['(stdin)', 'a, b, (c, d) = 1, 2, [3, 4]']])
      lhs = MultipleLHSExpression.new(outputs[0][1][1][0][1])
      rhs = MultipleRHSExpression.new(outputs[0][1][1][0][2])
      lvals = outputs[0][1][1][0][1]
      lhs.assignment_pairs(rhs.constant_values).should ==
          [[lvals[0], 1], [lvals[1], 2], [lvals[2][1][0], 3], [lvals[2][1][1], 4]]
    end
    
    it 'can pair off with nested subassignment' do
      outputs = Annotations.annotate_inputs(
          [['(stdin)', 'a, b, (c, (d, f), g), e = 1, 2, [[9, 8], [3, 4], 5], 6']])
      lhs = MultipleLHSExpression.new(outputs[0][1][1][0][1])
      rhs = MultipleRHSExpression.new(outputs[0][1][1][0][2])
      lvals = outputs[0][1][1][0][1]
      lhs.assignment_pairs(rhs.constant_values).should ==
          [[lvals[0], 1], [lvals[1], 2], [lvals[2][1][0], [9, 8]],
           [lvals[2][1][1][1][0], 3], [lvals[2][1][1][1][1], 4], [lvals[2][1][2], 5],
           [lvals[3], 6]]
    end
    
    it 'works with splats and no subassignments' do
      outputs = Annotations.annotate_inputs(
          [['(stdin)', 'a, *b, c, d = 1, 2, [3, 4]']])
      lhs = MultipleLHSExpression.new(outputs[0][1][1][0][1])
      rhs = MultipleRHSExpression.new(outputs[0][1][1][0][2])
      lvals = outputs[0][1][1][0][1]
      lhs.assignment_pairs(rhs.constant_values).should == [[lvals[1][0], 1], [lvals[2], []], [lvals[3][0], 2], [lvals[3][1], [3, 4]]]
    end<commit_msg>Added specs for nested splats and subassignments! And it *works*!<commit_after>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe SexpAnalysis::SingleLHSExpression do
  describe '#name' do
    it 'extracts the name from the identifier wrapped by the SingleLHSExpression' do
      tree = Sexp.new([:var_field, [:@ident, "a", [1, 0]]])
      ExpandedIdentifierAnnotation.new.annotate!(tree)
      SingleLHSExpression.new(tree).names.should == ['a']
    end
  end
  
  describe '#assignment_pairs' do
    it 'should simply pair the given values to the LHS node' do
      tree = Sexp.new([:var_field, [:@ident, "$b", [1, 0]]])
      ExpandedIdentifierAnnotation.new.annotate!(tree)
      SingleLHSExpression.new(tree).assignment_pairs(4).should == [[tree, 4]]
      SingleLHSExpression.new(tree).assignment_pairs([1, 2]).should == [[tree, [1, 2]]]
    end
  end
end

describe SexpAnalysis::MultipleLHSExpression do
  describe '#name' do
    it 'extracts the names from a simple set of expressions' do
      input = 'a, b, c, d = nil'
      tree = Sexp.new(Ripper.sexp(input))[1][0][1]
      ExpandedIdentifierAnnotation.new.annotate!(tree)
      MultipleLHSExpression.new(tree).names.should == ['a', 'b', 'c', 'd']
    end
    it 'extracts all the names from the subexpressions' do
      input = 'a, (Z, ((b, $f, j), (i, p)), *d, e), *k, $l = nil'
      tree = Sexp.new(Ripper.sexp(input))[1][0][1]
      ExpandedIdentifierAnnotation.new.annotate!(tree)
      MultipleLHSExpression.new(tree).names.should == ['a', 'Z', 'b', '$f', 'j', 'i', 'p', 'd', 'e', 'k', '$l']
    end
  end
  
  describe '#assignment_pairs' do
    it 'should pair off each constituent element with a value' do
      outputs = Annotations.annotate_inputs(
          [['(stdin)', 'a, b, c, d = 1, 2, [3, 4]']])
      lhs = MultipleLHSExpression.new(outputs[0][1][1][0][1])
      rhs = MultipleRHSExpression.new(outputs[0][1][1][0][2])
      lvals = outputs[0][1][1][0][1]
      lhs.assignment_pairs(rhs.constant_values).should ==
          [[lvals[0], 1], [lvals[1], 2], [lvals[2], [3, 4]], [lvals[3], nil]]
    end
    
    it 'should pair off each constituent element with a value with subassignment' do
      outputs = Annotations.annotate_inputs(
          [['(stdin)', 'a, b, (c, d) = 1, 2, [3, 4]']])
      lhs = MultipleLHSExpression.new(outputs[0][1][1][0][1])
      rhs = MultipleRHSExpression.new(outputs[0][1][1][0][2])
      lvals = outputs[0][1][1][0][1]
      lhs.assignment_pairs(rhs.constant_values).should ==
          [[lvals[0], 1], [lvals[1], 2], [lvals[2][1][0], 3], [lvals[2][1][1], 4]]
    end
    
    it 'can pair off with nested subassignment' do
      outputs = Annotations.annotate_inputs(
          [['(stdin)', 'a, b, (c, (d, f), g), e = 1, 2, [[9, 8], [3, 4], 5], 6']])
      lhs = MultipleLHSExpression.new(outputs[0][1][1][0][1])
      rhs = MultipleRHSExpression.new(outputs[0][1][1][0][2])
      lvals = outputs[0][1][1][0][1]
      lhs.assignment_pairs(rhs.constant_values).should ==
          [[lvals[0], 1], [lvals[1], 2], [lvals[2][1][0], [9, 8]],
           [lvals[2][1][1][1][0], 3], [lvals[2][1][1][1][1], 4], [lvals[2][1][2], 5],
           [lvals[3], 6]]
    end
    
    it 'works with unmet splats and no subassignments' do
      outputs = Annotations.annotate_inputs(
          [['(stdin)', 'a, *b, c, d = 1, 2, [3, 4]']])
      lhs = MultipleLHSExpression.new(outputs[0][1][1][0][1])
      rhs = MultipleRHSExpression.new(outputs[0][1][1][0][2])
      lvals = outputs[0][1][1][0][1]
      lhs.assignment_pairs(rhs.constant_values).should ==
          [[lvals[1][0], 1], [lvals[2], []], [lvals[3][0], 2], [lvals[3][1], [3, 4]]]
    end
    
    it 'works with met splats and no subassignments' do
      outputs = Annotations.annotate_inputs(
          [['(stdin)', 'a, *b, c, d = 1, 2, 3, 4, 5, 6, [3, 4]']])
      lhs = MultipleLHSExpression.new(outputs[0][1][1][0][1])
      rhs = MultipleRHSExpression.new(outputs[0][1][1][0][2])
      lvals = outputs[0][1][1][0][1]
      lhs.assignment_pairs(rhs.constant_values).should ==
          [[lvals[1][0], 1], [lvals[2], [2, 3, 4, 5]], [lvals[3][0], 6], [lvals[3][1], [3, 4]]]
    end
    
    it 'works with nested splats and subassignments' do
      outputs = Annotations.annotate_inputs(
          [['(stdin)', 'a, *b, (c, *d, e) = 1, 2, 3, [4, 5, 6, 3, 4]']])
      lhs = MultipleLHSExpression.new(outputs[0][1][1][0][1])
      rhs = MultipleRHSExpression.new(outputs[0][1][1][0][2])
      lvals = outputs[0][1][1][0][1]
      lhs.assignment_pairs(rhs.constant_values).should ==
          [[lvals[1][0], 1], [lvals[2], [2, 3]], [lvals[3][0][1][1][0], 4],
           [lvals[3][0][1][2], [5, 6, 3]], [lvals[3][0][1][3][0], 4]]
    end
{'fraction_non_alphanumeric': 0.20833333333333334, 'fraction_numerical': 0.04731027857829011, 'mean_word_length': 3.4563937934724454, 'pattern_counts': {'":': 0, '<': 3, '<?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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2761016', 'n_tokens_mistral': 3285, 'n_tokens_neox': 3171, 'n_words': 702}