<commit_before>require 'compendium'
require 'compendium/dsl'

describe Compendium::DSL do
  subject do
    Class.new do
      extend Compendium::DSL
    end
  end

  describe "#option" do
    before { subject.option :starting_on, :date }

    its(:options) { should include :starting_on }
    specify { subject.options[:starting_on].should be_date }

    it "should allow previously defined options to be redefined" do
      subject.option :starting_on, :boolean
      subject.options[:starting_on].should be_boolean
      subject.options[:starting_on].should_not be_date
    end

    it "should allow overriding default value" do
      proc = -> { Date.new(2013, 6, 1) }
      subject.option :starting_on, :date, default: proc
      subject.options[:starting_on].default.should == proc
    end
  end

  describe "#query" do
    let(:report_class) do
      Class.new(Compendium::Report) do
        query :test
      end
    end

    subject { report_class }

    its(:queries) { should include :test }

    it "should relate the new query back to the report instance" do
      r = subject.new
      r.test.report.should == r
    end

    it "should not relate a query to the report class" do
      subject.test.report.should be_nil
    end

    context "when given a through option" do
      before { report_class.query :through, through: :test }
      subject { report_class.queries[:through] }

      it { should be_a Compendium::ThroughQuery }
      its(:through) { should == [:test] }
    end

    context "when given a collection option" do
      before { report_class.query :collection, collection: [] }
      subject { report_class.queries[:collection] }

      it { should be_a Compendium::CollectionQuery }
    end
  end

  describe "#chart" do
    before { subject.chart(:chart) }

    its(:queries) { should include :chart }
  end

  describe "#data" do
    before { subject.data(:data) }

    its(:queries) { should include :data }
  end

  describe "#metric" do
    let(:metric_proc) { ->{ :metric } }

    before do
      subject.query :test
      subject.metric :test_metric, metric_proc, through: :test
    end

    it "should add a metric to the given query" do
      subject.queries[:test].metrics.first.name.should == :test_metric
    end

    it "should set the metric command" do
      subject.queries[:test].metrics.first.command.should == metric_proc
    end
<commit_msg>Add DSL spec to ensure CollectionQueries are built properly<commit_after>require 'compendium'
require 'compendium/dsl'

describe Compendium::DSL do
  subject do
    Class.new do
      extend Compendium::DSL
    end
  end

  describe "#option" do
    before { subject.option :starting_on, :date }

    its(:options) { should include :starting_on }
    specify { subject.options[:starting_on].should be_date }

    it "should allow previously defined options to be redefined" do
      subject.option :starting_on, :boolean
      subject.options[:starting_on].should be_boolean
      subject.options[:starting_on].should_not be_date
    end

    it "should allow overriding default value" do
      proc = -> { Date.new(2013, 6, 1) }
      subject.option :starting_on, :date, default: proc
      subject.options[:starting_on].default.should == proc
    end
  end

  describe "#query" do
    let(:report_class) do
      Class.new(Compendium::Report) do
        query :test
      end
    end

    subject { report_class }

    its(:queries) { should include :test }

    it "should relate the new query back to the report instance" do
      r = subject.new
      r.test.report.should == r
    end

    it "should not relate a query to the report class" do
      subject.test.report.should be_nil
    end

    context "when given a through option" do
      before { report_class.query :through, through: :test }
      subject { report_class.queries[:through] }

      it { should be_a Compendium::ThroughQuery }
      its(:through) { should == [:test] }
    end

    context "when given a collection option" do
      subject { report_class.queries[:collection] }

      context "that is an enumerable" do
        before { report_class.query :collection, collection: [] }

        it { should be_a Compendium::CollectionQuery }
      end

      context "that is a symbol" do
        let(:query) { double("Query") }

        before do
          Compendium::Query.any_instance.stub(:get_associated_query).with(:query).and_return(query)
          report_class.query :collection, collection: :query
        end

        its(:collection) { should == query }
      end

      context "that is a query" do
        let(:query) { Compendium::Query.new(:query, {}, ->{}) }
        before { report_class.query :collection, collection: query }

        its(:collection) { should == query }
      end
    end
  end

  describe "#chart" do
    before { subject.chart(:chart) }

    its(:queries) { should include :chart }
  end

  describe "#data" do
    before { subject.data(:data) }

    its(:queries) { should include :data }
  end

  describe "#metric" do
    let(:metric_proc) { ->{ :metric } }

    before do
      subject.query :test
      subject.metric :test_metric, metric_proc, through: :test
    end

    it "should add a metric to the given query" do
      subject.queries[:test].metrics.first.name.should == :test_metric
    end

    it "should set the metric command" do
      subject.queries[:test].metrics.first.command.should == metric_proc
    end

{'fraction_non_alphanumeric': 0.10018382352941177, 'fraction_numerical': 0.0022058823529411764, 'mean_word_length': 2.775850104094379, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '5584244', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1567, 'n_words': 480}