spec/lib/query/token_spec.rb in picky-4.0.0pre1 vs spec/lib/query/token_spec.rb in picky-4.0.0pre2

- old
+ new

@@ -10,109 +10,44 @@ it 'is not equal if the originals are not equal' do described_class.processed('similar~', 'Similar~').should_not == described_class.processed('similar', 'Similar') end end - describe 'next_similar_token' do - before(:each) do - @bundle = stub :bundle, :similar => [:array, :of, :similar] - @category = stub :category, :bundle_for => @bundle - - @token = described_class.processed 'similar~', 'Similar~' - end - it 'returns the right next tokens' do - next_token = @token.next_similar_token @category - next_token.text.should == :array - next_token = next_token.next_similar_token @category - next_token.text.should == :of - next_token = next_token.next_similar_token @category - next_token.text.should == :similar - next_token = next_token.next_similar_token @category - next_token.should == nil - end - end - - describe 'next_similar' do - before(:each) do - @bundle = stub :bundle - end - describe 'original' do - context 'with stub' do - before(:each) do - @bundle.stub! :similar => [:array, :of, :similar] - - @token = described_class.processed 'similar~', 'Similar' - end - it 'should have a certain original text' do - @token.next_similar @bundle - - @token.original.should == :array - end + describe 'similar_tokens_for' do + let(:token) { described_class.processed 'similar~', 'Similar~' } + context 'with similar' do + before(:each) do + @bundle = stub :bundle, :similar => ['array', 'of', 'similar'] + @category = stub :category, :bundle_for => @bundle end - end - context 'similar' do - context 'with stub' do - before(:each) do - @bundle.stub! :similar => [:array, :of, :similar] - - @token = described_class.processed 'similar~', 'Similar' + it 'returns a list of tokens' do + token.similar_tokens_for(@category).each do |token| + token.should be_kind_of(described_class) end - it 'generates all similar' do - @token.next_similar(@bundle).should == :array - @token.next_similar(@bundle).should == :of - @token.next_similar(@bundle).should == :similar - @token.next_similar(@bundle).should == nil - end - it 'should have a certain text' do - @token.next_similar @bundle - @token.next_similar @bundle - @token.next_similar @bundle - @token.next_similar @bundle - - @token.text.should == :similar - end end - end - context 'non-similar' do - context 'with stub' do - before(:each) do - @bundle.stub! :similar => [:array, :of, :similar] - - @token = described_class.processed 'nonsimilar', 'Nonsimilar' + it 'returns all non-similar tokens' do + token.similar_tokens_for(@category).each do |token| + token.should_not be_similar end - it 'generates all similar' do - @token.next_similar(@bundle).should == nil - end - it 'should have a certain text' do - @token.next_similar @bundle - - @token.text.should == 'nonsimilar' - end end - end - end - - describe "generate_similarity_for" do - before(:each) do - @bundle = stub :bundle - - @token = described_class.processed 'flarb~', 'FLARB~' - end - context "with similar" do - before(:each) do - @bundle.stub! :similar => [:array, :of, :similar] + it 'returns a list of tokens with the right text' do + token.similar_tokens_for(@category).map(&:text).should == ['array', 'of', 'similar'] end - it "returns an array of the right size" do - @token.generate_similarity_for(@bundle).size.should == 3 + it 'returns a list of tokens with the right original' do + token.similar_tokens_for(@category).map(&:original).should == ['array', 'of', 'similar'] end + it 'returns a list of tokens with the right categorization' do + token.similar_tokens_for(@category).map(&:user_defined_categories).should == [[@category], [@category], [@category]] + end end - context "without similar" do + context 'without similar' do before(:each) do - @bundle.stub! :similar => [] + @bundle = stub :bundle, :similar => [] + @category = stub :category, :bundle_for => @bundle end - it "returns an array of the right size" do - @token.generate_similarity_for(@bundle).size.should == 0 + it 'returns an empty list' do + token.similar_tokens_for(@category).should == [] end end end # describe 'to_solr' do @@ -165,18 +100,30 @@ it_should_qualify 'a:b:c', [['a'], 'b:c'] it_should_qualify 'a,b:c', [['a','b'], 'c'] it_should_qualify 'a,b,c:d', [['a','b','c'], 'd'] it_should_qualify ':', [nil, ''] it_should_qualify 'vorname:qualifier', [['vorname'], 'qualifier'] - it_should_qualify 'with:qualifier', [['with'], 'qualifier'] - it_should_qualify 'without qualifier', [nil, 'without qualifier'] - it_should_qualify 'name:', [nil, 'name'] - it_should_qualify ':broken qualifier', [[], 'broken qualifier'] - it_should_qualify '', [nil, ''] - it_should_qualify 'fn:text', [['fn'], 'text'] end + describe 'qualifier' do + it 'returns the right thing' do + token = described_class.processed 'b' + + token.qualifiers.should == [] + end + it 'returns the right thing' do + token = described_class.processed 'a:b' + + token.qualifiers.should == ['a'] + end + it 'returns the right thing' do + token = described_class.processed 'a,b:c' + + token.qualifiers.should == ['a', 'b'] + end + end + describe 'processed' do it 'should return a new token' do described_class.processed('some text', 'SOME TEXT').should be_kind_of(described_class) end it 'generates a token' do @@ -197,10 +144,80 @@ token.process end end + describe 'partialize' do + context 'token explicitly partial' do + let(:token) { described_class.new 'a*' } + it 'is afterwards partial' do + token.partialize + + token.should be_partial + end + end + context 'token explicitly nonpartial' do + let(:token) { described_class.new 'a"' } + it 'is afterwards not partial' do + token.partialize + + token.should_not be_partial + end + end + context 'token nonpartial' do + let(:token) { described_class.new 'a' } + it 'is afterwards not partial' do + token.partialize + + token.should_not be_partial + end + end + context 'special case' do + let(:token) { described_class.new 'a*"' } + it 'is afterwards not partial - last one wins' do + token.partialize + + token.should_not be_partial + end + end + context 'special case' do + let(:token) { described_class.new 'a"*' } + it 'is afterwards partial - last one wins' do + token.partialize + + token.should be_partial + end + end + end + + describe 'similarize' do + context 'token explicitly similar' do + let(:token) { described_class.new 'a~' } + it 'is afterwards similar' do + token.similarize + + token.should be_similar + end + end + context 'token explicitly nonsimilar' do + let(:token) { described_class.new 'a"' } + it 'is afterwards not similar' do + token.similarize + + token.should_not be_similar + end + end + context 'token nonsimilar' do + let(:token) { described_class.new 'a' } + it 'is afterwards not similar' do + token.similarize + + token.should_not be_similar + end + end + end + describe 'symbolize!' do before(:each) do @token = described_class.processed 'string', 'String' end it 'is not symbolized' do @@ -321,102 +338,98 @@ end end describe 'qualifiers' do context 'with qualifier' do - before(:each) do - @token = described_class.processed('sp:qualifier') - end + let(:token) { described_class.processed 'sp:qualifier' } it 'should return the qualifier' do - @token.qualifiers.should == ['sp'] + token.qualifiers.should == ['sp'] end end context 'with incorrect qualifier' do - before(:each) do - @token = described_class.processed('specific:qualifier') - end + let(:token) { described_class.processed 'specific:qualifier' } it 'should return the qualifier' do - @token.qualifiers.should == ['specific'] + token.qualifiers.should == ['specific'] end end context 'with multiple qualifiers' do - before(:each) do - @token = described_class.processed('sp,spec:qualifier') - end + let(:token) { described_class.processed 'sp,spec:qualifier' } it 'should return the qualifier' do - @token.qualifiers.should == ['sp', 'spec'] + token.qualifiers.should == ['sp', 'spec'] end end context 'without qualifier' do - before(:each) do - @token = described_class.processed('noqualifier') + let(:token) { described_class.processed 'noqualifier' } + it 'is correct' do + token.qualifiers.should == [] end - it 'should return nil' do - @token.qualifiers.should == nil - end end end describe 'partial=' do context 'partial nil' do - before(:each) do - @token = described_class.new 'text' - end + let(:token) { described_class.new 'text' } it 'should set partial' do - @token.partial = true + token.partial = true - @token.instance_variable_get(:@partial).should be_true + token.should be_partial end it 'should set partial' do - @token.partial = false + token.partial = false - @token.instance_variable_get(:@partial).should be_false + token.should_not be_partial end end context 'partial not nil' do - before(:each) do - @token = described_class.processed 'text*' + let(:token) { described_class.processed 'text"' } + it 'should not set partial' do + token.partial = true + + token.should_not be_partial end + end + context 'partial not nil' do + let(:token) { described_class.processed 'text*' } it 'should not set partial' do - @token.instance_variable_set :@partial, false + token.instance_variable_set :@partial, false - @token.partial = true + token.partial = true - @token.instance_variable_get(:@partial).should be_false + token.should_not be_partial end it 'should not set partial' do - @token.partial = false + token.partial = false - @token.instance_variable_get(:@partial).should be_true + token.should be_partial end end end describe 'partialize!' do it 'should not partialize a token if the text ends with "' do token = described_class.processed 'text"' - token.instance_variable_get(:@partial).should be_false + token.should_not be_partial end it 'should partialize a token if the text ends with *' do token = described_class.processed 'text*' - token.instance_variable_get(:@partial).should be_true + token.should be_partial end it 'should not partialize a token if the text ends with ~' do token = described_class.processed 'text~' - token.instance_variable_get(:@partial).should be_nil + token.should_not be_partial end it 'lets the last one win' do token = described_class.processed 'text"*' - token.partial?.should == true + token.should be_partial end it 'lets the last one win' do token = described_class.processed 'text*"' - token.partial?.should == false + token.should_not be_partial end end describe "processed" do it 'should remove *' do \ No newline at end of file