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

Inline ember-dev into internal-test-helpers package. #16054

Merged
merged 2 commits into from
Jan 5, 2018
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions broccoli/addon.js

This file was deleted.

4 changes: 1 addition & 3 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const MergeTrees = require('broccoli-merge-trees');
const Funnel = require('broccoli-funnel');
const babelHelpers = require('./broccoli/babel-helpers');
const bootstrapModule = require('./broccoli/bootstrap-modules');
const addon = require('./broccoli/addon');
const concat = require('./broccoli/concat-bundle');
const testIndexHTML = require('./broccoli/test-index-html');
const toES5 = require('./broccoli/to-es5');
Expand Down Expand Up @@ -38,7 +37,7 @@ const {
} = require('./broccoli/packages');
const SHOULD_ROLLUP = true;

module.exports = function(options) {
module.exports = function() {
let loader = internalLoader();
let license = emberLicense();
let nodeModule = nodeModuleUtils();
Expand Down Expand Up @@ -89,7 +88,6 @@ module.exports = function(options) {
// ES5
let dependenciesES5 = dependenciesES6().map(toES5);
let emberES5 = emberCoreES6.map(toES5);
emberTests.push(addon('ember-dev', options.project));
let emberTestsES5 = emberTests.map(toES5);

// Bundling
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
"ember-cli-dependency-checker": "^2.0.1",
"ember-cli-sauce": "^2.1.0",
"ember-cli-yuidoc": "^0.8.8",
"ember-dev": "github:emberjs/ember-dev#eace534",
"ember-publisher": "0.0.7",
"eslint": "^3.0.0",
"eslint-plugin-ember-internal": "^1.1.0",
Expand Down
92 changes: 92 additions & 0 deletions packages/internal-test-helpers/lib/ember-dev/assertion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* globals QUnit */

import { callWithStub, checkTest } from './utils';

const BREAK = {};

/*
This assertion class is used to test assertions made using Ember.assert.
It injects two helpers onto `window`:

- expectAssertion(func: Function, [expectedMessage: String | RegExp])

This function calls `func` and asserts that `Ember.assert` is invoked during
the execution. Moreover, it takes a String or a RegExp as a second optional
argument that can be used to test if a specific assertion message was
generated.

- ignoreAssertion(func: Function)

This function calls `func` and disables `Ember.assert` during the execution.
In particular, this prevents `Ember.assert` from throw errors that would
disrupt the control flow.
*/
export default function AssertionAssert(env) {
this.env = env;
}

AssertionAssert.prototype = {
reset() { },
assert() { },

inject() {
let expectAssertion = (func, expectedMessage) => {
if (this.env.runningProdBuild) {
QUnit.ok(true, 'Assertions disabled in production builds.');
return;
}

let sawCall;
let actualMessage;

// The try-catch statement is used to "exit" `func` as soon as
// the first useful assertion has been produced.
try {
callWithStub(this.env, 'assert', func, (message, test) => {
sawCall = true;
if (checkTest(test)) { return; }
actualMessage = message;
throw BREAK;
});
} catch (e) {
if (e !== BREAK) {
throw e;
}
}

assert(sawCall, actualMessage, expectedMessage);
};

let ignoreAssertion = (func) => {
callWithStub(this.env, 'assert', func);
};

window.expectAssertion = expectAssertion;
window.ignoreAssertion = ignoreAssertion;
},

restore() {
window.expectAssertion = null;
window.ignoreAssertion = null;
}
};

function assert(sawCall, actualMessage, expectedMessage) {
// Run assertions in an order that is useful when debugging a test failure.
if (!sawCall) {
QUnit.ok(false, `Expected Ember.assert to be called (Not called with any value).`);
} else if (!actualMessage) {
QUnit.ok(false, `Expected a failing Ember.assert (Ember.assert called, but without a failing test).`);
} else {
if (expectedMessage) {
if (expectedMessage instanceof RegExp) {
QUnit.ok(expectedMessage.test(actualMessage), `Expected failing Ember.assert: '${expectedMessage}', but got '${actualMessage}'.`);
} else {
QUnit.equal(actualMessage, expectedMessage, `Expected failing Ember.assert: '${expectedMessage}', but got '${actualMessage}'.`);
}
} else {
// Positive assertion that assert was called
QUnit.ok(true, 'Expected a failing Ember.assert.');
}
}
}
59 changes: 59 additions & 0 deletions packages/internal-test-helpers/lib/ember-dev/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import MethodCallTracker from './method-call-tracker';

class DebugAssert {
constructor(methodName, env) {
this.methodName = methodName;
this.env = env;
}

inject() {}

restore() {
this.reset();
}

reset() {
if (this.tracker) {
this.tracker.restoreMethod();
}

this.tracker = null;
}

assert() {
if (this.tracker) {
this.tracker.assert();
}
}

// Run an expectation callback within the context of a new tracker, optionally
// accepting a function to run, which asserts immediately
runExpectation(func, callback) {
let originalTracker;

// When helpers are passed a callback, they get a new tracker context
if (func) {
originalTracker = this.tracker;
this.tracker = null;
}

if (!this.tracker) {
this.tracker = new MethodCallTracker(this.env, this.methodName);
}

// Yield to caller with tracker instance
callback(this.tracker);

// Once the given callback is invoked, the pending assertions should be
// flushed immediately
if (func) {
func();
this.assert();
this.reset();

this.tracker = originalTracker;
}
}
}

export default DebugAssert;
78 changes: 78 additions & 0 deletions packages/internal-test-helpers/lib/ember-dev/deprecation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import DebugAssert from './debug';
import { callWithStub } from './utils';

class DeprecationAssert extends DebugAssert {
constructor(env) {
super('deprecate', env);
}

inject() {
// Expects no deprecation to happen within a function, or if no function is
// passed, from the time of calling until the end of the test.
//
// expectNoDeprecation(function() {
// fancyNewThing();
// });
//
// expectNoDeprecation();
// Ember.deprecate("Old And Busted");
//
let expectNoDeprecation = (func) => {
if (typeof func !== 'function') {
func = null;
}

this.runExpectation(func, (tracker) => {
if (tracker.isExpectingCalls()) {
throw new Error("expectNoDeprecation was called after expectDeprecation was called!");
}

tracker.expectNoCalls();
});
};

// Expect a deprecation to happen within a function, or if no function
// is pass, from the time of calling until the end of the test. Can be called
// multiple times to assert deprecations with different specific messages
// were fired.
//
// expectDeprecation(function() {
// Ember.deprecate("Old And Busted");
// }, /* optionalStringOrRegex */);
//
// expectDeprecation(/* optionalStringOrRegex */);
// Ember.deprecate("Old And Busted");
//
let expectDeprecation = (func, message) => {
if (typeof func !== 'function') {
message = func;
func = null;
}

this.runExpectation(func, (tracker) => {
if (tracker.isExpectingNoCalls()) {
throw new Error("expectDeprecation was called after expectNoDeprecation was called!");
}

tracker.expectCall(message);
});
};

let ignoreDeprecation = (func) => {
callWithStub(this.env, 'deprecate', func);
};

window.expectNoDeprecation = expectNoDeprecation;
window.expectDeprecation = expectDeprecation;
window.ignoreDeprecation = ignoreDeprecation;
}

restore() {
super.restore();
window.expectDeprecation = null;
window.expectNoDeprecation = null;
window.ignoreDeprecation = null;
}
}

export default DeprecationAssert;
19 changes: 19 additions & 0 deletions packages/internal-test-helpers/lib/ember-dev/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import DeprecationAssert from "./deprecation";
import WarningAssert from "./warning";
import RemainingViewAssert from "./remaining-view";
import RemainingTemplateAssert from "./remaining-template";
import AssertionAssert from "./assertion";
import RunLoopAssert from "./run-loop";

import {buildCompositeAssert} from "./utils";

var EmberDevTestHelperAssert = buildCompositeAssert([
DeprecationAssert,
WarningAssert,
RemainingViewAssert,
RemainingTemplateAssert,
AssertionAssert,
RunLoopAssert
]);

export default EmberDevTestHelperAssert;
Loading