Skip to content

Commit 3d4fec5

Browse files
authored
Merge pull request #401 from 0xced/HelpTextTests-reset-attribute-overrides
Reset attribute overrides when the test is disposed
2 parents 0922e48 + cdfe655 commit 3d4fec5

File tree

1 file changed

+51
-70
lines changed

1 file changed

+51
-70
lines changed

tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs

Lines changed: 51 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
using System;
44
using System.Collections.Generic;
5-
using System.Globalization;
65
using System.Linq;
76
using System.Reflection;
8-
using CommandLine.Core;
97
using CommandLine.Infrastructure;
108
using CommandLine.Tests.Fakes;
11-
using CommandLine.Tests.Unit.Infrastructure;
129
using CommandLine.Text;
1310
using FluentAssertions;
1411
using Xunit;
1512
using System.Text;
1613

1714
namespace CommandLine.Tests.Unit.Text
1815
{
19-
public class HelpTextTests
16+
public class HelpTextTests : IDisposable
2017
{
18+
public void Dispose()
19+
{
20+
ReflectionHelper.SetAttributeOverride(null);
21+
}
22+
2123
[Fact]
2224
public void Create_empty_instance()
2325
{
@@ -573,91 +575,70 @@ public void Default_set_to_sequence_should_be_properly_printed()
573575
[Fact]
574576
public void AutoBuild_when_no_assembly_attributes()
575577
{
576-
try
577-
{
578-
string expectedCopyright = "Copyright (C) 1 author";
578+
string expectedCopyright = "Copyright (C) 1 author";
579579

580-
ReflectionHelper.SetAttributeOverride(new Attribute[0]);
580+
ReflectionHelper.SetAttributeOverride(new Attribute[0]);
581581

582-
ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
583-
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
584-
bool onErrorCalled = false;
585-
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
586-
{
587-
onErrorCalled = true;
588-
return ht;
589-
}, ex => ex);
590-
591-
onErrorCalled.Should().BeTrue();
592-
actualResult.Copyright.Should().Be(expectedCopyright);
593-
}
594-
finally
582+
ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
583+
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
584+
bool onErrorCalled = false;
585+
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
595586
{
596-
ReflectionHelper.SetAttributeOverride(null);
597-
}
587+
onErrorCalled = true;
588+
return ht;
589+
}, ex => ex);
590+
591+
onErrorCalled.Should().BeTrue();
592+
actualResult.Copyright.Should().Be(expectedCopyright);
598593
}
599594

600595
[Fact]
601596
public void AutoBuild_with_assembly_title_and_version_attributes_only()
602597
{
603-
try
604-
{
605-
string expectedTitle = "Title";
606-
string expectedVersion = "1.2.3.4";
598+
string expectedTitle = "Title";
599+
string expectedVersion = "1.2.3.4";
607600

608-
ReflectionHelper.SetAttributeOverride(new Attribute[]
609-
{
610-
new AssemblyTitleAttribute(expectedTitle),
611-
new AssemblyInformationalVersionAttribute(expectedVersion)
612-
});
613-
614-
ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
615-
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
616-
bool onErrorCalled = false;
617-
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
618-
{
619-
onErrorCalled = true;
620-
return ht;
621-
}, ex => ex);
622-
623-
onErrorCalled.Should().BeTrue();
624-
actualResult.Heading.Should().Be(string.Format("{0} {1}", expectedTitle, expectedVersion));
625-
}
626-
finally
601+
ReflectionHelper.SetAttributeOverride(new Attribute[]
602+
{
603+
new AssemblyTitleAttribute(expectedTitle),
604+
new AssemblyInformationalVersionAttribute(expectedVersion)
605+
});
606+
607+
ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
608+
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
609+
bool onErrorCalled = false;
610+
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
627611
{
628-
ReflectionHelper.SetAttributeOverride(null);
629-
}
612+
onErrorCalled = true;
613+
return ht;
614+
}, ex => ex);
615+
616+
onErrorCalled.Should().BeTrue();
617+
actualResult.Heading.Should().Be(string.Format("{0} {1}", expectedTitle, expectedVersion));
630618
}
631619

632620

633621
[Fact]
634622
public void AutoBuild_with_assembly_company_attribute_only()
635623
{
636-
try
637-
{
638-
string expectedCompany = "Company";
624+
string expectedCompany = "Company";
639625

640-
ReflectionHelper.SetAttributeOverride(new Attribute[]
641-
{
642-
new AssemblyCompanyAttribute(expectedCompany)
643-
});
626+
ReflectionHelper.SetAttributeOverride(new Attribute[]
627+
{
628+
new AssemblyCompanyAttribute(expectedCompany)
629+
});
644630

645-
ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
646-
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
647-
bool onErrorCalled = false;
648-
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
649-
{
650-
onErrorCalled = true;
651-
return ht;
652-
}, ex => ex);
653-
654-
onErrorCalled.Should().BeFalse(); // Other attributes have fallback logic
655-
actualResult.Copyright.Should().Be(string.Format("Copyright (C) {0} {1}", DateTime.Now.Year, expectedCompany));
656-
}
657-
finally
631+
ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
632+
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
633+
bool onErrorCalled = false;
634+
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
658635
{
659-
ReflectionHelper.SetAttributeOverride(null);
660-
}
636+
onErrorCalled = true;
637+
return ht;
638+
}, ex => ex);
639+
640+
onErrorCalled.Should().BeFalse(); // Other attributes have fallback logic
641+
actualResult.Copyright.Should().Be(string.Format("Copyright (C) {0} {1}", DateTime.Now.Year, expectedCompany));
661642
}
662643

663644
[Fact]

0 commit comments

Comments
 (0)