<commit_before>require 'spec_helper'

module Adrift
  describe Pattern do
    before do 
      @tags = Pattern.tags.dup
      Pattern.tags.clear
    end

    after do
      Pattern.tags.replace(@tags)
    end

    let(:class_tag) { double('class tag', :label => ':class', :specialize => 'my_app/models/users') }
    let(:class_name_tag) { double('class name tag', :label => ':class_name', :specialize => 'users') }
    let(:id_tag) { double('id tag', :label => ':id', :specialize => '1') }

    describe "#specialize" do
      it "replaces a known tag" do
        Pattern.tags << class_tag
        pattern = Pattern.new(':class')
        pattern.specialize.should == 'my_app/models/users'
      end

      it "replaces a known tag every time it appears" do
        Pattern.tags << class_name_tag
        pattern = Pattern.new(':class_name/:class_name/:class_name')
        pattern.specialize.should == 'users/users/users'
      end

      it "doesn't replace unknown tags" do
        pattern = Pattern.new(':unknown_class')
        pattern.specialize.should == ':unknown_class'
      end

      it "replaces several known tags" do
        Pattern.tags << class_name_tag
        Pattern.tags << id_tag
        pattern = Pattern.new(':class_name/:id')
        pattern.specialize.should == 'users/1'
      end

      it "doesn't get confused with unknown tags and other text" do
        Pattern.tags << class_name_tag
        Pattern.tags << id_tag
        pattern = Pattern.new(':class_namesometext:id:unknowntag')
        pattern.specialize.should == 'userssometext1:unknowntag'
      end

      context "when two tag's labels start with the same string" do
        context "and the shortest one was registered first" do
          it "returns the right specialized string" do
            Pattern.tags << class_tag
            Pattern.tags << class_name_tag
            pattern = Pattern.new(':class_name:class')
            pattern.specialize.should == 'usersmy_app/models/users'
            pattern = Pattern.new(':class:class_name')
            pattern.specialize.should == 'my_app/models/usersusers'
          end
        end

        context "and the longest one was registered first" do
          it "returns the right specialized string" do
            Pattern.tags << class_name_tag
            Pattern.tags << class_tag
            pattern = Pattern.new(':class_name:class')
            pattern.specialize.should == 'usersmy_app/models/users'
            pattern = Pattern.new(':class:class_name')
            pattern.specialize.should == 'my_app/models/usersusers'
          end
        end
      end

      it "only asks the tags that appears in the pattern for its specializations" do
        Pattern.tags << class_tag
        Pattern.tags << class_name_tag
        Pattern.tags << id_tag
        pattern = Pattern.new(':class/:id')
        class_tag.should_receive(:specialize).with(1,2,3)
        id_tag.should_receive(:specialize).with(1,2,3)
        class_name_tag.should_not_receive(:specialize)
        pattern.specialize(1,2,3)
      end
    end
  end

  module Pattern::Tags
    describe Attachment do
      let(:tag) { Attachment.new }

      describe "#label" do
        it "returns ':attachment'" do
          tag.label.should == ':attachment'
        end
      end

      describe "#specialize" do<commit_msg>add a Pattern spec<commit_after>require 'spec_helper'

module Adrift
  describe Pattern do
    before do 
      @tags = Pattern.tags.dup
      Pattern.tags.clear
    end

    after do
      Pattern.tags.replace(@tags)
    end

    let(:class_tag) { double('class tag', :label => ':class', :specialize => 'my_app/models/users') }
    let(:class_name_tag) { double('class name tag', :label => ':class_name', :specialize => 'users') }
    let(:id_tag) { double('id tag', :label => ':id', :specialize => '1') }

    describe "#specialize" do
      it "replaces a known tag" do
        Pattern.tags << class_tag
        pattern = Pattern.new(':class')
        pattern.specialize.should == 'my_app/models/users'
      end

      it "replaces a known tag every time it appears" do
        Pattern.tags << class_name_tag
        pattern = Pattern.new(':class_name/:class_name/:class_name')
        pattern.specialize.should == 'users/users/users'
      end

      it "doesn't replace unknown tags" do
        pattern = Pattern.new(':unknown_class')
        pattern.specialize.should == ':unknown_class'
      end

      it "replaces several known tags" do
        Pattern.tags << class_name_tag
        Pattern.tags << id_tag
        pattern = Pattern.new(':class_name/:id')
        pattern.specialize.should == 'users/1'
      end

      it "doesn't get confused with unknown tags and other text" do
        Pattern.tags << class_name_tag
        Pattern.tags << id_tag
        pattern = Pattern.new(':class_namesometext:id:unknowntag')
        pattern.specialize.should == 'userssometext1:unknowntag'
      end

      context "when two tag's labels start with the same string" do
        context "and the shortest one was registered first" do
          it "returns the right specialized string" do
            Pattern.tags << class_tag
            Pattern.tags << class_name_tag
            pattern = Pattern.new(':class_name:class')
            pattern.specialize.should == 'usersmy_app/models/users'
            pattern = Pattern.new(':class:class_name')
            pattern.specialize.should == 'my_app/models/usersusers'
          end
        end

        context "and the longest one was registered first" do
          it "returns the right specialized string" do
            Pattern.tags << class_name_tag
            Pattern.tags << class_tag
            pattern = Pattern.new(':class_name:class')
            pattern.specialize.should == 'usersmy_app/models/users'
            pattern = Pattern.new(':class:class_name')
            pattern.specialize.should == 'my_app/models/usersusers'
          end
        end
      end

      context "when there are two tags with the exactly same label" do
        it "uses the most recently registered" do
          repeated_tag = double('class tag 2', :label => ':class', :specialize => 'repeated')
          repeated_tag.specialize.should_not == class_tag.specialize
          Pattern.tags << class_tag
          Pattern.tags << repeated_tag
          Pattern.new(':class').specialize.should == 'repeated'
        end
      end

      it "only asks the tags that appears in the pattern for its specializations" do
        Pattern.tags << class_tag
        Pattern.tags << class_name_tag
        Pattern.tags << id_tag
        pattern = Pattern.new(':class/:id')
        class_tag.should_receive(:specialize).with(1,2,3)
        id_tag.should_receive(:specialize).with(1,2,3)
        class_name_tag.should_not_receive(:specialize)
        pattern.specialize(1,2,3)
      end
    end
  end

  module Pattern::Tags
    describe Attachment do
      let(:tag) { Attachment.new }

      describe "#label" do
        it "returns ':attachment'" do
          tag.label.should == ':attachment'
        end
      end

      describe "#specialize" do
{'fraction_non_alphanumeric': 0.10670603121045973, 'fraction_numerical': 0.0035146914100941938, 'mean_word_length': 2.3860066634935744, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 17, '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': '1753332', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1987, 'n_words': 519}