test('[UI_factory._decorate] correct, we can see standard methods applied and not migrated from children', function (t) { t.plan(6); //SETUP var uf = UI_factory(); var some_fake_pub_sub = {test_property:true}; var container = new createjs.Container(); var test_function_names = ['foo', 'bar', 'baz']; for (var i = 0; i < test_function_names.length; i++) { var tmp = new createjs.Container(); tmp._ = {}; tmp._[test_function_names[i]] = {}; var add_to_stage = require('lib/primitives/add_to_stage'); tmp._.add_to_stage = add_to_stage(tmp); container.addChild(tmp); } container.setBounds(0, 0, 0, 0); //TEST var result = uf._decorate(container, some_fake_pub_sub); t.ok(check.function(result._.add_to_stage), 'add_to_stage'); t.ok(check.object(result._.place), 'place'); t.ok(check.function(result._.append_to), 'append_to'); //also check that the standard functions are NOT child functions // they should be functions that point to and act on the parent container for (var j = 0; j < test_function_names.length; j++) { t.notOk(result._.add_to_stage === result.children[j]._.add_to_stage, 'add to stage on parent is not a reference to a child add_to_stage'); } });
syntax[metric].forEach(function (s) { var identifier; if (check.function(s.identifier)) { identifier = s.identifier(node); } else { identifier = s.identifier; } if (check.function(s.filter) === false || s.filter(node) === true) { halsteadItemEncountered(currentReport, metric, identifier); } });
test('[UI_factory._decorate_text_output] correct', function (t) { t.plan(3); //SETUP var uf = UI_factory(); var text_factory = require('lib/factory/text_factory'); var tf = text_factory(); var new_text = tf.get(); //TEST var result = uf._decorate_text_output(new_text); t.ok(check.function(result._.add_to_stage), 'we should see it decorated with add_to_stage'); t.ok(check.object(result._.place), 'and place'); t.ok(check.function(result._.set_text_output), 'and set_text'); });
function coerce (datum) { if (disableCoercions || check.primitive(datum)) { return Promise.resolve(datum) } if (check.instanceStrict(datum, Promise)) { return coerceThing(datum, 'promises', coercePromise).then(coerce) } if (check.instanceStrict(datum, Buffer)) { return coerceThing(datum, 'buffers', coerceBuffer) } if (check.instanceStrict(datum, Map)) { return coerceThing(datum, 'maps', coerceMap) } if ( check.iterable(datum) && check.not.string(datum) && check.not.array(datum) ) { return coerceThing(datum, 'iterables', coerceIterable) } if (check.function(datum.toJSON)) { return Promise.resolve(datum.toJSON()) } return Promise.resolve(datum) }
_.each(definitions, function(def, key) { var value = values[key]; if (_.isUndefined(value) || _.isNull(value)) { if (!_.isUndefined(def.default) && !_.isNull(def.default)) { // if there is a default value, use that if (check.function(def.default)) { value = def.default(); } else { value = def.default; } } else if (def.nullable || def.autoIncrement) { // if we haven't found any values yet, and this property is nullable // or automatically assigned, return and move on to the next value return; } else { // otherwise throw an error throw new Error('Missing required property ' + key); } } // assert that the types match try { check.assert[def.type](value); } catch (e) { throw new TypeError('Field ' + key + ' requires type ' + def.type + ' but has type ' + typeof value); } if (check.function(def.validate)) { // if there is a validation function in the definitions, assert that the // value passes that as well var valid = def.validate(value); if (valid === true) { // everything is fine } else if (valid === false) { // did not pass validation function throw new TypeError('Value ' + value + ' did not pass custom ' + 'validation function on field ' + key); } else { throw new TypeError('validate() function for ' + key + ' must return ' + 'a boolean.'); } } });
function incrementCounter (node, syntax, name, incrementFn, currentReport) { var amount = syntax[name]; if (check.number(amount)) { incrementFn(currentReport, amount); } else if (check.function(amount)) { incrementFn(currentReport, amount(node)); } }
test('[UI_factory._decorate_buttons] correct, verifying append_to', function (t) { t.plan(2); var uf = UI_factory(); var container = new createjs.Container(); container.setBounds(0, 0, 0, 0); var result = uf._decorate_buttons(container); t.ok(check.object(result._)); t.ok(check.function(result._.append_to)); });
test('[UI_factory._decorate_text_input] correct', function (t) { t.plan(5); //SETUP var html_factory = require('lib/factory/html_factory'); var hf = html_factory(); var text_factory = require('lib/factory/text_factory'); var tf = text_factory(); var container = new createjs.Container(); //get the Text object tf.text({text:String('test')}); var te = tf.get(); //get the text input element hf.html({ type:'text', }); var h = hf.get(); //add to the container container.addChild(te); container.addChild(h); container.setBounds(0, 0, 0, 0); //TEST var uf = UI_factory(); var result = uf._decorate_text_input(container); t.ok(check.object(result._.place), 'place should be there'); t.ok(check.function(result._.place.above), 'and should have functions on it'); t.ok(check.function(result._.add_to_stage), 'we should have add_to_stage on there'); t.ok(check.function(result._.append_to), 'and append_to'); t.ok(check.function(result._.get_text_input_value), 'get value should be set as well'); });
function processDependencies (node, syntax, clearDependencies) { var dependencies; if (check.function(syntax.dependencies)) { dependencies = syntax.dependencies(node, clearDependencies); if (check.object(dependencies) || check.array(dependencies)) { report.dependencies = report.dependencies.concat(dependencies); } return true; } return false; }
test('[html_factory.get] correct', function (t) { t.plan(8); var hf = html_factory(); hf.html({type:'radio'}); var result = hf.get() t.ok(result.children, "we should have a container"); t.equal(result.children.length, 2, "...that has two children"); t.ok(result.children[0].htmlElement, "...the first child is an HTML element"); t.ok(check.string(result.children[1].text), "...the second is Text"); t.deepEqual(result.children[0].htmlElement, result._.htmlElement, "...we should see the HTML exposed in our namespace _"); t.equal(result.children[1].text, result._.text, "...and the text value exposed in _"); t.equal(result._.htmlElement.type, 'radio', '...and the type of the HTML input should be "radio"'); t.ok(check.function(result._.append_to), "...and we can see the apply_to method"); });
test('[html_factory._decorate] correct', function (t) { t.plan(2); var container = new createjs.Container(); container._ = {}; var html = document.createElement('input'); html.type = 'radio'; html.id = "1"; container._.htmlElement = html; var hf = html_factory(); var result = hf._decorate(container); t.ok(result._.append_to, "the decorated method was applied to the container"); t.ok(check.function(result._.append_to), "...and that it's a method"); });
test('[html_factory._decorate] correct, add_to_stage', function (t) { t.plan(2); var container = new createjs.Container(); container._ = {}; var html = document.createElement('input'); html.type = 'radio'; html.id = "1"; container._.htmlElement = html; var hf = html_factory(); var result = hf._decorate(container); t.ok(check.object(result._)); t.ok(check.function(result._.add_to_stage)); });
test('[add_to_stage] correct, get back a function', function (t) { t.plan(1); //SETUP var canvas = document.getElementsByTagName('canvas')[0]; var stage = new createjs.Stage(canvas); var primitives = require("lib/util/primitives"); primitives.set('stage', stage); //TEST var text = new createjs.Text('some string'); var add_text = add_to_stage(text); t.ok(check.function(add_text)); //CLEANUP stage.removeAllChildren(); primitives.clear(); });
test('[shape_factory._decorate] correct, add_to_stage', function (t) { t.plan(2); SETUP(); //setup var sf = shape_factory(); var container = new createjs.Container(); var shape = new createjs.Shape(); shape.setBounds(1, 1, 1, 1); var text = new createjs.Text(); container.addChild(shape); container.addChild(text); var color = {fill:"some fill", stroke: "some stroke"} var result = sf._decorate(container, color); t.ok(check.object(result._)); t.ok(check.function(result._.add_to_stage)); TEARDOWN(); });
test('[shape_factory.get][inegration testing] correct, defaults, circle', function (t) { t.plan(24); SETUP(); var sf = shape_factory(); sf.shape({type:"circle"}); var result = sf.get(); t.equal(result.children.length, 2); t.ok(check.instanceStrict(result.children[0], createjs.Shape), "first child should should be createjs.Shape"); t.ok(check.number(result.children[0].graphics.command.radius), "as a circle it should have a valid radius property"); t.equal(result.children[0].graphics.command.radius, 1, "should have default radius for a circle"); t.ok(check.instanceStrict(result.children[1], createjs.Text), "second child should should be createjs.Text"); t.ok(check.string(result.children[1].text), "...its value should be set"); t.ok(check.number(Number(result.children[1].text)), "...to a number"); //we should see all the event handlers / modifiers set //NOTE: we cannot tell the difference between the Shape.(function) and Container.(function) (of this type) // they must be inherited from the same place, so we just have to check that they're set and that they're functions var shape = result.children[0]; t.ok(result._.on); t.ok(check.function(result._.on)); t.ok(result._.off); t.ok(check.function(result._.off)); t.ok(result._.addEventListener); t.ok(check.function(result._.addEventListener)); t.ok(result._.hasEventListener); t.ok(check.function(result._.hasEventListener)); t.ok(result._.removeAllEventListeners); t.ok(check.function(result._.removeAllEventListeners)); t.ok(result._.removeEventListener); t.ok(check.function(result._.removeEventListener)); t.deepEqual(result.getBounds(), {x:-1, y:-1, width:2, height:2}, "and we should be able to see the default bounds"); t.ok(result._.place, "the place property should be defined... "); t.ok(check.object(result._.place), "...and the place object should be attached"); var colorString = require("color-string"); t.ok(colorString.get(result._.shape_fill), "the fill should be a random color"); t.equal(result._.shape_stroke, null, "and stroke should be the default (null)"); TEARDOWN(); });
test('[shape_factory._decorate] correct', function (t) { t.plan(21); SETUP(); //setup var sf = shape_factory(); var container = new createjs.Container(); var shape = new createjs.Shape(); shape.setBounds(1, 1, 1, 1); var text = new createjs.Text(); container.addChild(shape); container.addChild(text); var color = {fill:"some fill", stroke: "some stroke"} var result = sf._decorate(container, color); t.ok(result._); t.ok(result._.on); t.ok(check.function(result._.on)); t.ok(result._.off); t.ok(check.function(result._.off)); t.ok(result._.addEventListener); t.ok(check.function(result._.addEventListener)); t.ok(result._.hasEventListener); t.ok(check.function(result._.hasEventListener)); t.ok(result._.removeAllEventListeners); t.ok(check.function(result._.removeAllEventListeners)); t.ok(result._.removeEventListener); t.ok(check.function(result._.removeEventListener)); t.equal(container.getBounds().x, 1); t.equal(container.getBounds().y, 1); t.equal(container.getBounds().width, 1); t.equal(container.getBounds().height, 1); t.ok(result._.place); t.ok(check.object(result._.place)); t.equal(result._.shape_fill, "some fill"); t.equal(result._.shape_stroke, "some stroke"); TEARDOWN(); });
test('[shape_factory.get][inegration testing] correct, defaults, rectangle', function (t) { t.plan(26); SETUP(); var sf = shape_factory(); sf.shape({type:"rect"}); var result = sf.get(); t.equal(result.children.length, 2); t.ok(check.instanceStrict(result.children[0], createjs.Shape), "first child should should be createjs.Shape"); t.ok(check.number(result.children[0].graphics.command.h), "as a rect it should have a valid height property"); t.ok(check.number(result.children[0].graphics.command.w), "...and width"); t.equal(result.children[0].graphics.command.h, 18, "should have default height for a rect"); t.equal(result.children[0].graphics.command.w, 36, "...and width"); t.ok(check.instanceStrict(result.children[1], createjs.Text), "second child should should be createjs.Text"); t.ok(check.string(result.children[1].text), "...its value should be set"); t.ok(check.number(Number(result.children[1].text)), "...to a number"); //we should see all the event handlers / modifiers set var shape = result.children[0]; t.ok(result._.on); t.ok(check.function(result._.on)); t.ok(result._.off); t.ok(check.function(result._.off)); t.ok(result._.addEventListener); t.ok(check.function(result._.addEventListener)); t.ok(result._.hasEventListener); t.ok(check.function(result._.hasEventListener)); t.ok(result._.removeAllEventListeners); t.ok(check.function(result._.removeAllEventListeners)); t.ok(result._.removeEventListener); t.ok(check.function(result._.removeEventListener)); t.deepEqual(result.getBounds(), {x:0, y:0, width:36, height:18}, "...and we should be able to see the default bounds"); t.ok(result._.place, "the place property should be defined... "); t.ok(check.object(result._.place), "...and the place object should be attached"); var colorString = require("color-string"); t.ok(colorString.get(result._.shape_fill), "the fill should be a random color"); t.equal(result._.shape_stroke, null, "and stroke should be the default (null)"); TEARDOWN(); });
_.each(clone, function(val, key) { if (_.has(definitions, key) && check.function(definitions[key].transform)) { clone[key] = definitions[key].transform(val); } });
_.each(values, function(val, key) { if (check.function(definitions[key].transform)) { values[key] = definitions[key].transform(val); } });
test('[UI_factory._decorate_radio_buttons] correct', function (t) { t.plan(12); //setup var uf = UI_factory(); var html_factory = require('lib/factory/html_factory'); var hf = html_factory(); var text_factory = require('lib/factory/text_factory'); var tf = text_factory(); var container = new createjs.Container(); for(var i = 0; i < 2; i++) { //create a container to hold the text and radio button var c = new createjs.Container(); //create the button hf.html({ type:'radio', }); var b = hf.get(); //create the text tf.text({text:'test' + String(i)}); var te = tf.get(); //add the Text and Button to the container c.addChild(te); c.addChild(b); //add the radio button container to the parent container // for return of all the radio buttons in the set container.addChild(c); } //setting bounds container.setBounds(0, 0, 0, 0); var result = uf._decorate_radio_buttons(container); //TESTING //the parent container should be decorated t.ok(check.object(result._), '_ namespace should exist'); t.ok(check.object(result._.place), 'place is on there'); t.ok(check.function(result._.add_to_stage), 'add_to_stage is on there'); t.ok(check.function(result._.append_to), 'append_to is on there'); t.ok(check.object(result._.radio_button_checked), 'should see the checked object'); var count = 0; for (button in result._.radio_button_checked) { t.ok(check.function(result._.radio_button_checked[button]), 'and each entry in checked should be a function') count += 1; } t.equal(count, 2, 'should see one entry for each radio button'); //then each of the children within the parent should be decorated for(var j = 0; j < result.children.length; j++) { t.ok(check.object(result.children[j]._), '_ namespace should exist'); t.ok(check.function(result.children[j]._.append_to), 'append_to is on there'); } });