Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom mocha tests #468

Merged
merged 7 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dist/
node_modules
test/languages
test/generated/
website/public
website/themes
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ npm-debug.*
scripts/main.js
scripts/main.js.map
scripts/split-*.js
test/languages/
test/generated/
tools/_temp/*
website/_site/
website/.asset-cache
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ the hundreds of contributions across the many languages that Locutus is assimila
Ideas that will be planned and find their way into a release at one point

- [ ] Address the 25 remaining test failures that are currently skipped (find out which ones via
`npm run test:languages:noskip`)
`npm run test:generated:noskip`)
- [ ] Compare example test cases for PHP against `php -r` to make sure they are correctly mimicking the most recent
stable behavior
- [ ] Have _one_ way of checking pure JS arrays vs PHP arrays (vs:
Expand Down
36 changes: 23 additions & 13 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ for fun experiments such as running PHP code directly in Node.js
- [Code](#code)
- [Build](#build)
- [Test](#test)
- [Generated tests](#generated-tests)
- [Running a single generated test](#running-a-single-generated-test)
- [Custom tests](#custom-tests)
- [Browser Playground](#browser-playground)
- [Commit](#commit)
- [Releasing](#releasing)
- [Website Development](#website-development)
Expand Down Expand Up @@ -100,32 +104,38 @@ yarn build

## Test

```bash
yarn test
```

Single out one function: `natsort`
### Generated tests

```bash
TEST_GREP=natsort yarn test:languages
yarn test
```

This first rewrites mocha test-cases based on `example` and `result` comments found in the function's headers. This is
This first rewrites mocha test-cases based on `example` and `result` comments found in the functions' headers. This is
useful if you're changing the tests themselves as well.

If that's not needed as you're iterating purely on the implementation, here's a faster way of singeling out `natsort`.
This re-uses an already generated mocha test:
### Running a single generated test

If that's not needed as you're iterating purely on the implementation, here's a faster way of singeling out a function
like `natsort` which re-uses an already generated Mocha test in Watch mode, so it gets executed as you change the
implementation file in `src/`:

```bash
env DEBUG=locutus:* ./node_modules/.bin/mocha \
./node_modules/.bin/mocha \
--require babel-register \
--reporter spec \
test/languages/php/array/test-natsort.js
--watch \
test/generated/php/array/test-natsort.js
```

By interfacing with Mocha directly you could also enable watch mode by adding `--watch`.
### Custom tests

As of v2.0.30 you can also write custom tests, an example can be found in
[`src/php/var/serialize.mocha.js`](src/php/var/serialize.mocha.js).

### Browser Playground

And there's a way to test functions inside Browsers with watching, via `yarn browser:watch`.
Aside from unit tests, you can also run functions inside web browsers, with Watching, via `yarn browser:watch`. This
allows you to quickly iterate and see how your functions behave in browsers.

## Commit

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"browser:watch": "budo test/browser/app.js --live --serve test/browser/bundle.js",
"build:dist": "babel src --out-dir dist --source-maps && cp package.json dist/",
"build:indices": "babel-node src/_util/cli.js reindex",
"build:tests:noskip": "rimraf test/languages && babel-node src/_util/cli.js writetests --noskip",
"build:tests": "rimraf test/languages && babel-node src/_util/cli.js writetests",
"build:tests:noskip": "rimraf test/generated && babel-node src/_util/cli.js writetests --noskip",
"build:tests": "rimraf test/generated && babel-node src/_util/cli.js writetests",
"build": "npm-run-all 'build:*'",
"injectweb": "rimraf website/source/{c,golang,php,python,ruby} && babel-node src/_util/cli.js injectweb",
"fix:js": "DEBUG=eslint:cli-engine eslint --fix --quiet --ext .js,.jsx,.ts,.tsx .",
Expand All @@ -42,11 +42,11 @@
"lint:js": "eslint --ext .js,.jsx,.ts,.tsx .",
"lint": "npm-run-all --parallel 'lint:**'",
"playground:start": "cd test/browser && babel-node server.js",
"test:languages:noskip": "yarn build:tests:noskip && cross-env DEBUG='locutus:*' mocha --require babel-register --reporter spec --recursive test/languages --grep \"${TEST_GREP:-}\"",
"test:languages": "yarn build:tests && cross-env DEBUG='locutus:*' mocha --require babel-register --reporter spec --recursive test/languages --grep \"${TEST_GREP:-}\"",
"test:generated:noskip": "yarn build:tests:noskip && cross-env DEBUG='locutus:*' mocha --require babel-register --reporter spec 'src/**/*.mocha.js' 'test/**/test-*.js'",
"test:languages": "yarn build:tests && cross-env DEBUG='locutus:*' mocha --require babel-register --reporter spec 'src/**/*.mocha.js' 'test/**/test-*.js'",
"test:module": "babel-node test/module/module.js",
"test:util": "mocha --require babel-register --reporter spec test/util/",
"test": "npm-run-all test:languages test:util test:module",
"test": "npm-run-all test:generated test:util test:module",
"website:install": "cd website && yarn",
"website:deploy": "cd website && yarn deploy",
"website:start": "cd website && yarn server",
Expand Down
2 changes: 1 addition & 1 deletion src/_util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class Util {

const basename = path.basename(params.filepath)
const subdir = path.dirname(params.filepath)
const testpath = this.__test + '/languages/' + subdir + '/test-' + basename
const testpath = this.__test + '/generated/' + subdir + '/test-' + basename
const testdir = path.dirname(testpath)
const relativeSrcForTestDir = path.relative(testdir, self.__src)
const relativeTestFileForRoot = path.relative(self.__root, testpath)
Expand Down
11 changes: 11 additions & 0 deletions src/php/var/serialize.mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const expect = require('chai').expect
const serialize = require('./serialize.js')

describe('src/php/var/serialize.js', function () {
it('should pass example 1', function (done) {
const expected = 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
const result = serialize(['Kevin', 'van', 'Zonneveld'])
expect(result).to.deep.equal(expected)
done()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ process.env.TZ = 'UTC'
var expect = require('chai').expect
var abs = require('../../../../src/c/math/abs.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/c/math/abs.js (tested in test/languages/c/math/test-abs.js)', function () {
describe('src/c/math/abs.js (tested in test/generated/c/math/test-abs.js)', function () {
it('should pass example 1', function (done) {
var expected = 4.2
var result = abs(4.2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ process.env.TZ = 'UTC'
var expect = require('chai').expect
var frexp = require('../../../../src/c/math/frexp.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/c/math/frexp.js (tested in test/languages/c/math/test-frexp.js)', function () {
describe('src/c/math/frexp.js (tested in test/generated/c/math/test-frexp.js)', function () {
it('should pass example 1', function (done) {
var expected = [0.5, 1]
var result = frexp(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ process.env.TZ = 'UTC'
var expect = require('chai').expect
var sprintf = require('../../../../src/c/stdio/sprintf.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/c/stdio/sprintf.js (tested in test/languages/c/stdio/test-sprintf.js)', function () {
describe('src/c/stdio/sprintf.js (tested in test/generated/c/stdio/test-sprintf.js)', function () {
it('should pass example 1', function (done) {
var expected = ' +00001'
var result = sprintf('%+10.*d', 5, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ process.env.TZ = 'UTC'
var expect = require('chai').expect
var Contains = require('../../../../src/golang/strings/Contains.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/golang/strings/Contains.js (tested in test/languages/golang/strings/test-Contains.js)', function () {
describe('src/golang/strings/Contains.js (tested in test/generated/golang/strings/test-Contains.js)', function () {
it('should pass example 1', function (done) {
var expected = true
var result = Contains('Kevin', 'K')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ process.env.TZ = 'UTC'
var expect = require('chai').expect
var Count = require('../../../../src/golang/strings/Count.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/golang/strings/Count.js (tested in test/languages/golang/strings/test-Count.js)', function () {
describe('src/golang/strings/Count.js (tested in test/generated/golang/strings/test-Count.js)', function () {
it('should pass example 1', function (done) {
var expected = 3
var result = Count("cheese", "e")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ process.env.TZ = 'UTC'
var expect = require('chai').expect
var Index = require('../../../../src/golang/strings/Index2.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/golang/strings/Index2.js (tested in test/languages/golang/strings/test-Index2.js)', function () {
describe('src/golang/strings/Index2.js (tested in test/generated/golang/strings/test-Index2.js)', function () {
it('should pass example 1', function (done) {
var expected = 0
var result = Index('Kevin', 'K')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ process.env.TZ = 'UTC'
var expect = require('chai').expect
var LastIndex = require('../../../../src/golang/strings/LastIndex.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/golang/strings/LastIndex.js (tested in test/languages/golang/strings/test-LastIndex.js)', function () {
describe('src/golang/strings/LastIndex.js (tested in test/generated/golang/strings/test-LastIndex.js)', function () {
it('should pass example 1', function (done) {
var expected = 3
var result = LastIndex('go gopher', 'go')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var _bc = require('../../../../src/php/_helpers/_bc.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/_helpers/_bc.js (tested in test/languages/php/_helpers/test-_bc.js)', function () {
describe('src/php/_helpers/_bc.js (tested in test/generated/php/_helpers/test-_bc.js)', function () {
it('should pass example 1', function (done) {
var expected = '+'
var $bc = _bc()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var _phpCastString = require('../../../../src/php/_helpers/_phpCastString.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/_helpers/_phpCastString.js (tested in test/languages/php/_helpers/test-_phpCastString.js)', function () {
describe('src/php/_helpers/_phpCastString.js (tested in test/generated/php/_helpers/test-_phpCastString.js)', function () {
it('should pass example 1', function (done) {
var expected = '1'
var result = _phpCastString(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var _php_cast_float = require('../../../../src/php/_helpers/_php_cast_float.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/_helpers/_php_cast_float.js (tested in test/languages/php/_helpers/test-_php_cast_float.js)', function () {
describe('src/php/_helpers/_php_cast_float.js (tested in test/generated/php/_helpers/test-_php_cast_float.js)', function () {
it('should pass example 1', function (done) {
var expected = 0
var result = _php_cast_float(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var _php_cast_int = require('../../../../src/php/_helpers/_php_cast_int.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/_helpers/_php_cast_int.js (tested in test/languages/php/_helpers/test-_php_cast_int.js)', function () {
describe('src/php/_helpers/_php_cast_int.js (tested in test/generated/php/_helpers/test-_php_cast_int.js)', function () {
it('should pass example 1', function (done) {
var expected = 0
var result = _php_cast_int(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var array_change_key_case = require('../../../../src/php/array/array_change_key_case.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/array/array_change_key_case.js (tested in test/languages/php/array/test-array_change_key_case.js)', function () {
describe('src/php/array/array_change_key_case.js (tested in test/generated/php/array/test-array_change_key_case.js)', function () {
it('should pass example 1', function (done) {
var expected = false
var result = array_change_key_case(42)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var array_chunk = require('../../../../src/php/array/array_chunk.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/array/array_chunk.js (tested in test/languages/php/array/test-array_chunk.js)', function () {
describe('src/php/array/array_chunk.js (tested in test/generated/php/array/test-array_chunk.js)', function () {
it('should pass example 1', function (done) {
var expected = [['Kevin', 'van'], ['Zonneveld']]
var result = array_chunk(['Kevin', 'van', 'Zonneveld'], 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var array_column = require('../../../../src/php/array/array_column.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/array/array_column.js (tested in test/languages/php/array/test-array_column.js)', function () {
describe('src/php/array/array_column.js (tested in test/generated/php/array/test-array_column.js)', function () {
it('should pass example 1', function (done) {
var expected = {0: "Alex", 1: "Elvis", 2: "Michael"}
var result = array_column([{name: 'Alex', value: 1}, {name: 'Elvis', value: 2}, {name: 'Michael', value: 3}], 'name')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var array_combine = require('../../../../src/php/array/array_combine.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/array/array_combine.js (tested in test/languages/php/array/test-array_combine.js)', function () {
describe('src/php/array/array_combine.js (tested in test/generated/php/array/test-array_combine.js)', function () {
it('should pass example 1', function (done) {
var expected = {0: 'kevin', 1: 'van', 2: 'zonneveld'}
var result = array_combine([0,1,2], ['kevin','van','zonneveld'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var array_count_values = require('../../../../src/php/array/array_count_values.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/array/array_count_values.js (tested in test/languages/php/array/test-array_count_values.js)', function () {
describe('src/php/array/array_count_values.js (tested in test/generated/php/array/test-array_count_values.js)', function () {
it('should pass example 1', function (done) {
var expected = {3:2, 5:1, "foo":2, "bar":1}
var result = array_count_values([ 3, 5, 3, "foo", "bar", "foo" ])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var array_diff = require('../../../../src/php/array/array_diff.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/array/array_diff.js (tested in test/languages/php/array/test-array_diff.js)', function () {
describe('src/php/array/array_diff.js (tested in test/generated/php/array/test-array_diff.js)', function () {
it('should pass example 1', function (done) {
var expected = {0:'Kevin'}
var result = array_diff(['Kevin', 'van', 'Zonneveld'], ['van', 'Zonneveld'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var array_diff_assoc = require('../../../../src/php/array/array_diff_assoc.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/array/array_diff_assoc.js (tested in test/languages/php/array/test-array_diff_assoc.js)', function () {
describe('src/php/array/array_diff_assoc.js (tested in test/generated/php/array/test-array_diff_assoc.js)', function () {
it('should pass example 1', function (done) {
var expected = {1: 'van', 2: 'Zonneveld'}
var result = array_diff_assoc({0: 'Kevin', 1: 'van', 2: 'Zonneveld'}, {0: 'Kevin', 4: 'van', 5: 'Zonneveld'})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var array_diff_key = require('../../../../src/php/array/array_diff_key.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/array/array_diff_key.js (tested in test/languages/php/array/test-array_diff_key.js)', function () {
describe('src/php/array/array_diff_key.js (tested in test/generated/php/array/test-array_diff_key.js)', function () {
it('should pass example 1', function (done) {
var expected = {"green":2, "blue":3, "white":4}
var result = array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var array_diff_uassoc = require('../../../../src/php/array/array_diff_uassoc.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/array/array_diff_uassoc.js (tested in test/languages/php/array/test-array_diff_uassoc.js)', function () {
describe('src/php/array/array_diff_uassoc.js (tested in test/generated/php/array/test-array_diff_uassoc.js)', function () {
it.skip('should pass example 1', function (done) {
var expected = {b: 'brown', c: 'blue', 0: 'red'}
var $array1 = {a: 'green', b: 'brown', c: 'blue', 0: 'red'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ini_set = require('../../../../src/php/info/ini_set') // eslint-disable-line
var ini_get = require('../../../../src/php/info/ini_get') // eslint-disable-line no-unused-vars,camelcase
var array_diff_ukey = require('../../../../src/php/array/array_diff_ukey.js') // eslint-disable-line no-unused-vars,camelcase

describe('src/php/array/array_diff_ukey.js (tested in test/languages/php/array/test-array_diff_ukey.js)', function () {
describe('src/php/array/array_diff_ukey.js (tested in test/generated/php/array/test-array_diff_ukey.js)', function () {
it('should pass example 1', function (done) {
var expected = {red: 2, purple: 4}
var $array1 = {blue: 1, red: 2, green: 3, purple: 4}
Expand Down
Loading
Loading