Skip to content

Commit

Permalink
Add some Support unit tests, #920 (#1093)
Browse files Browse the repository at this point in the history
* Add unit tests for IndexableFieldExtensions, #920

* Add unit test for ByteArrayOutputStream, #920

* Add unit tests for CastTo, #920

* Add unit tests for ListExtensions, #920

* Add unit test for AssemblyUtils, #920

* Add unit tests for DictionaryExtensions, #920

* Fix TestByteArrayOutputStream on .NET FX, #920
  • Loading branch information
paulirwin authored Jan 12, 2025
1 parent c3b6896 commit 24fb64b
Show file tree
Hide file tree
Showing 6 changed files with 559 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Lucene.Net.Attributes;
using Lucene.Net.Index;
using Lucene.Net.Util;
using NUnit.Framework;
using System;
using System.Collections.Generic;

#nullable enable

namespace Lucene.Net.Documents.Extensions
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

[TestFixture]
public class TestIndexableFieldExtensions : LuceneTestCase
{
public static IEnumerable<TestCaseData> TestCases()
{
#pragma warning disable CS8974 // Converting method group to non-delegate type
yield return new TestCaseData(new Int32Field("field", byte.MaxValue, Field.Store.NO), byte.MaxValue, IndexableFieldExtensions.GetByteValueOrDefault);
yield return new TestCaseData(new Int32Field("field", short.MaxValue, Field.Store.NO), short.MaxValue, IndexableFieldExtensions.GetInt16ValueOrDefault);
yield return new TestCaseData(new Int32Field("field", int.MaxValue, Field.Store.NO), int.MaxValue, IndexableFieldExtensions.GetInt32ValueOrDefault);
yield return new TestCaseData(new Int64Field("field", long.MaxValue, Field.Store.NO), long.MaxValue, IndexableFieldExtensions.GetInt64ValueOrDefault);
yield return new TestCaseData(new SingleField("field", float.MaxValue, Field.Store.NO), float.MaxValue, IndexableFieldExtensions.GetSingleValueOrDefault);
yield return new TestCaseData(new DoubleField("field", double.MaxValue, Field.Store.NO), double.MaxValue, IndexableFieldExtensions.GetDoubleValueOrDefault);
yield return new TestCaseData(null, (byte)0, IndexableFieldExtensions.GetByteValueOrDefault);
yield return new TestCaseData(null, (short)0, IndexableFieldExtensions.GetInt16ValueOrDefault);
yield return new TestCaseData(null, 0, IndexableFieldExtensions.GetInt32ValueOrDefault);
yield return new TestCaseData(null, 0L, IndexableFieldExtensions.GetInt64ValueOrDefault);
yield return new TestCaseData(null, 0f, IndexableFieldExtensions.GetSingleValueOrDefault);
yield return new TestCaseData(null, 0d, IndexableFieldExtensions.GetDoubleValueOrDefault);
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), (byte)0, IndexableFieldExtensions.GetByteValueOrDefault);
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), (short)0, IndexableFieldExtensions.GetInt16ValueOrDefault);
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), 0, IndexableFieldExtensions.GetInt32ValueOrDefault);
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), 0L, IndexableFieldExtensions.GetInt64ValueOrDefault);
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), 0f, IndexableFieldExtensions.GetSingleValueOrDefault);
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), 0d, IndexableFieldExtensions.GetDoubleValueOrDefault);
#pragma warning restore CS8974 // Converting method group to non-delegate type
}

[Test, LuceneNetSpecific]
[TestCaseSource(nameof(TestCases))]
public void TestIndexableFieldExtensions_TestCases(IIndexableField? field, object expected, Delegate func)
{
Assert.AreEqual(expected, func.DynamicInvoke(field));
}
}
}
37 changes: 37 additions & 0 deletions src/Lucene.Net.Tests/Support/IO/TestByteArrayOutputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Lucene.Net.Attributes;
using Lucene.Net.Util;
using NUnit.Framework;
using System.Text;

namespace Lucene.Net.Support.IO
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

[TestFixture]
public class TestByteArrayOutputStream : LuceneTestCase
{
[Test, LuceneNetSpecific]
public void TestToString()
{
ByteArrayOutputStream s = new ByteArrayOutputStream();
var bytes = Encoding.UTF8.GetBytes("hello, world");
s.Write(bytes, 0, bytes.Length);
Assert.AreEqual("hello, world", s.ToString());
}
}
}
36 changes: 36 additions & 0 deletions src/Lucene.Net.Tests/Support/TestAssemblyUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Lucene.Net.Attributes;
using Lucene.Net.Util;
using NUnit.Framework;
using System.Linq;

namespace Lucene.Net.Support
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

[TestFixture]
public class TestAssemblyUtils : LuceneTestCase
{
[Test, LuceneNetSpecific]
public void TestGetReferencedAssemblies()
{
var assemblies = AssemblyUtils.GetReferencedAssemblies().ToList();
Assert.Greater(assemblies.Count, 0);
Assert.IsTrue(assemblies.Contains(typeof(LuceneVersion).Assembly)); // Lucene.Net should definitely be in the list
}
}
}
70 changes: 70 additions & 0 deletions src/Lucene.Net.Tests/Support/TestDictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Lucene.Net.Attributes;
using Lucene.Net.Util;
using NUnit.Framework;
using System.Collections.Generic;

namespace Lucene.Net.Support
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

[TestFixture]
public class TestDictionaryExtensions : LuceneTestCase
{
[Test, LuceneNetSpecific]
public void TestPutAll()
{
var dictionary1 = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
var dictionary2 = new Dictionary<string, string>
{
{ "key1", "value1.1" },
{ "key3", "value3" }
};

dictionary1.PutAll(dictionary2);

Assert.AreEqual(3, dictionary1.Count);
Assert.AreEqual("value1.1", dictionary1["key1"]);
Assert.AreEqual("value2", dictionary1["key2"]);
Assert.AreEqual("value3", dictionary1["key3"]);
}

[Test, LuceneNetSpecific]
public void TestPut()
{
var dictionary = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
};

var oldFirst = dictionary.Put("key1", "value1.1");
var oldSecond = dictionary.Put("key3", "value3");

Assert.AreEqual(3, dictionary.Count);
Assert.AreEqual("value1.1", dictionary["key1"]);
Assert.AreEqual("value2", dictionary["key2"]);
Assert.AreEqual("value3", dictionary["key3"]);
Assert.AreEqual("value1", oldFirst);
Assert.IsNull(oldSecond);
}
}
}
96 changes: 96 additions & 0 deletions src/Lucene.Net.Tests/Support/Util/TestCastTo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Lucene.Net.Attributes;
using NUnit.Framework;
using System;
using System.Collections.Generic;

namespace Lucene.Net.Util
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

[TestFixture]
public class TestCastTo : LuceneTestCase
{
public static IEnumerable<TestCaseData> TestFromSuccessCases()
{
yield return new TestCaseData((byte)1, (short)1);
yield return new TestCaseData((byte)1, 1);
yield return new TestCaseData((byte)1, 1L);
yield return new TestCaseData((byte)1, 1f);
yield return new TestCaseData((byte)1, 1d);
yield return new TestCaseData((short)2, (byte)2);
yield return new TestCaseData((short)2, 2);
yield return new TestCaseData((short)2, 2L);
yield return new TestCaseData((short)2, 2f);
yield return new TestCaseData((short)2, 2d);
yield return new TestCaseData(3, (byte)3);
yield return new TestCaseData(3, (short)3);
yield return new TestCaseData(3, 3L);
yield return new TestCaseData(3, 3f);
yield return new TestCaseData(3, 3d);
yield return new TestCaseData(4L, (byte)4);
yield return new TestCaseData(4L, (short)4);
yield return new TestCaseData(4L, 4);
yield return new TestCaseData(4L, 4f);
yield return new TestCaseData(4L, 4d);
yield return new TestCaseData(5f, (byte)5);
yield return new TestCaseData(5f, (short)5);
yield return new TestCaseData(5f, 5);
yield return new TestCaseData(5f, 5L);
yield return new TestCaseData(5f, 5d);
yield return new TestCaseData(6d, (byte)6);
yield return new TestCaseData(6d, (short)6);
yield return new TestCaseData(6d, 6);
yield return new TestCaseData(6d, 6L);
yield return new TestCaseData(6d, 6f);
}

[Test, LuceneNetSpecific]
[TestCaseSource(nameof(TestFromSuccessCases))]
public void TestFrom_Success(object value, object expected)
{
var castTo = typeof(CastTo<>).MakeGenericType(expected.GetType());
var from = castTo.GetMethod("From")?.MakeGenericMethod(value.GetType())
?? throw new InvalidOperationException("Could not find method CastTo<T>.From<TSource>");
Assert.AreEqual(expected, from.Invoke(null, new[] { value }));
}

public static IEnumerable<TestCaseData> TestFromInvalidCastCases()
{
yield return new TestCaseData(1, "1");
yield return new TestCaseData(new object(), 1);
}

[Test, LuceneNetSpecific]
[TestCaseSource(nameof(TestFromInvalidCastCases))]
public void TestFrom_InvalidCast(object value, object expected)
{
var castTo = typeof(CastTo<>).MakeGenericType(expected.GetType());
var from = castTo.GetMethod("From")?.MakeGenericMethod(value.GetType())
?? throw new InvalidOperationException("Could not find method CastTo<T>.From<TSource>");
try
{
from.Invoke(null, new[] { value });
Assert.Fail("Expected an exception");
}
catch
{
// ignored
}
}
}
}
Loading

0 comments on commit 24fb64b

Please sign in to comment.