<commit_before>require_relative '../spec_helper'

RSpec.describe 'Box' do
  def define_class name, &block
    stub_const(name, Class.new).tap do |c|
      c.class_eval(&block)
    end
  end

  before do
    define_class 'UsersCollection' do
      attr_accessor :logger

      def initialize args
        args.each { |k,v| send "#{k}=", v }
      end
    end

    define_class 'Logger' do
      attr_accessor :level

      def initialize args
        args.each { |k,v| send "#{k}=", v }
      end
    end

    define_class 'UsersApi' do
      class << self
        attr_accessor :logger
      end
    end
  end

  let(:users_collection) { UsersCollection.new logger: logger }
  let(:logger) { Logger.new level: log_level }

  let(:users_api) do
    UsersApi.tap do |api|
      api.logger = logger
    end
  end

  def log_level
    'INFO'
  end

  describe 'log_level' do
    it 'is a new instance every time' do
      expect(log_level).to eq 'INFO'
      expect(log_level).not_to be log_level
    end

    it 'has the log_level' do
      expect(logger.level).to eq log_level
    end

    it 'has memoized log_level' do
      expect(logger.level).to be logger.level
    end
  end

  describe 'users_collection' do
    it 'exists' do
      expect(users_collection).to be_a UsersCollection
    end

    it 'has a logger' do
      expect(users_collection.logger).to be_a Logger
    end

    it 'memoizes the logger' do
      expect(users_collection.logger).to be users_collection.logger
    end
  end
<commit_msg>Minumun basic behavior of the Box implemented<commit_after>require_relative '../spec_helper'

RSpec.describe 'Box' do
  def define_class name, &block
    stub_const(name, Class.new).tap do |c|
      c.class_eval(&block)
    end
  end

  before do
    define_class 'UsersCollection' do
      attr_accessor :logger

      def initialize args
        args.each { |k,v| send "#{k}=", v }
      end
    end

    define_class 'Logger' do
      def initialize args
        args.each { |k,v| send "#{k}=", v }
      end

      def level
        config[:level]
      end

      def level= value
        config[:level] = value
      end

      def config
        @config ||= {}
      end
    end

    define_class 'UsersApi' do
      class << self
        attr_accessor :logger
      end
    end

    define_class 'Server' do
      def initialize config
        @config = config
      end

      def logger
        @config.logger
      end
    end

    define_class 'MainBox' do
      class << self
        def get name, &block
          define_method name, &block
        end

        def let name, &block
          define_method name do
            var_name = "@#{name}"

            if value = instance_variable_get(var_name)
              value
            else
              instance_variable_set var_name, instance_eval(&block)
            end
          end
        end
      end

      get(:log_level) { 'INFO' }
      let(:logger) { Logger.new level: log_level }
      let(:server) { Server.new self }
    end
  end

  let(:users_collection) { UsersCollection.new logger: logger }
  let(:logger) { box.logger }

  let(:users_api) do
    UsersApi.tap do |api|
      api.logger = logger
    end
  end

  let(:box) { MainBox.new }

  def log_level
    box.log_level
  end

  describe 'box' do
    it 'memoizes' do
      expect(box.logger).to be box.logger
    end

    it 'doesn\'t share between instances' do
      expect(box.logger).not_to be MainBox.new.logger
    end
  end

  describe 'server' do
    it 'is configured' do
      expect(box.server.logger).to be box.logger
    end
  end

  describe 'logger' do
    it 'is a new instance every time' do
      expect(log_level).to eq 'INFO'
      expect(log_level).not_to be log_level
    end

    it 'has the log_level' do
      expect(logger.level).to eq log_level
    end

    it 'has memoized log_level' do
      expect(logger.level).to be logger.level
    end
  end

  describe 'users_collection' do
    it 'exists' do
      expect(users_collection).to be_a UsersCollection
    end

    it 'has a logger' do
      expect(users_collection.logger).to be_a Logger
    end

    it 'memoizes the logger' do
      expect(users_collection.logger).to be users_collection.logger
    end
  end

{'fraction_non_alphanumeric': 0.08386336866902237, 'fraction_numerical': 0.0, 'mean_word_length': 2.103801169590643, 'pattern_counts': {'":': 0, '<': 9, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3076935', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1323, 'n_words': 399}