commit bb1cf0bc87ec90c8a9f848195a69e636d51403ce Author: Junv Zhao Date: Fri Apr 14 17:39:38 2017 +0800 init diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..851db16 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build diff --git a/.gradle/2.14.1/taskArtifacts/cache.properties b/.gradle/2.14.1/taskArtifacts/cache.properties new file mode 100644 index 0000000..3f02190 --- /dev/null +++ b/.gradle/2.14.1/taskArtifacts/cache.properties @@ -0,0 +1 @@ +#Wed Apr 05 09:15:28 CST 2017 diff --git a/.gradle/2.14.1/taskArtifacts/cache.properties.lock b/.gradle/2.14.1/taskArtifacts/cache.properties.lock new file mode 100644 index 0000000..673f79e Binary files /dev/null and b/.gradle/2.14.1/taskArtifacts/cache.properties.lock differ diff --git a/.gradle/2.14.1/taskArtifacts/fileHashes.bin b/.gradle/2.14.1/taskArtifacts/fileHashes.bin new file mode 100644 index 0000000..dca3549 Binary files /dev/null and b/.gradle/2.14.1/taskArtifacts/fileHashes.bin differ diff --git a/.gradle/2.14.1/taskArtifacts/fileSnapshots.bin b/.gradle/2.14.1/taskArtifacts/fileSnapshots.bin new file mode 100644 index 0000000..952011c Binary files /dev/null and b/.gradle/2.14.1/taskArtifacts/fileSnapshots.bin differ diff --git a/.gradle/2.14.1/taskArtifacts/fileSnapshotsToTreeSnapshotsIndex.bin b/.gradle/2.14.1/taskArtifacts/fileSnapshotsToTreeSnapshotsIndex.bin new file mode 100644 index 0000000..c6e9ada Binary files /dev/null and b/.gradle/2.14.1/taskArtifacts/fileSnapshotsToTreeSnapshotsIndex.bin differ diff --git a/.gradle/2.14.1/taskArtifacts/taskArtifacts.bin b/.gradle/2.14.1/taskArtifacts/taskArtifacts.bin new file mode 100644 index 0000000..5b7c6a2 Binary files /dev/null and b/.gradle/2.14.1/taskArtifacts/taskArtifacts.bin differ diff --git a/.gradletasknamecache b/.gradletasknamecache new file mode 100644 index 0000000..6e1c162 --- /dev/null +++ b/.gradletasknamecache @@ -0,0 +1,97 @@ +installApp +startScripts +run +assemble +build +jacocoTestReport +buildDependents +buildNeeded +classes +compileJava +processResources +clean +jar +testClasses +compileTestJava +processTestResources +init +wrapper +assembleDist +distTar +startScripts +distZip +startScripts +installDist +startScripts +javadoc +buildEnvironment +components +dependencies +dependencyInsight +help +model +projects +properties +tasks +cleanIdea +cleanIdeaModule +cleanIdeaProject +idea +ideaModule +ideaProject +ideaWorkspace +check +checkstyleMain +checkstyleTest +test +cleanIdeaWorkspace +Pattern: +Pattern: +Pattern: +installApp +startScripts +run +assemble +build +jacocoTestReport +buildDependents +buildNeeded +classes +compileJava +processResources +clean +jar +testClasses +compileTestJava +processTestResources +init +wrapper +assembleDist +distTar +startScripts +distZip +startScripts +installDist +startScripts +javadoc +buildEnvironment +components +dependencies +dependencyInsight +help +model +projects +properties +tasks +cleanIdea +cleanIdeaModule +cleanIdeaProject +idea +ideaModule +ideaProject +ideaWorkspace +check +checkstyleMain +checkstyleTest +test +cleanIdeaWorkspace diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..0afec5d --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..dc8dcb6 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_google_guava_guava_21_0.xml b/.idea/libraries/Gradle__com_google_guava_guava_21_0.xml new file mode 100644 index 0000000..32b92b3 --- /dev/null +++ b/.idea/libraries/Gradle__com_google_guava_guava_21_0.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__junit_junit_4_11.xml b/.idea/libraries/Gradle__junit_junit_4_11.xml new file mode 100644 index 0000000..dc26b34 --- /dev/null +++ b/.idea/libraries/Gradle__junit_junit_4_11.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..8262f72 --- /dev/null +++ b/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a807377 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.7 + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b7e2d8e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/RobotSimulator.iml b/.idea/modules/RobotSimulator.iml new file mode 100644 index 0000000..8a25f79 --- /dev/null +++ b/.idea/modules/RobotSimulator.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/RobotSimulator_main.iml b/.idea/modules/RobotSimulator_main.iml new file mode 100644 index 0000000..339ec7b --- /dev/null +++ b/.idea/modules/RobotSimulator_main.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/RobotSimulator_test.iml b/.idea/modules/RobotSimulator_test.iml new file mode 100644 index 0000000..62b9c65 --- /dev/null +++ b/.idea/modules/RobotSimulator_test.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..517c0ad --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,1921 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + instanceOf + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + project + + + true + + + + DIRECTORY + + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1491355005333 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0d1a6aa --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# RobotSimulator + + +### How to run the program + * Please make sure you have Java 8 SDK installed first. + * If you have `gradle` installed, then just run `gradle build`. Otherwise type `./gradlew build` or `./gradlew.bat + build` in your terminal to + build the project. Then open `build/reports` folder to see checkstyle, unit test, code coverage results. Jacoco + html reports in the folder of `build/jacocoHtml`. + * Type `gradle run` to run the Robot Simulator Application, and you will see some output based on the + `src/main/resources/input.txt` + file. (` + ./gradlew + run` or ` + ./gradlew.bat run`) + +### Features + * Checkstyle for code style + * Gradle for project build + * 100% code coverage (Jacoco) + * All tests passed with Junit. + diff --git a/RobotSimulator.iml b/RobotSimulator.iml new file mode 100644 index 0000000..cbef26a --- /dev/null +++ b/RobotSimulator.iml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/RobotSimulator.ipr b/RobotSimulator.ipr new file mode 100644 index 0000000..2ae6d8e --- /dev/null +++ b/RobotSimulator.ipr @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.6 + + + + + + + + + + + + + diff --git a/RobotSimulator.iws b/RobotSimulator.iws new file mode 100644 index 0000000..d5bc759 --- /dev/null +++ b/RobotSimulator.iws @@ -0,0 +1,207 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + localhost + 5050 + + + + + + + + + + + + + + + diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..11ba60d --- /dev/null +++ b/build.gradle @@ -0,0 +1,40 @@ +group 'au.com.seek' +version '1.0-SNAPSHOT' + +apply plugin: 'java' +apply plugin: 'idea' +apply plugin: 'jacoco' +apply plugin: 'checkstyle' +apply plugin: 'application' + +sourceCompatibility = 1.8 +compileJava.options.encoding = 'UTF-8' + +repositories { + maven { url 'http://182.140.210.215:8081/repository/maven-public/' } + mavenCentral() +} + +dependencies { + compile 'com.google.guava:guava:21.0' + testCompile group: 'junit', name: 'junit', version: '4.11' +} + +jacocoTestReport { + reports { + xml.enabled true + csv.enabled true + html.destination "${buildDir}/jacocoHtml" + } +} + +build { + dependsOn jacocoTestReport +} + +checkstyle { + configFile = new File("${project.projectDir}/checkstyle_rules.xml") + checkstyleMain.exclude "**/src/test" +} + +mainClassName = "au.com.seek.RobotSimulator" \ No newline at end of file diff --git a/checkstyle-cachefile b/checkstyle-cachefile new file mode 100644 index 0000000..0ee45c5 --- /dev/null +++ b/checkstyle-cachefile @@ -0,0 +1,14 @@ +#Thu Apr 06 08:41:33 CST 2017 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/command/ReportCommand.java=1491383770000 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/command/TurnRightCommand.java=1491383770000 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/RobotSimulator.java=1491386986000 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/domain/Robot.java=1491385232000 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/executor/Executor.java=1491386727000 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/command/MoveForwardCommand.java=1491383770000 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/command/TurnLeftCommand.java=1491383770000 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/domain/Tabletop.java=1491385286000 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/domain/Coordinate.java=1491359857000 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/domain/Direction.java=1491362416000 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/command/RobotCommand.java=1491383770000 +configuration*?=D4253C640606282CC41CCBFE1BA2D21B121751B2 +/Users/junv/code/RobotSimulator/src/main/java/au/com/seek/parser/InputParser.java=1491385972000 diff --git a/checkstyle_rules.xml b/checkstyle_rules.xml new file mode 100644 index 0000000..a4a42ad --- /dev/null +++ b/checkstyle_rules.xml @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/checkstyle_suppressions.xml b/checkstyle_suppressions.xml new file mode 100644 index 0000000..9e23118 --- /dev/null +++ b/checkstyle_suppressions.xml @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..3baa851 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..8752e40 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Apr 05 09:18:07 CST 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..27309d9 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..832fdb6 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..8576562 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ + +rootProject.name = 'RobotSimulator' diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..9604c91 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main/.DS_Store b/src/main/.DS_Store new file mode 100644 index 0000000..55c1fcb Binary files /dev/null and b/src/main/.DS_Store differ diff --git a/src/main/java/au/com/seek/RobotSimulator.java b/src/main/java/au/com/seek/RobotSimulator.java new file mode 100644 index 0000000..dc3ae71 --- /dev/null +++ b/src/main/java/au/com/seek/RobotSimulator.java @@ -0,0 +1,18 @@ +package au.com.seek; + +import au.com.seek.executor.Executor; +import au.com.seek.parser.InputParser; + +import java.io.IOException; + +public class RobotSimulator { + private static final String INPUT_FILE = "input.txt"; + + public static void main(String[] args) throws IOException { + new RobotSimulator().run(new InputParser(INPUT_FILE)); + } + + private void run(InputParser parser) { + new Executor(parser.getRobot()).execute(parser.parseCommands()); + } +} diff --git a/src/main/java/au/com/seek/command/MoveForwardCommand.java b/src/main/java/au/com/seek/command/MoveForwardCommand.java new file mode 100644 index 0000000..257d2ed --- /dev/null +++ b/src/main/java/au/com/seek/command/MoveForwardCommand.java @@ -0,0 +1,11 @@ +package au.com.seek.command; + +import au.com.seek.domain.Robot; + + +public class MoveForwardCommand implements RobotCommand { + @Override + public void execute(Robot robot) { + robot.moveForward(); + } +} diff --git a/src/main/java/au/com/seek/command/ReportCommand.java b/src/main/java/au/com/seek/command/ReportCommand.java new file mode 100644 index 0000000..7af3d35 --- /dev/null +++ b/src/main/java/au/com/seek/command/ReportCommand.java @@ -0,0 +1,11 @@ +package au.com.seek.command; + +import au.com.seek.domain.Robot; + +public class ReportCommand implements RobotCommand { + + @Override + public void execute(Robot robot) { + System.out.println(robot.report()); + } +} diff --git a/src/main/java/au/com/seek/command/RobotCommand.java b/src/main/java/au/com/seek/command/RobotCommand.java new file mode 100644 index 0000000..0d119b6 --- /dev/null +++ b/src/main/java/au/com/seek/command/RobotCommand.java @@ -0,0 +1,8 @@ +package au.com.seek.command; + + +import au.com.seek.domain.Robot; + +public interface RobotCommand { + void execute(Robot robot); +} diff --git a/src/main/java/au/com/seek/command/TurnLeftCommand.java b/src/main/java/au/com/seek/command/TurnLeftCommand.java new file mode 100644 index 0000000..b68971b --- /dev/null +++ b/src/main/java/au/com/seek/command/TurnLeftCommand.java @@ -0,0 +1,11 @@ +package au.com.seek.command; + +import au.com.seek.domain.Robot; + + +public class TurnLeftCommand implements RobotCommand { + @Override + public void execute(Robot robot) { + robot.turnLeft(); + } +} diff --git a/src/main/java/au/com/seek/command/TurnRightCommand.java b/src/main/java/au/com/seek/command/TurnRightCommand.java new file mode 100644 index 0000000..b523b54 --- /dev/null +++ b/src/main/java/au/com/seek/command/TurnRightCommand.java @@ -0,0 +1,11 @@ +package au.com.seek.command; + +import au.com.seek.domain.Robot; + + +public class TurnRightCommand implements RobotCommand { + @Override + public void execute(Robot robot) { + robot.turnRight(); + } +} diff --git a/src/main/java/au/com/seek/domain/Coordinate.java b/src/main/java/au/com/seek/domain/Coordinate.java new file mode 100644 index 0000000..e47b03b --- /dev/null +++ b/src/main/java/au/com/seek/domain/Coordinate.java @@ -0,0 +1,28 @@ +package au.com.seek.domain; + + +public class Coordinate { + private int xAxis; + private int yAxis; + + public Coordinate(int xAxis, int yAxis) { + this.xAxis = xAxis; + this.yAxis = yAxis; + } + + int getXAxis() { + return xAxis; + } + + void setXAxis(int xAxis) { + this.xAxis = xAxis; + } + + int getYAxis() { + return yAxis; + } + + void setYAxis(int yAxis) { + this.yAxis = yAxis; + } +} diff --git a/src/main/java/au/com/seek/domain/Direction.java b/src/main/java/au/com/seek/domain/Direction.java new file mode 100644 index 0000000..62346dc --- /dev/null +++ b/src/main/java/au/com/seek/domain/Direction.java @@ -0,0 +1,100 @@ +package au.com.seek.domain; + + +public enum Direction { + NORTH { + @Override + public Direction turnRight() { + return EAST; + } + + @Override + public Direction turnLeft() { + return WEST; + } + + @Override + public int stepValueOnXAxis() { + return 0; + } + + @Override + public int stepValueOnYAxis() { + return STEP_VALUE; + } + }, + EAST { + @Override + public Direction turnRight() { + return SOUTH; + } + + @Override + public Direction turnLeft() { + return NORTH; + } + + @Override + public int stepValueOnXAxis() { + return STEP_VALUE; + } + + @Override + public int stepValueOnYAxis() { + return 0; + } + }, + SOUTH { + @Override + public Direction turnRight() { + return WEST; + } + + @Override + public Direction turnLeft() { + return EAST; + } + + @Override + public int stepValueOnXAxis() { + return 0; + } + + @Override + public int stepValueOnYAxis() { + return NEGATIVE_STEP_VALUE; + } + }, + WEST { + @Override + public Direction turnRight() { + return NORTH; + } + + @Override + public Direction turnLeft() { + return SOUTH; + } + + @Override + public int stepValueOnXAxis() { + return NEGATIVE_STEP_VALUE; + } + + @Override + public int stepValueOnYAxis() { + return 0; + } + }; + + private static final int NEGATIVE_STEP_VALUE = -1; + private static final int STEP_VALUE = 1; + + public abstract Direction turnRight(); + + public abstract Direction turnLeft(); + + public abstract int stepValueOnXAxis(); + + public abstract int stepValueOnYAxis(); +} diff --git a/src/main/java/au/com/seek/domain/Robot.java b/src/main/java/au/com/seek/domain/Robot.java new file mode 100644 index 0000000..310cb53 --- /dev/null +++ b/src/main/java/au/com/seek/domain/Robot.java @@ -0,0 +1,54 @@ +package au.com.seek.domain; + + +public class Robot { + private Coordinate coordinate; + private Direction direction; + + public Robot(Coordinate coordinate, Direction direction) { + this.coordinate = coordinate; + this.direction = direction; + } + + public String report() { + return String.format("%s,%s,%s", getCoordinate().getXAxis(), + getCoordinate().getYAxis(), getDirection().toString()); + } + + public void turnLeft() { + this.direction = getDirection().turnLeft(); + } + + public void turnRight() { + this.direction = getDirection().turnRight(); + } + + public void moveForward() { + if (isGoingToFallingWhenMove()) { + return; + } + getCoordinate().setXAxis(moveOnXAxis()); + getCoordinate().setYAxis(moveOnYAxis()); + } + + private int moveOnYAxis() { + return getCoordinate().getYAxis() + getDirection().stepValueOnYAxis(); + } + + private int moveOnXAxis() { + return getCoordinate().getXAxis() + getDirection().stepValueOnXAxis(); + } + + private boolean isGoingToFallingWhenMove() { + return Tabletop.isCoordinateOutOfTabletop(new Coordinate(moveOnXAxis(), moveOnYAxis())); + } + + private Coordinate getCoordinate() { + return this.coordinate; + } + + private Direction getDirection() { + return this.direction; + } + +} diff --git a/src/main/java/au/com/seek/domain/Tabletop.java b/src/main/java/au/com/seek/domain/Tabletop.java new file mode 100644 index 0000000..70cee98 --- /dev/null +++ b/src/main/java/au/com/seek/domain/Tabletop.java @@ -0,0 +1,11 @@ +package au.com.seek.domain; + + +public class Tabletop { + public static final int X_AXIS_MAXIMUM = 5; + public static final int Y_AXIS_MAXIMUM = 5; + + public static boolean isCoordinateOutOfTabletop(Coordinate coordinate) { + return (coordinate.getXAxis() > X_AXIS_MAXIMUM) || (coordinate.getYAxis() > Y_AXIS_MAXIMUM); + } +} diff --git a/src/main/java/au/com/seek/executor/Executor.java b/src/main/java/au/com/seek/executor/Executor.java new file mode 100644 index 0000000..cdd3ece --- /dev/null +++ b/src/main/java/au/com/seek/executor/Executor.java @@ -0,0 +1,20 @@ +package au.com.seek.executor; + + +import au.com.seek.command.RobotCommand; +import au.com.seek.domain.Robot; + +import java.util.List; + +public class Executor { + + private final Robot robot; + + public Executor(Robot robot) { + this.robot = robot; + } + + public void execute(List commands) { + commands.forEach(command -> command.execute(robot)); + } +} diff --git a/src/main/java/au/com/seek/parser/InputParser.java b/src/main/java/au/com/seek/parser/InputParser.java new file mode 100644 index 0000000..4728518 --- /dev/null +++ b/src/main/java/au/com/seek/parser/InputParser.java @@ -0,0 +1,82 @@ +package au.com.seek.parser; + + +import au.com.seek.command.*; +import au.com.seek.domain.Coordinate; +import au.com.seek.domain.Direction; +import au.com.seek.domain.Robot; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.List; +import java.util.Map; + +import static java.lang.Integer.valueOf; + +public class InputParser { + private static final int BUFFER_SIZE = 1024; + private static final String PLACE_COMMAND = "PLACE"; + private static final String SEPARATOR = ","; + private static final String LEFT = "LEFT"; + private static final String RIGHT = "RIGHT"; + private static final String MOVE = "MOVE"; + private static final int BLANK_LENGTH = 1; + private static final String REPORT = "REPORT"; + + private static final Map COMMAND_STRING_MAP = + ImmutableMap.of(LEFT, new TurnLeftCommand(), + RIGHT, new TurnRightCommand(), + MOVE, new MoveForwardCommand(), + REPORT, new ReportCommand()); + + private String file; + private List commands = Lists.newArrayList(); + private Robot robot; + + public InputParser(String file) throws IOException { + this.file = file; + parse(); + } + + public List parseCommands() { + return commands; + } + + public Robot getRobot() { + return robot; + } + + private void parse() throws IOException { + InputStream in = getClass().getClassLoader().getResourceAsStream(file); + BufferedReader reader = new BufferedReader(new InputStreamReader(in), BUFFER_SIZE); + String line; + + while ((line = reader.readLine()) != null) { + if (line.startsWith(PLACE_COMMAND)) { + parseRobot(line); + continue; + } + if (robot == null) { + continue; + } + commands.add(getCommand(line)); + } + } + + private RobotCommand getCommand(String line) { + return COMMAND_STRING_MAP.get(line); + } + + private void parseRobot(String line) { + String robotString = line.substring(PLACE_COMMAND.length() + BLANK_LENGTH); + String[] split = robotString.split(SEPARATOR); + robot = new Robot(new Coordinate(valueOf(split[0]), valueOf(split[1])), Direction.valueOf(split[2])); + //reset all the commands + commands = Lists.newArrayList(); + } + +} diff --git a/src/main/resources/input.txt b/src/main/resources/input.txt new file mode 100644 index 0000000..40e0b98 --- /dev/null +++ b/src/main/resources/input.txt @@ -0,0 +1,6 @@ +PLACE 1,2,EAST +MOVE +MOVE +LEFT +MOVE +REPORT \ No newline at end of file diff --git a/src/test/.DS_Store b/src/test/.DS_Store new file mode 100644 index 0000000..55c1fcb Binary files /dev/null and b/src/test/.DS_Store differ diff --git a/src/test/java/au/com/seek/RobotSimulatorTest.java b/src/test/java/au/com/seek/RobotSimulatorTest.java new file mode 100644 index 0000000..66686fa --- /dev/null +++ b/src/test/java/au/com/seek/RobotSimulatorTest.java @@ -0,0 +1,32 @@ +package au.com.seek; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertThat; + + +public class RobotSimulatorTest { + private final ByteArrayOutputStream outputContent = new ByteArrayOutputStream(); + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outputContent)); + } + + @After + public void cleanUpStreams() { + System.setOut(null); + } + + @Test + public void should_run_commands_and_print_result_when_use_simple_input_file() throws Exception { + RobotSimulator.main(new String[]{}); + assertThat(outputContent.toString(), containsString("3,3,NORTH")); + } +} diff --git a/src/test/java/au/com/seek/command/MoveForwardCommandTest.java b/src/test/java/au/com/seek/command/MoveForwardCommandTest.java new file mode 100644 index 0000000..772e287 --- /dev/null +++ b/src/test/java/au/com/seek/command/MoveForwardCommandTest.java @@ -0,0 +1,24 @@ +package au.com.seek.command; + +import au.com.seek.domain.Robot; +import au.com.seek.domain.Coordinate; +import au.com.seek.domain.Direction; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + + +public class MoveForwardCommandTest { + @Test + public void should_update_place_after_execute() throws Exception { + Coordinate coordinate = new Coordinate(0, 0); + Robot robot = new Robot(coordinate, Direction.NORTH); + assertThat(robot.report(), equalTo("0,0,NORTH")); + + MoveForwardCommand command = new MoveForwardCommand(); + command.execute(robot); + assertThat(robot.report(), equalTo("0,1,NORTH")); + } + +} \ No newline at end of file diff --git a/src/test/java/au/com/seek/command/ReportCommandTest.java b/src/test/java/au/com/seek/command/ReportCommandTest.java new file mode 100644 index 0000000..da8647e --- /dev/null +++ b/src/test/java/au/com/seek/command/ReportCommandTest.java @@ -0,0 +1,39 @@ +package au.com.seek.command; + +import au.com.seek.domain.Robot; +import au.com.seek.domain.Coordinate; +import au.com.seek.domain.Direction; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertThat; + + +public class ReportCommandTest { + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outContent)); + } + + @After + public void cleanUpStreams() { + System.setOut(null); + } + + @Test + public void should_print_location_after_execute() throws Exception { + Coordinate coordinate = new Coordinate(0, 0); + Robot robot = new Robot(coordinate, Direction.NORTH); + ReportCommand command = new ReportCommand(); + command.execute(robot); + assertThat(outContent.toString(), containsString("0,0,NORTH")); + } + +} \ No newline at end of file diff --git a/src/test/java/au/com/seek/command/TurnLeftCommandTest.java b/src/test/java/au/com/seek/command/TurnLeftCommandTest.java new file mode 100644 index 0000000..3fff18e --- /dev/null +++ b/src/test/java/au/com/seek/command/TurnLeftCommandTest.java @@ -0,0 +1,25 @@ +package au.com.seek.command; + +import au.com.seek.domain.Robot; +import au.com.seek.domain.Coordinate; +import au.com.seek.domain.Direction; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + + +public class TurnLeftCommandTest { + + @Test + public void should_update_place_after_execute() throws Exception { + Coordinate coordinate = new Coordinate(0, 0); + Robot robot = new Robot(coordinate, Direction.NORTH); + assertThat(robot.report(), equalTo("0,0,NORTH")); + + TurnLeftCommand command = new TurnLeftCommand(); + command.execute(robot); + assertThat(robot.report(), equalTo("0,0,WEST")); + } + +} \ No newline at end of file diff --git a/src/test/java/au/com/seek/command/TurnRightCommandTest.java b/src/test/java/au/com/seek/command/TurnRightCommandTest.java new file mode 100644 index 0000000..b38031b --- /dev/null +++ b/src/test/java/au/com/seek/command/TurnRightCommandTest.java @@ -0,0 +1,24 @@ +package au.com.seek.command; + +import au.com.seek.domain.Robot; +import au.com.seek.domain.Coordinate; +import au.com.seek.domain.Direction; +import org.junit.Test; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +public class TurnRightCommandTest { + + @Test + public void should_update_place_after_execute() throws Exception { + Coordinate coordinate = new Coordinate(0, 0); + Robot robot = new Robot(coordinate, Direction.NORTH); + assertThat(robot.report(), equalTo("0,0,NORTH")); + + TurnRightCommand command = new TurnRightCommand(); + command.execute(robot); + assertThat(robot.report(), equalTo("0,0,EAST")); + } + +} \ No newline at end of file diff --git a/src/test/java/au/com/seek/domain/DirectionTest.java b/src/test/java/au/com/seek/domain/DirectionTest.java new file mode 100644 index 0000000..02aee35 --- /dev/null +++ b/src/test/java/au/com/seek/domain/DirectionTest.java @@ -0,0 +1,84 @@ +package au.com.seek.domain; + + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class DirectionTest { + + @Test + public void should_return_new_direction_when_do_turn_left() throws Exception { + + assertThat(Direction.NORTH.turnLeft(), is(Direction.WEST)); + + assertThat(Direction.WEST.turnLeft(), is(Direction.SOUTH)); + + assertThat(Direction.SOUTH.turnLeft(), is(Direction.EAST)); + + assertThat(Direction.EAST.turnLeft(), is(Direction.NORTH)); + } + + @Test + public void should_return_new_direction_when_do_turn_right() throws Exception { + + assertThat(Direction.NORTH.turnRight(), is(Direction.EAST)); + + assertThat(Direction.EAST.turnRight(), is(Direction.SOUTH)); + + assertThat(Direction.SOUTH.turnRight(), is(Direction.WEST)); + + assertThat(Direction.WEST.turnRight(), is(Direction.NORTH)); + } + + + @Test + public void should_return_step_value_on_x_axis() throws Exception { + + assertThat(Direction.NORTH.stepValueOnXAxis(), is(0)); + + assertThat(Direction.EAST.stepValueOnXAxis(), is(1)); + + assertThat(Direction.SOUTH.stepValueOnXAxis(), is(0)); + + assertThat(Direction.WEST.stepValueOnXAxis(), is(-1)); + } + + @Test + public void should_return_step_value_on_y_axis() throws Exception { + + assertThat(Direction.NORTH.stepValueOnYAxis(), is(1)); + + assertThat(Direction.EAST.stepValueOnYAxis(), is(0)); + + assertThat(Direction.SOUTH.stepValueOnYAxis(), is(-1)); + + assertThat(Direction.WEST.stepValueOnYAxis(), is(0)); + } + + @Test + public void should_return_direction_string_as_enum() throws Exception { + + assertThat(Direction.NORTH.toString(), equalTo("NORTH")); + + assertThat(Direction.WEST.toString(), equalTo("WEST")); + + assertThat(Direction.SOUTH.toString(), equalTo("SOUTH")); + + assertThat(Direction.EAST.toString(), equalTo("EAST")); + } + + @Test + public void should_return_all_the_values_of_direction() throws Exception { + Direction[] directions = {Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST}; + assertThat(Direction.values(), equalTo(directions)); + } + + @Test + public void test_value_of() throws Exception { + Direction east = Direction.valueOf("EAST"); + assertThat(east, is(Direction.EAST)); + } +} diff --git a/src/test/java/au/com/seek/domain/RobotTest.java b/src/test/java/au/com/seek/domain/RobotTest.java new file mode 100644 index 0000000..c1f69a7 --- /dev/null +++ b/src/test/java/au/com/seek/domain/RobotTest.java @@ -0,0 +1,83 @@ +package au.com.seek.domain; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + + +public class RobotTest { + + @Test + public void should_report_robot_location() throws Exception { + Coordinate coordinate = new Coordinate(0, 0); + Robot robot = new Robot(coordinate, Direction.EAST); + + String report = robot.report(); + assertThat(report, equalTo("0,0,EAST")); + } + + @Test + public void should_report_robot_location_when_x_and_y_axis_is_not_zero() throws Exception { + Coordinate coordinate = new Coordinate(3, 5); + Robot robot = new Robot(coordinate, Direction.SOUTH); + + String report = robot.report(); + assertThat(report, equalTo("3,5,SOUTH")); + } + + @Test + public void should_change_direction_when_do_turn_left() throws Exception { + Coordinate coordinate = new Coordinate(3, 3); + Robot robot = new Robot(coordinate, Direction.NORTH); + + robot.turnLeft(); + assertThat(robot.report(), equalTo("3,3,WEST")); + } + + @Test + public void should_change_direction_when_do_turn_right() throws Exception { + Coordinate coordinate = new Coordinate(0, 1); + Robot robot = new Robot(coordinate, Direction.NORTH); + + robot.turnRight(); + assertThat(robot.report(), equalTo("0,1,EAST")); + } + + @Test + public void should_change_coordinates_when_do_move_and_facing_north() throws Exception { + Coordinate coordinate = new Coordinate(0, 1); + Robot robot = new Robot(coordinate, Direction.NORTH); + + robot.moveForward(); + assertThat(robot.report(), equalTo("0,2,NORTH")); + } + + @Test + public void should_change_coordinates_when_do_move_and_facing_east() throws Exception { + Coordinate coordinate = new Coordinate(0, 1); + Robot robot = new Robot(coordinate, Direction.EAST); + + robot.moveForward(); + assertThat(robot.report(), equalTo("1,1,EAST")); + } + + @Test + public void should_remain_same_coordinates_if_robot_is_going_to_falling_in_x_axis() throws Exception { + Coordinate coordinate = new Coordinate(5, 0); + Robot robot = new Robot(coordinate, Direction.EAST); + + robot.moveForward(); + assertThat(robot.report(), equalTo("5,0,EAST")); + } + + @Test + public void should_remain_same_coordinates_if_robot_is_going_to_falling_in_y_axis() throws Exception { + Coordinate coordinate = new Coordinate(0, 5); + Robot robot = new Robot(coordinate, Direction.NORTH); + + robot.moveForward(); + assertThat(robot.report(), equalTo("0,5,NORTH")); + } + +} \ No newline at end of file diff --git a/src/test/java/au/com/seek/domain/TabletopTest.java b/src/test/java/au/com/seek/domain/TabletopTest.java new file mode 100644 index 0000000..1187005 --- /dev/null +++ b/src/test/java/au/com/seek/domain/TabletopTest.java @@ -0,0 +1,33 @@ +package au.com.seek.domain; + + +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class TabletopTest { + @Test + public void just_for_coverage() throws Exception { + Tabletop tabletop = new Tabletop(); + assertThat(tabletop == null, is(false)); + } + + @Test + public void should_return_true_if_coordinate_are_too_large() throws Exception { + Coordinate coordinate = new Coordinate(6, 5); + assertThat(Tabletop.isCoordinateOutOfTabletop(coordinate), is(true)); + } + + @Test + public void should_return_false_if_coordinate_is_on_the_border_tabletop() throws Exception { + Coordinate coordinate = new Coordinate(5, 5); + assertThat(Tabletop.isCoordinateOutOfTabletop(coordinate), is(false)); + } + + @Test + public void should_return_false_if_coordinate_is_within_tabletop() throws Exception { + Coordinate coordinate = new Coordinate(3, 1); + assertThat(Tabletop.isCoordinateOutOfTabletop(coordinate), is(false)); + } +} \ No newline at end of file diff --git a/src/test/java/au/com/seek/parser/InputParserTest.java b/src/test/java/au/com/seek/parser/InputParserTest.java new file mode 100644 index 0000000..2cd1169 --- /dev/null +++ b/src/test/java/au/com/seek/parser/InputParserTest.java @@ -0,0 +1,57 @@ +package au.com.seek.parser; + +import au.com.seek.domain.Robot; +import au.com.seek.command.RobotCommand; +import org.junit.Test; + +import java.util.List; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + + +public class InputParserTest { + @Test + public void should_return_commands_and_robot() throws Exception { + InputParser parser = new InputParser("simple_input.txt"); + Robot robot = parser.getRobot(); + List commands = parser.parseCommands(); + assertThat(robot.report(), equalTo("0,0,NORTH")); + assertThat(commands.size(),is(3)); + assertThat(commands.get(0).getClass().getSimpleName(), equalTo("MoveForwardCommand")); + assertThat(commands.get(1).getClass().getSimpleName(), equalTo("TurnRightCommand")); + assertThat(commands.get(2).getClass().getSimpleName(), equalTo("ReportCommand")); + } + + @Test + public void should_skip_the_commands_before_place_robot() throws Exception { + InputParser parser = new InputParser("has_invalid_inputs.txt"); + Robot robot = parser.getRobot(); + List commands = parser.parseCommands(); + assertThat(robot.report(), equalTo("2,4,EAST")); + assertThat(commands.size(),is(3)); + assertThat(commands.get(0).getClass().getSimpleName(), equalTo("TurnLeftCommand")); + assertThat(commands.get(1).getClass().getSimpleName(), equalTo("MoveForwardCommand")); + assertThat(commands.get(2).getClass().getSimpleName(), equalTo("ReportCommand")); + } + + @Test + public void should_replace_the_robot_when_there_is_another_place_command() throws Exception { + InputParser parser = new InputParser("double_place_commands.txt"); + Robot robot = parser.getRobot(); + List commands = parser.parseCommands(); + assertThat(robot.report(), equalTo("3,3,EAST")); + assertThat(commands.size(),is(4)); + + assertThat(commands.get(0).getClass().getSimpleName(), equalTo("MoveForwardCommand")); + assertThat(commands.get(1).getClass().getSimpleName(), equalTo("TurnRightCommand")); + assertThat(commands.get(2).getClass().getSimpleName(), equalTo("MoveForwardCommand")); + assertThat(commands.get(3).getClass().getSimpleName(), equalTo("ReportCommand")); + } + +// @Test +// public void should_throw_exception_if_file_is_not_found() throws Exception { +// InputParser parser = new InputParser("empty.txt"); +// } +} \ No newline at end of file diff --git a/src/test/resources/double_place_commands.txt b/src/test/resources/double_place_commands.txt new file mode 100644 index 0000000..3904f94 --- /dev/null +++ b/src/test/resources/double_place_commands.txt @@ -0,0 +1,7 @@ +PLACE 0,0,NORTH +MOVE +PLACE 3,3,EAST +MOVE +RIGHT +MOVE +REPORT \ No newline at end of file diff --git a/src/test/resources/empty.txt b/src/test/resources/empty.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/has_invalid_inputs.txt b/src/test/resources/has_invalid_inputs.txt new file mode 100644 index 0000000..cc8b68f --- /dev/null +++ b/src/test/resources/has_invalid_inputs.txt @@ -0,0 +1,6 @@ +RIGHT +MOVE +PLACE 2,4,EAST +LEFT +MOVE +REPORT \ No newline at end of file diff --git a/src/test/resources/simple_input.txt b/src/test/resources/simple_input.txt new file mode 100644 index 0000000..64c9adf --- /dev/null +++ b/src/test/resources/simple_input.txt @@ -0,0 +1,4 @@ +PLACE 0,0,NORTH +MOVE +RIGHT +REPORT \ No newline at end of file