Skip to content

Commit

Permalink
Feat/added dice rolling (#12)
Browse files Browse the repository at this point in the history
* Update labeler.yml and add dice rolling endpoint

* Update labeler configuration and sync labels

* Update labeler configuration, Added permissions to the triage job in PR Labeler workflow, and updated lebeler to v5

* Refactor calculator tests and add dice rolling functionality.

-Changed random algorithm, used RandomNumberGenerator class instead of general Random.

* Refactor DiceRollingTest and Program.cs according to deepsource scan result.
  • Loading branch information
Andronovo-bit authored Mar 22, 2024
1 parent eb64c51 commit 61343f4
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 84 deletions.
26 changes: 19 additions & 7 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
ci/cd:
- .github/workflows/*

# Add 'release' label to any PR that is opened against the `main` branch
# Add 'release' label to any PR that is opened against the `main` branch
release:
- base-branch: [ master, main ]

# Add 'feature' label to any PR where the head branch name starts with `feature` or has a `feature` section in the name
feature:
- head-branch: ['^feature', 'feature']
- head-branch: ['^feature', 'feature','^Feature', 'Feature', 'feature^', 'Feature^', 'feat', 'Feat', '^feat', 'Feat^']

# Add 'Documentation' label to any change to .md files within the entire repository
Documentation:
documentation:
- changed-files:
- any-glob-to-any-file: ['*.md', 'docs/*']

# Add 'bug' label to any PR that has the word 'bug' in the title
bug:
- title: ['bug', 'Bug', 'BUG', 'fix', 'Fix', 'FIX']

# Add CI/CD label to any PR that has the word 'CI' or 'CD' in the title
ci-cd:
- changed-files:
- any-glob-to-any-file: '**/*.md'
- any-glob-to-any-file: '**/.github/workflows/*'

# Add 'security' label to any PR that has the word 'security' in the title
security:
- title: ['security', 'Security', 'SECURITY', 'secure', 'Secure', 'SECURE']


7 changes: 6 additions & 1 deletion .github/workflows/pr-labeler.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
name: Pull Request Labeling
on: pull_request

jobs:
triage:
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v4
- uses: actions/labeler@v5
with:
sync-labels: true
repo-token: "${{ secrets.GITHUB_TOKEN }}"

size-label:
Expand Down
103 changes: 51 additions & 52 deletions WebApiCICDWorkFlow.Tests/CalculatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,67 +1,66 @@

namespace WebApiCICDWorkFlow.Tests
namespace WebApiCICDWorkFlow.Tests;

[TestClass]
public class CalculatorTests
{
[TestClass]
public class CalculatorTests
[TestMethod]
public void Add_TwoNumbers_ReturnsSum()
{
[TestMethod]
public void Add_TwoNumbers_ReturnsSum()
{
// Arrange
var a = 5;
var b = 10;
var expected = 15;
// Arrange
var a = 5;
var b = 10;
var expected = 15;

// Act
var result = Calculator.Add(a, b);
// Act
var result = Calculator.Add(a, b);

// Assert
Assert.AreEqual(expected, result, "The sum of the two numbers is not correct.");
}
// Assert
Assert.AreEqual(expected, result, "The sum of the two numbers is not correct.");
}

[TestMethod]
public void Subtract_TwoNumbers_ReturnsDifference()
{
// Arrange
var a = 10;
var b = 5;
var expected = 5;
[TestMethod]
public void Subtract_TwoNumbers_ReturnsDifference()
{
// Arrange
var a = 10;
var b = 5;
var expected = 5;

// Act
var result = Calculator.Subtract(a, b);
// Act
var result = Calculator.Subtract(a, b);

// Assert
Assert.AreEqual(expected, result, "The difference of the two numbers is not correct.");
}
// Assert
Assert.AreEqual(expected, result, "The difference of the two numbers is not correct.");
}

[TestMethod]
public void Multiply_TwoNumbers_ReturnsProduct()
{
// Arrange
var a = 5;
var b = 10;
var expected = 50;
[TestMethod]
public void Multiply_TwoNumbers_ReturnsProduct()
{
// Arrange
var a = 5;
var b = 10;
var expected = 50;

// Act
var result = Calculator.Multiply(a, b);
// Act
var result = Calculator.Multiply(a, b);

// Assert
Assert.AreEqual(expected, result, "The product of the two numbers is not correct.");
}
// Assert
Assert.AreEqual(expected, result, "The product of the two numbers is not correct.");
}

[TestMethod]
public void Divide_TwoNumbers_ReturnsQuotient()
{
// Arrange
var a = 10;
var b = 5;
var expected = 2;
[TestMethod]
public void Divide_TwoNumbers_ReturnsQuotient()
{
// Arrange
var a = 10;
var b = 5;
var expected = 2;

// Act
var result = Calculator.Divide(a, b);
// Act
var result = Calculator.Divide(a, b);

// Assert
Assert.AreEqual(expected, result, "The quotient of the two numbers is not correct.");
}
// Assert
Assert.AreEqual(expected, result, "The quotient of the two numbers is not correct.");
}
}
}
13 changes: 13 additions & 0 deletions WebApiCICDWorkFlow.Tests/DiceRollingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace WebApiCICDWorkFlow.Tests;

[TestClass]
public class DiceRollingTest
{
[TestMethod]
public void TestRollDice()
{
int result = DiceRolling.RollDice();
Assert.IsTrue(result >= 1 && result <= 6);
}
}

36 changes: 18 additions & 18 deletions WebApiCICDWorkFlow/Calculator.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
namespace WebApiCICDWorkFlow
namespace WebApiCICDWorkFlow;

public class Calculator
{
public class Calculator
public static float Add(float a, float b)
{
public static float Add(float a, float b)
{
return a + b;
}
return a + b;
}

public static float Subtract(float a, float b)
{
return a - b;
}
public static float Subtract(float a, float b)
{
return a - b;
}

public static float Multiply(float a, float b)
{
return a * b;
}
public static float Multiply(float a, float b)
{
return a * b;
}

public static float Divide(float a, float b)
{
return a / b;
}
public static float Divide(float a, float b)
{
return a / b;
}
}

16 changes: 16 additions & 0 deletions WebApiCICDWorkFlow/DiceRolling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Security.Cryptography;

namespace WebApiCICDWorkFlow;

public class DiceRolling
{
// This method will return a random number between 1 and 6
public static int RollDice()
{
var randomGenerator = RandomNumberGenerator.Create();
byte[] randomNumber = new byte[1];
randomGenerator.GetBytes(randomNumber);
return (randomNumber[0] % 6) + 1;

}
}
16 changes: 10 additions & 6 deletions WebApiCICDWorkFlow/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
using System.Text.Json.Serialization;
using WebApiCICDWorkFlow;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -29,14 +31,16 @@
{
float result = operation switch
{
Operation.Add => a + b,
Operation.Subtract => a - b,
Operation.Multiply => a * b,
Operation.Divide => a / b,
Operation.Add => Calculator.Add(a, b),
Operation.Subtract => Calculator.Subtract(a, b),
Operation.Multiply => Calculator.Multiply(a, b),
Operation.Divide => Calculator.Divide(a, b),
_ => 0
};
return result;
}).WithName("WebApiCICDWorkFlow").WithOpenApi();
return Results.Ok(result);
}).WithName("Calculator").WithOpenApi();

app.MapGet("/dice", () => Results.Ok(DiceRolling.RollDice())).WithName("DiceRolling").WithOpenApi();

await app.RunAsync();

Expand Down

0 comments on commit 61343f4

Please sign in to comment.