Skip to content

Commit

Permalink
Add integration tests to compare js and java implementations
Browse files Browse the repository at this point in the history
Issue #9
  • Loading branch information
alxndrsn committed Jan 10, 2019
1 parent 18cd8a2 commit 133ad4c
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ ifdef ComSpec
endif


.PHONY: default test travis
.PHONY: default test travis test-integration

default: test assemble-java android

test: test-js test-bootstrap test-java

travis: test

test-integration:
cd integration-tests && npm install && npm test


.PHONY: setup-js test-js release-js

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ twitter-bootstrap widget [available from `npm`](https://www.npmjs.com/package/bi
require('bikram-sambat-bootstrap');

For usage example, see `bootstrap/dist`.

# Testing

To run the fast test suite

make test

To run the integration test suite, comparing Java and Javascript implementations

make test-integration
4 changes: 4 additions & 0 deletions integration-tests/.gitignore
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
51 changes: 51 additions & 0 deletions integration-tests/JavaImplementationWrapper.java
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]);
}
}
14 changes: 14 additions & 0 deletions integration-tests/java-implementation-wrapper.js
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 };
17 changes: 17 additions & 0 deletions integration-tests/package.json
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"
}
}
58 changes: 58 additions & 0 deletions integration-tests/tests.js
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('-'); }
4 changes: 2 additions & 2 deletions java/android-demo-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ buildscript {
mavenCentral()
maven { url "https://jitpack.io" }
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.android.tools.build:gradle:3.2.0'
}
}
apply plugin: 'com.android.application'
Expand All @@ -30,7 +31,6 @@ dependencies {

android {
compileSdkVersion 23
buildToolsVersion '26.0.1'

packagingOptions {
exclude 'META-INF/LICENSE'
Expand Down
1 change: 0 additions & 1 deletion java/android-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies {

android {
compileSdkVersion 23
buildToolsVersion '26.0.1'

defaultConfig {
minSdkVersion 7
Expand Down
3 changes: 2 additions & 1 deletion java/build.gradle
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'
}
}
2 changes: 1 addition & 1 deletion java/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip

0 comments on commit 133ad4c

Please sign in to comment.