Skip to content

Commit

Permalink
Added automated testing to Quoter
Browse files Browse the repository at this point in the history
More can always be done, but it's a good start
  • Loading branch information
frossm committed Jun 30, 2023
1 parent 3ad9d44 commit d7041c0
Show file tree
Hide file tree
Showing 11 changed files with 505 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.fross</groupId>
<artifactId>quoter</artifactId>
<version>5.0.7</version>
<version>5.0.8</version>
<packaging>jar</packaging>

<name>quoter</name>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: quoter
version: '5.0.7'
version: '5.0.8'
summary: Command line utility to pull stock and index quotes
description: |
Quoter fetches online stock quotes and index data for easy display on
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/fross/quoter/CommandLineParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public class CommandLineParser {

@Parameter(names = { "-w", "--width" }, description = "Set screen width in columns for 3 month trend display")
protected int clWidth = 120;

@Parameter(names = { "-n", "--hide-index" }, description = "Do not display index information")
protected boolean clHideIndex = false;

@Parameter(names = { "-a", "--auto-refresh" }, description = "Set a refresh time for quotes in seconds", validateWith = AutoRefreshValidator.class)
protected int clAutoRefresh = 0;

@Parameter(names = { "-d", "--trend-duration" }, description = "Set the number of historical days to include in the trend")
protected int clTrendDuration = 0;

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/fross/quoter/HistoricalQuotes.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ public void displayTrend(String symb) {
Output.debugPrint("Slots per Cost Unit: " + slotsPerCostUnit);

// Display the symbol informational header
Output.printColorln(Ansi.Color.WHITE, "\n\n+--" + NUM_DAYS_IN_TREND + " Day Trend" + "-".repeat(graphWidth - String.valueOf(NUM_DAYS_IN_TREND).length() + 8) + "+");
Output.printColorln(Ansi.Color.WHITE,
"\n\n+--" + NUM_DAYS_IN_TREND + " Day Trend" + "-".repeat(graphWidth - String.valueOf(NUM_DAYS_IN_TREND).length() + 8) + "+");
Output.printColorln(Ansi.Color.YELLOW, symb.toUpperCase());
Output.printColorln(Ansi.Color.YELLOW, "Current Price: " + symbolData.get("latestPrice"));
Output.printColorln(Ansi.Color.YELLOW, NUM_DAYS_IN_TREND + " Day Low: " + String.format("%,.2f", sv));
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/fross/quoter/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ public static void main(String[] args) {
}

// CLI: Debug Switch
if (cli.clDebug == true)
if (cli.clDebug == true) {
Debug.enable();
}

// CLI: list Favorites
if (cli.clListFavorites == true) {
Expand All @@ -125,6 +126,8 @@ public static void main(String[] args) {
}
if (cli.clTrendDuration != 0) {
Prefs.set("trendduration", cli.clTrendDuration);
Output.printColorln(Ansi.Color.YELLOW, "Default trend duration has been set for " + cli.clTrendDuration + " days");
System.exit(0);
}

// CLI: Display Version & Latest GitHub Release
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/fross/quoter/Prefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import java.util.prefs.Preferences;

/**
* Prefs: Holds the logic and calls to the java preferences system. Used to save
* and restore the stacks between sessions.
* Prefs: Holds the logic and calls to the java preferences system. Used to save and restore the stacks between sessions.
*
* @author michael.d.fross
*
Expand Down Expand Up @@ -62,7 +61,7 @@ public static boolean queryBoolean(String key) {
public static Double queryDouble(String key) {
return prefs.getDouble(key, 0);
}

/**
* QueryInt(): Returns an int preference item
*
Expand All @@ -71,7 +70,7 @@ public static Double queryDouble(String key) {
*/
public static int queryInt(String key) {
return prefs.getInt(key, 0);
}
}

/**
* QueryString(): Returns a String preference item
Expand Down Expand Up @@ -122,9 +121,10 @@ public static void set(String key, double value) {
public static void set(String key, String value) {
prefs.put(key, value);
}

/**
* remove(): Remove the provided preference
*
* @param key
*/
public static void remove(String key) {
Expand Down
154 changes: 154 additions & 0 deletions src/test/java/org/fross/quoter/CommandLineParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/**************************************************************************************************************
* Quoter.jar
*
* Quoter is a command line program that display stock quotes and index data.
*
* Copyright (c) 2019-2022 Michael Fross
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
***************************************************************************************************************/
package org.fross.quoter;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import com.beust.jcommander.JCommander;

class CommandLineParserTest {
// Test all of the short options
@Test
void testShortCommandLineArgs() {
// Test Short Options
String[] argv1 = { "-z", "-w", "100", "-n", "-a", "60", "-d", "30", "-s", "-l", "-r", "-i", "-t", "-x", "out.csv", "-D", "-v", "-h" };

CommandLineParser cli = new CommandLineParser();
JCommander jc = new JCommander();
jc.setProgramName("Quoter");

jc = JCommander.newBuilder().addObject(cli).build();
jc.parse(argv1);

assertTrue(cli.clNoColor);
assertEquals(cli.clWidth, 100);
assertTrue(cli.clHideIndex);
assertEquals(cli.clAutoRefresh, 60);
assertEquals(cli.clTrendDuration, 30);
assertTrue(cli.clSave);
assertTrue(cli.clListFavorites);
assertTrue(cli.clRemoveFavorites);
assertTrue(cli.clIgnoreFavorites);
assertTrue(cli.clTrend);
assertEquals(cli.clExport, "out.csv");
assertTrue(cli.clDebug);
assertTrue(cli.clVersion);
assertTrue(cli.clHelp);
}

// Test all of the long options
@Test
void testLongCommandLineArgs() {
// Test Long Options
String[] argv1 = { "--no-color", "--width", "100", "--hide-index", "--auto-refresh", "60", "--trend-duration", "30", "--save", "--list-favorites",
"--remove-favorites", "--ignore-favorites", "--trend", "--export", "out.csv", "--debug", "--version", "--help" };

CommandLineParser cli = new CommandLineParser();
JCommander jc = new JCommander();
jc.setProgramName("Quoter");

jc = JCommander.newBuilder().addObject(cli).build();
jc.parse(argv1);

assertTrue(cli.clNoColor);
assertEquals(cli.clWidth, 100);
assertTrue(cli.clHideIndex);
assertEquals(cli.clAutoRefresh, 60);
assertEquals(cli.clTrendDuration, 30);
assertTrue(cli.clSave);
assertTrue(cli.clListFavorites);
assertTrue(cli.clRemoveFavorites);
assertTrue(cli.clIgnoreFavorites);
assertTrue(cli.clTrend);
assertEquals(cli.clExport, "out.csv");
assertTrue(cli.clDebug);
assertTrue(cli.clVersion);
assertTrue(cli.clHelp);
}

// Test a mixture of short and long option types
@Test
void testMixedCommandLineArgs1() {
// Test Mix of Options
String[] argv3 = { "--no-color", "-?", "-s", "--version", "--trend", "-d 45" };

CommandLineParser cli = new CommandLineParser();
JCommander jc = new JCommander();
jc.setProgramName("Quoter");

jc = JCommander.newBuilder().addObject(cli).build();
jc.parse(argv3);

assertTrue(cli.clNoColor);
assertEquals(cli.clWidth, 120);
assertFalse(cli.clHideIndex);
assertEquals(cli.clAutoRefresh, 0);
assertEquals(cli.clTrendDuration, 0);
assertTrue(cli.clSave);
assertFalse(cli.clListFavorites);
assertFalse(cli.clRemoveFavorites);
assertFalse(cli.clIgnoreFavorites);
assertTrue(cli.clTrend);
assertEquals(cli.clExport, "");
assertFalse(cli.clDebug);
assertTrue(cli.clVersion);
assertTrue(cli.clHelp);
}

// Test a mixture of short and long option types
void testMixedCommandLineArgs2() {
// Test Mix of Options
String[] argv4 = { "-w", "400", "--hide-index", "--ignore-favorites", "-x", "testoutput", "-v", "-a", "500", "--debug" };

CommandLineParser cli = new CommandLineParser();
JCommander jc = new JCommander();
jc.setProgramName("Quoter");

jc = JCommander.newBuilder().addObject(cli).build();
jc.parse(argv4);

assertFalse(cli.clNoColor);
assertEquals(cli.clWidth, 400);
assertTrue(cli.clHideIndex);
assertEquals(cli.clAutoRefresh, 500);
assertEquals(cli.clTrendDuration, 0);
assertFalse(cli.clSave);
assertFalse(cli.clListFavorites);
assertFalse(cli.clRemoveFavorites);
assertTrue(cli.clIgnoreFavorites);
assertFalse(cli.clTrend);
assertEquals(cli.clExport, "testoutput");
assertTrue(cli.clDebug);
assertTrue(cli.clVersion);
assertFalse(cli.clHelp);
}

}
Loading

0 comments on commit d7041c0

Please sign in to comment.