mirror of
https://github.com/wassname/nvatom.git
synced 2026-06-27 16:10:36 +08:00
Add tests for notelinks
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
'include': 'source.gfm'
|
||||
}
|
||||
{
|
||||
'match': '(\\[\\[)([^\\[\\]\|]+)(\\]\\])'
|
||||
'match': '(?<!\\[)(\\[\\[)([^\\[\\]\|]+)(\\]\\])(?!\\])'
|
||||
'name': 'interlink'
|
||||
'captures':
|
||||
'1':
|
||||
|
||||
+7
-5
@@ -6,17 +6,17 @@ module.exports =
|
||||
class NoteLink
|
||||
constructor: ->
|
||||
@subscriptions = new CompositeDisposable
|
||||
@subscriptions.add atom.commands.add 'atom-workspace', 'nvatom:openInterlink': => @openInterlink()
|
||||
@subscriptions.add atom.commands.add 'atom-workspace', 'nvatom:openInterlink': => NoteLink.openInterlink()
|
||||
|
||||
destroy: ->
|
||||
@subscriptions.dispose()
|
||||
|
||||
openInterlink: ->
|
||||
@openInterlink: ->
|
||||
editor = atom.workspace.getActiveTextEditor()
|
||||
return unless editor?
|
||||
return unless Utility.isNote(editor.getPath())
|
||||
|
||||
noteTitle = @getInterlinkUnderCursor(editor)
|
||||
noteTitle = NoteLink.getInterlinkUnderCursor(editor)
|
||||
return unless noteTitle?
|
||||
return unless noteTitle.length
|
||||
|
||||
@@ -26,11 +26,13 @@ class NoteLink
|
||||
fs.writeFileSync(notePath, '')
|
||||
atom.workspace.open(notePath)
|
||||
|
||||
getInterlinkUnderCursor: (editor) ->
|
||||
@getInterlinkUnderCursor: (editor) ->
|
||||
cursorPosition = editor.getCursorBufferPosition()
|
||||
token = editor.tokenForBufferPosition(cursorPosition)
|
||||
return unless token
|
||||
return unless token.value
|
||||
return unless token.scopes.indexOf('markup.underline.link.interlink.gfm') > -1
|
||||
|
||||
return token.value
|
||||
interlink = Utility.trim(token.value)
|
||||
return unless interlink.length
|
||||
return interlink
|
||||
|
||||
+3
-1
@@ -10,6 +10,8 @@ class Utility
|
||||
|
||||
@getPrimaryNoteExtension: -> if atom.config.get('nvatom.extensions').length then atom.config.get('nvatom.extensions')[0] else '.md'
|
||||
|
||||
@isNote: (filePath) -> filePath.startsWith(Utility.getNoteDirectory()) and path.extname(filePath) in atom.config.get('nvatom.extensions')
|
||||
@isNote: (filePath) ->
|
||||
filePath = fs.normalize(filePath)
|
||||
return filePath.startsWith(Utility.getNoteDirectory()) and path.extname(filePath) in atom.config.get('nvatom.extensions')
|
||||
|
||||
@trim: (str) -> str?.replace /^\s+|\s+$/g, ''
|
||||
|
||||
+3
-1
@@ -38,5 +38,7 @@
|
||||
"fs-plus": "2.x",
|
||||
"underscore-plus": "^1.6.6"
|
||||
},
|
||||
"devDependencies": {}
|
||||
"devDependencies": {
|
||||
"temp": "0.8.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
fs = require 'fs-plus'
|
||||
path = require 'path'
|
||||
temp = require 'temp'
|
||||
NoteLink = require '../lib/notelink'
|
||||
|
||||
describe 'Notelink', ->
|
||||
defaultDirectory = atom.config.get('nvatom.directory')
|
||||
noteDirectory = null
|
||||
|
||||
beforeEach ->
|
||||
workspaceElement = atom.views.getView(atom.workspace)
|
||||
activationPromise = atom.packages.activatePackage('nvatom')
|
||||
noteDirectory = temp.mkdirSync()
|
||||
|
||||
atom.config.set('nvatom.directory', noteDirectory)
|
||||
|
||||
atom.commands.dispatch workspaceElement, 'nvatom:toggle'
|
||||
waitsForPromise ->
|
||||
activationPromise
|
||||
|
||||
afterEach ->
|
||||
atom.config.set('nvatom.directory', defaultDirectory)
|
||||
|
||||
describe 'when getInerlinkUnderCursor is called', ->
|
||||
editor = null
|
||||
|
||||
beforeEach ->
|
||||
waitsForPromise ->
|
||||
atom.workspace.open(path.join(noteDirectory, 'Interlink.md')).then (o) -> editor = o
|
||||
|
||||
it 'returns a trimmed interlink text', ->
|
||||
testdata = [
|
||||
{ position: [0, 2], text: '[[Car]]', expected: 'Car' },
|
||||
{ position: [0, 2], text: '[[Notational Velocity]]', expected: 'Notational Velocity' },
|
||||
{ position: [0, 2], text: '[[한글 Alphabet Test]]', expected: '한글 Alphabet Test' },
|
||||
{ position: [0, 2], text: '[[ Car ]]', expected: 'Car' },
|
||||
{ position: [0, 2], text: '[[Car/Mini]]', expected: 'Car/Mini' },
|
||||
]
|
||||
|
||||
for testitem in testdata
|
||||
editor.setText testitem.text
|
||||
editor.setCursorBufferPosition testitem.position
|
||||
expect(NoteLink.getInterlinkUnderCursor(editor)).toBe testitem.expected
|
||||
|
||||
it 'returns undefined for invalid text', ->
|
||||
testdata = [
|
||||
{ position: [0, 2], text: '[[]]' },
|
||||
{ position: [0, 3], text: '[[]]' },
|
||||
{ position: [0, 2], text: '[[ ]]' },
|
||||
{ position: [0, 1], text: '[Car]' },
|
||||
{ position: [0, 2], text: '[[[Car]]]' },
|
||||
{ position: [0, 2], text: '[[Car]' },
|
||||
{ position: [0, 2], text: '[[Car]]]' },
|
||||
{ position: [0, 1], text: 'Car' },
|
||||
]
|
||||
|
||||
for testitem in testdata
|
||||
editor.setText testitem.text
|
||||
editor.setCursorBufferPosition testitem.position
|
||||
expect(NoteLink.getInterlinkUnderCursor(editor)).toBe undefined
|
||||
|
||||
describe 'when openInterlink is called', ->
|
||||
describe 'when the editor path is under the note directory', ->
|
||||
editor = null
|
||||
|
||||
beforeEach ->
|
||||
waitsForPromise ->
|
||||
atom.workspace.open(path.join(noteDirectory, 'Interlink.md')).then (o) -> editor = o
|
||||
|
||||
it 'opens the referred notes', ->
|
||||
editor.setText '[[Car]]'
|
||||
editor.setCursorBufferPosition [0, 2]
|
||||
|
||||
editorPromise = NoteLink.openInterlink()
|
||||
expect(editorPromise).not.toBe undefined
|
||||
waitsForPromise ->
|
||||
editorPromise
|
||||
|
||||
runs ->
|
||||
expect(atom.workspace.getActiveTextEditor().getPath().endsWith('Car.md')).toBe true
|
||||
|
||||
describe 'when the editor path is not under the note directory', ->
|
||||
editor = null
|
||||
|
||||
beforeEach ->
|
||||
waitsForPromise ->
|
||||
atom.workspace.open(path.join(temp.mkdirSync(), 'Interlink.md')).then (o) -> editor = o
|
||||
|
||||
it 'does nothing', ->
|
||||
editor.setText '[[Car]]'
|
||||
editor.setCursorBufferPosition [0, 2]
|
||||
|
||||
editorPromise = NoteLink.openInterlink()
|
||||
expect(editorPromise).toBe undefined
|
||||
Reference in New Issue
Block a user