Permalink
Browse files

Merge branch 'master' into multi-struct-fail

Conflicts:
	js/rx/test/runner.js
	perl/t/lib/Test/RxSpec.pm
	php/Rx.php
	php/rx-test.php
	util/runtests.pl
  • Loading branch information...
2 parents 5850975 + 9f099e9 commit d7898299ff833e6ec1ddbce97158aff3cbfba2ef Ronald J Kimball committed Apr 5, 2012
View
@@ -12,36 +12,61 @@ Rx.prototype.expand_uri = function (name) {
var matches = name.match(/^\/(\w*)\/(\w+)$/);
if (! matches)
- throw "couldn't understand type name '" + name + "'";
+ throw new Rx.Error("couldn't understand type name '" + name + "'");
if (! this.prefix_registry[ matches[1] ])
- throw "unknown prefix '" + matches[1] + "' in type name '" + name + "'";
+ throw new Rx.Error("unknown prefix '" + matches[1] + "' in type name '" + name + "'");
return this.prefix_registry[ matches[1] ] + matches[2];
}
+Rx.prototype.addPrefix = function (name, base) {
+ if (this.prefix_registry[ name ])
+ throw new Rx.Error("the prefix '" + name + "' is already registered");
+
+ this.prefix_registry[ name ] = base;
+}
+
Rx.prototype.registerType = function (type, opt) {
var uri = type.uri;
if (this.type_registry[ uri ])
- throw "tried to register type for already-registered uri " + uri;
+ throw new Rx.Error("tried to register type for already-registered uri " + uri);
this.type_registry[ uri ] = type;
};
+Rx.prototype.learnType = function (uri, schema) {
+ if (this.type_registry[ uri ])
+ throw new Rx.Error("tried to learn type for already-registered uri " + uri);
+
+ // make sure schema is valid
+ // should this be in a try/catch?
+ this.makeSchema(schema);
+
+ this.type_registry[ uri ] = { schema: schema };
+};
+
Rx.prototype.typeFor = function (typeName) {
var uri = this.expand_uri(typeName);
var typeChecker = this.type_registry[ uri ];
- if (! typeChecker) throw 'unknown type: ' + uri;
+ if (! typeChecker) throw new Rx.Error('unknown type: ' + uri);
return typeChecker;
};
Rx.prototype.makeSchema = function (schema) {
if (schema.constructor == String) schema = { type: schema };
var typeChecker = this.typeFor(schema.type);
- return new typeChecker(schema, this);
+
+ if (typeof(typeChecker) == 'object') {
+ if (! Rx.Util._x_subset_keys_y(schema, { type: true }))
+ throw new Rx.Error('composed type does not take check arguments');
+ return this.makeSchema(typeChecker.schema);
+ } else {
+ return new typeChecker(schema, this);
+ }
};
Rx.Error = function (message) { this.message = message };
View
@@ -5,6 +5,9 @@ Rx.CoreType = {};
// Simple types
Rx.CoreType.allType = function (opt, rx) {
+ if (! Rx.Util._x_subset_keys_y(opt, {type: true, of: true }))
+ throw new Rx.Error('unknown argument for all type');
+
if (! opt.of) throw new Rx.Error('no of given for //all');
if (opt.of.length == 0)
@@ -21,6 +24,9 @@ Rx.CoreType.allType.prototype.check = function (v) {
};
Rx.CoreType.anyType = function (opt, rx) {
+ if (! Rx.Util._x_subset_keys_y(opt, {type: true, of: true }))
+ throw new Rx.Error('unknown argument for any type');
+
this.alts = null;
if (opt.of) {
if (opt.of.length == 0)
@@ -38,17 +44,26 @@ Rx.CoreType.anyType.prototype.check = function (v) {
return false;
};
-Rx.CoreType.boolType = function (opt) {};
+Rx.CoreType.boolType = function (opt) {
+ if (! Rx.Util._x_subset_keys_y(opt, {type: true }))
+ throw new Rx.Error('unknown argument for bool type');
+};
Rx.CoreType.boolType.uri = 'tag:codesimply.com,2008:rx/core/bool';
Rx.CoreType.boolType.prototype.check = function (v) {
return((typeof(v) == 'boolean') || (v instanceof Boolean));
};
-Rx.CoreType.defType = function (opt) {};
+Rx.CoreType.defType = function (opt) {
+ if (! Rx.Util._x_subset_keys_y(opt, {type: true }))
+ throw new Rx.Error('unknown argument for def type');
+};
Rx.CoreType.defType.uri = 'tag:codesimply.com,2008:rx/core/def';
Rx.CoreType.defType.prototype.check = function (v) { return v != null; };
-Rx.CoreType.failType = function (opt) {};
+Rx.CoreType.failType = function (opt) {
+ if (! Rx.Util._x_subset_keys_y(opt, {type: true }))
+ throw new Rx.Error('unknown argument for fail type');
+};
Rx.CoreType.failType.uri = 'tag:codesimply.com,2008:rx/core/fail';
Rx.CoreType.failType.prototype.check = function (v) { false; };
@@ -77,7 +92,10 @@ Rx.CoreType.intType.prototype.check = function (v) {
return true;
};
-Rx.CoreType.nilType = function (opt) {};
+Rx.CoreType.nilType = function (opt) {
+ if (! Rx.Util._x_subset_keys_y(opt, {type: true }))
+ throw new Rx.Error('unknown argument for nil type');
+};
Rx.CoreType.nilType.uri = 'tag:codesimply.com,2008:rx/core/nil';
Rx.CoreType.nilType.prototype.check = function (v) { return v === null };
@@ -105,21 +123,29 @@ Rx.CoreType.numType.prototype.check = function (v) {
};
Rx.CoreType.strType = function (opt) {
- if (! Rx.Util._x_subset_keys_y(opt, {type: true, value: true }))
+ if (! Rx.Util._x_subset_keys_y(opt, {type: true, length: true, value: true }))
throw new Rx.Error('unknown argument for str type');
if (typeof(opt.value) != "undefined")
if (opt.value.constructor != String)
throw new Rx.Error('invalid value parameter for str type');
this.value = opt.value;
+
+ if (opt.length) {
+ this.length_check = new Rx.Util.RangeChecker( opt.length );
+ }
};
Rx.CoreType.strType.uri = 'tag:codesimply.com,2008:rx/core/str';
Rx.CoreType.strType.prototype.check = function (v) {
if (! ((typeof(v) == 'string') || (v instanceof String))) return false;
+ if (this.length_check && ! this.length_check.check(v.length)) return false;
if (this.value != null && v != this.value) return false;
return true;
};
-Rx.CoreType.oneType = function (opt) {};
+Rx.CoreType.oneType = function (opt) {
+ if (! Rx.Util._x_subset_keys_y(opt, {type: true }))
+ throw new Rx.Error('unknown argument for one type');
+};
Rx.CoreType.oneType.uri = 'tag:codesimply.com,2008:rx/core/one';
Rx.CoreType.oneType.prototype.check = function (v) {
// for some reason this was false: (false instanceof Boolean)
@@ -159,8 +185,9 @@ Rx.CoreType.arrType.prototype.check = function (v) {
}
Rx.CoreType.recType = function (opt, rx) {
- if (! Rx.Util._x_subset_keys_y(opt, Rx.CoreType.recType._valid_options))
- throw new Rx.Error('unknown argument for map type');
+ if (! Rx.Util._x_subset_keys_y(opt, {type: true, rest: true,
+ required: true, optional: true }))
+ throw new Rx.Error('unknown argument for rec type');
this.known = {};
@@ -185,12 +212,6 @@ Rx.CoreType.recType = function (opt, rx) {
if (opt.rest) this.restSchema = rx.makeSchema(opt.rest);
};
Rx.CoreType.recType.uri = 'tag:codesimply.com,2008:rx/core/rec';
-Rx.CoreType.recType._valid_options = {
- type: true,
- rest: true,
- required: true,
- optional: true
-};
Rx.CoreType.recType.prototype.check = function (v) {
if (!(((v != null) && (typeof(v) == 'object')) && ! (v instanceof Array)))
return false;
@@ -221,7 +242,7 @@ Rx.CoreType.recType.prototype.check = function (v) {
Rx.CoreType.mapType = function (opt, rx) {
if (! Rx.Util._x_subset_keys_y(opt, { type: true, values: true }))
- throw new Rx.Error('unknown argument for mapall type');
+ throw new Rx.Error('unknown argument for map type');
this.valueSchema = rx.makeSchema(opt.values);
};
View
@@ -56,7 +56,8 @@ var loadRxTests = (function (specRoot) {
var schemaName = schemaToTest[i];
var schema = testSchema[ schemaName ];
- if (schema.invalid) {
+ if (schema.invalid ||
+ (schema.composedtype && schema.composedtype.invalid)) {
totalTests++;
if (schema.pass || schema.fail)
throw 'invalid test: ' + schemaName + ' is invalid but has pass/fail';
View
@@ -21,9 +21,37 @@ for (schemaName in rxPlan.testSchema) schemaToTest.push(schemaName);
schemaToTest = schemaToTest.sort();
for (i in schemaToTest) {
+ var rx = new Rx({ defaultTypes: true });
+
+ for (coreType in Rx.CoreType) rx.registerType( Rx.CoreType[coreType] );
+
var schemaName = schemaToTest[i];
var schemaTest = rxPlan.testSchema[ schemaName ];
+ if (schemaTest.composedtype) {
+ try {
+ rx.learnType(schemaTest.composedtype.uri,
+ schemaTest.composedtype.schema);
+ } catch (e) {
+ if (schemaTest.composedtype.invalid && (e instanceof Rx.Error)) {
+ pass('BAD COMPOSED TYPE: ' + schemaName);
+ continue;
+ }
+ diag("exception thrown when learning type " + schemaName + ": " + e.message);
+ throw e;
+ }
+
+ if (schemaTest.composedtype.invalid) {
+ fail('BAD COMPOSED TYPE: ' + schemaName);
+ continue;
+ }
+
+ if (schemaTest.composedtype.prefix) {
+ rx.addPrefix(schemaTest.composedtype.prefix[0],
+ schemaTest.composedtype.prefix[1]);
+ }
+ }
+
var rxChecker;
try {
Oops, something went wrong.

0 comments on commit d789829

Please sign in to comment.