pulsar/spec/spec-helper.coffee

333 lines
11 KiB
CoffeeScript
Raw Normal View History

2013-09-18 05:58:41 +04:00
require '../src/window'
require 'jasmine-json'
require '../vendor/jasmine-jquery'
path = require 'path'
2014-02-24 05:09:05 +04:00
_ = require 'underscore-plus'
fs = require 'fs-plus'
Grim = require 'grim'
KeymapManager = require '../src/keymap-extensions'
{Point} = require 'text-buffer'
2013-09-18 05:58:41 +04:00
Project = require '../src/project'
Workspace = require '../src/workspace'
ServiceHub = require 'service-hub'
2014-09-23 01:35:13 +04:00
TextEditor = require '../src/text-editor'
TextEditorElement = require '../src/text-editor-element'
2013-09-18 05:58:41 +04:00
TokenizedBuffer = require '../src/tokenized-buffer'
2014-09-23 01:35:13 +04:00
TextEditorComponent = require '../src/text-editor-component'
2013-04-07 12:18:08 +04:00
pathwatcher = require 'pathwatcher'
clipboard = require '../src/safe-clipboard'
atom.themes.requireStylesheet '../static/jasmine'
atom.themes.initialLoadComplete = true
2013-09-18 01:54:33 +04:00
fixturePackagesPath = path.resolve(__dirname, './fixtures/packages')
atom.packages.packageDirPaths.unshift(fixturePackagesPath)
2014-04-01 02:28:55 +04:00
atom.keymaps.loadBundledKeymaps()
keyBindingsToRestore = atom.keymaps.getKeyBindings()
commandsToRestore = atom.commands.getSnapshot()
styleElementsToRestore = atom.styles.getSnapshot()
window.addEventListener 'core:close', -> window.close()
document.querySelector('html').style.overflow = 'auto'
document.body.style.overflow = 'auto'
2013-02-20 23:44:50 +04:00
# Allow document.title to be assigned in specs without screwing up spec window title
documentTitle = null
Object.defineProperty document, 'title',
get: -> documentTitle
set: (title) -> documentTitle = title
Set.prototype.jasmineToString = ->
result = "Set {"
first = true
@forEach (element) ->
result += ", " unless first
result += element.toString()
first = false
result + "}"
Set.prototype.isEqual = (other) ->
if other instanceof Set
return false if @size isnt other.size
values = @values()
until (next = values.next()).done
return false unless other.has(next.value)
true
else
false
2013-02-20 23:44:50 +04:00
jasmine.getEnv().addEqualityTester(_.isEqual) # Use underscore's definition of equality for toEqual assertions
2014-06-18 21:04:07 +04:00
2015-04-16 10:15:46 +03:00
if process.env.CI
2014-07-07 21:14:38 +04:00
jasmine.getEnv().defaultTimeoutInterval = 60000
2014-06-18 21:04:07 +04:00
else
jasmine.getEnv().defaultTimeoutInterval = 5000
2013-02-20 23:44:50 +04:00
specPackageName = null
specPackagePath = null
specProjectPath = null
isCoreSpec = false
{specDirectory, resourcePath} = atom.getLoadSettings()
2013-11-22 02:55:25 +04:00
if specDirectory
specPackagePath = path.resolve(specDirectory, '..')
try
2014-02-18 03:25:51 +04:00
specPackageName = JSON.parse(fs.readFileSync(path.join(specPackagePath, 'package.json')))?.name
specProjectPath = path.join(specDirectory, 'fixtures')
2015-04-07 06:45:02 +03:00
isCoreSpec = specDirectory is fs.realpathSync(__dirname)
beforeEach ->
documentTitle = null
2013-11-13 05:34:13 +04:00
projectPath = specProjectPath ? path.join(@specDirectory, 'fixtures')
atom.packages.serviceHub = new ServiceHub
atom.project = new Project({notificationManager: atom.notifications, packageManager: atom.packages, confirm: atom.confirm})
atom.project.setPaths([projectPath])
atom.workspace = new Workspace({
config: atom.config, project: atom.project, packageManager: atom.packages,
grammarRegistry: atom.grammars, notificationManager: atom.notifications,
setRepresentedFilename: jasmine.createSpy('setRepresentedFilename'),
2015-10-03 06:48:05 +03:00
setDocumentEdited: atom.setDocumentEdited.bind(atom), atomVersion: atom.getVersion(),
clipboard: atom.clipboard, viewRegistry: atom.views, assert: atom.assert.bind(atom)
})
atom.themes.workspace = atom.workspace
2014-04-01 02:28:55 +04:00
atom.keymaps.keyBindings = _.clone(keyBindingsToRestore)
atom.commands.restoreSnapshot(commandsToRestore)
atom.styles.restoreSnapshot(styleElementsToRestore)
atom.views.clearDocumentRequests()
2015-09-03 19:50:56 +03:00
atom.workspaceParentSelectorctor = '#jasmine-content'
window.resetTimeouts()
2014-12-30 10:52:16 +03:00
spyOn(_._, "now").andCallFake -> window.now
spyOn(window, "setTimeout").andCallFake window.fakeSetTimeout
spyOn(window, "clearTimeout").andCallFake window.fakeClearTimeout
atom.packages.packageStates = {}
serializedWindowState = null
spyOn(atom, 'saveStateSync')
2014-11-20 21:42:49 +03:00
atom.grammars.clearGrammarOverrides()
spy = spyOn(atom.packages, 'resolvePackagePath').andCallFake (packageName) ->
if specPackageName and packageName is specPackageName
resolvePackagePath(specPackagePath)
else
resolvePackagePath(packageName)
resolvePackagePath = _.bind(spy.originalValue, atom.packages)
2013-10-09 03:23:34 +04:00
# prevent specs from modifying Atom's menus
spyOn(atom.menu, 'sendToBrowserProcess')
# reset config before each spec; don't load or save from/to `config.json`
spyOn(atom.config, 'load')
spyOn(atom.config, 'save')
atom.config.set "core.destroyEmptyPanes", false
atom.config.set "editor.fontFamily", "Courier"
atom.config.set "editor.fontSize", 16
atom.config.set "editor.autoIndent", false
atom.config.set "core.disabledPackages", ["package-that-throws-an-exception",
"package-with-broken-package-json", "package-with-broken-keymap"]
atom.config.set "editor.useShadowDOM", true
2014-12-30 10:52:41 +03:00
advanceClock(1000)
window.setTimeout.reset()
atom.config.load.reset()
atom.config.save.reset()
2012-12-13 03:23:36 +04:00
# make editor display updates synchronous
TextEditorElement::setUpdatedSynchronously(true)
spyOn(atom, "setRepresentedFilename")
2014-02-19 22:01:56 +04:00
spyOn(pathwatcher.File.prototype, "detectResurrectionAfterDelay").andCallFake -> @detectResurrection()
2014-09-23 01:35:13 +04:00
spyOn(TextEditor.prototype, "shouldPromptToSave").andReturn false
# make tokenization synchronous
TokenizedBuffer.prototype.chunkSize = Infinity
spyOn(TokenizedBuffer.prototype, "tokenizeInBackground").andCallFake -> @tokenizeNextChunk()
clipboardContent = 'initial clipboard content'
spyOn(clipboard, 'writeText').andCallFake (text) -> clipboardContent = text
spyOn(clipboard, 'readText').andCallFake -> clipboardContent
addCustomMatchers(this)
afterEach ->
atom.packages.deactivatePackages()
atom.menu.template = []
2014-09-30 06:25:55 +04:00
atom.contextMenu.clear()
2015-07-02 00:35:59 +03:00
atom.notifications.clear()
2014-11-20 03:51:00 +03:00
atom.workspace?.destroy()
atom.workspace = null
atom.themes.workspace = null
delete atom.state.workspace
2014-04-03 01:19:06 +04:00
atom.project?.destroy()
atom.project = null
atom.themes.removeStylesheet('global-editor-styles')
2014-01-04 04:42:11 +04:00
delete atom.state.packageStates
atom.reset()
document.getElementById('jasmine-content').innerHTML = '' unless window.debugContent
ensureNoPathSubscriptions()
waits(0) # yield to ui thread to make screen update more frequently
ensureNoPathSubscriptions = ->
2013-04-07 12:18:08 +04:00
watchedPaths = pathwatcher.getWatchedPaths()
pathwatcher.closeAllWatchers()
if watchedPaths.length > 0
throw new Error("Leaking subscriptions for paths: " + watchedPaths.join(", "))
ensureNoDeprecatedFunctionsCalled = ->
deprecations = Grim.getDeprecations()
if deprecations.length > 0
originalPrepareStackTrace = Error.prepareStackTrace
Error.prepareStackTrace = (error, stack) ->
output = []
for deprecation in deprecations
2014-04-22 22:01:05 +04:00
output.push "#{deprecation.originName} is deprecated. #{deprecation.message}"
output.push _.multiplyString("-", output[output.length - 1].length)
for stack in deprecation.getStacks()
for {functionName, location} in stack
output.push "#{functionName} -- #{location}"
output.push ""
output.join("\n")
error = new Error("Deprecated function(s) #{deprecations.map(({originName}) -> originName).join ', '}) were called.")
error.stack
Error.prepareStackTrace = originalPrepareStackTrace
throw error
emitObject = jasmine.StringPrettyPrinter.prototype.emitObject
jasmine.StringPrettyPrinter.prototype.emitObject = (obj) ->
if obj.inspect
@append obj.inspect()
else
emitObject.call(this, obj)
2012-11-17 04:12:04 +04:00
jasmine.unspy = (object, methodName) ->
throw new Error("Not a spy") unless object[methodName].hasOwnProperty('originalValue')
2012-11-17 04:12:04 +04:00
object[methodName] = object[methodName].originalValue
2014-11-20 03:20:39 +03:00
jasmine.attachToDOM = (element) ->
jasmineContent = document.querySelector('#jasmine-content')
jasmineContent.appendChild(element) unless jasmineContent.contains(element)
2014-11-26 00:51:56 +03:00
deprecationsSnapshot = null
jasmine.snapshotDeprecations = ->
2015-01-07 20:11:55 +03:00
deprecationsSnapshot = _.clone(Grim.deprecations)
2014-11-26 00:51:56 +03:00
jasmine.restoreDeprecationsSnapshot = ->
2015-01-07 20:11:55 +03:00
Grim.deprecations = deprecationsSnapshot
2014-11-26 00:51:56 +03:00
jasmine.useRealClock = ->
jasmine.unspy(window, 'setTimeout')
jasmine.unspy(window, 'clearTimeout')
jasmine.unspy(_._, 'now')
addCustomMatchers = (spec) ->
spec.addMatchers
toBeInstanceOf: (expected) ->
notText = if @isNot then " not" else ""
this.message = => "Expected #{jasmine.pp(@actual)} to#{notText} be instance of #{expected.name} class"
@actual instanceof expected
toHaveLength: (expected) ->
if not @actual?
this.message = => "Expected object #{@actual} has no length method"
false
else
notText = if @isNot then " not" else ""
this.message = => "Expected object with length #{@actual.length} to#{notText} have length #{expected}"
2015-04-07 06:45:02 +03:00
@actual.length is expected
toExistOnDisk: (expected) ->
notText = this.isNot and " not" or ""
@message = -> return "Expected path '" + @actual + "'" + notText + " to exist."
2013-11-01 00:43:44 +04:00
fs.existsSync(@actual)
2014-02-05 01:06:47 +04:00
toHaveFocus: ->
notText = this.isNot and " not" or ""
if not document.hasFocus()
console.error "Specs will fail because the Dev Tools have focus. To fix this close the Dev Tools or click the spec runner."
@message = -> return "Expected element '" + @actual + "' or its descendants" + notText + " to have focus."
element = @actual
element = element.get(0) if element.jquery
element is document.activeElement or element.contains(document.activeElement)
2014-02-05 01:06:47 +04:00
2014-08-14 00:41:56 +04:00
toShow: ->
notText = if @isNot then " not" else ""
element = @actual
element = element.get(0) if element.jquery
2014-10-17 04:31:30 +04:00
@message = -> return "Expected element '#{element}' or its descendants#{notText} to show."
2014-08-14 00:41:56 +04:00
element.style.display in ['block', 'inline-block', 'static', 'fixed']
window.waitsForPromise = (args...) ->
if args.length > 1
2015-04-07 06:45:02 +03:00
{shouldReject, timeout} = args[0]
else
shouldReject = false
fn = _.last(args)
window.waitsFor timeout, (moveOn) ->
promise = fn()
if shouldReject
promise.catch.call(promise, moveOn)
promise.then ->
jasmine.getEnv().currentSpec.fail("Expected promise to be rejected, but it was resolved")
moveOn()
else
promise.then(moveOn)
promise.catch.call promise, (error) ->
2015-05-08 21:16:19 +03:00
jasmine.getEnv().currentSpec.fail("Expected promise to be resolved, but it was rejected with: #{error?.message} #{jasmine.pp(error)}")
moveOn()
window.resetTimeouts = ->
window.now = 0
window.timeoutCount = 0
2014-05-21 00:03:44 +04:00
window.intervalCount = 0
window.timeouts = []
2014-05-21 00:03:44 +04:00
window.intervalTimeouts = {}
window.fakeSetTimeout = (callback, ms) ->
id = ++window.timeoutCount
window.timeouts.push([id, window.now + ms, callback])
id
window.fakeClearTimeout = (idToClear) ->
2015-04-07 06:45:02 +03:00
window.timeouts = window.timeouts.filter ([id]) -> id isnt idToClear
window.fakeSetInterval = (callback, ms) ->
2014-05-21 00:03:44 +04:00
id = ++window.intervalCount
action = ->
callback()
2014-05-21 00:03:44 +04:00
window.intervalTimeouts[id] = window.fakeSetTimeout(action, ms)
window.intervalTimeouts[id] = window.fakeSetTimeout(action, ms)
id
window.fakeClearInterval = (idToClear) ->
2014-05-21 00:03:44 +04:00
window.fakeClearTimeout(@intervalTimeouts[idToClear])
window.advanceClock = (delta=1) ->
window.now += delta
callbacks = []
window.timeouts = window.timeouts.filter ([id, strikeTime, callback]) ->
if strikeTime <= window.now
callbacks.push(callback)
false
else
true
callback() for callback in callbacks