Skip to content

Commit

Permalink
add simple testsuite asserting js input / php output
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Oct 25, 2014
1 parent f61b73f commit 1c7e6f8
Show file tree
Hide file tree
Showing 38 changed files with 594 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
node_modules
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ What does it converts?
- Function.prototype.call
- Date (missing)

Testing
---

Tests are simple input (js) / output (php) comparisions.

1. Create your source `.js` file at `test/fixtures/js_feature.js`
2. Convert your `.js` to `.php` manually: `node test/generate.js js_feature.js`
3. Run `npm test`

License
---

Expand Down
1 change: 1 addition & 0 deletions examples/expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var result = (((6 * 2) / 14) * 32 ) / 4;
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
},
"keywords": [ "javascript", "php", "transpiler", "transcompiler" ],
"devDependencies": {
"browserify": "~6.0.*"
"browserify": "~6.0.*",
"mocha": "~2.0.1",
"assert": "~1.1.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha ./test/suite.js"
},
"author": "Endel Dreyer",
"license": "MIT"
Expand Down
54 changes: 54 additions & 0 deletions test/fixtures/class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class ClassExample {

constructor(something) {
this._something = something
}

toArray(model, array) {
return array;
}

hello() {
return "Hello world";
}

get something() {
return "Something: " + this._something;
}

set something(value) {
this._something = value + 10;
}

creating(model) {
Mail.send({
body: Module.template('signup-confirmation').compile({
base_url: AppConfig.get("retrieve.email.url")
}),
subject: "Sign-up confirmation",
to: model.email,
from: "somebody@example.com"
});
}

updating(model) {
if (model.isDirty('status') && model.status == 1) {

Mail.send({
body: Module.template('signup-approved').compile({
BASE_URL: AppConfig.get("retrieve.email.url")
}),
subject: "Approved!",
to: model.email,
from: "somebody@example.com"
});

}
}

}

// Instantiate class and call getter method
example = new ClassExample("awesome");
var_dump(example.hello.apply());
var_dump(example.hello());
34 changes: 34 additions & 0 deletions test/fixtures/class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
class ClassExample
{
public function __construct($something) {
$this->_something = $something;
}
public function toArray($model, $array) {
return $array;
}
public function hello() {
return "Hello world";
}
public function creating($model) {
Mail::send(array("body" => Module::template('signup-confirmation')->compile(array("base_url" => AppConfig::get("retrieve.email.url"))), "subject" => "Sign-up confirmation", "to" => $model->email, "from" => "somebody@example.com"));
}
public function updating($model) {
if ($model->isDirty('status') && $model->status == 1) {
Mail::send(array("body" => Module::template('signup-approved')->compile(array("BASE_URL" => AppConfig::get("retrieve.email.url"))), "subject" => "Approved!", "to" => $model->email, "from" => "somebody@example.com"));
}}
function __get($_property) {
if ($_property === 'something') {
return "Something: " + $this->_something;
}
}
function __set($_property, $value) {
if ($_property === 'something') {
$this->_something = $value + 10;
}
}

}
$example = new ClassExample("awesome");
var_dump(call_user_func_array(array($example, 'hello'), array()));
var_dump($example->hello());
10 changes: 10 additions & 0 deletions test/fixtures/closures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
d = 10;
var closure = function(a) {
var c = 1;
return function(b) {
return a + b + c + d;
}
}

calling = closure(5);
var_dump(calling(4) == 20);
12 changes: 12 additions & 0 deletions test/fixtures/closures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
$d = 10;
$closure = function ($a) use (&$d) {
$c = 1;
return function ($b) use (&$a, &$c, &$d) {
return $a + $b + $c + $d;
}
;
}
;
$calling = $closure(5);
var_dump($calling(4) == 20);
24 changes: 24 additions & 0 deletions test/fixtures/conditionals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var x = 1, y = 50;
var ternary = (x == 1) ? "Yes" : "No"

var_dump(ternary);

var_dump( x < y )
var_dump( x == 2 )

if (x < y) {
echo("x < y");
} else if (x == 2) {
echo("x==2");
} else {
echo("else...");
}

switch (x) {
case 5:
echo("Five!");
case 1:
echo("One!");
default:
echo("Default!");
}
22 changes: 22 additions & 0 deletions test/fixtures/conditionals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
$x = 1;
$y = 50;
$ternary = ($x == 1) ? "Yes" : "No";
var_dump($ternary);
var_dump($x < $y);
var_dump($x == 2);
if ($x < $y) {
echo("x < y");
} else if ($x == 2) {
echo("x==2");
} else {echo("else...");
}switch ($x){case 5:
echo("Five!");

case 1:
echo("One!");

default:
echo("Default!");

}
13 changes: 13 additions & 0 deletions test/fixtures/core_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var items = ["One", "Two", "Three"];

items.unshift("Zero");
items.shift();
items.push("Four");

var_dump(items);
echo(items.join(", "))

echo(items.length)
echo(items.indexOf({name: "Three"}))
echo(items.join(", "))

10 changes: 10 additions & 0 deletions test/fixtures/core_array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
$items = array("One", "Two", "Three");
array_unshift($items, "Zero");
array_shift($items);
array_push($items, "Four");
var_dump($items);
echo(join(", ", $items));
echo(count($items));
echo(array_search(array("name" => "Three"), $items));
echo(join(", ", $items));
23 changes: 23 additions & 0 deletions test/fixtures/core_function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Klass {
without_params() {
return "I'm a method without params."
}

with_default_params(param1="default") {
return param1;
}
}

function global_function() {
return "I'm a global function";
}

function global_function_with_params() {
return "I'm a global function with params";
}

klass = new Klass
var_dump(klass.without_params.apply())
var_dump(klass.with_default_params.apply(null))
var_dump(klass.with_default_params.apply(null, ["Hey!"]))
var_dump(klass.with_default_params.call(null, "Hey!"))
22 changes: 22 additions & 0 deletions test/fixtures/core_function.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
class Klass
{
public function without_params() {
return "I'm a method without params.";
}
public function with_default_params($param1 = "default") {
return $param1;
}

}
function global_function() {
return "I'm a global function";
}
function global_function_with_params() {
return "I'm a global function with params";
}
$klass = new Klass();
var_dump(call_user_func_array(array($klass, 'without_params'), array()));
var_dump(call_user_func_array(array($klass, 'with_default_params'), array()));
var_dump(call_user_func_array(array($klass, 'with_default_params'), array("Hey!")));
var_dump(call_user_func(array($klass, 'with_default_params'), "Hey!"));
12 changes: 12 additions & 0 deletions test/fixtures/core_json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
JSON.stringify({
integer: 5,
string: "hey",
nested: {objects:{here: "yey"}}
});

var_dump(JSON.stringify({
integer: 5,
string: "hey",
nested: { objects: { here: "yey" } }
}));

3 changes: 3 additions & 0 deletions test/fixtures/core_json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
json_encode(array("integer" => 5, "string" => "hey", "nested" => array("objects" => array("here" => "yey"))));
var_dump(json_encode(array("integer" => 5, "string" => "hey", "nested" => array("objects" => array("here" => "yey")))));
4 changes: 4 additions & 0 deletions test/fixtures/core_math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var_dump(Math.E);
var_dump(Math.round(5.55557));

var_dump( Math.sin(5 + 8 * Math.sqrt(200)) * Math.PI )
4 changes: 4 additions & 0 deletions test/fixtures/core_math.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
var_dump(M_E);
var_dump(round(5.55557));
var_dump(sin(5 + 8 * sqrt(200)) * M_PI);
18 changes: 18 additions & 0 deletions test/fixtures/core_string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var str = "Hello";
echo(" trimmed ".trim().trim().substr(1).trim().trim().trim());

var_dump("hello".replace("lo", "ium").toUpperCase())

var_dump("something".toUpperCase())
var_dump("something".indexOf("meth"))

echo(" trimmed".trimLeft());
echo("trimmed ".trimRight());

echo(str.toUpperCase().substr(1));
echo(str.toLowerCase());
echo(str.substr(1));

if (str.match(/endel/)) {
var_dump(str);
}
14 changes: 14 additions & 0 deletions test/fixtures/core_string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
$str = "Hello";
echo(trim(trim(trim(substr(trim(trim(" trimmed ")), 1)))));
var_dump(strtoupper(str_replace("lo", "ium", "hello")));
var_dump(strtoupper("something"));
var_dump(strpos("something", "meth"));
echo(ltrim(" trimmed"));
echo(rtrim("trimmed "));
echo(substr(strtoupper($str), 1));
echo(strtolower($str));
echo(substr($str, 1));
if (preg_match('/endel/', $str)) {
var_dump($str);
}
1 change: 1 addition & 0 deletions test/fixtures/expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var result = (((6 * 2) / 14) * 32 ) / 4;
2 changes: 2 additions & 0 deletions test/fixtures/expression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$result = 6 * 2 / 14 * 32 / 4;
17 changes: 17 additions & 0 deletions test/fixtures/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

function something(x, y = "something", z = 5) {
var_dump(x, y, z);
}

function sum(a, b) {
return a + b;
}

function hello(a, b) {
return "hello!";
}

var_dump(hello.apply(hello, [5,6]))
var_dump(hello(5, 6))

// var_dump(something(5), sum(1,2))
12 changes: 12 additions & 0 deletions test/fixtures/function.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
function something($x, $y = "something", $z = 5) {
var_dump($x, $y, $z);
}
function sum($a, $b) {
return $a + $b;
}
function hello($a, $b) {
return "hello!";
}
var_dump(call_user_func_array('hello', array(5, 6)));
var_dump(hello(5, 6));
Loading

0 comments on commit 1c7e6f8

Please sign in to comment.