Skip to content
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
121 changes: 51 additions & 70 deletions tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using CommandLine.Core;
using CommandLine.Infrastructure;
using CommandLine.Tests.Fakes;
using CommandLine.Tests.Unit.Infrastructure;
using CommandLine.Text;
using FluentAssertions;
using Xunit;
using System.Text;

namespace CommandLine.Tests.Unit.Text
{
public class HelpTextTests
public class HelpTextTests : IDisposable
{
public void Dispose()
{
ReflectionHelper.SetAttributeOverride(null);
}

[Fact]
public void Create_empty_instance()
{
Expand Down Expand Up @@ -573,91 +575,70 @@ public void Default_set_to_sequence_should_be_properly_printed()
[Fact]
public void AutoBuild_when_no_assembly_attributes()
{
try
{
string expectedCopyright = "Copyright (C) 1 author";
string expectedCopyright = "Copyright (C) 1 author";

ReflectionHelper.SetAttributeOverride(new Attribute[0]);
ReflectionHelper.SetAttributeOverride(new Attribute[0]);

ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
bool onErrorCalled = false;
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
{
onErrorCalled = true;
return ht;
}, ex => ex);

onErrorCalled.Should().BeTrue();
actualResult.Copyright.Should().Be(expectedCopyright);
}
finally
ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
bool onErrorCalled = false;
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
{
ReflectionHelper.SetAttributeOverride(null);
}
onErrorCalled = true;
return ht;
}, ex => ex);

onErrorCalled.Should().BeTrue();
actualResult.Copyright.Should().Be(expectedCopyright);
}

[Fact]
public void AutoBuild_with_assembly_title_and_version_attributes_only()
{
try
{
string expectedTitle = "Title";
string expectedVersion = "1.2.3.4";
string expectedTitle = "Title";
string expectedVersion = "1.2.3.4";

ReflectionHelper.SetAttributeOverride(new Attribute[]
{
new AssemblyTitleAttribute(expectedTitle),
new AssemblyInformationalVersionAttribute(expectedVersion)
});

ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
bool onErrorCalled = false;
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
{
onErrorCalled = true;
return ht;
}, ex => ex);

onErrorCalled.Should().BeTrue();
actualResult.Heading.Should().Be(string.Format("{0} {1}", expectedTitle, expectedVersion));
}
finally
ReflectionHelper.SetAttributeOverride(new Attribute[]
{
new AssemblyTitleAttribute(expectedTitle),
new AssemblyInformationalVersionAttribute(expectedVersion)
});

ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
bool onErrorCalled = false;
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
{
ReflectionHelper.SetAttributeOverride(null);
}
onErrorCalled = true;
return ht;
}, ex => ex);

onErrorCalled.Should().BeTrue();
actualResult.Heading.Should().Be(string.Format("{0} {1}", expectedTitle, expectedVersion));
}


[Fact]
public void AutoBuild_with_assembly_company_attribute_only()
{
try
{
string expectedCompany = "Company";
string expectedCompany = "Company";

ReflectionHelper.SetAttributeOverride(new Attribute[]
{
new AssemblyCompanyAttribute(expectedCompany)
});
ReflectionHelper.SetAttributeOverride(new Attribute[]
{
new AssemblyCompanyAttribute(expectedCompany)
});

ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
bool onErrorCalled = false;
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
{
onErrorCalled = true;
return ht;
}, ex => ex);

onErrorCalled.Should().BeFalse(); // Other attributes have fallback logic
actualResult.Copyright.Should().Be(string.Format("Copyright (C) {0} {1}", DateTime.Now.Year, expectedCompany));
}
finally
ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
TypeInfo.Create(typeof (Simple_Options)), new Error[0]);
bool onErrorCalled = false;
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht =>
{
ReflectionHelper.SetAttributeOverride(null);
}
onErrorCalled = true;
return ht;
}, ex => ex);

onErrorCalled.Should().BeFalse(); // Other attributes have fallback logic
actualResult.Copyright.Should().Be(string.Format("Copyright (C) {0} {1}", DateTime.Now.Year, expectedCompany));
}

[Fact]
Expand Down