Skip to content

Commit

Permalink
Merge pull request #98 from yeekian/branch-AddComponentCommand
Browse files Browse the repository at this point in the history
Update test cases for AddComponentCommand
  • Loading branch information
yeekian authored Oct 29, 2024
2 parents f50f071 + c597581 commit 422ad0a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/tutorlink/commons/Commons.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Commons {
public static final String ERROR_DUPLICATE_COMPONENT = "Error! Component (Name %s) already exists in the list!";
public static final String ERROR_INVALID_WEIGHTAGE = "Error! Weightage must be double that is between 0 and 1!";
public static final String ERROR_INVALID_MAX_SCORE =
"Error! Max Score must be double that is more than or equal to 0";
"Error! Max Score must be double that is more than or equal to 0!";

//@@author RCPilot1604
//Invalid
Expand Down
90 changes: 86 additions & 4 deletions src/test/java/tutorlink/command/AddComponentCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import tutorlink.appstate.AppState;
import tutorlink.exceptions.IllegalValueException;
import tutorlink.result.CommandResult;


import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class AddComponentCommandTest {
private AppState appState;
Expand All @@ -25,14 +27,94 @@ void setup() {
}

@Test
void execute_addOne_expectOne() {
arguments.put("c/","Quiz 1");
arguments.put("w/","0.3");
arguments.put("m/","100");
void execute_validArguments_componentAddedSuccessfully() {
arguments.put("c/", "Quiz 1");
arguments.put("w/", "0.3");
arguments.put("m/", "100");
CommandResult result = command.execute(appState, arguments);
assertNotNull(result);
assertEquals("Component Quiz 1 of weight 0.3, with max score 100 added successfully!", result.toString());
assertEquals(1, appState.components.getComponentArrayList().size());
}

@Test
void execute_nullArguments_throwsIllegalValueException() {
IllegalValueException exception = assertThrows(IllegalValueException.class, () -> {
command.execute(appState, arguments);
});
assertEquals("Error! Null parameter passed!", exception.getMessage());
}

@Test
void execute_missingWeightageArgument_throwsIllegalValueException() {
arguments.put("c/", "Quiz 1");
arguments.put("m/", "100");

IllegalValueException exception = assertThrows(IllegalValueException.class, () -> {
command.execute(appState, arguments);
});
assertEquals("Error! Null parameter passed!", exception.getMessage());
}

@Test
void execute_extraArgument_componentAddedSuccessfully() {
arguments.put("c/", "Quiz 1");
arguments.put("w/", "0.5");
arguments.put("m/", "100");
arguments.put("extra/", "extra value");

CommandResult result = command.execute(appState, arguments);
assertNotNull(result);
assertEquals("Component Quiz 1 of weight 0.5, with max score 100 added successfully!", result.toString());
assertEquals(1, appState.components.getComponentArrayList().size());
}

@Test
void execute_weightageOutOfRange_throwsIllegalValueException() {
arguments.put("c/", "Quiz 1");
arguments.put("w/", "1.5");
arguments.put("m/", "100");

IllegalValueException exception = assertThrows(IllegalValueException.class, () -> {
command.execute(appState, arguments);
});
assertEquals("Error! Weightage must be double that is between 0 and 1!", exception.getMessage());
}

@Test
void execute_negativeMaxScore_throwsIllegalValueException() {
arguments.put("c/", "Quiz 1");
arguments.put("w/", "0.4");
arguments.put("m/", "-10");

IllegalValueException exception = assertThrows(IllegalValueException.class, () -> {
command.execute(appState, arguments);
});
assertEquals("Error! Max Score must be double that is more than or equal to 0!", exception.getMessage());
}

@Test
void execute_weightageNotDouble_throwsIllegalValueException() {
arguments.put("c/", "Quiz 1");
arguments.put("w/", "one");
arguments.put("m/", "100");

IllegalValueException exception = assertThrows(IllegalValueException.class, () -> {
command.execute(appState, arguments);
});
assertEquals("Error! Weightage must be double that is between 0 and 1!", exception.getMessage());
}

@Test
void execute_maxScoreNotDouble_throwsIllegalValueException() {
arguments.put("c/", "Quiz 1");
arguments.put("w/", "0.4");
arguments.put("m/", "hundred");

IllegalValueException exception = assertThrows(IllegalValueException.class, () -> {
command.execute(appState, arguments);
});
assertEquals("Error! Max Score must be double that is more than or equal to 0!", exception.getMessage());
}
}
//@@author

0 comments on commit 422ad0a

Please sign in to comment.