Skip to content

Commit

Permalink
✅ test: new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed May 24, 2016
1 parent 0148185 commit fe4f94b
Show file tree
Hide file tree
Showing 16 changed files with 480 additions and 44 deletions.
50 changes: 26 additions & 24 deletions lib/fakerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const any = '0123456789' + chars;
module.exports = function(localeID = "default") {
let self = this;

let locale = require("./locales/" + localeID + "/index");
let locale;
try {
locale = require("./locales/" + localeID + "/index");
} catch (ignored) {}

if (locale) {
if (localeID != "default") {
let fallbackID = locale._meta.fallback || "default"
Expand Down Expand Up @@ -118,30 +122,28 @@ module.exports = function(localeID = "default") {

self.populate = function(format, ...args) {
let res = format;
if (format.indexOf("#{") != -1) {
res = format.replace(maskRE, function(match, cap) {
// Kikeressük van-e ilyen a locale-ban
let part = get(self.locale, cap);
if (part) {
if (isFunction(part)) {
part = part.call(self, ...args);
}

if (isArray(part))
// Ha tömböt kaptunk, akkor abból random választunk és rekurzívan hívunk,
// mert lehet abban is egy mask van
return self.populate(self.random.arrayElement(part), ...args);
else if (isString(part))
// Ha szöveg, akkor rekurzívan hívunk mert lehet az is mask
return self.populate(part, ...args);
else if (isNumber(part) || isObject(part))
// Ha szám, vagy objektum akkor visszatérünk
return part;
res = format.replace(maskRE, function(match, cap) {
// Kikeressük van-e ilyen a locale-ban
let part = get(self.locale, cap);
if (part) {
if (isFunction(part)) {
part = part.call(self, ...args);
}
// Ha nincs, akkor marad ami volt
return match;
});
}

if (isArray(part))
// Ha tömböt kaptunk, akkor abból random választunk és rekurzívan hívunk,
// mert lehet abban is egy mask van
return self.populate(self.random.arrayElement(part), ...args);
else if (isString(part))
// Ha szöveg, akkor rekurzívan hívunk mert lehet az is mask
return self.populate(part, ...args);
else if (isNumber(part) || isObject(part))
// Ha szám, vagy objektum akkor visszatérünk
return part;
}
// Ha nincs, akkor marad ami volt
return match;
});

// Replace symbols
if (isString(res))
Expand Down
25 changes: 13 additions & 12 deletions lib/locales/default/address/geoLocationNearBy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
module.export = function(coordinate, radius, isMetric) {
function randomFloat(min, max) {
return Math.random() * (max - min) + min;
}
module.exports = function(coordinate, radius, isMetric) {

function degreesToRadians(degrees) {
return degrees * (Math.PI / 180.0);
Expand All @@ -11,16 +8,16 @@ module.export = function(coordinate, radius, isMetric) {
return radians * (180.0 / Math.PI);
}

function kilometersToMiles(miles) {
function milesToKm(miles) {
return miles * 0.621371;
}

function coordinateWithOffset(coordinate, bearing, distance, isMetric) {
var R = 6378.137; // Radius of the Earth (http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html)
var d = isMetric ? distance : kilometersToMiles(distance); // Distance in km
var d = isMetric ? distance : milesToKm(distance); // Distance in km

var lat1 = degreesToRadians(coordinate[0]); //Current lat point converted to radians
var lon1 = degreesToRadians(coordinate[1]); //Current long point converted to radians
var lat1 = degreesToRadians(coordinate.latitude); //Current lat point converted to radians
var lon1 = degreesToRadians(coordinate.longitude); //Current long point converted to radians

var lat2 = Math.asin(Math.sin(lat1) * Math.cos(d / R) + Math.cos(lat1) * Math.sin(d / R) * Math.cos(bearing));

Expand All @@ -40,15 +37,19 @@ module.export = function(coordinate, radius, isMetric) {

// If there is no coordinate, the best we can do is return a random GPS coordinate.
if (coordinate === undefined) {
return [this.latitude(), this.longitude()]
return this.address.geoLocation();
}
radius = radius || 10.0;
isMetric = isMetric || false;
isMetric = isMetric || true;

// TODO: implement either a gaussian/uniform distribution of points in cicular region.
// Possibly include param to function that allows user to choose between distributions.

// This approach will likely result in a higher density of points near the center.
var randomCoord = coordinateWithOffset(coordinate, degreesToRadians(Math.random() * 360.0), radius, isMetric);
return [randomCoord[0].toFixed(4), randomCoord[1].toFixed(4)];
var randomCoord = coordinateWithOffset(coordinate, degreesToRadians(this.random.number(360)), radius, isMetric);

return {
latitude: randomCoord[0],
longitude: randomCoord[1]
}
}
1 change: 0 additions & 1 deletion lib/locales/default/date/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module.exports = {
timezone: require("./timezone"),

past(years, refDate) {
debugger;
let date = refDate ? new Date(Date.parse(refDate)) : new Date();
let min = 1000;
let max = (years || 1) * 365 * 24 * 3600 * 1000;
Expand Down
1 change: 0 additions & 1 deletion lib/locales/default/internet/domainSuffix.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module["exports"] = [
"org",
"biz",
"info",
"name",
"eu",
"co"
];
9 changes: 4 additions & 5 deletions lib/locales/default/internet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ module.exports = {
],

email(firstName, lastName) {
firstName = firstName ? firstName.toLowerCase() : "#{names.firstName}";
lastName = lastName ? lastName.toLowerCase() : "#{names.lastName}";
firstName = this.slugify(firstName ? firstName : this.populate("#{names.firstName}")).toLowerCase();
lastName = this.slugify(lastName ? lastName : this.populate("#{names.lastName}")).toLowerCase();

return [
`${firstName}.${lastName}@#{internet.emailDomain}`,
Expand Down Expand Up @@ -85,7 +85,7 @@ module.exports = {
ip() {
let parts = [];
for(let i = 0; i < 4; i++)
parts.push(this.random.number(255, 1));
parts.push(this.random.number(254, 1));

return parts.join(".");
},
Expand All @@ -98,8 +98,7 @@ module.exports = {
var redStr = red.toString(16);
var greenStr = green.toString(16);
var blueStr = blue.toString(16);
return '#' +
(redStr.length === 1 ? '0' : '') + redStr +
return /*'#' +*/ (redStr.length === 1 ? '0' : '') + redStr +
(greenStr.length === 1 ? '0' : '') + greenStr +
(blueStr.length === 1 ? '0': '') + blueStr;
}
Expand Down
38 changes: 37 additions & 1 deletion test/specs/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("Fakerator", () => {

expect(fakerator).to.be.exist;

expect(fakerator.locale).to.be.an("Object");
expect(fakerator.seed).to.be.an("Function");
expect(fakerator.random.number).to.be.an("Function");
expect(fakerator.random.boolean).to.be.an("Function");
Expand All @@ -26,6 +27,41 @@ describe("Fakerator", () => {
expect(fakerator.populate).to.be.an("Function");
});

// TODO: check locale, fallback
it("check 'default' locale", () => {

let fakerator = new Fakerator();

expect(fakerator).to.be.exist;
expect(fakerator.locale).to.be.an("Object");
expect(fakerator.locale._meta.id).to.be.equal("default");
expect(fakerator.locale._meta.language).to.be.equal("English");
expect(fakerator.locale._meta.country).to.be.equal("Great Britain");

});

it("check loading locale", () => {

let fakerator = new Fakerator("hu-HU");

expect(fakerator).to.be.exist;
expect(fakerator.locale).to.be.an("Object");
expect(fakerator.locale._meta.id).to.be.equal("hu-HU");
expect(fakerator.locale._meta.fallback).to.be.null;
expect(fakerator.locale._meta.language).to.be.equal("Hungarian");
expect(fakerator.locale._meta.country).to.be.equal("Hungary");
expect(fakerator.locale.names).to.be.an("Object");
expect(fakerator.locale.lorem).to.be.an("Object");
});

it("check fallback", () => {

let fakerator = new Fakerator("xy");

expect(fakerator).to.be.exist;
expect(fakerator.locale).to.be.an("Object");
expect(fakerator.locale._meta.id).to.be.equal("default");
expect(fakerator.locale.names).to.be.an("Object");
expect(fakerator.locale.lorem).to.be.an("Object");
});

})
79 changes: 79 additions & 0 deletions test/specs/locales/default/address.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { expect } from "chai";

import Fakerator from "lib/fakerator";

describe("Default address", () => {

let fakerator;

beforeEach( () => {
fakerator = new Fakerator();
fakerator.seed(8080);
});

it("check address.country", () => {
expect(fakerator.populate("#{address.country}")).to.be.equal("Qatar");
expect(fakerator.address.country()).to.be.equal("Bulgaria");
});

it("check address.city", () => {
expect(fakerator.populate("#{address.city}")).to.be.equal("Merlestad");
expect(fakerator.address.city()).to.be.equal("East Vernon");
});

it("check address.cityPrefix", () => {
expect(fakerator.populate("#{address.cityPrefix}")).to.be.equal("Lake");
expect(fakerator.address.cityPrefix()).to.be.equal("North");
});

it("check address.citySuffix", () => {
expect(fakerator.populate("#{address.citySuffix}")).to.be.equal("chester");
expect(fakerator.address.citySuffix()).to.be.equal("land");
});

it("check address.street", () => {
expect(fakerator.populate("#{address.street}")).to.be.equal("96214 Annette Radial Apt. 543");
expect(fakerator.address.street()).to.be.equal("64055 Jonathon Prairie Apt. 378");
});

it("check address.streetName", () => {
expect(fakerator.populate("#{address.streetName}")).to.be.equal("Weber Plain");
expect(fakerator.address.streetName()).to.be.equal("Gabriel Islands");
});

it("check address.streetSuffix", () => {
expect(fakerator.populate("#{address.streetSuffix}")).to.be.equal("Route");
expect(fakerator.address.streetSuffix()).to.be.equal("Crest");
});

it("check address.buildingNumber", () => {
expect(fakerator.populate("#{address.buildingNumber}")).to.be.equal("196");
expect(fakerator.address.buildingNumber()).to.be.equal("14353");
});

it("check address.postCode", () => {
expect(fakerator.populate("#{address.postCode}")).to.be.equal("19621-4353");
expect(fakerator.address.postCode()).to.be.equal("54360-6405");
});

it("check address.geoLocation", () => {
let res = fakerator.address.geoLocation();
expect(res).to.be.an("Object");
expect(res.latitude).to.be.closeTo(40.4233, 0.01);
expect(res.longitude).to.be.closeTo(-131.9741, 0.01);
});

it("check address.geoLocationNearBy", () => {
let res = fakerator.address.geoLocationNearBy({
latitude: 40,
longitude: 19
}, 100);
expect(res).to.be.an("Object");
expect(res.latitude).to.be.closeTo(39.85372, 0.01);
expect(res.longitude).to.be.closeTo(17.8442, 0.01);

});



})
24 changes: 24 additions & 0 deletions test/specs/locales/default/company.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from "chai";

import Fakerator from "lib/fakerator";

describe("Default company", () => {

let fakerator;

beforeEach( () => {
fakerator = new Fakerator();
fakerator.seed(8080);
});

it("check company.name", () => {
expect(fakerator.populate("#{company.name}")).to.be.equal("Weber, Gleichner and Kertzmann Inc.");
expect(fakerator.company.name()).to.be.equal("Orn-Johnson Inc.");
});

it("check company.suffix", () => {
expect(fakerator.populate("#{company.suffix}")).to.be.equal("LLC");
expect(fakerator.company.suffix()).to.be.equal("Ltd.");
});

})
43 changes: 43 additions & 0 deletions test/specs/locales/default/date.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect } from "chai";

import Fakerator from "lib/fakerator";

describe("Default date", () => {

let fakerator;

beforeEach( () => {
fakerator = new Fakerator();
fakerator.seed(8080);
});

it("check date.timezone", () => {
expect(fakerator.populate("#{date.timezone}")).to.be.equal("Asia/Bangkok");
expect(fakerator.date.timezone()).to.be.equal("America/Lima");
});

it("check date.past", () => {
//expect(fakerator.populate("#{date.past}")).to.be.an("Date");
expect(fakerator.date.past()).to.be.an("Date");
expect(fakerator.date.past(10)).to.be.an("Date");
});

it("check date.future", () => {
//expect(fakerator.populate("#{date.future}")).to.be.an("Date");
expect(fakerator.date.future()).to.be.an("Date");
expect(fakerator.date.future(10)).to.be.an("Date");
});

it("check date.between", () => {
//expect(fakerator.populate("#{date.between}")).to.be.an("Date");
expect(fakerator.date.between()).to.be.an("Date");
expect(fakerator.date.between(new Date(), new Date())).to.be.an("Date");
});

it("check date.recent", () => {
//expect(fakerator.populate("#{date.recent}")).to.be.an("Date");
expect(fakerator.date.recent()).to.be.an("Date");
expect(fakerator.date.recent(10)).to.be.an("Date");
});

})
22 changes: 22 additions & 0 deletions test/specs/locales/default/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect } from "chai";

import Fakerator from "lib/fakerator";

describe("Locale default", () => {

let fakerator;

beforeEach( () => {
fakerator = new Fakerator();
});

it("check locale definitions", () => {
expect(fakerator.locale.names).to.be.an("Object");
expect(fakerator.locale.phone).to.be.an("Object");
expect(fakerator.locale.address).to.be.an("Object");
expect(fakerator.locale.company).to.be.an("Object");
expect(fakerator.locale.internet).to.be.an("Object");
expect(fakerator.locale.lorem).to.be.an("Object");
expect(fakerator.locale.date).to.be.an("Object");
});
})
Loading

0 comments on commit fe4f94b

Please sign in to comment.