Skip to content

Commit

Permalink
add more php function and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
puritys committed Nov 25, 2014
1 parent 138a635 commit a6b96d7
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 7 deletions.
17 changes: 17 additions & 0 deletions nodejs/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var casting = require('./casting_type.js');

exports.array_merge = function (arr1, arr2) {
var res, pro;
if (casting.is_array(arr1) && casting.is_array(arr2)) {
res = arr1.concat(arr2);
} else if (casting.is_object(arr1) && casting.is_object(arr2)) {
res = {};
for(pro in arr1) {
res[pro] = arr1[pro];
}
for(pro in arr2) {
res[pro] = arr2[pro];
}
}
return res;
};
29 changes: 29 additions & 0 deletions tests/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var phplikeMod = require('./include.js');
var assert = require("assert");

//mocha lib/ --grep mthod_get
describe('Test method: array_merge', function() {
it('merge index array', function() {
var arr1 = [1], arr2 = [2];
var res = phplikeMod.array_merge(arr1, arr2);
assert.equal(1, res[0]);
assert.equal(2, res[1]);
assert.equal("undefined", typeof(arr1[1]));


});
it('merge assoc array', function() {
var arr1 = {"a": "b"}, arr2 = {"c": "d"};
var res = phplikeMod.array_merge(arr1, arr2);

assert.equal("b", res["a"]);
assert.equal("d", res["c"]);
assert.equal("undefined", typeof(arr1["c"]));


});


});


9 changes: 2 additions & 7 deletions tests/base64_encode.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
//s = base64_decode(s);
//print_r(s);

var phplikeMod = require('./include.js');


var assert = require("assert");

//mocha lib/ --grep mthod_get
Expand All @@ -13,12 +8,12 @@ describe('base64', function() {
it('encode', function() {
var res = phplikeMod.base64_encode(text);
assert.equal(encodeText, res);
})
});

it('decode', function() {
var res = phplikeMod.base64_decode(encodeText);
assert.equal(text, res);
})
});

});

Expand Down
38 changes: 38 additions & 0 deletions tests/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,42 @@ describe('Test function: time', function() {

});

describe('Test function: parse_str', function() {
it('string to object', function() {
var str = "a=b&c=d";
var res = phplikeMod.parse_str(str);
assert.equal("b", res['a']);
assert.equal("d", res['c']);



})

});

describe('Test function: clone', function() {
it('clone a object(assoc array)', function() {
var obj = {"a": "aa"};
var res = phplikeMod.clone(obj);
res["a"] = "bb";

assert.equal("bb", res['a']);
assert.equal("aa", obj['a']);
});

it('clone a array', function() {
var obj = [1];
var res = phplikeMod.clone(obj);
res[0] = 5;

assert.equal(1, res.length);

assert.equal(5, res[0]);
assert.equal(1, obj[0]);
});



});


33 changes: 33 additions & 0 deletions tests/curl.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,37 @@ describe('Test method: curl_setopt', function() {



describe('Test method: reformatCurlData', function() {

it('get param from url', function() {
var c = phplikeMod.curl_init();
var url = "http://www.google.com.tw/?a=b&a1=cc";
c.url = url;
c.param = "c=d&c1=dd";
var res = phplikeMod.reformatCurlData(c);
assert.equal("b", res.param["a"]);
assert.equal("d", res.param["c"]);
assert.equal("cc", res.param["a1"]);
assert.equal("dd", res.param["c1"]);
assert.equal("http://www.google.com.tw/", res.url);
assert.equal(url, c.url);



});

it('get param from url and merge param which type is object', function() {
var c = phplikeMod.curl_init();
c.url = "http://www.google.com.tw/?a=b&a1=cc";
c.param = {"c": "d", "c1": "dd"};
var res = phplikeMod.reformatCurlData(c);
assert.equal("b", res.param["a"]);
assert.equal("d", res.param["c"]);
assert.equal("cc", res.param["a1"]);
assert.equal("dd", res.param["c1"]);

});

});


1 change: 1 addition & 0 deletions tests/include.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//require('./../nodejs/index.js');
global.UNIT_TEST = true;

var module = require('./../module.js');

Expand Down

0 comments on commit a6b96d7

Please sign in to comment.