Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for ExceptionExtensions, #1112 #1120

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add unit tests for ExceptionExtensions, #1112
paulirwin committed Jan 22, 2025
commit 5636dc9afd0945b089034fc00b7bc490b8ed5b57
76 changes: 76 additions & 0 deletions src/Lucene.Net.Tests/Util/TestExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using NUnit.Framework;
using System;
using System.Linq;

#nullable enable

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.
*/

/// <summary>
/// Tests for <see cref="ExceptionExtensions"/>
/// </summary>
[TestFixture]
public class TestExceptionExtensions : LuceneTestCase
{
[Test]
public void TestAddSuppressed()
{
var e = new Exception("main");
var suppressed1 = new Exception("suppressed1");
var suppressed2 = new Exception("suppressed2");

e.AddSuppressed(suppressed1);
e.AddSuppressed(suppressed2);

Assert.AreEqual(2, e.GetSuppressed().Length);
Assert.IsTrue(e.GetSuppressed().Any(i => i.Message == suppressed1.Message));
Assert.IsTrue(e.GetSuppressed().Any(i => i.Message == suppressed2.Message));

// test GetSuppressedAsList
Assert.AreEqual(2, e.GetSuppressedAsList().Count);
Assert.IsTrue(e.GetSuppressedAsList().Any(i => i.Message == suppressed1.Message));
Assert.IsTrue(e.GetSuppressedAsList().Any(i => i.Message == suppressed2.Message));

// test GetSuppressedAsListOrDefault
var listOrDefault = e.GetSuppressedAsListOrDefault();
Assert.IsNotNull(listOrDefault);
Assert.AreEqual(2, listOrDefault!.Count);
Assert.IsTrue(listOrDefault.Any(i => i.Message == suppressed1.Message));
Assert.IsTrue(listOrDefault.Any(i => i.Message == suppressed2.Message));
}

[Test]
public void TestGetSuppressedAsListOrDefault_InitialStateNull()
{
var e = new Exception("test");
var list = e.GetSuppressedAsListOrDefault();
Assert.IsNull(list);
}

[Test]
public void TestGetSuppressedAsList_CreatesEmptyList()
{
var e = new Exception("test");
var list = e.GetSuppressedAsList();
Assert.IsNotNull(list);
Assert.AreEqual(0, list.Count);
}
}
}