From a97d836f32163b9e39a49f60495ca3742c0bedb4 Mon Sep 17 00:00:00 2001
From: "Eduardo L. Buratti" <elburatti@inf.ufpr.br>
Date: Thu, 18 Aug 2016 11:56:26 -0300
Subject: [PATCH] Add test framework

---
 package.json           |  4 +++
 test/global.js         | 29 +++++++++++++++++
 test/mocha.opts        |  9 +++++
 test/util/hash.spec.js | 74 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 116 insertions(+)
 create mode 100644 test/global.js
 create mode 100644 test/mocha.opts
 create mode 100644 test/util/hash.spec.js

diff --git a/package.json b/package.json
index 10de3b23..87749f10 100644
--- a/package.json
+++ b/package.json
@@ -32,5 +32,9 @@
     "mongodb": "^2.2.5",
     "osprey": "^0.3.2",
     "raml2html": "^2.4.0"
+  },
+  "devDependencies": {
+    "chai": "^3.5.0",
+    "mocha": "^3.0.2"
   }
 }
diff --git a/test/global.js b/test/global.js
new file mode 100644
index 00000000..b3bc09ca
--- /dev/null
+++ b/test/global.js
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
+ * Departamento de Informatica - Universidade Federal do Parana
+ *
+ * This file is part of blendb.
+ *
+ * blendb is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * blendb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with blendb.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+'use strict';
+
+process.env.NODE_ENV = 'test';
+
+global.expect = require('chai').expect;
+
+// Add the ./src directory to require's search path to facilitate import
+// modules later on (avoiding the require('../../../../module') problem).
+require('app-module-path').addPath(__dirname + '/../src');
diff --git a/test/mocha.opts b/test/mocha.opts
new file mode 100644
index 00000000..150fdfab
--- /dev/null
+++ b/test/mocha.opts
@@ -0,0 +1,9 @@
+--require ./test/global.js
+--reporter spec
+--ui bdd
+--recursive
+--colors
+--timeout 60000
+--slow 300
+--check-leaks
+--globals expect
diff --git a/test/util/hash.spec.js b/test/util/hash.spec.js
new file mode 100644
index 00000000..2de7ca6e
--- /dev/null
+++ b/test/util/hash.spec.js
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
+ * Departamento de Informatica - Universidade Federal do Parana
+ *
+ * This file is part of blendb.
+ *
+ * blendb is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * blendb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with blendb.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+'use strict';
+
+/* globals expect */
+/* jshint -W024 */
+/* jshint expr:true, maxlen:false */
+/* jscs:disable maximumLineLength */
+
+const hash = require('util/hash');
+
+describe('hash utility library', () => {
+    it('should generate a sha1 hash for a collection of objects', () => {
+        let h = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
+
+        expect(h).to.be.a('string');
+        expect(h).to.not.be.empty;
+    });
+
+    it('should generate the same hash for the same input', () => {
+        let h1 = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
+        let h2 = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
+
+        expect(h1).to.be.a('string');
+        expect(h2).to.be.a('string');
+        expect(h1).to.be.equal(h2);
+    });
+
+    it('should generate the same hash for different order of input', () => {
+        let h1 = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
+        let h2 = hash.sha1('test', ['list', 'of', 'things'], { obj: 'test' });
+
+        expect(h1).to.be.a('string');
+        expect(h2).to.be.a('string');
+        expect(h1).to.be.equal(h2);
+    });
+
+    it('should not generate the same hash for distinct input', () => {
+        let h1 = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
+        let h2 = hash.sha1('test', { obj: 'test', x: true },
+            ['list', 'of', 'things']);
+
+        expect(h1).to.be.a('string');
+        expect(h2).to.be.a('string');
+        expect(h1).to.not.be.equal(h2);
+    });
+
+    it('should not generate the same hash for different order in deep lists', () => {
+        let h1 = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
+        let h2 = hash.sha1('test', { obj: 'test' }, ['of', 'list', 'things']);
+
+        expect(h1).to.be.a('string');
+        expect(h2).to.be.a('string');
+        expect(h1).to.not.be.equal(h2);
+    });
+});
-- 
GitLab