Skip to content

Commit

Permalink
More docs (#40)
Browse files Browse the repository at this point in the history
* Format

* More doc

* Improve

* More

* More

* More

* More
  • Loading branch information
rogervinas committed Nov 22, 2023
1 parent 1d0e900 commit 12430c3
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 97 deletions.
74 changes: 44 additions & 30 deletions bash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,41 @@
[Bash](https://www.gnu.org/software/bash/) testing with [BATS](https://bats-core.readthedocs.io/en/stable/#)

⚠️ This project includes **BATS** git submodules so once you clone the repo you will need to init and update them:

```
git submodule init
git submodule update
```

* [Show me the code](#show-me-the-code)
* [Implementation](#implementation)
* [Test](#test)
* [Run this project using 🐳 docker](#run-this-project-using--docker)
* [Run this project locally](#run-this-project-locally)
- [Show me the code](#show-me-the-code)
- [Implementation](#implementation)
- [Test](#test)
- [Run this project using 🐳 docker](#run-this-project-using--docker)
- [Run this project locally](#run-this-project-locally)

## Show me the code

### Implementation

1) Create `helloMessage` function in [src/hello-message.bash](src/hello-message.bash):
1. Create `helloMessage` function in [src/hello-message.bash](src/hello-message.bash):

```shell
function helloMessage() {
echo "Hello World!"
}
```

2) Create `helloConsole` function in [src/hello-console.bash](src/hello-console.bash):
2. Create `helloConsole` function in [src/hello-console.bash](src/hello-console.bash):

```shell
function helloConsole() {
local text=$1
echo "$text"
}
```

3) Create `helloApp` function in [src/hello-app.bash](src/hello-app.bash):
3. Create `helloApp` function in [src/hello-app.bash](src/hello-app.bash):

```shell
function helloApp() {
local messageFn=$1
Expand All @@ -47,7 +51,8 @@ function helloApp() {

Note that `helloApp` function receives the two other functions as parameters and just executes them.

4) Create a main script [src/hello-bash](src/hello.bash) that just loads the 3 required scripts and executes `helloApp` passing `helloMessage` and `helloConsole` functions as parameters:
4. Create a main script [src/hello-bash](src/hello.bash) that just loads the 3 required scripts and executes `helloApp` passing `helloMessage` and `helloConsole` functions as parameters:

```shell
source "$(dirname "${BASH_SOURCE[0]}")/hello-message.bash"
source "$(dirname "${BASH_SOURCE[0]}")/hello-console.bash"
Expand All @@ -58,11 +63,12 @@ helloApp helloMessage helloConsole

### Test

Based on [BATS Tutorial > Your first test](https://bats-core.readthedocs.io/en/stable/tutorial.html#your-first-test) ...
Following [BATS Tutorial > Your first test](https://bats-core.readthedocs.io/en/stable/tutorial.html#your-first-test) ...

1. For simplicity create all tests in [test/hello.bats](test/hello.bats) file

1) For simplicity we create all tests in [test/hello.bats](test/hello.bats) file
2. Configure current directory and load some helper modules in `setup` function:

2) We configure current directory and load some helper modules in `setup` function:
```shell
setup() {
load 'test_helper/bats-support/load'
Expand All @@ -73,7 +79,8 @@ setup() {
}
```

3) We test `helloMessage` function using [assert_output](https://github.com/bats-core/bats-assert#assert_output) helper:
3. Test `helloMessage` function using [assert_output](https://github.com/bats-core/bats-assert#assert_output) helper:

```shell
@test "helloMessage should return hello world" {
source hello-message.bash
Expand All @@ -83,7 +90,8 @@ setup() {
}
```

4) We test `helloApp` function creating mock functions for `helloMessage` and `helloConsole`:
4. Test `helloApp` function creating mock functions for `helloMessage` and `helloConsole`:

```shell
@test "helloApp should print hello message" {
function helloMessageMock() {
Expand All @@ -102,7 +110,8 @@ setup() {
}
```

5) We test the whole `hello.bash` script too:
5. Test the whole `hello.bash` script too:

```shell
@test "hello.bash should print hello world" {
run hello.bash
Expand All @@ -111,7 +120,8 @@ setup() {
}
```

6) Test output should look like:
6. Test output should look like:

```
hello.bats
✓ helloMessage should return hello world
Expand All @@ -124,26 +134,30 @@ hello.bats
Take a look at the other [Libraries and Add-ons](https://bats-core.readthedocs.io/en/stable/writing-tests.html#libraries-and-add-ons) that may be useful in the future. For example, there is a couple of **bats-mock** libraries that can be used to mock programs (but unfortunately not able to mock functions).

## Run this project using 🐳 [docker](https://www.docker.com/)
* Execute `./docker-run.sh`
* Once inside the container:
* Test with `./test/bats/bin/bats test/hello.bats`
* Run with `./src/hello.bash`

- Execute `./docker-run.sh`
- Once inside the container:
- Test with `./test/bats/bin/bats test/hello.bats`
- Run with `./src/hello.bash`

## Run this project locally

### Pre-requisites
* Ensure you have **bash** installed `bash --version` (this project uses version 5.2)

- Ensure you have **bash** installed `bash --version` (this project uses version 5.2)

### Create project from scratch
* Create an empty project with `src` and `test` folders
* Follow [Quick installation](https://bats-core.readthedocs.io/en/stable/tutorial.html#quick-installation) guide to install **BATS** submodules:
```
git submodule add https://github.com/bats-core/bats-core.git test/bats
git submodule add https://github.com/bats-core/bats-support.git test/test_helper/bats-support
git submodule add https://github.com/bats-core/bats-assert.git test/test_helper/bats-assert
```

- Create an empty project with `src` and `test` folders
- Follow [Quick installation](https://bats-core.readthedocs.io/en/stable/tutorial.html#quick-installation) guide to install **BATS** submodules:

```
git submodule add https://github.com/bats-core/bats-core.git test/bats
git submodule add https://github.com/bats-core/bats-support.git test/test_helper/bats-support
git submodule add https://github.com/bats-core/bats-assert.git test/test_helper/bats-assert
```

### Run locally
* Test with `./test/bats/bin/bats test/hello.bats`
* Run with `./src/hello.bash`

- Test with `./test/bats/bin/bats test/hello.bats`
- Run with `./src/hello.bash`
187 changes: 164 additions & 23 deletions dotnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,175 @@

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

- [Show me the code](#show-me-the-code)
- [Implementation](#implementation)
- [Test](#test)
- [Run this project using 🐳 docker](#run-this-project-using--docker)
- [Run this project locally](#run-this-project-locally)

## Show me the code

### Implementation

1. Create `HelloMessage` interface and `HelloWorldMessage` implementing it in [Hello.Main/HelloMessage.cs](Hello.Main/HelloMessage.cs):

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

public class HelloWorldMessage : HelloMessage
{
public string Text {
get {
return "Hello World!";
}
}
}
```

Creating it as an interface will allow us to mock it for testing using [Moq](https://www.devlooped.com/moq/) which does not support mocking final classes. Maybe other libraries support that but using an interface is simpler.

2. Same way create `HelloConsole` interface and `HelloSystemConsole` class implementing it in [Hello.Main/HelloConsole.cs](Hello.Main/HelloConsole.cs):

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

public class HelloSystemConsole : HelloConsole
{
public void Print(String text) {
Console.WriteLine(text);
}
}
```

4. Create `HelloApp` in [Hello.Main/HelloApp.cs](Hello.Main/HelloApp.cs):

```csharp
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);
}
}
```

5. Create main [Hello.Main/Program.cs](Hello.Main/Program.cs) that wraps it all together:

```csharp
var message = new HelloWorldMessage();
var console = new HelloSystemConsole();
var app = new HelloApp(message, console);
app.PrintHello();
```

### Test

Following [NUnit > Writing Tests](https://docs.nunit.org/articles/nunit/writing-tests/attributes.html) guide ...

1. Test `HelloMessage` in [Hello.Test/HelloMessageTest.cs](Hello.Test/HelloMessageTest.cs):

```csharp
[Test]
public void ShouldReturnHelloWorld()
{
var message = new HelloWorldMessage();
Assert.That(message.Text, Is.EqualTo("Hello World!"));
}
```

2. Test `HelloApp` in [Hello.Test/HelloAppTest.cs](Hello.Test/HelloAppTest.cs):

```csharp
[Test]
public void ShouldPrintHelloMessage()
{
var messageText = "Hello Test!";

// 2.1 Create a mock of HelloMessage
var messageMock = new Mock<HelloMessage>();
// - Expect HelloMessage mock to receive a call to .Text
// and return "Hello Test!"
messageMock.Setup(message => message.Text).Returns(messageText);
// Get the mock object to pass it to HelloApp
var message = messageMock.Object;

// 2.2 Create a mock of HelloConsol
var consoleMock = new Mock<HelloConsole>();
// - No need to set expectations for this one
// - Get the mock object to pass it to HelloApp
var console = consoleMock.Object;

// 2.3 Create a HelloApp, the one we want to test, passing the mocks
var app = new HelloApp(message, console);
// - Execute the method we want to test
app.PrintHello();

// 2.4 Verify HelloConsole mock has received one time
// a call to .Print with "Hello Test!"
consoleMock.Verify(console => console.Print(messageText), Times.Once);
}
```

3. Test output should look like:

```
NUnit Adapter 4.2.0.0: Test execution complete
Passed ShouldPrintHelloMessage [180 ms]
Passed ShouldReturnHelloWorld [7 ms]
Test Run Successful.
Total tests: 2
Passed: 2
Total time: 2.7702 Seconds
```

## 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`

- 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`
- Install [.NET](https://dotnet.microsoft.com/download)
- Check [.NET CLI](https://learn.microsoft.com/dotnet/core/tools/) executing `dotnet --version`

### 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
```

- 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
```

### 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`
6 changes: 3 additions & 3 deletions go/HelloApp.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

type HelloApp struct {
message HelloMessage
console HelloConsole
message HelloMessage
console HelloConsole
}

func (app *HelloApp) PrintHello() {
app.console.Print(app.message.Text())
app.console.Print(app.message.Text())
}
Loading

0 comments on commit 12430c3

Please sign in to comment.