<commit_before>require 'rails_helper'

describe Udongo::Search::Term do
  before(:each) do
    @synonym_a = create(:search_synonym, locale: 'nl', term: 'Dave', synonyms: 'developer')
    @synonym_b = create(:search_synonym, locale: 'nl', term: 'foobar', synonyms: 'blub,blubber,blubst')
    @synonym_c = create(:search_synonym, locale: 'nl', term: 'Zoeken', synonyms: "le search,c'est fou")
    @synonym_d = create(:search_synonym, locale: 'fr', term: 'Chercher', synonyms: "le search,c'est fou")
  end

  describe '#locale' do
    let(:instance) { described_class.new('foobar') }

    it 'defaults to Udongo.config.i18n.app.default_locale without controller input' do
      allow(Udongo.config.i18n.app).to receive(:default_locale) { 'jp' }
      expect(instance.locale).to eq :jp
    end

    it 'value passed from controller' do
      controller = OpenStruct.new(locale: :en)
      instance = described_class.new('foobar', controller: controller)
      expect(instance.locale).to eq :en
    end
  end

  describe '#synonym' do
    it 'nil' do
      instance = described_class.new('Unknown term')
      expect(instance.synonym).to be nil
    end

    it 'matches single synonym' do
      instance = described_class.new('developer')
      expect(instance.synonym).to eq @synonym_a
    end

    describe 'finds a match in a comma-separated list of 3 synonyms' do
      it 'matches on first' do
        instance = described_class.new('blub')
        expect(instance.synonym).to eq @synonym_b
      end

      it 'matches on second' do
        instance = described_class.new('blubber')
        expect(instance.synonym).to eq @synonym_b
      end

      it 'matches on third' do
        instance = described_class.new('blubst')
        expect(instance.synonym).to eq @synonym_b
      end
    end

    it 'matches locale' do
      controller = OpenStruct.new(locale: 'fr')
      instance = described_class.new('le search', controller: controller)

      expect(instance.synonym).to eq @synonym_d
    end
  end

  describe '#value' do
    it 'repeats incoming term without synonym match' do
      instance = described_class.new('Unknown term')
      expect(instance.value).to eq 'Unknown term'
    end

    it 'returns synonym term after synonym match' do
      instance = described_class.new('blubber')
      expect(instance.value).to eq 'foobar'
    end
  end

  it '#responds_to?' do
    expect(described_class.new('')).to respond_to(
      :locale, :synonym, :value<commit_msg>use the newer subject{} syntax instead of let(:instance), part 2<commit_after>require 'rails_helper'

describe Udongo::Search::Term do
  before(:each) do
    @synonym_a = create(:search_synonym, locale: 'nl', term: 'Dave', synonyms: 'developer')
    @synonym_b = create(:search_synonym, locale: 'nl', term: 'foobar', synonyms: 'blub,blubber,blubst')
    @synonym_c = create(:search_synonym, locale: 'nl', term: 'Zoeken', synonyms: "le search,c'est fou")
    @synonym_d = create(:search_synonym, locale: 'fr', term: 'Chercher', synonyms: "le search,c'est fou")
  end

  describe '#locale' do
    subject { described_class.new('foobar') }

    it 'defaults to Udongo.config.i18n.app.default_locale without controller input' do
      allow(Udongo.config.i18n.app).to receive(:default_locale) { 'jp' }
      expect(subject.locale).to eq :jp
    end

    it 'value passed from controller' do
      controller = OpenStruct.new(locale: :en)
      subject = described_class.new('foobar', controller: controller)
      expect(subject.locale).to eq :en
    end
  end

  describe '#synonym' do
    it 'nil' do
      subject = described_class.new('Unknown term')
      expect(subject.synonym).to be nil
    end

    it 'matches single synonym' do
      subject = described_class.new('developer')
      expect(subject.synonym).to eq @synonym_a
    end

    describe 'finds a match in a comma-separated list of 3 synonyms' do
      it 'matches on first' do
        subject = described_class.new('blub')
        expect(subject.synonym).to eq @synonym_b
      end

      it 'matches on second' do
        subject = described_class.new('blubber')
        expect(subject.synonym).to eq @synonym_b
      end

      it 'matches on third' do
        subject = described_class.new('blubst')
        expect(subject.synonym).to eq @synonym_b
      end
    end

    it 'matches locale' do
      controller = OpenStruct.new(locale: 'fr')
      subject = described_class.new('le search', controller: controller)

      expect(subject.synonym).to eq @synonym_d
    end
  end

  describe '#value' do
    it 'repeats incoming term without synonym match' do
      subject = described_class.new('Unknown term')
      expect(subject.value).to eq 'Unknown term'
    end

    it 'returns synonym term after synonym match' do
      subject = described_class.new('blubber')
      expect(subject.value).to eq 'foobar'
    end
  end

  it '#responds_to?' do
    expect(described_class.new('')).to respond_to(
      :locale, :synonym, :value
{'fraction_non_alphanumeric': 0.11512959614225438, 'fraction_numerical': 0.0022101667671287923, 'mean_word_length': 3.269296740994854, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3728738', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1562, 'n_words': 430}