Skip to content

Commit

Permalink
Add the assert module
Browse files Browse the repository at this point in the history
  • Loading branch information
iBelieve committed Mar 28, 2016
1 parent ea06030 commit 39cfa6d
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docs/corejs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ The following pollyfills are included (links are to documentation, usually on MD
Core Modules
------------

- assert (exposed as Assert to qml)
- url (exposed as Url to qml)
- querystring (exposed as Querystring)
- punycode (exposed as Punycode)
- path (incomplete, exposed as Paths, not Path, because of the QtQuick.Path)
- fs (incomplete, exposed as FileSystem)
- fs (incomplete, exposed as Filesystem)

Usage
-----
Expand Down
29 changes: 29 additions & 0 deletions qmlify/patches/inherits/inherits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Index: inherits/inherits.js
===================================================================
--- inherits/inherits.js
+++ inherits/inherits.js
@@ -1,1 +1,23 @@
-module.exports = require('util').inherits
+if (typeof Object.create === 'function') {
+ // implementation from standard node.js 'util' module
+ module.exports = function inherits(ctor, superCtor) {
+ ctor.super_ = superCtor
+ ctor.prototype = Object.create(superCtor.prototype, {
+ constructor: {
+ value: ctor,
+ enumerable: false,
+ writable: true,
+ configurable: true
+ }
+ });
+ };
+} else {
+ // old school shim for old browsers
+ module.exports = function inherits(ctor, superCtor) {
+ ctor.super_ = superCtor
+ var TempCtor = function () {}
+ TempCtor.prototype = superCtor.prototype
+ ctor.prototype = new TempCtor()
+ ctor.prototype.constructor = ctor
+ }
+}
35 changes: 35 additions & 0 deletions qmlify/patches/util/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Index: util/util.js
===================================================================
--- util/util.js
+++ util/util.js
@@ -347,9 +347,9 @@

function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
var output = [];
for (var i = 0, l = value.length; i < l; ++i) {
- if (hasOwnProperty(value, String(i))) {
+ if (_hasOwnProperty(value, String(i))) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
String(i), true));
} else {
output.push('');
@@ -378,9 +378,9 @@
if (desc.set) {
str = ctx.stylize('[Setter]', 'special');
}
}
- if (!hasOwnProperty(visibleKeys, key)) {
+ if (!_hasOwnProperty(visibleKeys, key)) {
name = '[' + key + ']';
}
if (!str) {
if (ctx.seen.indexOf(desc.value) < 0) {
@@ -580,7 +580,7 @@
}
return origin;
};

-function hasOwnProperty(obj, prop) {
+function _hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
1 change: 1 addition & 0 deletions quickly/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"test": "qmlify -d test/build test && cp -r test/assets test/build/ && qmltestrunner -input test/build"
},
"dependencies": {
"assert": "^1.3.0",
"aurelia-polyfills": "^1.0.0-beta.1.0.6",
"es6-promise": "^3.1.2",
"url": "^0.11.0",
Expand Down
8 changes: 4 additions & 4 deletions quickly/src/nodejs/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <QFile>
#include <QTextStream>

QString FileSystem::readFile(const QString &path) const
QString Filesystem::readFile(const QString &path) const
{
QString resolvedPath = resolve(path);

Expand All @@ -28,7 +28,7 @@ QString FileSystem::readFile(const QString &path) const
return in.readAll();
}

QString FileSystem::resolve(const QString &pathOrUrl) const
QString Filesystem::resolve(const QString &pathOrUrl) const
{
if (pathOrUrl.startsWith("file://")) {
return pathOrUrl.right(pathOrUrl.length() - 7);
Expand All @@ -37,9 +37,9 @@ QString FileSystem::resolve(const QString &pathOrUrl) const
}
}

QObject *FileSystem::qmlSingleton(QQmlEngine *engine, QJSEngine *scriptEngine)
QObject *Filesystem::qmlSingleton(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(scriptEngine)

return new FileSystem(engine);
return new Filesystem(engine);
}
10 changes: 5 additions & 5 deletions quickly/src/nodejs/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef FILE_SYSTEM_H
#define FILE_SYSTEM_H
#ifndef FILESYSTEM_H
#define FILESYSTEM_H

#include "basemodule.h"

#include <QString>
#include <QQmlEngine>

class FileSystem : public BaseModule
class Filesystem : public BaseModule
{
Q_OBJECT

public:
FileSystem(QQmlEngine *engine) : BaseModule(engine) {}
Filesystem(QQmlEngine *engine) : BaseModule(engine) {}

Q_INVOKABLE QString readFile(const QString &path) const;
// Q_INVOKABLE void writeFile(const QString &path, const QString &data) const;
Expand All @@ -35,4 +35,4 @@ class FileSystem : public BaseModule
QString resolve(const QString &pathOrUrl) const;
};

#endif // FILE_SYSTEM_H
#endif // FILESYSTEM_H
2 changes: 1 addition & 1 deletion quickly/src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ void Plugin::registerTypes(const char *uri)

qDebug() << "Registering singletons...";

qmlRegisterSingletonType<FileSystem>(uri, 0, 1, "FileSystem", FileSystem::qmlSingleton);
qmlRegisterSingletonType<Filesystem>(uri, 0, 1, "Filesystem", Filesystem::qmlSingleton);
qmlRegisterSingletonType<Path>(uri, 0, 1, "Paths", Path::qmlSingleton);
}
3 changes: 2 additions & 1 deletion quickly/src/qmldir
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ Http 0.1 polyfills/http.js

Url 0.1 dependencies/url/url.js
Punycode 0.1 dependencies/punycode/punycode.js
QueryString 0.1 dependencies/querystring/index.js
Querystring 0.1 dependencies/querystring/index.js
Assert 0.1 dependencies/assert/assert.js
7 changes: 5 additions & 2 deletions quickly/src/quickly.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
"exports": {
"localstorage": "QtQuick.LocalStorage/LocalStorage 2.0",
"quickly-polyfills": "Quickly/Polyfills",
"fs": "Quickly/FileSystem",
"fs": "Quickly/Filesystem",
"path": "Quickly/Path",
"url": "Quickly/Url"
"url": "Quickly/Url",
"assert": "Quickly/Assert",
"querystring": "Quickly/Querystring",
"punycode": "Quickly/Punycode"
}
}
20 changes: 20 additions & 0 deletions quickly/test/nodejs/tst_assert.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import QtQuick 2.3
import QtTest 1.0
import Quickly 0.1

TestCase {
name: "AssertTests"

function test_assert_ok_when_true() {
Assert.ok(true)
}

function test_assert_ok_when_false_throws_exception() {
try {
Assert.fail(true)
fail("An error should have been thrown!")
} catch (error) {
// Do nothing
}
}
}
6 changes: 3 additions & 3 deletions quickly/test/nodejs/tst_fs.qml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import QtTest 1.0
import Quickly 0.1

TestCase {
name: "FileSystemTests"
name: "FilesystemTests"

function test_readFile_valid_file_works() {
compare(FileSystem.readFile(Qt.resolvedUrl("../assets/sample_file.txt")), "Sample File\n")
compare(Filesystem.readFile(Qt.resolvedUrl("../assets/sample_file.txt")), "Sample File\n")
}

function test_readFile_no_file_throws_exception() {
try {
FileSystem.readFile("missing_file.txt")
Filesystem.readFile("missing_file.txt")
fail("An error should have been thrown!")
} catch (error) {
compare(error.message, "File does not exist or is not readable: missing_file.txt")
Expand Down

0 comments on commit 39cfa6d

Please sign in to comment.