Skip to content

Commit 24fb64b

Browse files
authored
Add some Support unit tests, apache#920 (apache#1093)
* Add unit tests for IndexableFieldExtensions, apache#920 * Add unit test for ByteArrayOutputStream, apache#920 * Add unit tests for CastTo, apache#920 * Add unit tests for ListExtensions, apache#920 * Add unit test for AssemblyUtils, apache#920 * Add unit tests for DictionaryExtensions, apache#920 * Fix TestByteArrayOutputStream on .NET FX, apache#920
1 parent c3b6896 commit 24fb64b

File tree

6 files changed

+559
-0
lines changed

6 files changed

+559
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Lucene.Net.Attributes;
2+
using Lucene.Net.Index;
3+
using Lucene.Net.Util;
4+
using NUnit.Framework;
5+
using System;
6+
using System.Collections.Generic;
7+
8+
#nullable enable
9+
10+
namespace Lucene.Net.Documents.Extensions
11+
{
12+
/*
13+
* Licensed to the Apache Software Foundation (ASF) under one or more
14+
* contributor license agreements. See the NOTICE file distributed with
15+
* this work for additional information regarding copyright ownership.
16+
* The ASF licenses this file to You under the Apache License, Version 2.0
17+
* (the "License"); you may not use this file except in compliance with
18+
* the License. You may obtain a copy of the License at
19+
*
20+
* http://www.apache.org/licenses/LICENSE-2.0
21+
*
22+
* Unless required by applicable law or agreed to in writing, software
23+
* distributed under the License is distributed on an "AS IS" BASIS,
24+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
* See the License for the specific language governing permissions and
26+
* limitations under the License.
27+
*/
28+
29+
[TestFixture]
30+
public class TestIndexableFieldExtensions : LuceneTestCase
31+
{
32+
public static IEnumerable<TestCaseData> TestCases()
33+
{
34+
#pragma warning disable CS8974 // Converting method group to non-delegate type
35+
yield return new TestCaseData(new Int32Field("field", byte.MaxValue, Field.Store.NO), byte.MaxValue, IndexableFieldExtensions.GetByteValueOrDefault);
36+
yield return new TestCaseData(new Int32Field("field", short.MaxValue, Field.Store.NO), short.MaxValue, IndexableFieldExtensions.GetInt16ValueOrDefault);
37+
yield return new TestCaseData(new Int32Field("field", int.MaxValue, Field.Store.NO), int.MaxValue, IndexableFieldExtensions.GetInt32ValueOrDefault);
38+
yield return new TestCaseData(new Int64Field("field", long.MaxValue, Field.Store.NO), long.MaxValue, IndexableFieldExtensions.GetInt64ValueOrDefault);
39+
yield return new TestCaseData(new SingleField("field", float.MaxValue, Field.Store.NO), float.MaxValue, IndexableFieldExtensions.GetSingleValueOrDefault);
40+
yield return new TestCaseData(new DoubleField("field", double.MaxValue, Field.Store.NO), double.MaxValue, IndexableFieldExtensions.GetDoubleValueOrDefault);
41+
yield return new TestCaseData(null, (byte)0, IndexableFieldExtensions.GetByteValueOrDefault);
42+
yield return new TestCaseData(null, (short)0, IndexableFieldExtensions.GetInt16ValueOrDefault);
43+
yield return new TestCaseData(null, 0, IndexableFieldExtensions.GetInt32ValueOrDefault);
44+
yield return new TestCaseData(null, 0L, IndexableFieldExtensions.GetInt64ValueOrDefault);
45+
yield return new TestCaseData(null, 0f, IndexableFieldExtensions.GetSingleValueOrDefault);
46+
yield return new TestCaseData(null, 0d, IndexableFieldExtensions.GetDoubleValueOrDefault);
47+
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), (byte)0, IndexableFieldExtensions.GetByteValueOrDefault);
48+
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), (short)0, IndexableFieldExtensions.GetInt16ValueOrDefault);
49+
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), 0, IndexableFieldExtensions.GetInt32ValueOrDefault);
50+
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), 0L, IndexableFieldExtensions.GetInt64ValueOrDefault);
51+
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), 0f, IndexableFieldExtensions.GetSingleValueOrDefault);
52+
yield return new TestCaseData(new StringField("field", "value", Field.Store.NO), 0d, IndexableFieldExtensions.GetDoubleValueOrDefault);
53+
#pragma warning restore CS8974 // Converting method group to non-delegate type
54+
}
55+
56+
[Test, LuceneNetSpecific]
57+
[TestCaseSource(nameof(TestCases))]
58+
public void TestIndexableFieldExtensions_TestCases(IIndexableField? field, object expected, Delegate func)
59+
{
60+
Assert.AreEqual(expected, func.DynamicInvoke(field));
61+
}
62+
}
63+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Lucene.Net.Attributes;
2+
using Lucene.Net.Util;
3+
using NUnit.Framework;
4+
using System.Text;
5+
6+
namespace Lucene.Net.Support.IO
7+
{
8+
/*
9+
* Licensed to the Apache Software Foundation (ASF) under one or more
10+
* contributor license agreements. See the NOTICE file distributed with
11+
* this work for additional information regarding copyright ownership.
12+
* The ASF licenses this file to You under the Apache License, Version 2.0
13+
* (the "License"); you may not use this file except in compliance with
14+
* the License. You may obtain a copy of the License at
15+
*
16+
* http://www.apache.org/licenses/LICENSE-2.0
17+
*
18+
* Unless required by applicable law or agreed to in writing, software
19+
* distributed under the License is distributed on an "AS IS" BASIS,
20+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
* See the License for the specific language governing permissions and
22+
* limitations under the License.
23+
*/
24+
25+
[TestFixture]
26+
public class TestByteArrayOutputStream : LuceneTestCase
27+
{
28+
[Test, LuceneNetSpecific]
29+
public void TestToString()
30+
{
31+
ByteArrayOutputStream s = new ByteArrayOutputStream();
32+
var bytes = Encoding.UTF8.GetBytes("hello, world");
33+
s.Write(bytes, 0, bytes.Length);
34+
Assert.AreEqual("hello, world", s.ToString());
35+
}
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Lucene.Net.Attributes;
2+
using Lucene.Net.Util;
3+
using NUnit.Framework;
4+
using System.Linq;
5+
6+
namespace Lucene.Net.Support
7+
{
8+
/*
9+
* Licensed to the Apache Software Foundation (ASF) under one or more
10+
* contributor license agreements. See the NOTICE file distributed with
11+
* this work for additional information regarding copyright ownership.
12+
* The ASF licenses this file to You under the Apache License, Version 2.0
13+
* (the "License"); you may not use this file except in compliance with
14+
* the License. You may obtain a copy of the License at
15+
*
16+
* http://www.apache.org/licenses/LICENSE-2.0
17+
*
18+
* Unless required by applicable law or agreed to in writing, software
19+
* distributed under the License is distributed on an "AS IS" BASIS,
20+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
* See the License for the specific language governing permissions and
22+
* limitations under the License.
23+
*/
24+
25+
[TestFixture]
26+
public class TestAssemblyUtils : LuceneTestCase
27+
{
28+
[Test, LuceneNetSpecific]
29+
public void TestGetReferencedAssemblies()
30+
{
31+
var assemblies = AssemblyUtils.GetReferencedAssemblies().ToList();
32+
Assert.Greater(assemblies.Count, 0);
33+
Assert.IsTrue(assemblies.Contains(typeof(LuceneVersion).Assembly)); // Lucene.Net should definitely be in the list
34+
}
35+
}
36+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Lucene.Net.Attributes;
2+
using Lucene.Net.Util;
3+
using NUnit.Framework;
4+
using System.Collections.Generic;
5+
6+
namespace Lucene.Net.Support
7+
{
8+
/*
9+
* Licensed to the Apache Software Foundation (ASF) under one or more
10+
* contributor license agreements. See the NOTICE file distributed with
11+
* this work for additional information regarding copyright ownership.
12+
* The ASF licenses this file to You under the Apache License, Version 2.0
13+
* (the "License"); you may not use this file except in compliance with
14+
* the License. You may obtain a copy of the License at
15+
*
16+
* http://www.apache.org/licenses/LICENSE-2.0
17+
*
18+
* Unless required by applicable law or agreed to in writing, software
19+
* distributed under the License is distributed on an "AS IS" BASIS,
20+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
* See the License for the specific language governing permissions and
22+
* limitations under the License.
23+
*/
24+
25+
[TestFixture]
26+
public class TestDictionaryExtensions : LuceneTestCase
27+
{
28+
[Test, LuceneNetSpecific]
29+
public void TestPutAll()
30+
{
31+
var dictionary1 = new Dictionary<string, string>
32+
{
33+
{ "key1", "value1" },
34+
{ "key2", "value2" }
35+
};
36+
var dictionary2 = new Dictionary<string, string>
37+
{
38+
{ "key1", "value1.1" },
39+
{ "key3", "value3" }
40+
};
41+
42+
dictionary1.PutAll(dictionary2);
43+
44+
Assert.AreEqual(3, dictionary1.Count);
45+
Assert.AreEqual("value1.1", dictionary1["key1"]);
46+
Assert.AreEqual("value2", dictionary1["key2"]);
47+
Assert.AreEqual("value3", dictionary1["key3"]);
48+
}
49+
50+
[Test, LuceneNetSpecific]
51+
public void TestPut()
52+
{
53+
var dictionary = new Dictionary<string, string>
54+
{
55+
{ "key1", "value1" },
56+
{ "key2", "value2" }
57+
};
58+
59+
var oldFirst = dictionary.Put("key1", "value1.1");
60+
var oldSecond = dictionary.Put("key3", "value3");
61+
62+
Assert.AreEqual(3, dictionary.Count);
63+
Assert.AreEqual("value1.1", dictionary["key1"]);
64+
Assert.AreEqual("value2", dictionary["key2"]);
65+
Assert.AreEqual("value3", dictionary["key3"]);
66+
Assert.AreEqual("value1", oldFirst);
67+
Assert.IsNull(oldSecond);
68+
}
69+
}
70+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using Lucene.Net.Attributes;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
6+
namespace Lucene.Net.Util
7+
{
8+
/*
9+
* Licensed to the Apache Software Foundation (ASF) under one or more
10+
* contributor license agreements. See the NOTICE file distributed with
11+
* this work for additional information regarding copyright ownership.
12+
* The ASF licenses this file to You under the Apache License, Version 2.0
13+
* (the "License"); you may not use this file except in compliance with
14+
* the License. You may obtain a copy of the License at
15+
*
16+
* http://www.apache.org/licenses/LICENSE-2.0
17+
*
18+
* Unless required by applicable law or agreed to in writing, software
19+
* distributed under the License is distributed on an "AS IS" BASIS,
20+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
* See the License for the specific language governing permissions and
22+
* limitations under the License.
23+
*/
24+
25+
[TestFixture]
26+
public class TestCastTo : LuceneTestCase
27+
{
28+
public static IEnumerable<TestCaseData> TestFromSuccessCases()
29+
{
30+
yield return new TestCaseData((byte)1, (short)1);
31+
yield return new TestCaseData((byte)1, 1);
32+
yield return new TestCaseData((byte)1, 1L);
33+
yield return new TestCaseData((byte)1, 1f);
34+
yield return new TestCaseData((byte)1, 1d);
35+
yield return new TestCaseData((short)2, (byte)2);
36+
yield return new TestCaseData((short)2, 2);
37+
yield return new TestCaseData((short)2, 2L);
38+
yield return new TestCaseData((short)2, 2f);
39+
yield return new TestCaseData((short)2, 2d);
40+
yield return new TestCaseData(3, (byte)3);
41+
yield return new TestCaseData(3, (short)3);
42+
yield return new TestCaseData(3, 3L);
43+
yield return new TestCaseData(3, 3f);
44+
yield return new TestCaseData(3, 3d);
45+
yield return new TestCaseData(4L, (byte)4);
46+
yield return new TestCaseData(4L, (short)4);
47+
yield return new TestCaseData(4L, 4);
48+
yield return new TestCaseData(4L, 4f);
49+
yield return new TestCaseData(4L, 4d);
50+
yield return new TestCaseData(5f, (byte)5);
51+
yield return new TestCaseData(5f, (short)5);
52+
yield return new TestCaseData(5f, 5);
53+
yield return new TestCaseData(5f, 5L);
54+
yield return new TestCaseData(5f, 5d);
55+
yield return new TestCaseData(6d, (byte)6);
56+
yield return new TestCaseData(6d, (short)6);
57+
yield return new TestCaseData(6d, 6);
58+
yield return new TestCaseData(6d, 6L);
59+
yield return new TestCaseData(6d, 6f);
60+
}
61+
62+
[Test, LuceneNetSpecific]
63+
[TestCaseSource(nameof(TestFromSuccessCases))]
64+
public void TestFrom_Success(object value, object expected)
65+
{
66+
var castTo = typeof(CastTo<>).MakeGenericType(expected.GetType());
67+
var from = castTo.GetMethod("From")?.MakeGenericMethod(value.GetType())
68+
?? throw new InvalidOperationException("Could not find method CastTo<T>.From<TSource>");
69+
Assert.AreEqual(expected, from.Invoke(null, new[] { value }));
70+
}
71+
72+
public static IEnumerable<TestCaseData> TestFromInvalidCastCases()
73+
{
74+
yield return new TestCaseData(1, "1");
75+
yield return new TestCaseData(new object(), 1);
76+
}
77+
78+
[Test, LuceneNetSpecific]
79+
[TestCaseSource(nameof(TestFromInvalidCastCases))]
80+
public void TestFrom_InvalidCast(object value, object expected)
81+
{
82+
var castTo = typeof(CastTo<>).MakeGenericType(expected.GetType());
83+
var from = castTo.GetMethod("From")?.MakeGenericMethod(value.GetType())
84+
?? throw new InvalidOperationException("Could not find method CastTo<T>.From<TSource>");
85+
try
86+
{
87+
from.Invoke(null, new[] { value });
88+
Assert.Fail("Expected an exception");
89+
}
90+
catch
91+
{
92+
// ignored
93+
}
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)