assert

Simple tests for assert

HTML

<ul class="result">
</ul>

CSS

li {
    font-family: sans-serif;
    padding: 6px;
    border-bottom: solid 1px #444;
}

li.success {
    background-color: rgba(0,255,0,0.2);
}

li.failure {
    background-color: rgba(255,0,0,0.2);
}

JavaScript

function defaultToJSON() {
    var o = {};
    for (var k in this) {
        if (Object.prototype.hasOwnProperty.call(this, k)) {
            o[k] = this[k];
        }
    }
    return o;
}

function transient(obj, key) {
    // ...  you need to implement this
    var oldToJSON = obj.toJSON || defaultToJSON;
    obj.toJSON = function() {
        var o = oldToJSON.call(obj);
        delete o[key];
        return o;
    };
}

// ... while the following should stay untouched

function addResult(className, description) {
    $("ul.result").append($("<li class='" + className+ "'>"+description+"</li>"));    
}


function assert(predicate, description) {  
    var className = predicate ? "success": "failure";
    addResult(className, description);       
}

function SomeObject() {
    this.someProp = { "name": "José Bové" }
    this.transientProp  = { "name": "Aimé Jacquet" }
}

var obj = new SomeObject();
transient(obj, "transientProp")

var obj2 = new SomeObject();
transient(obj2, "transientProp")
obj2.transientProp.age = 53;

assert(obj.someProp !== undefined,
    "someProp should stay accessible" )

assert(obj.transientProp !== undefined,
    "transientProp should stay accessible")

assert(obj.transientProp.age === undefined, 
    "transientProp should not be shared between objects")

assert(obj2.transientProp.age === 53,
    "transientProp should not be shared between objects")

assert(JSON.parse(JSON.stringify(obj)).someProp !== undefined,
    "someProp should still be serialized")

assert(JSON.parse(JSON.stringify(obj)).transientProp === undefined, 
    "transientProp should not be serialized")