Skip to content

Commit

Permalink
Merge pull request #150 from leancodepl/feature/pipe-analyzers
Browse files Browse the repository at this point in the history
LeanPipe analyzers
  • Loading branch information
jakubfijalkowski authored Aug 16, 2023
2 parents 29696f0 + c073c90 commit 274f17b
Show file tree
Hide file tree
Showing 24 changed files with 171 additions and 393 deletions.
17 changes: 17 additions & 0 deletions examples/analyzers/topic_known_types.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using LeanCode.Contracts;

public class Topic1 : ITopic, IProduceNotification<int> { }

public class Topic2 : ITopic, IProduceNotification<DateTimeOffset> { }

public class Topic3 : ITopic, IProduceNotification<Guid> { }

public class Topic4 : ITopic, IProduceNotification<int[]> { }

public class Topic5 : ITopic, IProduceNotification<List<int>> { }

public class Topic6 : ITopic, IProduceNotification<string> { }

public class Topic7 : ITopic, IProduceNotification<Dictionary<int, string>> { }
5 changes: 1 addition & 4 deletions examples/notifications/generic.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using LeanCode.Contracts;

namespace Notifications.Generic;
Expand All @@ -10,9 +9,7 @@ public class Topic2 : ITopic, IProduceNotification<Notification2<int, DTO1>> { }

public class Topic3 : ITopic, IProduceNotification<Notification2<DateTimeOffset, DTO2<int>>> { }

public class Topic4 : ITopic, IProduceNotification<Dictionary<int, DTO2<int>>> { }

public class Topic5
public class Topic4
: ITopic,
IProduceNotification<Notification1<int>>,
IProduceNotification<Notification2<byte, TimeSpan>> { }
Expand Down
31 changes: 0 additions & 31 deletions examples/notifications/known_types.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using LeanCode.ContractsGenerator.Analyzers;
using Xunit;

namespace LeanCode.ContractsGenerator.Tests.ExampleBased.Analyzers;
Expand All @@ -9,13 +10,16 @@ public void Context_is_tracked_correctly()
{
"analyzers/context.cs"
.AnalyzeFails()
.WithError("CNTR0004", "Dto1.A<0: System.Decimal>")
.WithError("CNTR0004", "Dto2.A<0: System.Decimal>")
.WithError("CNTR0004", "Dto3:System.IDisposable")
.WithError("CNTR0004", "Dto4:System.IDisposable")
.WithError("CNTR0004", "Dto5:Inner<0: System.Decimal>")
.WithError("CNTR0004", "Dto6:Inner<0: Inner><0: System.Decimal>")
.WithError("CNTR0004", "Query1->System.Decimal")
.WithError("CNTR0004", "Query2->Inner<0: System.Decimal>");
.WithError(AnalyzerCodes.InternalTypeIsNotKnown, "Dto1.A<0: System.Decimal>")
.WithError(AnalyzerCodes.InternalTypeIsNotKnown, "Dto2.A<0: System.Decimal>")
.WithError(AnalyzerCodes.InternalTypeIsNotKnown, "Dto3:System.IDisposable")
.WithError(AnalyzerCodes.InternalTypeIsNotKnown, "Dto4:System.IDisposable")
.WithError(AnalyzerCodes.InternalTypeIsNotKnown, "Dto5:Inner<0: System.Decimal>")
.WithError(
AnalyzerCodes.InternalTypeIsNotKnown,
"Dto6:Inner<0: Inner><0: System.Decimal>"
)
.WithError(AnalyzerCodes.InternalTypeIsNotKnown, "Query1->System.Decimal")
.WithError(AnalyzerCodes.InternalTypeIsNotKnown, "Query2->Inner<0: System.Decimal>");
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using LeanCode.ContractsGenerator.Analyzers;
using Xunit;

namespace LeanCode.ContractsGenerator.Tests.ExampleBased.Analyzers;
Expand All @@ -9,7 +10,7 @@ public void Duplicated_error_codes_in_command_are_detected()
{
"analyzers/error_codes.cs"
.AnalyzeFails()
.WithError("CNTR0003", "Cmd1.ErrorCodes")
.WithError("CNTR0003", "Cmd2.ErrorCodes");
.WithError(AnalyzerCodes.DuplicateErrorCodes, "Cmd1.ErrorCodes")
.WithError(AnalyzerCodes.DuplicateErrorCodes, "Cmd2.ErrorCodes");
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using LeanCode.ContractsGenerator.Analyzers;
using Xunit;

namespace LeanCode.ContractsGenerator.Tests.ExampleBased.Analyzers;
Expand All @@ -10,9 +11,9 @@ public void External_types_are_reported_as_errors()
"analyzers/external_types.cs"
.AnalyzeFails()
.WithErrorNumber(4)
.WithError("CNTR0004", "Dto.Wrong1")
.WithError("CNTR0004", "Dto.Wrong2")
.WithError("CNTR0004", "Dto.Wrong3")
.WithError("CNTR0004", "Query->System.Decimal");
.WithError(AnalyzerCodes.InternalTypeIsNotKnown, "Dto.Wrong1")
.WithError(AnalyzerCodes.InternalTypeIsNotKnown, "Dto.Wrong2")
.WithError(AnalyzerCodes.InternalTypeIsNotKnown, "Dto.Wrong3")
.WithError(AnalyzerCodes.InternalTypeIsNotKnown, "Query->System.Decimal");
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using LeanCode.ContractsGenerator.Analyzers;
using Xunit;

namespace LeanCode.ContractsGenerator.Tests.ExampleBased.Analyzers;
Expand All @@ -11,7 +12,7 @@ public void Invalid_types_are_reported()
.AnalyzeFails()
.WithErrorNumber(1)
.WithError(
"CNTR0006",
AnalyzerCodes.UnsupportedType,
"Dto.Wrong1",
messagePattern: ".+Use `DateTimeOffset` with zero offset instead.+"
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using LeanCode.ContractsGenerator.Analyzers;
using Xunit;

namespace LeanCode.ContractsGenerator.Tests.ExampleBased.Analyzers;

public class KnownTypesInTopics
{
[Fact]
public void Known_types_are_forbidden_in_topics()
{
"analyzers/topic_known_types.cs"
.AnalyzeFails()
.WithErrorNumber(7)
.WithError(AnalyzerCodes.TopicMustProduceInternalType, "Topic1")
.WithError(AnalyzerCodes.TopicMustProduceInternalType, "Topic2")
.WithError(AnalyzerCodes.TopicMustProduceInternalType, "Topic3")
.WithError(AnalyzerCodes.TopicMustProduceInternalType, "Topic4")
.WithError(AnalyzerCodes.TopicMustProduceInternalType, "Topic5")
.WithError(AnalyzerCodes.TopicMustProduceInternalType, "Topic6")
.WithError(AnalyzerCodes.TopicMustProduceInternalType, "Topic7");
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using LeanCode.ContractsGenerator.Analyzers;
using Xunit;

namespace LeanCode.ContractsGenerator.Tests.ExampleBased.Analyzers;
Expand All @@ -11,12 +12,12 @@ public void Topics_with_nullable_notifications_are_reported()
.AnalyzeFails()
.WithErrorNumber(2)
.WithError(
"CNTR0008",
AnalyzerCodes.TopicProducesNullableNotification,
"NullableNotificationTopic",
messagePattern: "Topic type .+ produces nullable notification type"
)
.WithError(
"CNTR0008",
AnalyzerCodes.TopicProducesNullableNotification,
"NullableNotificationTopic",
messagePattern: "Topic type .+ produces nullable notification type."
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using LeanCode.ContractsGenerator.Analyzers;
using Xunit;

namespace LeanCode.ContractsGenerator.Tests.ExampleBased.Analyzers;
Expand All @@ -11,22 +12,22 @@ public void Topics_without_notifications_are_reported()
.AnalyzeFails()
.WithErrorNumber(4)
.WithError(
"CNTR0007",
AnalyzerCodes.TopicDoesNotProduceNotification,
"EmptyTopic",
messagePattern: "Topic type .+ doesn't produce any notification."
)
.WithError(
"CNTR0007",
AnalyzerCodes.TopicDoesNotProduceNotification,
"EmptyInheritedTopic",
messagePattern: "Topic type .+ doesn't produce any notification."
)
.WithError(
"CNTR0007",
AnalyzerCodes.TopicDoesNotProduceNotification,
"InheritedInterfaceEmptyTopic",
messagePattern: "Topic type .+ doesn't produce any notification."
)
.WithError(
"CNTR0007",
AnalyzerCodes.TopicDoesNotProduceNotification,
"ConcreteEmptyTopic",
messagePattern: "Topic type .+ doesn't produce any notification."
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using LeanCode.Contracts;
using Notifications.Generic;
using Xunit;
using static LeanCode.ContractsGenerator.Tests.TypeRefExtensions;
using static LeanCode.ContractsGenerator.Tests.NotificationTypeRefExtensions;

namespace LeanCode.ContractsGenerator.Tests.ExampleBased.Notifications;

Expand All @@ -13,14 +16,13 @@ public void Single_generic_argument_notification()
.WithDto("Notifications.Generic.Notification1")
.WithTopic("Notifications.Generic.Topic1")
.WithNotification(
NotificationTypeRefExtensions.WithTag(
WithTag(
TypeRefExtensions
.Internal("Notifications.Generic.Notification1")
.WithArguments(TypeRefExtensions.Known(KnownType.Int32)),
LeanCode.Contracts.NotificationTagGenerator.Generate(typeof(Notification1<int>))
.WithArguments(Known(KnownType.Int32)),
NotificationTagGenerator.Generate(typeof(Notification1<int>))
)
);
// Notifications.Generic.Notification1[!Int32]
}

[Fact]
Expand All @@ -31,19 +33,16 @@ public void Multiple_generic_arguments_notification()
.WithDto("Notifications.Generic.DTO1")
.WithTopic("Notifications.Generic.Topic2")
.WithNotification(
NotificationTypeRefExtensions.WithTag(
WithTag(
TypeRefExtensions
.Internal("Notifications.Generic.Notification2")
.WithArguments(
TypeRefExtensions.Known(KnownType.Int32),
Known(KnownType.Int32),
TypeRefExtensions.Internal("Notifications.Generic.DTO1")
),
LeanCode.Contracts.NotificationTagGenerator.Generate(
typeof(Notification2<int, DTO1>)
)
NotificationTagGenerator.Generate(typeof(Notification2<int, DTO1>))
)
);
// Notifications.Generic.Notification2[!Int32,Notifications.Generic.DTO1]
}

[Fact]
Expand All @@ -54,74 +53,43 @@ public void Nested_generic_arguments_notification()
.WithDto("Notifications.Generic.DTO2")
.WithTopic("Notifications.Generic.Topic3")
.WithNotification(
NotificationTypeRefExtensions.WithTag(
WithTag(
TypeRefExtensions
.Internal("Notifications.Generic.Notification2")
.WithArguments(
TypeRefExtensions.Known(KnownType.DateTimeOffset),
Known(KnownType.DateTimeOffset),
TypeRefExtensions
.Internal("Notifications.Generic.DTO2")
.WithArguments(TypeRefExtensions.Known(KnownType.Int32))
.WithArguments(Known(KnownType.Int32))
),
LeanCode.Contracts.NotificationTagGenerator.Generate(
NotificationTagGenerator.Generate(
typeof(Notification2<DateTimeOffset, DTO2<int>>)
)
)
);
// Notifications.Generic.Notification2[!DateTimeOffset,Notifications.Generic.DTO2[!Int32]]
}

[Fact]
public void Nested_known_type_generic_notification()
{
"notifications/generic.cs"
.Compiles()
.WithDto("Notifications.Generic.DTO2")
.WithTopic("Notifications.Generic.Topic4")
.WithNotification(
NotificationTypeRefExtensions.WithTag(
TypeRefExtensions.Map(
TypeRefExtensions.Known(KnownType.Int32),
TypeRefExtensions
.Internal("Notifications.Generic.DTO2")
.WithArguments(TypeRefExtensions.Known(KnownType.Int32))
),
LeanCode.Contracts.NotificationTagGenerator.Generate(
typeof(Dictionary<int, DTO2<int>>)
)
)
);
// !Map[!Int32,Notifications.Generic.DTO2[!Int32]]
}

[Fact]
public void Multiple_generic_notifications_topic()
{
"notifications/generic.cs"
.Compiles()
.WithTopic("Notifications.Generic.Topic5")
.WithTopic("Notifications.Generic.Topic4")
.WithNotification(
NotificationTypeRefExtensions.WithTag(
WithTag(
TypeRefExtensions
.Internal("Notifications.Generic.Notification1")
.WithArguments(TypeRefExtensions.Known(KnownType.Int32)),
LeanCode.Contracts.NotificationTagGenerator.Generate(typeof(Notification1<int>))
.WithArguments(Known(KnownType.Int32)),
NotificationTagGenerator.Generate(typeof(Notification1<int>))
)
)
// !Map[!Int32,Notifications.Generic.DTO2[!Int32]]
.WithNotification(
NotificationTypeRefExtensions.WithTag(
WithTag(
TypeRefExtensions
.Internal("Notifications.Generic.Notification2")
.WithArguments(
TypeRefExtensions.Known(KnownType.Uint8),
TypeRefExtensions.Known(KnownType.TimeSpan)
),
LeanCode.Contracts.NotificationTagGenerator.Generate(
typeof(Notification2<byte, TimeSpan>)
)
.WithArguments(Known(KnownType.Uint8), Known(KnownType.TimeSpan)),
NotificationTagGenerator.Generate(typeof(Notification2<byte, TimeSpan>))
)
);
// Notifications.Generic.Notification2[!Uint8,!TimeSpan]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Notifications.Internal;
using Xunit;
using LeanCode.Contracts;
using static LeanCode.ContractsGenerator.Tests.NotificationTypeRefExtensions;

namespace LeanCode.ContractsGenerator.Tests.ExampleBased.Notifications;

Expand All @@ -10,22 +12,18 @@ public void Internal_notifications()
{
"notifications/internal.cs"
.Compiles()
.WithDto("Notifications.Internal.DTO1")
.WithDto("Notifications.Internal.DTO2")
.WithTopic("Notifications.Internal.Topic")
.WithNotification(
NotificationTypeRefExtensions.WithTag(
WithTag(
TypeRefExtensions.Internal("Notifications.Internal.DTO1"),
LeanCode.Contracts.NotificationTagGenerator.Generate(typeof(DTO1))
NotificationTagGenerator.Generate(typeof(DTO1))
)
)
// Notifications.Internal.DTO1
.WithNotification(
NotificationTypeRefExtensions.WithTag(
WithTag(
TypeRefExtensions.Internal("Notifications.Internal.DTO2"),
LeanCode.Contracts.NotificationTagGenerator.Generate(typeof(DTO2))
NotificationTagGenerator.Generate(typeof(DTO2))
)
);
// Notifications.Internal.DTO2
}
}
Loading

0 comments on commit 274f17b

Please sign in to comment.