forked from alxndrsn/bikram-sambat.js
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration tests to compare js and java implementations
Issue #9
- Loading branch information
alxndrsn
committed
Jan 10, 2019
1 parent
18cd8a2
commit 133ad4c
Showing
11 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/package-lock.json | ||
/node_modules/ | ||
/bikram-sambat-generated.jar | ||
/JavaImplementationWrapper.class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import bikramsambat.*; | ||
|
||
public class JavaImplementationWrapper { | ||
private static final BsCalendar cal = BsCalendar.getInstance(); | ||
|
||
public static void main(String... args) { | ||
String methodName = args[0]; | ||
|
||
try { | ||
if(methodName.equals("daysInMonth")) { | ||
print(daysInMonth(args)); | ||
} else if(methodName.equals("toGreg")) { | ||
print(toGreg(args)); | ||
} else { | ||
} | ||
} catch(Exception ex) { | ||
err(1, "Exception caught while trying to execute " + methodName + "(): " + ex); | ||
} | ||
err(2, "Method not supported: " + methodName + "()"); | ||
|
||
} | ||
|
||
private static void print(Object s) { | ||
System.out.print(s); | ||
System.exit(0); | ||
} | ||
|
||
private static void err(int status, Object s) { | ||
System.err.println("JavaImplementationWrapper threw a fatal error: " + s); | ||
System.exit(status); | ||
} | ||
|
||
private static int daysInMonth(String... args) throws Exception { | ||
int year = intFrom(args, 1); | ||
int month = intFrom(args, 2); | ||
|
||
return cal.daysInMonth(year, month); | ||
} | ||
|
||
private static BsGregorianDate toGreg(String... args) throws Exception { | ||
int year = intFrom(args, 1); | ||
int month = intFrom(args, 2); | ||
int day = intFrom(args, 3); | ||
|
||
return cal.toGreg(new BikramSambatDate(year, month, day)); | ||
} | ||
|
||
private static int intFrom(String[] args, int idx) { | ||
return Integer.parseInt(args[idx]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const execSync = require('child_process').execSync; | ||
|
||
const daysInMonth = (bsYear, month) => exec('daysInMonth', bsYear, month); | ||
const toGreg = (year, month, day) => exec('toGreg', year, month, day); | ||
|
||
function exec(method, ...args) { | ||
args = args.map(arg => arg.toString()).join(' '); | ||
|
||
const cmd = `java -classpath .:bikram-sambat-generated.jar JavaImplementationWrapper ${method} ${args}`; | ||
|
||
return execSync(cmd, { encoding:'utf-8' }); | ||
} | ||
|
||
module.exports = { daysInMonth, toGreg }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "integration-tests", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"update-java-lib": "cd ../java/lib && ../gradlew jar && cp build/libs/bikram-sambat.jar ../../integration-tests/bikram-sambat-generated.jar", | ||
"compile-java-wrapper": "javac -classpath bikram-sambat-generated.jar JavaImplementationWrapper.java", | ||
"test": "npm run update-java-lib && npm run compile-java-wrapper && echo Running tests... && mocha --reporter progress tests.js" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"mocha": "^5.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const assert = require('chai').assert; | ||
const js = require('../js/src/index'); | ||
const java = require('./java-implementation-wrapper'); | ||
|
||
describe('Comparison of implementations', function() { | ||
|
||
describe('daysInMonth()', function() { | ||
const data = require('../test-data/daysInMonth'); | ||
|
||
Object.keys(data).forEach(bsYear => { | ||
for(let month=1; month<=12; ++month) { | ||
|
||
it(`should return same value for month ${month} of year ${bsYear} BS`, function() { | ||
assert.equal(java.daysInMonth(bsYear, month), | ||
js.daysInMonth(bsYear, month)); | ||
}); | ||
|
||
} | ||
}); | ||
}); | ||
|
||
describe('toBik_euro()', function() { | ||
}); | ||
|
||
describe('toBik_dev()', function() { | ||
}); | ||
|
||
describe('toGreg()', function() { | ||
const data = require('../test-data/toGreg'); | ||
|
||
data.forEach(testCase => { | ||
|
||
const bs = testCase.bs; | ||
|
||
it(`should return same value for ${fmtArray(bs)}`, function() { | ||
|
||
// when | ||
const javaResult = java.toGreg(...bs); | ||
const jsResult = fmtObj(js.toGreg(...bs)); | ||
|
||
// then | ||
assert.equal(javaResult, jsResult); | ||
|
||
}); | ||
|
||
}); | ||
}); | ||
|
||
}); | ||
|
||
function zPad(num, digits) { | ||
num = "" + num; | ||
while(num.length < digits) num = "0" + num; | ||
return num; | ||
} | ||
|
||
function fmtObj(d) { return `${d.year}-${zPad(d.month, 2)}-${zPad(d.day, 2)}`; } | ||
function fmtArray(arr) { return arr.map(e => zPad(e, 2)).join('-'); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
buildscript { | ||
repositories { | ||
jcenter() | ||
google() | ||
} | ||
|
||
dependencies { | ||
classpath 'com.android.tools.build:gradle:2.3.1' | ||
classpath 'com.android.tools.build:gradle:3.2.0' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters