Skip to content

Commit

Permalink
Now exclude the global prefix from built in types (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
glennawatson authored Jun 10, 2019
1 parent ed2272f commit 8a49eb7
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 158 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ src/ReactiveUI.Events*/Events_*.cs
# macOS
.DS_Store

src/*.Tests/**/ApiApprovalTests*.received.txt
src/*.Tests/**/*.received.txt
.idea/

# Fody Weavers (for tests)
Expand Down
23 changes: 12 additions & 11 deletions src/Pharmacist.Core/Generation/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ public static IReadOnlyCollection<ITypeDefinition> GetReferenceTypeDefinitionsWi
return map.GetValueOrDefault(name);
}

public static string GetBuiltInType(string typeName)
{
if (_fullToBuiltInTypes.TryGetValue(typeName, out var builtInName))
{
return builtInName;
}

return typeName;
}

/// <summary>
/// Get a list of non-generic public type definitions.
/// </summary>
Expand Down Expand Up @@ -158,7 +148,8 @@ public static IType GetRealType(this IType type, ICompilation compilation)
/// <returns>A type descriptor including the generic arguments.</returns>
public static string GenerateFullGenericName(this IType currentType)
{
var sb = new StringBuilder("global::" + GetBuiltInType(currentType.FullName));
var (isBuiltIn, typeName) = GetBuiltInType(currentType.FullName);
var sb = new StringBuilder(!isBuiltIn ? "global::" + typeName : typeName);

if (currentType.TypeParameterCount > 0)
{
Expand All @@ -177,5 +168,15 @@ private static IImmutableList<ITypeDefinition> GetPublicTypeDefinitionsWithEvent
comp => comp.GetPublicNonGenericTypeDefinitions().Where(x => x.Events.Any(eventInfo => eventInfo.Accessibility == Accessibility.Public))
.ToImmutableList());
}

private static (bool isInternalType, string typeName) GetBuiltInType(string typeName)
{
if (_fullToBuiltInTypes.TryGetValue(typeName, out var builtInName))
{
return (true, builtInName);
}

return (false, typeName);
}
}
}
92 changes: 92 additions & 0 deletions src/Pharmacist.Tests/IntegrationTests/IntegrationTestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

using NuGet.Configuration;
using NuGet.Frameworks;
using NuGet.LibraryModel;
using NuGet.Packaging.Core;
using NuGet.Protocol.Core.Types;

using Pharmacist.Core;
using Pharmacist.Core.NuGet;

using Shouldly;

namespace Pharmacist.Tests.IntegrationTests
{
internal static class IntegrationTestHelper
{
private static readonly Regex _whitespaceRegex = new Regex(@"\s");

public static async Task CheckResultsAgainstTemplate(PackageIdentity[] package, IReadOnlyCollection<NuGetFramework> frameworks, [CallerFilePath]string filePath = null)
{
using (var memoryStream = new MemoryStream())
{
await ObservablesForEventGenerator.ExtractEventsFromNuGetPackages(memoryStream, package, frameworks).ConfigureAwait(false);
CheckContents(memoryStream, package[0], filePath);
}
}

public static async Task CheckResultsAgainstTemplate(LibraryRange[] package, IReadOnlyCollection<NuGetFramework> frameworks, [CallerFilePath]string filePath = null)
{
var bestPackageIdentity = await NuGetPackageHelper.GetBestMatch(package[0], new SourceRepository(new PackageSource(NuGetPackageHelper.DefaultNuGetSource), NuGetPackageHelper.Providers), CancellationToken.None).ConfigureAwait(false);

using (var memoryStream = new MemoryStream())
{
await ObservablesForEventGenerator.ExtractEventsFromNuGetPackages(memoryStream, package, frameworks).ConfigureAwait(false);
CheckContents(memoryStream, bestPackageIdentity, filePath);
}
}

private static void CheckContents(MemoryStream memoryStream, PackageIdentity bestPackageIdentity, string filePath)
{
var sourceDirectory = Path.GetDirectoryName(filePath);

var approvedFileName = Path.Combine(sourceDirectory, $"{bestPackageIdentity.Id}.{bestPackageIdentity.Version}.approved.txt");
var receivedFileName = Path.Combine(sourceDirectory, $"{bestPackageIdentity.Id}.{bestPackageIdentity.Version}.received.txt");

if (!File.Exists(approvedFileName))
{
File.Create(approvedFileName);
}

if (!File.Exists(receivedFileName))
{
File.Create(receivedFileName);
}

memoryStream.Flush();

memoryStream.Position = 0;
using (var sr = new StreamReader(memoryStream))
{
var actualContents = sr.ReadToEnd().Trim('\n').Trim('\r');
var expectedContents = File.ReadAllText(approvedFileName);

string normalizedActual = _whitespaceRegex.Replace(actualContents, string.Empty);
string normalizedExpected = _whitespaceRegex.Replace(expectedContents, string.Empty);

if (!string.Equals(normalizedActual, normalizedExpected, StringComparison.InvariantCulture))
{
File.WriteAllText(receivedFileName, actualContents);
ShouldlyConfiguration.DiffTools.GetDiffTool().Open(receivedFileName, approvedFileName, true);
}

normalizedActual.ShouldNotBeEmpty();

normalizedActual.ShouldBe(normalizedExpected, StringCompareShould.IgnoreLineEndings);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Threading.Tasks;

using NuGet.Frameworks;
using NuGet.LibraryModel;
using NuGet.Versioning;

using Pharmacist.Core.NuGet;

using Xunit;

namespace Pharmacist.Tests.IntegrationTests
{
public class LibraryRangeIntegrationTests
{
/// <summary>
/// Tests to make sure the Tizen platform produces the expected results.
/// </summary>
/// <returns>A task to monitor the progress.</returns>
[Fact]
public Task TizenNuGetTest()
{
var package = new[] { new LibraryRange("Tizen.NET.API4", VersionRange.Parse("4.0.1.*"), LibraryDependencyTarget.Package) };
var frameworks = new[] { FrameworkConstants.CommonFrameworks.NetStandard20 };

return IntegrationTestHelper.CheckResultsAgainstTemplate(package, frameworks);
}

/// <summary>
/// Tests to make sure the Xamarin.Essentials platform produces the expected results.
/// </summary>
/// <returns>A task to monitor the progress.</returns>
[Fact]
public Task XamarinEssentialsNuGetTest()
{
var package = new[] { new LibraryRange("Xamarin.Essentials", VersionRange.Parse("1.1.*"), LibraryDependencyTarget.Package) };
var frameworks = "MonoAndroid81".ToFrameworks();

return IntegrationTestHelper.CheckResultsAgainstTemplate(package, frameworks);
}

/// <summary>
/// Tests to make sure the Xamarin.Forms platform produces the expected results.
/// </summary>
/// <returns>A task to monitor the progress.</returns>
[Fact]
public Task XamarinFormsNuGetTest()
{
var package = new[] { new LibraryRange("Xamarin.Forms", VersionRange.Parse("4.0.0.*"), LibraryDependencyTarget.Package) };
var frameworks = "MonoAndroid81".ToFrameworks();

return IntegrationTestHelper.CheckResultsAgainstTemplate(package, frameworks);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,23 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using NuGet.Frameworks;
using NuGet.Packaging.Core;
using NuGet.Versioning;

using Pharmacist.Core;
using Pharmacist.Core.NuGet;

using Shouldly;

using Xunit;

namespace Pharmacist.Tests
namespace Pharmacist.Tests.IntegrationTests
{
/// <summary>
/// Tests to make sure that integration tests produce correct results.
/// </summary>
public class PackageIdentityIntegrationTests
{
private static readonly Regex _whitespaceRegex = new Regex(@"\s");

/// <summary>
/// Tests to make sure the Tizen platform produces the expected results.
/// </summary>
Expand All @@ -38,7 +30,7 @@ public Task TizenNuGetTest()
var package = new[] { new PackageIdentity("Tizen.NET.API4", new NuGetVersion("4.0.1.14152")) };
var frameworks = new[] { FrameworkConstants.CommonFrameworks.NetStandard20 };

return CheckResultsAgainstTemplate(package, frameworks);
return IntegrationTestHelper.CheckResultsAgainstTemplate(package, frameworks);
}

/// <summary>
Expand All @@ -51,7 +43,7 @@ public Task XamarinEssentialsNuGetTest()
var package = new[] { new PackageIdentity("Xamarin.Essentials", new NuGetVersion("1.1.0")) };
var frameworks = "MonoAndroid81".ToFrameworks();

return CheckResultsAgainstTemplate(package, frameworks);
return IntegrationTestHelper.CheckResultsAgainstTemplate(package, frameworks);
}

/// <summary>
Expand All @@ -64,30 +56,7 @@ public Task XamarinFormsNuGetTest()
var package = new[] { new PackageIdentity("Xamarin.Forms", new NuGetVersion("4.0.0.482894")) };
var frameworks = "MonoAndroid81".ToFrameworks();

return CheckResultsAgainstTemplate(package, frameworks);
}

private static async Task CheckResultsAgainstTemplate(PackageIdentity[] package, IReadOnlyCollection<NuGetFramework> frameworks)
{
using (var memoryStream = new MemoryStream())
{
await ObservablesForEventGenerator.ExtractEventsFromNuGetPackages(memoryStream, package, frameworks).ConfigureAwait(false);
memoryStream.Flush();

memoryStream.Position = 0;
using (var sr = new StreamReader(memoryStream))
{
var actualContents = sr.ReadToEnd();
var expectedContents = File.ReadAllText($"TestExpectedResults/{package[0].Id}.{package[0].Version}.txt");

string normalizedActual = _whitespaceRegex.Replace(actualContents, string.Empty);
string normalizedExpected = _whitespaceRegex.Replace(expectedContents, string.Empty);

normalizedActual.ShouldNotBeEmpty();

normalizedActual.ShouldBe(normalizedExpected, StringCompareShould.IgnoreLineEndings);
}
}
return IntegrationTestHelper.CheckResultsAgainstTemplate(package, frameworks);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace ElmSharp

namespace ElmSharp
{
/// <summary>
/// A class that contains extension methods to wrap events for classes contained within the <see cref = "ElmSharp"/> namespace.
Expand Down Expand Up @@ -3593,7 +3594,7 @@ namespace Tizen.NUI
/// <summary>
/// Gets an observable which signals when the <see cref = "Tizen.NUI.PropertyNotification.Notified"/> event triggers.
/// </summary>
public global::System.IObservable<global::Tizen.NUI.PropertyNotification.NotifyEventArgs> Notified => global::System.Reactive.Linq.Observable.FromEventPattern<global::Tizen.NUI.DaliEventHandler<global::object, global::Tizen.NUI.PropertyNotification.NotifyEventArgs>, global::Tizen.NUI.PropertyNotification.NotifyEventArgs>(x => _data.Notified += x, x => _data.Notified -= x).Select(x => x.EventArgs);
public global::System.IObservable<global::Tizen.NUI.PropertyNotification.NotifyEventArgs> Notified => global::System.Reactive.Linq.Observable.FromEventPattern<global::Tizen.NUI.DaliEventHandler<object, global::Tizen.NUI.PropertyNotification.NotifyEventArgs>, global::Tizen.NUI.PropertyNotification.NotifyEventArgs>(x => _data.Notified += x, x => _data.Notified -= x).Select(x => x.EventArgs);
}

/// <summary>
Expand All @@ -3614,7 +3615,7 @@ namespace Tizen.NUI
/// <summary>
/// Gets an observable which signals when the <see cref = "Tizen.NUI.ScrollView.SnapStarted"/> event triggers.
/// </summary>
public global::System.IObservable<global::Tizen.NUI.ScrollView.SnapStartedEventArgs> SnapStarted => global::System.Reactive.Linq.Observable.FromEventPattern<global::Tizen.NUI.DaliEventHandler<global::object, global::Tizen.NUI.ScrollView.SnapStartedEventArgs>, global::Tizen.NUI.ScrollView.SnapStartedEventArgs>(x => _data.SnapStarted += x, x => _data.SnapStarted -= x).Select(x => x.EventArgs);
public global::System.IObservable<global::Tizen.NUI.ScrollView.SnapStartedEventArgs> SnapStarted => global::System.Reactive.Linq.Observable.FromEventPattern<global::Tizen.NUI.DaliEventHandler<object, global::Tizen.NUI.ScrollView.SnapStartedEventArgs>, global::Tizen.NUI.ScrollView.SnapStartedEventArgs>(x => _data.SnapStarted += x, x => _data.SnapStarted -= x).Select(x => x.EventArgs);
}

/// <summary>
Expand Down Expand Up @@ -3831,15 +3832,15 @@ namespace Tizen.NUI.BaseComponents
/// <summary>
/// Gets an observable which signals when the <see cref = "Tizen.NUI.BaseComponents.Scrollable.ScrollCompleted"/> event triggers.
/// </summary>
public global::System.IObservable<global::Tizen.NUI.BaseComponents.Scrollable.CompletedEventArgs> ScrollCompleted => global::System.Reactive.Linq.Observable.FromEventPattern<global::Tizen.NUI.DaliEventHandler<global::object, global::Tizen.NUI.BaseComponents.Scrollable.CompletedEventArgs>, global::Tizen.NUI.BaseComponents.Scrollable.CompletedEventArgs>(x => _data.ScrollCompleted += x, x => _data.ScrollCompleted -= x).Select(x => x.EventArgs);
public global::System.IObservable<global::Tizen.NUI.BaseComponents.Scrollable.CompletedEventArgs> ScrollCompleted => global::System.Reactive.Linq.Observable.FromEventPattern<global::Tizen.NUI.DaliEventHandler<object, global::Tizen.NUI.BaseComponents.Scrollable.CompletedEventArgs>, global::Tizen.NUI.BaseComponents.Scrollable.CompletedEventArgs>(x => _data.ScrollCompleted += x, x => _data.ScrollCompleted -= x).Select(x => x.EventArgs);
/// <summary>
/// Gets an observable which signals when the <see cref = "Tizen.NUI.BaseComponents.Scrollable.ScrollStarted"/> event triggers.
/// </summary>
public global::System.IObservable<global::Tizen.NUI.BaseComponents.Scrollable.StartedEventArgs> ScrollStarted => global::System.Reactive.Linq.Observable.FromEventPattern<global::Tizen.NUI.DaliEventHandler<global::object, global::Tizen.NUI.BaseComponents.Scrollable.StartedEventArgs>, global::Tizen.NUI.BaseComponents.Scrollable.StartedEventArgs>(x => _data.ScrollStarted += x, x => _data.ScrollStarted -= x).Select(x => x.EventArgs);
public global::System.IObservable<global::Tizen.NUI.BaseComponents.Scrollable.StartedEventArgs> ScrollStarted => global::System.Reactive.Linq.Observable.FromEventPattern<global::Tizen.NUI.DaliEventHandler<object, global::Tizen.NUI.BaseComponents.Scrollable.StartedEventArgs>, global::Tizen.NUI.BaseComponents.Scrollable.StartedEventArgs>(x => _data.ScrollStarted += x, x => _data.ScrollStarted -= x).Select(x => x.EventArgs);
/// <summary>
/// Gets an observable which signals when the <see cref = "Tizen.NUI.BaseComponents.Scrollable.ScrollUpdated"/> event triggers.
/// </summary>
public global::System.IObservable<global::Tizen.NUI.BaseComponents.Scrollable.UpdatedEventArgs> ScrollUpdated => global::System.Reactive.Linq.Observable.FromEventPattern<global::Tizen.NUI.DaliEventHandler<global::object, global::Tizen.NUI.BaseComponents.Scrollable.UpdatedEventArgs>, global::Tizen.NUI.BaseComponents.Scrollable.UpdatedEventArgs>(x => _data.ScrollUpdated += x, x => _data.ScrollUpdated -= x).Select(x => x.EventArgs);
public global::System.IObservable<global::Tizen.NUI.BaseComponents.Scrollable.UpdatedEventArgs> ScrollUpdated => global::System.Reactive.Linq.Observable.FromEventPattern<global::Tizen.NUI.DaliEventHandler<object, global::Tizen.NUI.BaseComponents.Scrollable.UpdatedEventArgs>, global::Tizen.NUI.BaseComponents.Scrollable.UpdatedEventArgs>(x => _data.ScrollUpdated += x, x => _data.ScrollUpdated -= x).Select(x => x.EventArgs);
}

/// <summary>
Expand Down Expand Up @@ -5822,4 +5823,4 @@ namespace Tizen.Uix.VoiceControl
/// </summary>
public static global::System.IObservable<global::Tizen.Uix.VoiceControl.StateChangedEventArgs> VoiceControlClientStateChanged => global::System.Reactive.Linq.Observable.FromEventPattern<global::System.EventHandler<global::Tizen.Uix.VoiceControl.StateChangedEventArgs>, global::Tizen.Uix.VoiceControl.StateChangedEventArgs>(x => global::Tizen.Uix.VoiceControl.VoiceControlClient.StateChanged += x, x => global::Tizen.Uix.VoiceControl.VoiceControlClient.StateChanged -= x).Select(x => x.EventArgs);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Xamarin.Essentials
namespace Xamarin.Essentials
{
/// <summary>
/// A class that contains extension methods to wrap events contained within static classes within the <see cref = "Xamarin.Essentials"/> namespace.
Expand Down
Loading

0 comments on commit 8a49eb7

Please sign in to comment.