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

describe DataProvider::Base do
  # Example implementation of DataProvider::Base
  class ProviderClass
    include DataProvider::Base

    provider :sum, :requires => [:array] do
      sum = 0

      given(:array).each do |number|
        sum += number.to_i
      end

      sum
    end

    provider :static do
      'StaticValue'
    end

    provider :billy do
      take([:identification, :fullname])
    end

    provider [:identification, :firstname] do
      'Billy'
    end

    provider [:identification, :lastname] do
      'Bragg'
    end

    provider [:identification, :fullname] do
      "#{scoped_take(:firstname)} #{scoped_take(:lastname)}"
    end
  end

  let(:provider){
    ProviderClass.new(:data => {:array => [1,2,4]})
  }

  describe "#has_provider?" do
    it 'provides the has_provider? instance method' do
      expect(provider.has_provider?(:sum)).to be true
      expect(provider.has_provider?(:static)).to be true
      expect(provider.has_provider?(:modulus)).to be false
    end

    it 'provides the has_provider? class method' do
      expect(ProviderClass.respond_to?(:has_provider?)).to eq true
      expect(ProviderClass.has_provider?(:sum)).to eq true
      expect(ProviderClass.has_provider?(:divid)).to eq false
    end
  end

  describe "#has_providers_with_scope?" do
    let(:klass){
      Class.new Object do
        include DataProvider::Base
        provider [:a, :b ,:c]
        provider :unscoped
      end
    }

    it "return true if there are providers defined with an array identifier that start with the given prefix" do
      expect(klass.new.has_providers_with_scope?(:unscoped)).to eq false
      # byebug
      expect(klass.has_providers_with_scope?(:a)).to eq true
      expect(klass.new.has_providers_with_scope?([:a, :b])).to eq true
      # scope means prefix, identfier may not be exactly the given array
      expect(klass.new.has_providers_with_scope?([:a, :b, :c])).to eq false
    end
  end

  describe "#take" do
    it 'lets you take data from it' do
      expect(provider.take(:sum)).to eq 7
      expect(provider.take(:static)).to eq 'StaticValue'
    end

    it 'raise a ProviderMissingException when attempting to take from unknown provider' do
      expect{provider.take(:unknown)}.to raise_error(DataProvider::ProviderMissingException)
    end


    it 'works from within a provider block' do
      expect(provider.take(:billy)).to eq '<NAME>'
    end
  end

  describe "#try_take" do
    it "acts like #take when the specified provider is present" do
      expect(provider.try_take(:sum)).to eq 7
    end

    it "returns nil when the specified provider is not found" do
      expect(provider.try_take(:square_root)).to eq nil
    end
  end

  describe "#scope" do
    it 'gives providers the current scope' do
      # create a temp class  with one provider that uses #scope
      klass = Class.new Object do
        include DataProvider::Base
        provider [:a, :b ,:c] do
          scope
        end
      end

      expect(klass.new.take([:a,:b,:c])).to eq [:a,:b]
    end
  end

  describe "#scoped_take" do
    it 'lets a provider call providers within its own scope' do
      expect(provider.take([:identification, :fullname])).to eq '<NAME>'
    end
    # it 'lets attribute providers'<commit_msg>spec to verify that #scope can be used to access 'parent provider'<commit_after>require File.dirname(__FILE__) + '/spec_helper'

describe DataProvider::Base do
  # Example implementation of DataProvider::Base
  class ProviderClass
    include DataProvider::Base

    provider :sum, :requires => [:array] do
      sum = 0

      given(:array).each do |number|
        sum += number.to_i
      end

      sum
    end

    provider :static do
      'StaticValue'
    end

    provider :billy do
      take([:identification, :fullname])
    end

    provider [:identification, :firstname] do
      'Billy'
    end

    provider [:identification, :lastname] do
      'Bragg'
    end

    provider [:identification, :fullname] do
      "#{scoped_take(:firstname)} #{scoped_take(:lastname)}"
    end
  end

  let(:provider){
    ProviderClass.new(:data => {:array => [1,2,4]})
  }

  describe "#has_provider?" do
    it 'provides the has_provider? instance method' do
      expect(provider.has_provider?(:sum)).to be true
      expect(provider.has_provider?(:static)).to be true
      expect(provider.has_provider?(:modulus)).to be false
    end

    it 'provides the has_provider? class method' do
      expect(ProviderClass.respond_to?(:has_provider?)).to eq true
      expect(ProviderClass.has_provider?(:sum)).to eq true
      expect(ProviderClass.has_provider?(:divid)).to eq false
    end
  end

  describe "#has_providers_with_scope?" do
    let(:klass){
      Class.new Object do
        include DataProvider::Base
        provider [:a, :b ,:c]
        provider :unscoped
      end
    }

    it "return true if there are providers defined with an array identifier that start with the given prefix" do
      expect(klass.new.has_providers_with_scope?(:unscoped)).to eq false
      # byebug
      expect(klass.has_providers_with_scope?(:a)).to eq true
      expect(klass.new.has_providers_with_scope?([:a, :b])).to eq true
      # scope means prefix, identfier may not be exactly the given array
      expect(klass.new.has_providers_with_scope?([:a, :b, :c])).to eq false
    end
  end

  describe "#take" do
    it 'lets you take data from it' do
      expect(provider.take(:sum)).to eq 7
      expect(provider.take(:static)).to eq 'StaticValue'
    end

    it 'raise a ProviderMissingException when attempting to take from unknown provider' do
      expect{provider.take(:unknown)}.to raise_error(DataProvider::ProviderMissingException)
    end


    it 'works from within a provider block' do
      expect(provider.take(:billy)).to eq 'Billy Bragg'
    end
  end

  describe "#try_take" do
    it "acts like #take when the specified provider is present" do
      expect(provider.try_take(:sum)).to eq 7
    end

    it "returns nil when the specified provider is not found" do
      expect(provider.try_take(:square_root)).to eq nil
    end
  end

  describe "#scope" do
    let(:klass){
      Class.new Object do
        include DataProvider::Base
        provider [:a, :b] do
          'woeha!'
        end
        provider [:a, :b ,:c] do
          scope
        end

        provider [:a, :b ,:eq] do
          take scope
        end
      end
    }

    it 'gives providers the current scope' do
      expect(klass.new.take([:a,:b,:c])).to eq [:a,:b]
    end

    it "can be used by providers to call the 'parent provider'" do
      expect(klass.new.take([:a,:b,:eq])).to eq klass.new.take([:a,:b])
    end
  end

  describe "#scoped_take" do
    it 'lets a provider call providers within its own scope' do
      expect(provider.take([:identification, :fullname])).to eq '<NAME>'
    end
    # it 'lets attribute providers'
{'fraction_non_alphanumeric': 0.10875483593638056, 'fraction_numerical': 0.0017194440464249892, 'mean_word_length': 2.899441340782123, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 12, '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': '7028184', 'n_tokens_mistral': 2160, 'n_tokens_neox': 2115, 'n_words': 647}