2013-09-20 00:51:53 +04:00
|
|
|
{fs} = require 'atom'
|
2013-10-21 17:20:19 +04:00
|
|
|
path = require 'path'
|
|
|
|
temp = require 'temp'
|
2013-09-18 05:58:41 +04:00
|
|
|
TextMateGrammar = require '../src/text-mate-grammar'
|
2013-01-09 04:15:24 +04:00
|
|
|
|
2012-12-23 23:50:58 +04:00
|
|
|
describe "the `syntax` global", ->
|
2013-03-26 03:31:01 +04:00
|
|
|
beforeEach ->
|
2013-11-20 22:48:55 +04:00
|
|
|
atom.packages.activatePackage('language-text', sync: true)
|
|
|
|
atom.packages.activatePackage('language-javascript', sync: true)
|
|
|
|
atom.packages.activatePackage('language-coffee-script', sync: true)
|
|
|
|
atom.packages.activatePackage('language-ruby', sync: true)
|
2013-03-26 03:31:01 +04:00
|
|
|
|
2013-03-22 05:04:10 +04:00
|
|
|
describe "serialization", ->
|
|
|
|
it "remembers grammar overrides by path", ->
|
2013-10-21 18:36:40 +04:00
|
|
|
filePath = '/foo/bar/file.js'
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(atom.syntax.selectGrammar(filePath).name).not.toBe 'Ruby'
|
|
|
|
atom.syntax.setGrammarOverrideForPath(filePath, 'source.ruby')
|
2013-11-21 01:51:12 +04:00
|
|
|
syntax2 = atom.deserializers.deserialize(atom.syntax.serialize())
|
2013-11-11 20:53:52 +04:00
|
|
|
syntax2.addGrammar(grammar) for grammar in atom.syntax.grammars when grammar isnt atom.syntax.nullGrammar
|
2013-10-21 18:36:40 +04:00
|
|
|
expect(syntax2.selectGrammar(filePath).name).toBe 'Ruby'
|
2013-03-22 05:04:10 +04:00
|
|
|
|
2013-03-21 05:23:47 +04:00
|
|
|
describe ".selectGrammar(filePath)", ->
|
2013-03-22 01:45:41 +04:00
|
|
|
it "can use the filePath to load the correct grammar based on the grammar's filetype", ->
|
2013-11-20 22:48:55 +04:00
|
|
|
atom.packages.activatePackage('language-git', sync: true)
|
2013-03-26 03:31:01 +04:00
|
|
|
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(atom.syntax.selectGrammar("file.js").name).toBe "JavaScript" # based on extension (.js)
|
|
|
|
expect(atom.syntax.selectGrammar(path.join(temp.dir, '.git', 'config')).name).toBe "Git Config" # based on end of the path (.git/config)
|
|
|
|
expect(atom.syntax.selectGrammar("Rakefile").name).toBe "Ruby" # based on the file's basename (Rakefile)
|
|
|
|
expect(atom.syntax.selectGrammar("curb").name).toBe "Null Grammar"
|
|
|
|
expect(atom.syntax.selectGrammar("/hu.git/config").name).toBe "Null Grammar"
|
2013-01-01 04:11:11 +04:00
|
|
|
|
|
|
|
it "uses the filePath's shebang line if the grammar cannot be determined by the extension or basename", ->
|
2013-09-18 03:15:53 +04:00
|
|
|
filePath = require.resolve("./fixtures/shebang")
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(atom.syntax.selectGrammar(filePath).name).toBe "Ruby"
|
2013-01-01 04:11:11 +04:00
|
|
|
|
2013-02-21 03:11:07 +04:00
|
|
|
it "uses the number of newlines in the first line regex to determine the number of lines to test against", ->
|
2013-11-20 22:48:55 +04:00
|
|
|
atom.packages.activatePackage('language-property-list', sync: true)
|
2013-03-26 03:31:01 +04:00
|
|
|
|
2013-02-21 00:38:49 +04:00
|
|
|
fileContent = "first-line\n<html>"
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(atom.syntax.selectGrammar("dummy.coffee", fileContent).name).toBe "CoffeeScript"
|
2013-02-21 00:38:49 +04:00
|
|
|
|
2013-02-21 03:11:07 +04:00
|
|
|
fileContent = '<?xml version="1.0" encoding="UTF-8"?>'
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(atom.syntax.selectGrammar("grammar.tmLanguage", fileContent).name).toBe "Null Grammar"
|
2013-02-21 03:11:07 +04:00
|
|
|
|
|
|
|
fileContent += '\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(atom.syntax.selectGrammar("grammar.tmLanguage", fileContent).name).toBe "Property List (XML)"
|
2013-02-21 03:11:07 +04:00
|
|
|
|
2013-01-09 04:15:24 +04:00
|
|
|
it "doesn't read the file when the file contents are specified", ->
|
2013-09-18 03:15:53 +04:00
|
|
|
filePath = require.resolve("./fixtures/shebang")
|
2013-11-01 01:58:01 +04:00
|
|
|
filePathContents = fs.readFileSync(filePath, 'utf8')
|
2013-09-18 05:58:41 +04:00
|
|
|
spyOn(fs, 'read').andCallThrough()
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(atom.syntax.selectGrammar(filePath, filePathContents).name).toBe "Ruby"
|
2013-09-18 05:58:41 +04:00
|
|
|
expect(fs.read).not.toHaveBeenCalled()
|
2013-01-09 04:15:24 +04:00
|
|
|
|
2013-03-22 02:54:49 +04:00
|
|
|
it "allows the default grammar to be overridden for a path", ->
|
2013-10-21 18:36:40 +04:00
|
|
|
filePath = '/foo/bar/file.js'
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(atom.syntax.selectGrammar(filePath).name).not.toBe 'Ruby'
|
|
|
|
atom.syntax.setGrammarOverrideForPath(filePath, 'source.ruby')
|
|
|
|
expect(atom.syntax.selectGrammar(filePath).name).toBe 'Ruby'
|
|
|
|
atom.syntax.clearGrammarOverrideForPath(filePath)
|
|
|
|
expect(atom.syntax.selectGrammar(filePath).name).not.toBe 'Ruby'
|
2013-01-01 04:11:11 +04:00
|
|
|
|
2013-06-25 05:23:54 +04:00
|
|
|
describe "when multiple grammars have matching fileTypes", ->
|
|
|
|
it "selects the grammar with the longest fileType match", ->
|
|
|
|
grammar1 = new TextMateGrammar
|
|
|
|
name: 'test1'
|
|
|
|
scopeName: 'source1'
|
|
|
|
fileTypes: ['test', 'more.test']
|
|
|
|
|
|
|
|
grammar2 = new TextMateGrammar
|
|
|
|
name: 'test2'
|
|
|
|
scopeName: 'source2'
|
|
|
|
fileTypes: ['test']
|
|
|
|
|
2013-11-11 20:53:52 +04:00
|
|
|
atom.syntax.addGrammar(grammar1)
|
|
|
|
atom.syntax.addGrammar(grammar2)
|
2013-06-25 05:23:54 +04:00
|
|
|
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(atom.syntax.selectGrammar('more.test', '')).toBe grammar1
|
2013-06-25 05:23:54 +04:00
|
|
|
|
2013-07-10 19:54:15 +04:00
|
|
|
describe "when there is no file path", ->
|
|
|
|
it "does not throw an exception (regression)", ->
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(-> atom.syntax.selectGrammar(null, '#!/usr/bin/ruby')).not.toThrow()
|
|
|
|
expect(-> atom.syntax.selectGrammar(null, '')).not.toThrow()
|
|
|
|
expect(-> atom.syntax.selectGrammar(null, null)).not.toThrow()
|
2013-07-10 19:54:15 +04:00
|
|
|
|
2013-03-26 03:31:01 +04:00
|
|
|
describe ".removeGrammar(grammar)", ->
|
|
|
|
it "removes the grammar, so it won't be returned by selectGrammar", ->
|
2013-11-11 20:53:52 +04:00
|
|
|
grammar = atom.syntax.selectGrammar('foo.js')
|
|
|
|
atom.syntax.removeGrammar(grammar)
|
|
|
|
expect(atom.syntax.selectGrammar('foo.js').name).not.toBe grammar.name
|
2013-03-26 03:31:01 +04:00
|
|
|
|
2012-12-23 23:50:58 +04:00
|
|
|
describe ".getProperty(scopeDescriptor)", ->
|
|
|
|
it "returns the property with the most specific scope selector", ->
|
2013-11-11 20:53:52 +04:00
|
|
|
atom.syntax.addProperties(".source.coffee .string.quoted.double.coffee", foo: bar: baz: 42)
|
|
|
|
atom.syntax.addProperties(".source .string.quoted.double", foo: bar: baz: 22)
|
|
|
|
atom.syntax.addProperties(".source", foo: bar: baz: 11)
|
2012-12-23 23:50:58 +04:00
|
|
|
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(atom.syntax.getProperty([".source.coffee", ".string.quoted.double.coffee"], "foo.bar.baz")).toBe 42
|
|
|
|
expect(atom.syntax.getProperty([".source.js", ".string.quoted.double.js"], "foo.bar.baz")).toBe 22
|
|
|
|
expect(atom.syntax.getProperty([".source.js", ".variable.assignment.js"], "foo.bar.baz")).toBe 11
|
|
|
|
expect(atom.syntax.getProperty([".text"], "foo.bar.baz")).toBeUndefined()
|
2012-12-24 00:19:20 +04:00
|
|
|
|
|
|
|
it "favors the most recently added properties in the event of a specificity tie", ->
|
2013-11-11 20:53:52 +04:00
|
|
|
atom.syntax.addProperties(".source.coffee .string.quoted.single", foo: bar: baz: 42)
|
|
|
|
atom.syntax.addProperties(".source.coffee .string.quoted.double", foo: bar: baz: 22)
|
2012-12-24 00:19:20 +04:00
|
|
|
|
2013-11-11 20:53:52 +04:00
|
|
|
expect(atom.syntax.getProperty([".source.coffee", ".string.quoted.single"], "foo.bar.baz")).toBe 42
|
|
|
|
expect(atom.syntax.getProperty([".source.coffee", ".string.quoted.single.double"], "foo.bar.baz")).toBe 22
|
2013-03-26 22:45:19 +04:00
|
|
|
|
|
|
|
describe ".removeProperties(name)", ->
|
|
|
|
it "allows properties to be removed by name", ->
|
2013-11-11 20:53:52 +04:00
|
|
|
atom.syntax.addProperties("a", ".source.coffee .string.quoted.double.coffee", foo: bar: baz: 42)
|
|
|
|
atom.syntax.addProperties("b", ".source .string.quoted.double", foo: bar: baz: 22)
|
2013-03-26 22:45:19 +04:00
|
|
|
|
2013-11-11 20:53:52 +04:00
|
|
|
atom.syntax.removeProperties("b")
|
|
|
|
expect(atom.syntax.getProperty([".source.js", ".string.quoted.double.js"], "foo.bar.baz")).toBeUndefined()
|
|
|
|
expect(atom.syntax.getProperty([".source.coffee", ".string.quoted.double.coffee"], "foo.bar.baz")).toBe 42
|