Skip to content

Commit

Permalink
More
Browse files Browse the repository at this point in the history
  • Loading branch information
rogervinas committed Nov 22, 2023
1 parent 17bd943 commit 885e149
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 74 deletions.
2 changes: 1 addition & 1 deletion java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class Main {

### Test

Following [JUnit5 > Writing Tests](https://junit.org/junit5/docs/current/user-guide/#writing-tests), [Mockito](https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#stubbing) and [AssertJ](https://assertj.github.io/doc/#assertj-core-assertions-guide) guides ...
Following [JUnit5 > Writing Tests](https://junit.org/junit5/docs/current/user-guide/#writing-tests), [Mockito > Stubbing](https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#stubbing) and [AssertJ > Core assertions guide](https://assertj.github.io/doc/#assertj-core-assertions-guide) guides ...

1. Test `HelloMessage` in [HelloMessageTest.java](src/test/java/org/hello/HelloMessageTest.java):

Expand Down
2 changes: 1 addition & 1 deletion kotlin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fun main() {

### Test

Following [Kotest > Writing Tests](https://kotest.io/docs/framework/writing-tests.html) and [MockK](https://mockk.io/#class-mock) guides ...
Following [Kotest > Writing Tests](https://kotest.io/docs/framework/writing-tests.html) and [MockK > Class mock](https://mockk.io/#class-mock) guides ...

1. Test `HelloMessage` in [HelloMessageTest.kt](src/test/kotlin/org/hello/HelloMessageTest.kt):

Expand Down
10 changes: 5 additions & 5 deletions python/main/hello_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class HelloApp:
def __init__(self, message, console):
self.message = message
self.console = console
def __init__(self, message, console):
self.message = message
self.console = console

def print_hello(self):
self.console.print(self.message.text)
def print_hello(self):
self.console.print(self.message.text)
4 changes: 2 additions & 2 deletions python/main/hello_console.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from builtins import print as __print__

class HelloConsole:
def print(self, text):
__print__(text)
def print(self, text):
__print__(text)
4 changes: 2 additions & 2 deletions python/main/hello_message.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class HelloMessage:
def __init__(self):
self.text = "Hello World!"
def __init__(self):
self.text = "Hello World!"
20 changes: 10 additions & 10 deletions python/test/test_hello_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@


class HelloAppTest(unittest.TestCase):
def test_should_print_hello_message(self):
message_text = "Hello Test!"
message = MagicMock()
message.text = message_text
def test_should_print_hello_message(self):
message_text = "Hello Test!"
message = MagicMock()
message.text = message_text

console = MagicMock()
console.print = MagicMock()
console = MagicMock()
console.print = MagicMock()

app = HelloApp(message, console)
app.print_hello()
app = HelloApp(message, console)
app.print_hello()

console.print.assert_called_once_with(message_text)
console.print.assert_called_once_with(message_text)


if __name__ == '__main__':
unittest.main()
unittest.main()
8 changes: 4 additions & 4 deletions python/test/test_hello_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@


class HelloMessageTest(unittest.TestCase):
def test_should_return_hello_world(self):
message = HelloMessage()
self.assertEqual(message.text, "Hello World!")
def test_should_return_hello_world(self):
message = HelloMessage()
self.assertEqual(message.text, "Hello World!")


if __name__ == '__main__':
unittest.main()
unittest.main()
50 changes: 25 additions & 25 deletions rust/src/hello_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@ use crate::hello_console::HelloConsole;
use crate::hello_message::HelloMessage;

pub struct HelloApp {
message: Box<dyn HelloMessage>,
console: Box<dyn HelloConsole>
message: Box<dyn HelloMessage>,
console: Box<dyn HelloConsole>
}

impl HelloApp {
pub fn new(message: Box<dyn HelloMessage>, console: Box<dyn HelloConsole>) -> Self {
Self { message, console }
}
pub fn new(message: Box<dyn HelloMessage>, console: Box<dyn HelloConsole>) -> Self {
Self { message, console }
}

pub fn print_hello(&self) {
self.console.print(self.message.text());
}
pub fn print_hello(&self) {
self.console.print(self.message.text());
}
}

#[cfg(test)]
mod tests {
use super::*;
use mockall::predicate::*;
use crate::hello_console::MockHelloConsole;
use crate::hello_message::MockHelloMessage;
use super::*;
use mockall::predicate::*;
use crate::hello_console::MockHelloConsole;
use crate::hello_message::MockHelloMessage;

#[test]
fn should_print_hello_message() {
let message_text = "Hello Test!";
let mut message = MockHelloMessage::new();
message.expect_text().return_const(String::from(message_text));
#[test]
fn should_print_hello_message() {
let message_text = "Hello Test!";
let mut message = MockHelloMessage::new();
message.expect_text().return_const(String::from(message_text));

let mut console = MockHelloConsole::new();
console.expect_print()
.with(eq(String::from(message_text)))
.times(1)
.return_const(());
let mut console = MockHelloConsole::new();
console.expect_print()
.with(eq(String::from(message_text)))
.times(1)
.return_const(());

let app = HelloApp::new(Box::new(message), Box::new(console));
app.print_hello();
}
let app = HelloApp::new(Box::new(message), Box::new(console));
app.print_hello();
}
}
14 changes: 7 additions & 7 deletions rust/src/hello_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ use mockall::automock;

#[cfg_attr(test, automock)]
pub trait HelloConsole {
fn print(&self, text: String);
fn print(&self, text: String);
}

pub struct HelloSystemConsole {}

impl HelloConsole for HelloSystemConsole {
fn print(&self, text: String) {
println!("{}", text);
}
fn print(&self, text: String) {
println!("{}", text);
}
}

impl HelloSystemConsole {
pub fn new() -> Self {
Self {}
}
pub fn new() -> Self {
Self {}
}
}
26 changes: 13 additions & 13 deletions rust/src/hello_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ use mockall::automock;

#[cfg_attr(test, automock)]
pub trait HelloMessage {
fn text(&self) -> String;
fn text(&self) -> String;
}

pub struct HelloWorldMessage {}

impl HelloMessage for HelloWorldMessage {
fn text(&self) -> String {
return String::from("Hello World!");
}
fn text(&self) -> String {
return String::from("Hello World!");
}
}

impl HelloWorldMessage {
pub fn new() -> Self {
Self {}
}
pub fn new() -> Self {
Self {}
}
}

#[cfg(test)]
mod tests {
use super::*;
use super::*;

#[test]
fn should_return_hello_world() {
let message = HelloWorldMessage::new();
assert_eq!(message.text(), String::from("Hello World!"))
}
#[test]
fn should_return_hello_world() {
let message = HelloWorldMessage::new();
assert_eq!(message.text(), String::from("Hello World!"))
}
}
8 changes: 4 additions & 4 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ mod hello_app;
use crate::hello_app::HelloApp;

fn main() {
let message = HelloWorldMessage::new();
let console = HelloSystemConsole::new();
let app = HelloApp::new(Box::new(message), Box::new(console));
app.print_hello();
let message = HelloWorldMessage::new();
let console = HelloSystemConsole::new();
let app = HelloApp::new(Box::new(message), Box::new(console));
app.print_hello();
}

0 comments on commit 885e149

Please sign in to comment.