Skip to content

Commit

Permalink
Fix: some restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnaSasDev committed Jan 10, 2025
1 parent 5401b8f commit 5868e63
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 deletions.
5 changes: 0 additions & 5 deletions src/CodeOfChaos.GeneratorTools/Backports/NullableAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ public DoesNotReturnIfAttribute(bool parameterValue) {
public bool ParameterValue { get; }
}

#endif

#if !NETCOREAPP

/// <summary>
/// Specifies that the method or property will ensure that the listed field and property members have not-null
/// values.
Expand Down Expand Up @@ -169,5 +165,4 @@ public MemberNotNullWhenAttribute(bool returnValue, params string[] members) {
/// <summary>Gets field or property member names.</summary>
public string[] Members { get; }
}

#endif
25 changes: 7 additions & 18 deletions src/CodeOfChaos.GeneratorTools/Backports/StackBackports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,24 @@ namespace CodeOfChaos.GeneratorTools;
// Code
// ---------------------------------------------------------------------------------------------------------------------
public static class StackBackports {
// Done so we can easily
#if NETSTANDARD2_0
public static bool TryPop<T>(this Stack<T> stack, [NotNullWhen(true)] out T? result) {
result = default;
if (stack.Count == 0) return false;
result = stack.Pop();
return result is not null;
}

public static bool TryPeek<T>(this Stack<T> stack, [NotNullWhen(true)] out T? result) {
result = default;
if (stack.Count == 0) return false;
result = stack.Peek();
return result is not null;
}
public static bool TryPop<T>(this Stack<T> stack, [NotNullWhen(true)] out T? result) => TryPopBackport(stack, out result);
#endif

#if NET9_0_OR_GREATER
public static bool TryPop<T>(Stack<T> stack, [NotNullWhen(true)] out T? result) {
public static bool TryPopBackport<T>(Stack<T> stack, [NotNullWhen(true)] out T? result) {
result = default;
if (stack.Count == 0) return false;
result = stack.Pop();
return result is not null;
}

public static bool TryPeek<T>(Stack<T> stack, [NotNullWhen(true)] out T? result) {
#if NETSTANDARD2_0
public static bool TryPeek<T>(this Stack<T> stack, [NotNullWhen(true)] out T? result) => TryPeekBackport(stack, out result);
#endif
public static bool TryPeekBackport<T>(Stack<T> stack, [NotNullWhen(true)] out T? result) {
result = default;
if (stack.Count == 0) return false;
result = stack.Peek();
return result is not null;
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task TryPop_EmptyStack_ReturnsFalse_AndOutNull() {
var stack = new Stack<int>();

// Act
bool result = StackBackports.TryPop(stack,out int value);
bool result = StackBackports.TryPopBackport(stack,out int value);

// Assert
await Assert.That(result).IsFalse().Because("TryPop should return false for an empty stack.");
Expand All @@ -31,7 +31,7 @@ public async Task TryPop_NonEmptyStack_ReturnsTrue_AndOutTopElement() {
stack.Push(30);// Top element

// Act
bool result = StackBackports.TryPop(stack,out int value);
bool result = StackBackports.TryPopBackport(stack,out int value);

// Assert
await Assert.That(result).IsTrue().Because("TryPop should return true for a non-empty stack.");
Expand All @@ -46,7 +46,7 @@ public async Task TryPeek_EmptyStack_ReturnsFalse_AndOutNull() {
var stack = new Stack<string>();

// Act
bool result = StackBackports.TryPeek(stack, out string? value);
bool result = StackBackports.TryPeekBackport(stack, out string? value);

// Assert
await Assert.That(result).IsFalse().Because("TryPeek should return false for an empty stack.");
Expand All @@ -63,7 +63,7 @@ public async Task TryPeek_NonEmptyStack_ReturnsTrue_AndOutTopElement() {
stack.Push("top"); // Top element

// Act
bool result = StackBackports.TryPeek(stack, out string? value);
bool result = StackBackports.TryPeekBackport(stack, out string? value);

// Assert
await Assert.That(result).IsTrue().Because("TryPeek should return true for a non-empty stack.");
Expand All @@ -79,7 +79,7 @@ public async Task TryPeekWithNullables_AllowsNullValueHandling() {
stack.Push(10);

// Act
bool result = StackBackports.TryPeek(stack, out int? value);
bool result = StackBackports.TryPeekBackport(stack, out int? value);

// Assert
await Assert.That(result).IsTrue().Because("TryPeek should return true for a non-empty stack (nullable).");
Expand All @@ -94,8 +94,8 @@ public async Task TryPeek_WithMutatingCollectionAfterPeek() {
stack.Push(42);

// Act
bool resultPeek = StackBackports.TryPeek(stack, out int resultPeekValue);
bool resultPop = StackBackports.TryPop(stack,out int resultPopValue);
bool resultPeek = StackBackports.TryPeekBackport(stack, out int resultPeekValue);
bool resultPop = StackBackports.TryPopBackport(stack,out int resultPopValue);

// Assert
await Assert.That(resultPeek).IsTrue().Because("TryPeek should succeed.");
Expand Down

0 comments on commit 5868e63

Please sign in to comment.