<commit_before>require 'spec_helper'
require 'observed/configurable'

module ConfigurableSpec
  class Foo
    include Observed::Configurable

    attribute :foo, default: 123
    attribute :bar, default: 234
    attribute :baz

    default :bar => 345
  end
end

describe Observed::Configurable do

  context 'without parameters for the constructor' do
    subject {
      ConfigurableSpec::Foo.new
    }
    it 'uses default values for attributes' do
      expect(subject.foo).to eq(123)
    end
    it 'overrides default values on `attribute name, :default => default_value`' do
      expect(subject.bar).to eq(345)
    end
    it 'raises errors when attributes without values are read' do
      expect { subject.baz }.to raise_error
    end
  end

  context 'with parameters for the constructor' do
    subject {
      ConfigurableSpec::Foo.new({foo: 1, bar: 2, baz: 3})
    }
    it 'prefers values from constructor parameters over defaults' do
      expect(subject.foo).to eq(1)
      expect(subject.bar).to eq(2)
      expect(subject.baz).to eq(3)
    end
  end

  context 'configured through `configure(args)` method' do
    subject {
      foo = ConfigurableSpec::Foo.new
      foo.configure({foo: 1, bar: 2, baz: 3})
      foo
    }
    it 'prefers values from `configure(args)` over defaults' do
      expect(subject.foo).to eq(1)
      expect(subject.bar).to eq(2)
      expect(subject.baz).to eq(3)
    end
  end

end
<commit_msg>Restructure a spec with shared examples for readability<commit_after>require 'spec_helper'
require 'observed/configurable'

module ConfigurableSpec
  class Foo
    include Observed::Configurable

    attribute :foo, default: 123
    attribute :bar, default: 234
    attribute :baz

    default :bar => 345
  end
end

describe Observed::Configurable do

  context 'without parameters for the constructor' do
    subject {
      ConfigurableSpec::Foo.new
    }
    it 'uses default values for attributes' do
      expect(subject.foo).to eq(123)
    end
    it 'overrides default values on `attribute name, :default => default_value`' do
      expect(subject.bar).to eq(345)
    end
    it 'raises errors when attributes without values are read' do
      expect { subject.baz }.to raise_error
    end
  end

  context 'with parameters for the constructor' do
    subject {
      ConfigurableSpec::Foo.new({foo: 1, bar: 2, baz: 3})
    }
    it 'prefers values from constructor parameters over defaults' do
      expect(subject.foo).to eq(1)
      expect(subject.bar).to eq(2)
      expect(subject.baz).to eq(3)
    end
  end

  context 'configured through `configure(args)` method' do
    subject {
      foo = ConfigurableSpec::Foo.new
      foo.configure(args)
      foo
    }
    shared_examples_for 'values are set' do
      it 'prefers values from `configure(args)` over defaults' do
        expect(subject.foo).to eq(1)
        expect(subject.bar).to eq(2)
        expect(subject.baz).to eq(3)
      end
    end
    context 'when args has symbol keys' do
      let(:args) {
        {foo: 1, bar: 2, baz: 3}
      }
      it_behaves_like 'values are set'
    end
  end

end

{'fraction_non_alphanumeric': 0.09268762026940347, 'fraction_numerical': 0.01731879409878127, 'mean_word_length': 2.8411330049261085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4860657', 'n_tokens_mistral': 1005, 'n_tokens_neox': 935, 'n_words': 307}