Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P1ch5 #9

Open
wants to merge 2 commits into
base: p1ch5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/openclassrooms/testing/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.openclassrooms.testing;

import java.util.HashSet;
import java.util.Set;

public class Calculator {

public int add(int a, int b) {
Expand All @@ -18,4 +21,16 @@ public void longCalculation() {
}
}

public Set<Integer> digitsSet(int number) {
Set<Integer> integers = new HashSet<Integer>();
String numberString = String.valueOf(number);

for (int i = 0; i < numberString.length(); i++){
integers.add(Integer.parseInt(numberString, i, i +1 ,10 ));
}


return integers;
}

}
32 changes: 27 additions & 5 deletions src/test/java/com/openclassrooms/testing/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import java.text.MessageFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -51,14 +55,14 @@ public static void showTestDuration() {
@Test
public void testAddTwoPositiveNumbers() {
// Arrange
int a = 2;
int a = 8;
int b = 3;

// Act
int somme = calculatorUnderTest.add(a, b);

// Assert
assertEquals(5, somme);
assertThat(somme).isEqualTo(5);
}

@Test
Expand All @@ -71,7 +75,8 @@ public void multiply_shouldReturnTheProduct_ofTwoIntegers() {
int produit = calculatorUnderTest.multiply(a, b);

// Assert
assertEquals(462, produit);

assertThat(produit).isEqualTo(462);
}

@ParameterizedTest(name = "{0} x 0 doit être égal à 0")
Expand All @@ -83,7 +88,8 @@ public void multiply_shouldReturnZero_ofZeroWithMultipleIntegers(int arg) {
int actualResult = calculatorUnderTest.multiply(arg, 0);

// Assert -- ça vaut toujours zéro !
assertEquals(0, actualResult);

assertThat(actualResult).isEqualTo(0);
}

@ParameterizedTest(name = "{0} + {1} doit être égal à {2}")
Expand All @@ -95,7 +101,8 @@ public void add_shouldReturnTheSum_ofMultipleIntegers(int arg1, int arg2, int ex
int actualResult = calculatorUnderTest.add(arg1, arg2);

// Assert
assertEquals(expectResult, actualResult);

assertThat(actualResult).isEqualTo(expectResult);
}

@Timeout(1)
Expand All @@ -110,4 +117,19 @@ public void longCalcul_shouldComputeInLessThan1Second() {
// ...
}


@Test
public void listDigits_shouldReturnsTheListOfDigits_ofPositiveInteger() {
// GIVEN
int number = 95897;

// WHEN
Set<Integer> actualDigits = calculatorUnderTest.digitsSet(number);

// THEN
Set<Integer> expectedDigits = Stream.of(5, 7, 8, 9).collect(Collectors.toSet());
assertEquals(expectedDigits, actualDigits);
}


}