Skip to content

Commit

Permalink
Sarthak | Refactors Arguments, it does not inherit from ArrayList<Str…
Browse files Browse the repository at this point in the history
…ing>
  • Loading branch information
SarthakMakhija committed Jan 10, 2025
1 parent eb31ffe commit cf62897
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
28 changes: 10 additions & 18 deletions src/main/java/com/codurance/training/commands/Arguments.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package com.codurance.training.commands;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* The class `Arguments` is showing signs of both mutability and immutability.
* Signs of mutability:
* - Arguments extends from ArrayList<String> which is mutable.
* - The method `skipOne` creates a new instance of Arguments, which means the author does not want to change the existing collection.
* An instance of Arguments is passed to all the Commands. Given, Arguments extends from ArrayList<String>, it gives all the commands
* the authority to mutate arguments.
* Technically, Arguments should never be mutated once created.
*/
public class Arguments extends ArrayList<String> {
public class Arguments {

private final List<String> arguments;

static Arguments skipOneAndCreate(String[] parts) {
if (parts == null) {
Expand All @@ -24,26 +16,26 @@ static Arguments skipOneAndCreate(String[] parts) {
}

Arguments(List<String> arguments) {
this.addAll(arguments);
this.arguments = arguments;
}

Arguments skipOne() {
return new Arguments(this.subList(1, this.size()));
return new Arguments(this.arguments.subList(1, this.count()));
}

int argumentAtIndexAsInt(int index) {
return Integer.parseInt(this.get(index));
return Integer.parseInt(this.arguments.get(index));
}

String argumentAtIndexAsString(int index) {
return get(index);
return this.arguments.get(index);
}

int count() {
return size();
return this.arguments.size();
}

boolean hasAny() {
return size() == 0;
boolean isEmpty() {
return this.arguments.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ShowCommand implements Command {

@Override
public void execute(Arguments arguments) throws Exception {
assert (arguments.hasAny());
assert (arguments.isEmpty());
this.writer.write(this.projects.format());
}
}
49 changes: 44 additions & 5 deletions src/test/java/com/codurance/training/commands/ArgumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;

public class ArgumentsTest {

@Test
public void skipOneAndCreateArguments() {
Arguments arguments = Arguments.skipOneAndCreate(new String[]{"an", "argument", "1"});
assertEquals(List.of("argument", "1"), arguments);
assertEquals("argument", arguments.argumentAtIndexAsString(0));
assertEquals("1", arguments.argumentAtIndexAsString(1));
}

@Test
public void skipOneAndCreateEmptyArguments() {
Arguments arguments = Arguments.skipOneAndCreate(new String[]{});
assertEquals(List.of(), arguments);
assertTrue(arguments.isEmpty());
}

@Test
public void skipOneAndCreateEmptyArgumentsGivenNull() {
Arguments arguments = Arguments.skipOneAndCreate(null);
assertEquals(List.of(), arguments);
assertTrue(arguments.isEmpty());
}

@Test
public void skipOneFromArguments() {
Arguments arguments = new Arguments(List.of("an", "argument", "1"));
assertEquals(List.of("argument", "1"), arguments.skipOne());
Arguments skipped = arguments.skipOne();
assertEquals("argument", skipped.argumentAtIndexAsString(0));
assertEquals("1", skipped.argumentAtIndexAsString(1));
}

@Test
Expand All @@ -43,4 +46,40 @@ public void argumentAtIndex1AsInt() {
Arguments arguments = new Arguments(List.of("0", "200", "1"));
assertEquals(200, arguments.argumentAtIndexAsInt(1));
}

@Test
public void argumentAtIndex0AsString() {
Arguments arguments = new Arguments(List.of("1", "argument", "1"));
assertEquals("1", arguments.argumentAtIndexAsString(0));
}

@Test
public void argumentAtIndex1AsString() {
Arguments arguments = new Arguments(List.of("0", "200", "1"));
assertEquals("200", arguments.argumentAtIndexAsString(1));
}

@Test
public void zeroArguments() {
Arguments arguments = new Arguments(List.of());
assertEquals(0, arguments.count());
}

@Test
public void threeArguments() {
Arguments arguments = new Arguments(List.of("0", "200", "1"));
assertEquals(3, arguments.count());
}

@Test
public void argumentsIsEmpty() {
Arguments arguments = Arguments.skipOneAndCreate(null);
assertTrue(arguments.isEmpty());
}

@Test
public void argumentsIsNotEmpty() {
Arguments arguments = new Arguments(List.of("argument"));
assertFalse(arguments.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void parseCheckCommand() {
CommandDescription commandDescription = commandLine.parse();

assertEquals(CommandType.CHECK, commandDescription.commandType());
assertEquals(List.of("100"), commandDescription.arguments());
assertEquals("100", commandDescription.arguments().argumentAtIndexAsString(0));
}

@Test
Expand All @@ -31,7 +31,7 @@ public void parseUncheckCommand() {
CommandDescription commandDescription = commandLine.parse();

assertEquals(CommandType.UNCHECK, commandDescription.commandType());
assertEquals(List.of("100"), commandDescription.arguments());
assertEquals("100", commandDescription.arguments().argumentAtIndexAsString(0));
}

@Test
Expand All @@ -40,7 +40,8 @@ public void parseAddProjectCommand() {
CommandDescription commandDescription = commandLine.parse();

assertEquals(CommandType.ADD, commandDescription.commandType());
assertEquals(List.of("project", "caizin"), commandDescription.arguments());
assertEquals("project", commandDescription.arguments().argumentAtIndexAsString(0));
assertEquals("caizin", commandDescription.arguments().argumentAtIndexAsString(1));
}

@Test
Expand All @@ -49,6 +50,8 @@ public void parseAddTaskCommand() {
CommandDescription commandDescription = commandLine.parse();

assertEquals(CommandType.ADD, commandDescription.commandType());
assertEquals(List.of("task", "caizin", "200"), commandDescription.arguments());
assertEquals("task", commandDescription.arguments().argumentAtIndexAsString(0));
assertEquals("caizin", commandDescription.arguments().argumentAtIndexAsString(1));
assertEquals("200", commandDescription.arguments().argumentAtIndexAsString(2));
}
}

0 comments on commit cf62897

Please sign in to comment.