Add spec for grammar score tie-breaking

This commit is contained in:
Kevin Sawicki
2015-05-18 13:49:02 -07:00
parent 4378255ab6
commit 00505188fa
4 changed files with 26 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
'name': 'Test Ruby'
'scopeName': 'test.rb'
'fileTypes': [
'rb'
]
'patterns': [
{
'match': 'ruby'
'name': 'meta.class.ruby'
}
]

View File

@@ -0,0 +1,4 @@
{
"name": "package-with-rb-filetype",
"version": "1.0.0"
}

View File

@@ -94,6 +94,16 @@ describe "the `grammars` global", ->
grammar2 = atom.grammars.loadGrammarSync(grammarPath2)
expect(atom.grammars.selectGrammar('more.test', '')).toBe grammar2
it "favors non-bundled packages when breaking scoring ties", ->
waitsForPromise ->
atom.packages.activatePackage(path.join(__dirname, 'fixtures', 'packages', 'package-with-rb-filetype'))
runs ->
atom.grammars.grammarForScopeName('source.ruby').bundledPackage = true
atom.grammars.grammarForScopeName('test.rb').bundledPackage = false
expect(atom.grammars.selectGrammar('test.rb').scopeName).toBe 'test.rb'
describe "when there is no file path", ->
it "does not throw an exception (regression)", ->
expect(-> atom.grammars.selectGrammar(null, '#!/usr/bin/ruby')).not.toThrow()

View File

@@ -42,6 +42,7 @@ class GrammarRegistry extends FirstMate.GrammarRegistry
score = grammar.getScore(filePath, fileContents)
if score > highestScore or not bestMatch?
bestMatch = grammar
highestScore = score
else if score is highestScore and bestMatch?.bundledPackage
bestMatch = grammar unless grammar.bundledPackage
bestMatch