Skip to content

Commit

Permalink
More docs 2 (#41)
Browse files Browse the repository at this point in the history
* More documentation

* Javascript doc

* More doc

* More

* More

* More

* More

* More

* More

* More
  • Loading branch information
rogervinas authored Nov 22, 2023
1 parent 5e1717b commit 4f12586
Show file tree
Hide file tree
Showing 35 changed files with 1,271 additions and 370 deletions.
18 changes: 13 additions & 5 deletions bash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ git submodule update

### Implementation

1. Create `helloMessage` function in [src/hello-message.bash](src/hello-message.bash):
1. Create `helloMessage` function in [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 [hello-console.bash](src/hello-console.bash):

```shell
function helloConsole() {
Expand All @@ -39,7 +39,7 @@ function helloConsole() {
}
```

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

```shell
function helloApp() {
Expand All @@ -65,7 +65,7 @@ helloApp helloMessage helloConsole

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 create all tests in [hello.bats](test/hello.bats) file

2. Configure current directory and load some helper modules in `setup` function:

Expand All @@ -90,22 +90,30 @@ setup() {
}
```

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

```shell
@test "helloApp should print hello message" {
# 4.1 Create a helloMessage function mock
# - Will return "Hello Test!"
function helloMessageMock() {
echo "Hello Test!"
}

# 4.2 Create a helloConsole function mock
# - Will print parameter passed within "helloConsoleMock[...]"
function helloConsoleMock() {
local text=$1
echo "helloConsoleMock[$text]"
}

# 4.3 Load helloApp function, the one we want to test
source hello-app.bash
# 4.4 Execute helloApp passing mock functions
run helloApp helloMessageMock helloConsoleMock

# Assert helloConsoleMock has been called once
# with the returned message by helloMessageMock
assert_output "helloConsoleMock[Hello Test!]"
}
```
Expand Down
18 changes: 9 additions & 9 deletions dotnet/Hello.Main/HelloApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

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

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

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

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

public class HelloSystemConsole : HelloConsole
{
public void Print(String text) {
Console.WriteLine(text);
}
public void Print(String text) {
Console.WriteLine(text);
}
}
14 changes: 7 additions & 7 deletions dotnet/Hello.Main/HelloMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

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

public class HelloWorldMessage : HelloMessage
{
public string Text {
get {
return "Hello World!";
}
public string Text {
get {
return "Hello World!";
}
}
}
26 changes: 13 additions & 13 deletions dotnet/Hello.Test/HelloAppTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ 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;
[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 consoleMock = new Mock<HelloConsole>();
var console = consoleMock.Object;

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

consoleMock.Verify(console => console.Print(messageText), Times.Once);
}
consoleMock.Verify(console => console.Print(messageText), Times.Once);
}
}
12 changes: 6 additions & 6 deletions dotnet/Hello.Test/HelloMessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace Hello.Test;

public class HelloMessageTest
{
[Test]
public void ShouldReturnHelloWorld()
{
var message = new HelloWorldMessage();
Assert.That(message.Text, Is.EqualTo("Hello World!"));
}
[Test]
public void ShouldReturnHelloWorld()
{
var message = new HelloWorldMessage();
Assert.That(message.Text, Is.EqualTo("Hello World!"));
}
}
104 changes: 52 additions & 52 deletions dotnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,59 @@

### Implementation

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

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

public class HelloWorldMessage : HelloMessage
{
public string Text {
get {
return "Hello World!";
}
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):
2. Same way create `HelloConsole` interface and `HelloSystemConsole` class implementing it in [HelloConsole.cs](Hello.Main/HelloConsole.cs):

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

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

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

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

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

public void PrintHello() {
console.Print(message.Text);
}
public void PrintHello() {
console.Print(message.Text);
}
}
```

Expand All @@ -82,49 +82,49 @@ app.PrintHello();

### Test

Following [NUnit > Writing Tests](https://docs.nunit.org/articles/nunit/writing-tests/attributes.html) guide ...
Following [NUnit > Writing Tests](https://docs.nunit.org/articles/nunit/writing-tests/attributes.html) and [Moq](https://github.com/devlooped/moq#moq) guides ...

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

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

2. Test `HelloApp` in [Hello.Test/HelloAppTest.cs](Hello.Test/HelloAppTest.cs):
2. Test `HelloApp` in [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);
var messageText = "Hello Test!";

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

// 2.4 Create a mock of HelloConsole
var consoleMock = new Mock<HelloConsole>();
// 2.5 No need to set expectations for this one
// 2.6 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);
// 2.4 Execute the method we want to test
app.PrintHello();

// 2.5 Verify HelloConsole mock Print() method
// has been called once with "Hello Test!"
consoleMock.Verify(console => console.Print(messageText), Times.Once);
}
```

Expand Down
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 4f12586

Please sign in to comment.