Skip to content

Implemented calculator#284

Open
Radikhovskiy wants to merge 3 commits into
mate-academy:masterfrom
Radikhovskiy:develop
Open

Implemented calculator#284
Radikhovskiy wants to merge 3 commits into
mate-academy:masterfrom
Radikhovskiy:develop

Conversation

@Radikhovskiy
Copy link
Copy Markdown

No description provided.


@Test
void addictionTwoNegative() {
number1 = -7;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you use the same numbers everywhere, so you can simplify the code, make them constants

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Часто бачив, як в наших тестах до завдань константи до чисел не записувались, тому вирішив прописати всі числа напряму
Впринципі, якщо ментор зможе мене переконати, що це так важливо, то можна змінити

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не обязательно выносить это в константы, но в тестах лучше использовать разные данные, это повышает вероятность найти ошибку

Comment thread src/test/java/core/basesyntax/CalculatorTest.java
package core.basesyntax;

public class Calculator {
public double calculate(double number1, double number2, char operation) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's bad practice to use numbers in variable names

return Math.pow(number1, number2);
case '/':
if (number2 == 0) {
throw new IllegalArgumentException("Can't divide by zero");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe ArithmeticException would be better here?

}
return number1 / number2;
default:
throw new IllegalStateException("Invalid operation");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe IllegalArgumentException would be better here?
image

import org.junit.jupiter.api.Test;

class CalculatorTest {
private static final Calculator calculator = new Calculator();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets initialize this variable in @BeforeAll method

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need it?
We can just initialize the variables at the beginning
There will be fewer lines of code

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Обычно в тестах объекты сложные и требуют определенной доинициализации (что делается в методе @BeforeAll). В данном случае объект простой, и в принципе можно было оставить инициализацию в момент объявления, но мы хотим что б вы знали про метод @BeforeAll и умели им пользоваться.

Comment on lines +12 to +13
private double number1;
private double number2;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numbers in variables naming


@Test
void addictionTwoNegative() {
number1 = -7;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не обязательно выносить это в константы, но в тестах лучше использовать разные данные, это повышает вероятность найти ошибку

}

@Test
void addictionZero_1() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is also bad practice to use numbers in method names.

Comment thread src/test/java/core/basesyntax/CalculatorTest.java
Comment on lines +42 to +58
@Test
void addictionZero_1() {
number1 = 0;
number2 = -14;
expected = -14;
actual = calculator.calculate(number1, number2, '+');
assertEquals(expected, actual);
}

@Test
void addictionZero_2() {
number1 = -14;
number2 = 0;
expected = -14;
actual = calculator.calculate(number1, number2, '+');
assertEquals(expected, actual);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can combine this tests, they test the same logic

Comment on lines +35 to +39
number1 = 7;
number2 = -14;
expected = -7;
actual = calculator.calculate(number1, number2, '+');
assertEquals(expected, actual);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it worth to add here the check with first negative operant and second positive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants