Skip to content

Commit

Permalink
add cmd line test for json output, use similar()
Browse files Browse the repository at this point in the history
  • Loading branch information
crotwell committed Mar 15, 2024
1 parent c57e286 commit 9047de0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/main/java/edu/sc/seis/TauP/Arrival.java
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ public void writeJSON(PrintWriter pw, String indent) throws IOException {
} else {
pw.write(innerIndent+" "+JSONWriter.valueToString("error")+": "+JSONWriter.valueToString("geometrical speading not finite")+","+NL);
}
pw.write(innerIndent+" "+JSONWriter.valueToString("refltranpsv")+": "+JSONWriter.valueToString((float)getReflTransPSV())+NL);
pw.write(innerIndent+" "+JSONWriter.valueToString("refltranpsv")+": "+JSONWriter.valueToString((float)getReflTransPSV())+", "+NL);
pw.write(innerIndent+" "+JSONWriter.valueToString("refltransh")+": "+JSONWriter.valueToString((float)getReflTransSH())+NL);
pw.write(innerIndent+"}");
} catch (TauPException e) {
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/edu/sc/seis/TauP/CmdLineOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.jupiter.api.Test;

import java.io.BufferedOutputStream;
Expand Down Expand Up @@ -112,6 +114,10 @@ public class CmdLineOutputTest {
"taup wkbj --help",
};

String[] jsonTestCmds = new String[] {
"taup time -h 10 -ph P -deg 35 --json",
};

String versionCmd = "taup --version";

String[] docCmds = new String[] {
Expand Down Expand Up @@ -144,6 +150,7 @@ public void regenSavedOutput() throws Exception {
allList.addAll(Arrays.asList(velplotTestCmds));
allList.addAll(Arrays.asList(reflTransPlotTestCmds));
allList.addAll(Arrays.asList(phaseDescribeTestCmds));
allList.addAll(Arrays.asList(jsonTestCmds));
for (String cmd : allList) {
System.err.println(cmd);
saveTestOutputToFile(cmd);
Expand Down Expand Up @@ -318,6 +325,11 @@ public void testTauPReflTransplot() throws Exception {
runTests(reflTransPlotTestCmds);
}


@Test
public void testTauPJSON() throws Exception {
runJsonTests(jsonTestCmds);
}
@Test
public void testTauPTable() throws Exception {
// this one takes a lot of memory
Expand All @@ -330,6 +342,12 @@ public void runTests(String[] cmds) throws Exception {
}
}

public void runJsonTests(String[] cmds) throws Exception {
for (int i = 0; i < cmds.length; i++) {
testJsonCmd(cmds[i]);
}
}

public void runCmd(String cmd) throws Exception {
String[] s = cmd.split(" +");
String tool = s[0];
Expand Down Expand Up @@ -371,6 +389,19 @@ public void testCmd(String cmd) throws Exception {
origErr.println("Done with " + cmd);
}

public void testJsonCmd(String cmd) throws Exception {
setUpStreams();
assertEquals(0, outContent.toByteArray().length, "sysout is not empty");
runCmd(cmd);
BufferedReader prior = getPriorOutput(cmd);
BufferedReader current = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(outContent.toByteArray())));
JSONTokener jsonIn = new JSONTokener(prior);
JSONObject priorJson = new JSONObject(jsonIn);
JSONTokener currentIn = new JSONTokener(current);
JSONObject currentJson = new JSONObject(currentIn);
assertTrue(priorJson.similar(currentJson), currentJson.toString(2));
}

/**
* test loading prior results text file from test resources. Kind of a meta-test... :)
* @throws Exception
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"model": "iasp91",
"sourcedepth": 10,
"receiverdepth": 0,
"phases": [ "P" ],
"arrivals": [
{
"distdeg": 35,
"phase": "P",
"time": 412.4329,
"rayparam": 8.613012,
"takeoff": 26.741543,
"incident": 26.69624,
"puristdist": 35,
"puristname": "P",
"amp": {
"factorpsv": 2.0197284E-9,
"factorsh": 0,
"geospread": 8.546455E-9,
"refltranpsv": 0.9386537,
"refltransh": 0
}
}
]
}
Loading

0 comments on commit 9047de0

Please sign in to comment.