<commit_before>require 'object_table'
require 'object_table/view'

require 'support/object_table_example'

describe ObjectTable::View do
  it_behaves_like 'an object table', ObjectTable::View

  describe '#columns' do
    let(:table){ ObjectTable.new(col1: [1, 2, 3], col2: 5) }

    subject{ ObjectTable::View.new(table){ col1 > 2 } }

    it 'should mask the columns of the parent table' do
      mask = table.col1 > 2
      table.columns.each do |k, v|
        expect(subject.columns[k]).to eql v[mask]
      end
    end
  end

  describe '#[]=' do
    let(:table) {  ObjectTable.new(col1: [1, 2, 3], col2: 5) }
    let(:view)  { ObjectTable::View.new(table){ col1 > 1 } }

    let(:column){ :col1 }
    let(:value) { 100 }

    subject{ view[column] = value; view }

    context 'on an existing column' do
      it 'should assign values to the column' do
        expect(subject.columns[column].to_a).to eql [value] * subject.nrows
      end

      it 'should not modify anything outside the view' do
        subject
        expect(table.columns[column].to_a).to eql [1, value, value]
      end
    end

  end

end<commit_msg>spec for assignment on a view<commit_after>require 'object_table'
require 'object_table/view'

require 'support/object_table_example'

describe ObjectTable::View do
  it_behaves_like 'an object table', ObjectTable::View

  describe '#columns' do
    let(:table){ ObjectTable.new(col1: [1, 2, 3], col2: 5) }

    subject{ ObjectTable::View.new(table){ col1 > 2 } }

    it 'should mask the columns of the parent table' do
      mask = table.col1 > 2
      table.columns.each do |k, v|
        expect(subject.columns[k]).to eql v[mask]
      end
    end
  end

  describe '#[]=' do
    let(:table) {  ObjectTable.new(col1: [0, 1, 2, 3], col2: 5) }
    let(:view)  { ObjectTable::View.new(table){ col1 > 0 } }

    let(:column){ :col1 }
    let(:value) { [10, 20, 30] }

    subject{ view[column] = value; view }

    context 'on an existing column' do
      it 'should assign values to the column' do
        expect(subject.columns[column].to_a).to eql value
      end

      it 'should not modify anything outside the view' do
        subject
        expect(table.columns[column].to_a).to eql [0] + value
      end

    end

    context 'with a scalar' do
      let(:value){ 10 }
      it 'should fill the column with that value' do
        expect(subject.columns[column].to_a).to eql ([value] * subject.nrows)
      end
    end

    context 'with the wrong length' do
      it 'should fail' do
        expect{subject[column] = [1, 2]}.to raise_error
      end
    end

    context 'for a new column' do
      let(:column){ :col3 }

      it 'should create a new column' do
        expect(subject.columns).to include column
        expect(subject.columns[column].to_a).to eql value
      end

      it 'should affect the parent table' do
        subject
        expect(table.columns).to include column
      end

      it 'should fill values outside the view with nil' do
        subject
        expect(table.columns[column].to_a).to eql [nil] + value
      end
    end

  end

end
{'fraction_non_alphanumeric': 0.11579285944033452, 'fraction_numerical': 0.017690575747828882, 'mean_word_length': 2.624708624708625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '496407', 'n_tokens_mistral': 1044, 'n_tokens_neox': 983, 'n_words': 314}