Skip to content

Commit

Permalink
Fix failing tests due to expecting specific exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Nov 19, 2024
1 parent e95e250 commit 332d0aa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ public virtual void TestDontKeepOrig()
return new TokenStreamComponents(tokenizer, new SynonymFilter(tokenizer, map, false));
});

AssertAnalyzesTo(analyzer, "a b c",
new string[] { "foo", "c" },
new int[] { 0, 4 },
new int[] { 3, 5 },
null,
new int[] { 1, 1 },
new int[] { 1, 1 },
AssertAnalyzesTo(analyzer, "a b c",
new string[] { "foo", "c" },
new int[] { 0, 4 },
new int[] { 3, 5 },
null,
new int[] { 1, 1 },
new int[] { 1, 1 },
true);
CheckAnalysisConsistency(Random, analyzer, false, "a b c");
}
Expand All @@ -196,13 +196,13 @@ public virtual void TestDoKeepOrig()
return new TokenStreamComponents(tokenizer, new SynonymFilter(tokenizer, map, false));
});

AssertAnalyzesTo(analyzer, "a b c",
new string[] { "a", "foo", "b", "c" },
new int[] { 0, 0, 2, 4 },
new int[] { 1, 3, 3, 5 },
null,
new int[] { 1, 0, 1, 1 },
new int[] { 1, 2, 1, 1 },
AssertAnalyzesTo(analyzer, "a b c",
new string[] { "a", "foo", "b", "c" },
new int[] { 0, 0, 2, 4 },
new int[] { 1, 3, 3, 5 },
null,
new int[] { 1, 0, 1, 1 },
new int[] { 1, 2, 1, 1 },
true);
CheckAnalysisConsistency(Random, analyzer, false, "a b c");
}
Expand Down Expand Up @@ -958,14 +958,14 @@ public virtual void TestEmpty()
Tokenizer tokenizer = new MockTokenizer(new StringReader("aa bb"));
try
{
new SynonymFilter(tokenizer, (new SynonymMap.Builder(true)).Build(), true);
_ = new SynonymFilter(tokenizer, new SynonymMap.Builder(true).Build(), true);
fail("did not hit expected exception");
}
catch (ArgumentNullException iae) // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention)
catch (ArgumentException iae)
{
// expected
assertTrue(iae.Message.Contains("fst must be non-null")); // LUCENENET: .NET Adds the parameter name to the message
assertTrue(iae.Message.Contains("Fst must be non-null")); // LUCENENET: .NET Adds the parameter name to the message
}
}
}
}
}
50 changes: 26 additions & 24 deletions src/Lucene.Net.Tests.Queries/TermFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,39 @@ public class TermFilterTest : LuceneTestCase
[Test]
public void TestCachability()
{
TermFilter a = TermFilter(@"field1", @"a");
TermFilter a = TermFilter("field1", "a");
var cachedFilters = new JCG.HashSet<Filter>();
cachedFilters.Add(a);
assertTrue(@"Must be cached", cachedFilters.Contains(TermFilter(@"field1", @"a")));
assertFalse(@"Must not be cached", cachedFilters.Contains(TermFilter(@"field1", @"b")));
assertFalse(@"Must not be cached", cachedFilters.Contains(TermFilter(@"field2", @"a")));
assertTrue("Must be cached", cachedFilters.Contains(TermFilter("field1", "a")));
assertFalse("Must not be cached", cachedFilters.Contains(TermFilter("field1", "b")));
assertFalse("Must not be cached", cachedFilters.Contains(TermFilter("field2", "a")));
}

[Test]
public void TestMissingTermAndField()
{
string fieldName = @"field1";
const string fieldName = "field1";
Directory rd = NewDirectory();
RandomIndexWriter w = new RandomIndexWriter(Random, rd);
Document doc = new Document();
doc.Add(NewStringField(fieldName, @"value1", Field.Store.NO));
doc.Add(NewStringField(fieldName, "value1", Field.Store.NO));
w.AddDocument(doc);
IndexReader reader = SlowCompositeReaderWrapper.Wrap(w.GetReader());
assertTrue(reader.Context is AtomicReaderContext);
var context = (AtomicReaderContext)reader.Context;
w.Dispose();

DocIdSet idSet = TermFilter(fieldName, @"value1").GetDocIdSet(context, context.AtomicReader.LiveDocs);
assertNotNull(@"must not be null", idSet);
DocIdSet idSet = TermFilter(fieldName, "value1").GetDocIdSet(context, context.AtomicReader.LiveDocs);
assertNotNull("must not be null", idSet);
DocIdSetIterator iter = idSet.GetIterator();
assertEquals(iter.NextDoc(), 0);
assertEquals(iter.NextDoc(), DocIdSetIterator.NO_MORE_DOCS);

idSet = TermFilter(fieldName, @"value2").GetDocIdSet(context, context.AtomicReader.LiveDocs);
assertNull(@"must be null", idSet);
idSet = TermFilter(fieldName, "value2").GetDocIdSet(context, context.AtomicReader.LiveDocs);
assertNull("must be null", idSet);

idSet = TermFilter(@"field2", @"value1").GetDocIdSet(context, context.AtomicReader.LiveDocs);
assertNull(@"must be null", idSet);
idSet = TermFilter("field2", "value1").GetDocIdSet(context, context.AtomicReader.LiveDocs);
assertNull("must be null", idSet);

reader.Dispose();
rd.Dispose();
Expand All @@ -81,7 +81,7 @@ public void TestRandom()
var terms = new JCG.List<Term>();
for (int i = 0; i < num; i++)
{
string field = @"field" + i;
string field = "field" + i;
string str = TestUtil.RandomRealisticUnicodeString(Random);
terms.Add(new Term(field, str));
Document doc = new Document();
Expand Down Expand Up @@ -121,10 +121,10 @@ public void TestHashCodeAndEquals()
int num = AtLeast(100);
for (int i = 0; i < num; i++)
{
string field1 = @"field" + i;
string field2 = @"field" + i + num;
string field1 = "field" + i;
string field2 = "field" + i + num;
string value1 = TestUtil.RandomRealisticUnicodeString(Random);
string value2 = value1 + @"x"; // this must be not equal to value1
string value2 = value1 + "x"; // this must be not equal to value1

TermFilter filter1 = TermFilter(field1, value1);
TermFilter filter2 = TermFilter(field1, value2);
Expand Down Expand Up @@ -164,19 +164,19 @@ public void TestNoTerms()
{
try
{
new TermFilter(null);
Assert.Fail(@"must fail - no term!");
_ = new TermFilter(null);
Assert.Fail("must fail - no term!");
}
catch (ArgumentNullException) // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention)
{
}

try
{
new TermFilter(new Term(null));
Assert.Fail(@"must fail - no field!");
_ = new TermFilter(new Term(null));
Assert.Fail("must fail - no field!");
}
catch (ArgumentNullException) // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention)
catch (ArgumentException)
{
}
}
Expand All @@ -185,15 +185,17 @@ public void TestNoTerms()
public void TestToString()
{
var termsFilter = new TermFilter(new Term("field1", "a"));
assertEquals(@"field1:a", termsFilter.ToString());
assertEquals("field1:a", termsFilter.ToString());
}

private TermFilter TermFilter(string field, string value)
// LUCENENET specific - made static
private static TermFilter TermFilter(string field, string value)
{
return TermFilter(new Term(field, value));
}

private TermFilter TermFilter(Term term)
// LUCENENET specific - made static
private static TermFilter TermFilter(Term term)
{
return new TermFilter(term);
}
Expand Down

0 comments on commit 332d0aa

Please sign in to comment.