From e69b6e37f295eff46dc9c8edecf348860f393f02 Mon Sep 17 00:00:00 2001 From: nightwing Date: Thu, 14 Mar 2013 20:55:50 +0400 Subject: [PATCH] add tests for LogiQL mode --- lib/ace/mode/_test/tokens_logiql.json | 190 ++++++++++++++++++++++++++ lib/ace/mode/livescript.js | 8 +- lib/ace/mode/logiql.js | 13 +- lib/ace/mode/logiql_test.js | 99 ++++++++++++++ lib/ace/test/all_browser.js | 1 + 5 files changed, 305 insertions(+), 6 deletions(-) create mode 100644 lib/ace/mode/_test/tokens_logiql.json create mode 100644 lib/ace/mode/logiql_test.js diff --git a/lib/ace/mode/_test/tokens_logiql.json b/lib/ace/mode/_test/tokens_logiql.json new file mode 100644 index 00000000..5f7eda49 --- /dev/null +++ b/lib/ace/mode/_test/tokens_logiql.json @@ -0,0 +1,190 @@ +[[ + "start", + ["comment.single","// ancestors"] +],[ + "start", + ["entity.name","parentof"], + ["text","("], + ["string","\"douglas\""], + ["keyword.other",","], + ["text"," "], + ["string","\"john\""], + ["text",")"], + ["keyword.end","."] +],[ + "start", + ["entity.name","parentof"], + ["text","("], + ["string","\"john\""], + ["keyword.other",","], + ["text"," "], + ["string","\"bob\""], + ["text",")"], + ["keyword.end","."] +],[ + "start", + ["entity.name","parentof"], + ["text","("], + ["string","\"bob\""], + ["keyword.other",","], + ["text"," "], + ["string","\"ebbon\""], + ["text",")"], + ["keyword.end","."] +],[ + "start" +],[ + "start", + ["entity.name","parentof"], + ["text","("], + ["string","\"douglas\""], + ["keyword.other",","], + ["text"," "], + ["string","\"jane\""], + ["text",")"], + ["keyword.end","."] +],[ + "start", + ["entity.name","parentof"], + ["text","("], + ["string","\"jane\""], + ["keyword.other",","], + ["text"," "], + ["string","\"jan\""], + ["text",")"], + ["keyword.end","."] +],[ + "start" +],[ + "start", + ["entity.name","ancestorof"], + ["text","("], + ["variable.parameter","A"], + ["keyword.other",","], + ["text"," "], + ["variable.parameter","B"], + ["text",") "], + ["keyword.start","<-"], + ["text"," "], + ["entity.name","parentof"], + ["text","("], + ["variable.parameter","A"], + ["keyword.other",","], + ["text"," "], + ["variable.parameter","B"], + ["text",")"], + ["keyword.end","."] +],[ + "start", + ["entity.name","ancestorof"], + ["text","("], + ["variable.parameter","A"], + ["keyword.other",","], + ["text"," "], + ["variable.parameter","C"], + ["text",") "], + ["keyword.start","<-"], + ["text"," "], + ["entity.name","ancestorof"], + ["text","("], + ["variable.parameter","A"], + ["keyword.other",","], + ["text"," "], + ["variable.parameter","B"], + ["text",")"], + ["keyword.other",","], + ["text"," "], + ["entity.name","parentof"], + ["text","("], + ["variable.parameter","B"], + ["keyword.other",","], + ["variable.parameter","C"], + ["text",")"], + ["keyword.end","."] +],[ + "start" +],[ + "start", + ["entity.name","grandparentof"], + ["text","("], + ["variable.parameter","A"], + ["keyword.other",","], + ["text"," "], + ["variable.parameter","B"], + ["text",") "], + ["keyword.start","<-"], + ["text"," "], + ["entity.name","parentof"], + ["text","("], + ["variable.parameter","A"], + ["keyword.other",","], + ["text"," "], + ["variable.parameter","C"], + ["text",")"], + ["keyword.other",","], + ["text"," "], + ["entity.name","parentof"], + ["text","("], + ["variable.parameter","C"], + ["keyword.other",","], + ["text"," "], + ["variable.parameter","B"], + ["text",")"], + ["keyword.end","."] +],[ + "start" +],[ + "start", + ["entity.name","cousins"], + ["text","("], + ["variable.parameter","A"], + ["keyword.other",","], + ["variable.parameter","B"], + ["text",") "], + ["keyword.start","<-"], + ["text"," "], + ["entity.name","grandparentof"], + ["text","("], + ["variable.parameter","C"], + ["keyword.other",","], + ["variable.parameter","A"], + ["text",")"], + ["keyword.other",","], + ["text"," "], + ["entity.name","grandparentof"], + ["text","("], + ["variable.parameter","C"], + ["keyword.other",","], + ["variable.parameter","B"], + ["text",")"], + ["keyword.end","."] +],[ + "start" +],[ + "start", + ["entity.name","parentof"], + ["text","["], + ["entity.name.type.logicblox","`arg"], + ["text","]("], + ["variable.parameter","A"], + ["keyword.other",","], + ["text"," "], + ["variable.parameter","B"], + ["text",") "], + ["keyword.start","->"], + ["text"," "], + ["entity.name","int"], + ["text","["], + ["constant.numeric","32"], + ["text","]("], + ["variable.parameter","A"], + ["text",")"], + ["keyword.other",","], + ["text"," "], + ["keyword.other","!"], + ["entity.name","string"], + ["text","("], + ["variable.parameter","B"], + ["text",")"], + ["keyword.end","."] +]] \ No newline at end of file diff --git a/lib/ace/mode/livescript.js b/lib/ace/mode/livescript.js index 4efd5480..5f759d9e 100644 --- a/lib/ace/mode/livescript.js +++ b/lib/ace/mode/livescript.js @@ -5,8 +5,8 @@ define(function(require, exports, module){ var indenter, prototype = extend$((import$(LiveScriptMode, superclass).displayName = 'LiveScriptMode', LiveScriptMode), superclass).prototype, constructor = LiveScriptMode; function LiveScriptMode(){ var that; - this.$tokenizer = new (require('ace/tokenizer')).Tokenizer(LiveScriptMode.Rules); - if (that = require('ace/mode/matching_brace_outdent')) { + this.$tokenizer = new (require('../tokenizer')).Tokenizer(LiveScriptMode.Rules); + if (that = require('../mode/matching_brace_outdent')) { this.$outdent = new that.MatchingBraceOutdent; } } @@ -25,7 +25,7 @@ define(function(require, exports, module){ prototype.toggleCommentLines = function(state, doc, startRow, endRow){ var comment, range, i$, i, out, line; comment = /^(\s*)#/; - range = new (require('ace/range')).Range(0, 0, 0, 0); + range = new (require('../range')).Range(0, 0, 0, 0); for (i$ = startRow; i$ <= endRow; ++i$) { i = i$; if (out = comment.test(line = doc.getLine(i))) { @@ -48,7 +48,7 @@ define(function(require, exports, module){ return (ref$ = this.$outdent) != null ? ref$.autoOutdent(doc, row) : void 8; }; return LiveScriptMode; - }(require('ace/mode/text').Mode)); + }(require('../mode/text').Mode)); keywordend = '(?![$\\w]|-[A-Za-z]|\\s*:(?![:=]))'; stringfill = { token: 'string', diff --git a/lib/ace/mode/logiql.js b/lib/ace/mode/logiql.js index 24bcd7d6..cdf6b607 100644 --- a/lib/ace/mode/logiql.js +++ b/lib/ace/mode/logiql.js @@ -36,8 +36,8 @@ var TextMode = require("./text").Mode; var Tokenizer = require("../tokenizer").Tokenizer; var LogiQLHighlightRules = require("./logiql_highlight_rules").LogiQLHighlightRules; var FoldMode = require("./folding/coffee").FoldMode; -var TokenIterator = require("ace/token_iterator").TokenIterator; -var Range = require("ace/range").Range; +var TokenIterator = require("../token_iterator").TokenIterator; +var Range = require("../range").Range; var Mode = function() { var highlighter = new LogiQLHighlightRules(); @@ -52,6 +52,15 @@ oop.inherits(Mode, TextMode); this.getNextLineIndent = function(state, line, tab) { var indent = this.$getIndent(line); + + var tokenizedLine = this.$tokenizer.getLineTokens(line, state); + var tokens = tokenizedLine.tokens; + var endState = tokenizedLine.state; + if (/comment|string/.test(endState)) + return indent; + if (tokens.length && tokens[tokens.length - 1].type == "comment.single") + return indent; + var match = line.match(); if (/(-->|<--|<-|->)\s*$/.test(line)) indent += tab; diff --git a/lib/ace/mode/logiql_test.js b/lib/ace/mode/logiql_test.js new file mode 100644 index 00000000..02e2d4d1 --- /dev/null +++ b/lib/ace/mode/logiql_test.js @@ -0,0 +1,99 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Distributed under the BSD license: + * + * Copyright (c) 2010, Ajax.org B.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Ajax.org B.V. nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ***** END LICENSE BLOCK ***** */ + +if (typeof process !== "undefined") { + require("amd-loader"); +} + +define(function(require, exports, module) { +"use strict"; + +var EditSession = require("../edit_session").EditSession; +var Tokenizer = require("../tokenizer").Tokenizer; +var LogiQLMode = require("./logiql").Mode; +var assert = require("../test/assertions"); + +module.exports = { + setUp : function() { + this.mode = new LogiQLMode(); + }, + + "test: toggle comment lines should prepend '//' to each line" : function() { + var session = new EditSession([" abc", "cde", "fg"]); + + this.mode.toggleCommentLines("start", session, 0, 1); + assert.equal(["// abc", "// cde", "fg"].join("\n"), session.toString()); + }, + + "test: auto indent after ->" : function() { + assert.equal(" ", this.mode.getNextLineIndent("start", "parent(a, b) ->", " ")); + }, + + "test: auto indent after <--" : function() { + assert.equal(" ", this.mode.getNextLineIndent("start", "foo <-- ", " ")); + }, + + "test: no auto indent in multi line comment" : function() { + assert.equal("", this.mode.getNextLineIndent("start", "/* -->", " ")); + assert.equal(" ", this.mode.getNextLineIndent("start", " /* ->", " ")); + assert.equal(" ", this.mode.getNextLineIndent("comment.block0", " abcd", " ")); + }, + + "test: no auto indent after -> in single line comment" : function() { + assert.equal("", this.mode.getNextLineIndent("start", "//->", " ")); + assert.equal(" ", this.mode.getNextLineIndent("start", " //->", " ")); + }, + + "test: trigger outdent if line ends with ." : function() { + assert.ok(this.mode.checkOutdent("start", " ", "\n")); + assert.ok(this.mode.checkOutdent("start", " a ", "\r\n")); + assert.ok(!this.mode.checkOutdent("start", "", "}")); + assert.ok(!this.mode.checkOutdent("start", " ", "a }")); + assert.ok(!this.mode.checkOutdent("start", " }", "}")); + }, + + "test: auto outdent should indent the line with the same indent as the line with the matching ->" : function() { + var session = new EditSession([" bar (a, b) ->", " foo(a)[1.2]", " bla.", " "], new LogiQLMode()); + this.mode.autoOutdent("start", session, 2); + assert.equal(" ", session.getLine(3)); + }, + + "test: no auto outdent if no matching brace is found" : function() { + var session = new EditSession([" bar (a, b) ->", " foo(a)[1.2].", " bla.", " "], new LogiQLMode()); + this.mode.autoOutdent("start", session, 2); + assert.equal(" ", session.getLine(3)); + } +}; + +}); + +if (typeof module !== "undefined" && module === require.main) { + require("asyncjs").test.testcase(module.exports).exec() +} diff --git a/lib/ace/test/all_browser.js b/lib/ace/test/all_browser.js index ce5857dc..e818c904 100644 --- a/lib/ace/test/all_browser.js +++ b/lib/ace/test/all_browser.js @@ -31,6 +31,7 @@ var testNames = [ "ace/mode/html_test", "ace/mode/javascript_test", "ace/mode/javascript_worker_test", + "ace/mode/logiql_test", "ace/mode/python_test", "ace/mode/text_test", "ace/mode/xml_test",