Skip to content

Commit

Permalink
.NET (#32)
Browse files Browse the repository at this point in the history
* Add dotnet

* Add tests

* Rename project

* README

* Add CI
  • Loading branch information
rogervinas authored Nov 21, 2023
1 parent ecb4e04 commit f810270
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: .NET

on:
push:
branches: [ "main" ]
paths:
- .github/workflows/dotnet.yml
- dotnet/**
pull_request:
branches: [ "main" ]
paths:
- .github/workflows/dotnet.yml
- dotnet/**

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

defaults:
run:
working-directory: dotnet

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Configure .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0
cache: false

- name: Test
run: dotnet test -v quiet -l:"console;verbosity=normal"
44 changes: 44 additions & 0 deletions dotnet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc
nupkg/

# Visual Studio Code
.vscode/

# Rider
.idea/

# Visual Studio
.vs/

# Fleet
.fleet/

# Code Rush
.cr/

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
msbuild.log
msbuild.err
msbuild.wrn

10 changes: 10 additions & 0 deletions dotnet/Hello.Main/Hello.Main.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
16 changes: 16 additions & 0 deletions dotnet/Hello.Main/HelloApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Hello.Main;

public class HelloApp
{
private HelloMessage message;
private HelloConsole console;

public HelloApp(HelloMessage message, HelloConsole console) {
this.message = message;
this.console = console;
}

public void PrintHello() {
console.Print(message.Text);
}
}
12 changes: 12 additions & 0 deletions dotnet/Hello.Main/HelloConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Hello.Main;

public interface HelloConsole {
void Print(String text);
}

public class HelloSystemConsole : HelloConsole
{
public void Print(String text) {
Console.WriteLine(text);
}
}
17 changes: 17 additions & 0 deletions dotnet/Hello.Main/HelloMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Hello.Main;

public interface HelloMessage
{
public String Text {
get;
}
}

public class HelloWorldMessage : HelloMessage
{
public string Text {
get {
return "Hello World!";
}
}
}
6 changes: 6 additions & 0 deletions dotnet/Hello.Main/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Hello.Main;

var message = new HelloWorldMessage();
var console = new HelloSystemConsole();
var app = new HelloApp(message, console);
app.PrintHello();
1 change: 1 addition & 0 deletions dotnet/Hello.Test/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
25 changes: 25 additions & 0 deletions dotnet/Hello.Test/Hello.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Hello.Main\Hello.Main.csproj" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions dotnet/Hello.Test/HelloAppTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Hello.Main;
using Moq;

namespace Hello.Test;

public class HelloAppTest
{
[Test]
public void ShouldPrintHelloMessage()
{
var messageText = "Hello Test!";
var messageMock = new Mock<HelloMessage>();
messageMock.Setup(message => message.Text).Returns(messageText);
var message = messageMock.Object;

var consoleMock = new Mock<HelloConsole>();
var console = consoleMock.Object;

var app = new HelloApp(message, console);
app.PrintHello();

consoleMock.Verify(console => console.Print(messageText), Times.Once);
}
}
13 changes: 13 additions & 0 deletions dotnet/Hello.Test/HelloMessageTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Hello.Main;

namespace Hello.Test;

public class HelloMessageTest
{
[Test]
public void ShouldReturnHelloWorld()
{
var message = new HelloWorldMessage();
Assert.That(message.Text, Is.EqualTo("Hello World!"));
}
}
28 changes: 28 additions & 0 deletions dotnet/Hello.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hello.Main", "Hello.Main\Hello.Main.csproj", "{A0D91AB6-E8CD-4023-BDC8-A5A4D54DC79B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hello.Test", "Hello.Test\Hello.Test.csproj", "{92B5ED7F-F84D-4467-AB5B-1B9BFEEDF240}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A0D91AB6-E8CD-4023-BDC8-A5A4D54DC79B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0D91AB6-E8CD-4023-BDC8-A5A4D54DC79B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0D91AB6-E8CD-4023-BDC8-A5A4D54DC79B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0D91AB6-E8CD-4023-BDC8-A5A4D54DC79B}.Release|Any CPU.Build.0 = Release|Any CPU
{92B5ED7F-F84D-4467-AB5B-1B9BFEEDF240}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92B5ED7F-F84D-4467-AB5B-1B9BFEEDF240}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92B5ED7F-F84D-4467-AB5B-1B9BFEEDF240}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92B5ED7F-F84D-4467-AB5B-1B9BFEEDF240}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
38 changes: 38 additions & 0 deletions dotnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[![CI](https://github.com/rogervinas/tests-everywhere/actions/workflows/dotnet.yml/badge.svg)](https://github.com/rogervinas/tests-everywhere/actions/workflows/dotnet.yml)
![.NET](https://img.shields.io/badge/.NET-8.0-blue?labelColor=black)

# .NET

[.NET C#](https://learn.microsoft.com/dotnet/csharp) testing with [NUnit](https://nunit.org/) and [Moq](https://www.devlooped.com/moq/)

## Run this project using 🐳 [docker](https://www.docker.com/)
* Execute `./docker-run.sh`
* Once inside the container:
* Test with `dotnet test -v quiet -l:"console;verbosity=normal"`
* Run with `dotnet run --project Hello.Main`
* Build with `dotnet publish -c Release`

## Run this project locally

### Pre-requisites
* Install [.NET](https://dotnet.microsoft.com/download)
* Check [.NET CLI](https://learn.microsoft.com/dotnet/core/tools/) executing `dotnet --version`

### Run locally
* Test with `dotnet test -v quiet -l:"console;verbosity=normal"`
* Run with `dotnet run --project Hello.Main`
* Build with `dotnet publish -c Release`

### Create project from scratch
* Execute these commands:
```
dotnet new sln --name Hello
dotnet new console --language "C#" --framework net8.0 --name Hello.Main
dotnet new nunit --language "C#" --framework net8.0 --name Hello.Test
dotnet add ./Hello.Test reference ./Hello.Main
dotnet add ./Hello.Test package Moq --version 4.20.69
dotnet sln Hello.sln add ./Hello.Main ./Hello.Test
```
6 changes: 6 additions & 0 deletions dotnet/docker-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
docker run -it --rm \
--name dotnet \
--volume $PWD:/project \
--workdir /project \
--entrypoint "/usr/bin/bash" \
mcr.microsoft.com/dotnet/sdk:8.0

0 comments on commit f810270

Please sign in to comment.