Skip to content

Commit

Permalink
[sql-hint addon] Add tests and fix codemirror#5817
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes authored and cone56 committed Jan 6, 2020
1 parent 409caec commit 9346fdd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 19 deletions.
41 changes: 23 additions & 18 deletions addon/hint/sql-hint.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,24 +275,29 @@
if (search.charAt(0) == "." || search.charAt(0) == identifierQuote) {
start = nameCompletion(cur, token, result, editor);
} else {
addMatches(result, search, defaultTable, function(w) {return {text:w, className: "CodeMirror-hint-table CodeMirror-hint-default-table"};});
addMatches(
result,
search,
tables,
function(w) {
if (typeof w === 'object') {
w.className = "CodeMirror-hint-table";
} else {
w = {text: w, className: "CodeMirror-hint-table"};
}

return w;
}
);
if (!disableKeywords)
addMatches(result, search, keywords, function(w) {return {text: w.toUpperCase(), className: "CodeMirror-hint-keyword"};});
}
var objectOrClass = function(w, className) {
if (typeof w === "object") {
w.className = className;
} else {
w = { text: w, className: className };
}
return w;
};
addMatches(result, search, defaultTable, function(w) {
return objectOrClass(w, "CodeMirror-hint-table CodeMirror-hint-default-table");
});
addMatches(
result,
search,
tables, function(w) {
return objectOrClass(w, "CodeMirror-hint-table");
}
);
if (!disableKeywords)
addMatches(result, search, keywords, function(w) {
return objectOrClass(w.toUpperCase(), "CodeMirror-hint-keyword");
});
}

return {list: result, from: Pos(cur.line, start), to: Pos(cur.line, end)};
});
Expand Down
62 changes: 61 additions & 1 deletion test/sql-hint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,44 @@
{text: "name", displayText: "name | The name"}]
}];

var displayTextTablesWithDefault = [
{
text: "Api__TokenAliases",
columns: [
{
text: "token",
displayText: "token | varchar(255) | Primary",
columnName: "token",
columnHint: "varchar(255) | Primary"
},
{
text: "alias",
displayText: "alias | varchar(255) | Primary",
columnName: "alias",
columnHint: "varchar(255) | Primary"
}
]
},
{
text: "mytable",
columns: [
{ text: "id", displayText: "id | Unique ID" },
{ text: "name", displayText: "name | The name" }
]
}
];

namespace = "sql-hint_";

function test(name, spec) {
testCM(name, function(cm) {
cm.setValue(spec.value);
cm.setCursor(spec.cursor);
var completion = CodeMirror.hint.sql(cm, {tables: spec.tables});
var completion = CodeMirror.hint.sql(cm, {
tables: spec.tables,
defaultTable: spec.defaultTable,
disableKeywords: spec.disableKeywords
});
if (!deepCompare(completion.list, spec.list))
throw new Failure("Wrong completion results " + JSON.stringify(completion.list) + " vs " + JSON.stringify(spec.list));
eqCharPos(completion.from, spec.from);
Expand All @@ -46,6 +77,15 @@
to: Pos(0, 3)
});

test("keywords_disabled", {
value: "SEL",
cursor: Pos(0, 3),
disableKeywords: true,
list: [],
from: Pos(0, 0),
to: Pos(0, 3)
});

test("from", {
value: "SELECT * fr",
cursor: Pos(0, 11),
Expand Down Expand Up @@ -185,6 +225,26 @@
mode: "text/x-sqlite"
});

test("displayText_default_table", {
value: "SELECT a",
cursor: Pos(0, 8),
disableKeywords: true,
defaultTable: "Api__TokenAliases",
tables: displayTextTablesWithDefault,
list: [
{
text: "alias",
displayText: "alias | varchar(255) | Primary",
columnName: "alias",
columnHint: "varchar(255) | Primary",
className: "CodeMirror-hint-table CodeMirror-hint-default-table"
},
{ text: "Api__TokenAliases", className: "CodeMirror-hint-table" }
],
from: Pos(0, 7),
to: Pos(0, 8)
});

test("displayText_table", {
value: "SELECT myt",
cursor: Pos(0, 10),
Expand Down

0 comments on commit 9346fdd

Please sign in to comment.