-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathunittests.js
More file actions
1 lines (1 loc) · 22.3 KB
/
unittests.js
File metadata and controls
1 lines (1 loc) · 22.3 KB
1
define(function(require,exports,module){var SpecRunnerUtils=brackets.getModule("spec/SpecRunnerUtils"),testContentCSS=require("text!unittest-files/regions.css"),testContentHTML=require("text!unittest-files/region-template.html"),CSSCodeHints=require("main");describe("unit:CSS Code Hinting",function(){var defaultContent="@media screen { \n body { \n }\n} \n.selector { \n \n b\n bord\n border-\n border-colo\n border-color: red;\n d\n disp\n display: \n display: in\n bordborder: \n color\n} \n",defaultHTMLContent="<html> \n<head> \n</head> \n<body> \n<div style=' \n \n b\n bord\n border-\n border-colo\n border-color: red;'>\n</div> \n</body> \n</html> \n",testDocument,testEditor;function setupTest(content,languageId){var mock=SpecRunnerUtils.createMockEditor(content,languageId);testDocument=mock.doc,testEditor=mock.editor}function tearDownTest(){SpecRunnerUtils.destroyMockEditor(testDocument),testEditor=null,testDocument=null}function extractHintList(hints){return $.map(hints,function($node){return $node.data("val")})}function expectHints(provider,implicitChar,returnWholeObj){expect(provider.hasHints(testEditor,implicitChar)).toBe(!0);var hintsObj=provider.getHints();return expect(hintsObj).toBeTruthy(),returnWholeObj?hintsObj:extractHintList(hintsObj.hints)}function expectNoHints(provider,implicitChar){expect(provider.hasHints(testEditor,implicitChar)).toBe(!1)}function verifyAttrHints(hintList,expectedFirstHint){expect(hintList.indexOf("div")).toBe(-1),expect(hintList[0]).toBe(expectedFirstHint)}function verifySecondAttrHint(hintList,expectedSecondHint){expect(hintList.indexOf("div")).toBe(-1),expect(hintList[1]).toBe(expectedSecondHint)}function selectHint(provider,expectedHint,implicitChar){var hintList=expectHints(provider,implicitChar);return expect(hintList.indexOf(expectedHint)).not.toBe(-1),provider.insertHint(expectedHint)}function fixPos(pos){return"sticky"in pos||(pos.sticky=null),pos}function expectCursorAt(pos){var selection=testEditor.getSelection();expect(fixPos(selection.start)).toEql(fixPos(selection.end)),expect(fixPos(selection.start)).toEql(fixPos(pos))}function verifyAllValues(hintList,values){expect(hintList.length).toBe(values.length),expect(hintList.sort().toString()).toBe(values.sort().toString())}describe("CSS properties in general (selection of correct property based on input)",function(){beforeEach(function(){var mock=SpecRunnerUtils.createMockEditor(defaultContent,"css");testEditor=mock.editor,testDocument=mock.doc}),afterEach(function(){SpecRunnerUtils.destroyMockEditor(testDocument),testEditor=null,testDocument=null}),it("should list all prop-name hints right after curly bracket",function(){var hintList;testEditor.setCursorPos({line:4,ch:11}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"display")}),it("should list all prop-name hints in new line",function(){var hintList;testEditor.setCursorPos({line:5,ch:1}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"display")}),it("should list all prop-name hints starting with 'b' in new line",function(){var hintList;testEditor.setCursorPos({line:6,ch:2}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"bottom")}),it("should list the second prop-name hint starting with 'b'",function(){var hintList;testEditor.setCursorPos({line:6,ch:2}),verifySecondAttrHint(expectHints(CSSCodeHints.cssPropHintProvider),"background-color")}),it("should list all prop-name hints starting with 'bord' ",function(){var hintList;testDocument.replaceRange(";",{line:6,ch:2}),testEditor.setCursorPos({line:7,ch:5}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"border")}),it("should list all prop-name hints starting with 'border-' ",function(){var hintList;testDocument.replaceRange(";",{line:7,ch:5}),testEditor.setCursorPos({line:8,ch:8}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"border-radius")}),it("should list only prop-name hint border-color",function(){testDocument.replaceRange(";",{line:8,ch:8}),testEditor.setCursorPos({line:9,ch:12});var hintList=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hintList,"border-color"),expect(hintList.length).toBe(17),expect(hintList[0]).toBe("border-color"),expect(hintList[1]).toBe("border-collapse: collapse;")}),it("should list prop-name hints at end of property-value finished by ;",function(){var hintList;testEditor.setCursorPos({line:10,ch:19}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"display")}),it("should NOT list prop-name hints right before curly bracket",function(){testEditor.setCursorPos({line:4,ch:10}),expectNoHints(CSSCodeHints.cssPropHintProvider)}),it("should NOT list prop-name hints after declaration of mediatype",function(){testEditor.setCursorPos({line:0,ch:15}),expectNoHints(CSSCodeHints.cssPropHintProvider)}),it("should NOT list prop-name hints if previous property is not closed properly",function(){testEditor.setCursorPos({line:16,ch:6}),expectNoHints(CSSCodeHints.cssPropHintProvider)}),it("should NOT list prop-name hints in media type declaration",function(){testEditor.setCursorPos({line:0,ch:1}),expect(CSSCodeHints.cssPropHintProvider.hasHints(testEditor,"m")).toBe(!1)})}),describe("CSS property hint insertion",function(){beforeEach(function(){var mock=SpecRunnerUtils.createMockEditor(defaultContent,"css");testEditor=mock.editor,testDocument=mock.doc}),afterEach(function(){SpecRunnerUtils.destroyMockEditor(testDocument),testEditor=null,testDocument=null}),it("should insert colon prop-name selected",function(){testDocument.replaceRange(";",{line:6,ch:2}),testEditor.setCursorPos({line:7,ch:5}),selectHint(CSSCodeHints.cssPropHintProvider,"border"),expect(testDocument.getLine(7)).toBe(" border: "),expectCursorAt({line:7,ch:9})}),it("should not insert semicolon after prop-value selected",function(){testDocument.replaceRange(";",{line:12,ch:5}),testEditor.setCursorPos({line:13,ch:10}),selectHint(CSSCodeHints.cssPropHintProvider,"block"),expect(testDocument.getLine(13)).toBe(" display: block")}),it("should insert full prop-value hint",function(){testDocument.replaceRange(";",{line:12,ch:5}),testEditor.setCursorPos({line:13,ch:5}),selectHint(CSSCodeHints.cssPropHintProvider,"display: block;"),expect(testDocument.getLine(13)).toBe(" display: block;: ")}),it("should insert prop-name directly after semicolon",function(){testEditor.setCursorPos({line:10,ch:19}),selectHint(CSSCodeHints.cssPropHintProvider,"display"),expect(testDocument.getLine(10)).toBe(" border-color: red;display: ")}),it("should insert nothing but the closure(semicolon) if prop-value is fully written",function(){testDocument.replaceRange(";",{line:15,ch:13}),testEditor.setCursorPos({line:16,ch:6}),selectHint(CSSCodeHints.cssPropHintProvider,"color"),expect(testDocument.getLine(16)).toBe(" color: "),expectCursorAt({line:16,ch:8})}),it("should insert prop-name before an existing one",function(){testEditor.setCursorPos({line:10,ch:1}),selectHint(CSSCodeHints.cssPropHintProvider,"margin"),expect(testDocument.getLine(10)).toBe(" margin: border-color: red;"),expectCursorAt({line:10,ch:9})}),it("should insert prop-name before an existing one when invoked with an implicit character",function(){testDocument.replaceRange("f",{line:10,ch:1}),testEditor.setCursorPos({line:10,ch:2}),selectHint(CSSCodeHints.cssPropHintProvider,"float","f"),expect(testDocument.getLine(10)).toBe(" float: border-color: red;"),expectCursorAt({line:10,ch:8})}),it("should replace the existing prop-value with the new selection",function(){testDocument.replaceRange(";",{line:12,ch:5}),testDocument.replaceRange("block",{line:13,ch:10}),testEditor.setCursorPos({line:13,ch:10}),selectHint(CSSCodeHints.cssPropHintProvider,"none"),expect(testDocument.getLine(13)).toBe(" display: none"),expectCursorAt({line:13,ch:14})})}),describe("CSS prop-value hints",function(){beforeEach(function(){var mock=SpecRunnerUtils.createMockEditor(defaultContent,"css");testEditor=mock.editor,testDocument=mock.doc}),afterEach(function(){SpecRunnerUtils.destroyMockEditor(testDocument),testEditor=null,testDocument=null}),it("should list all prop-values for 'display' after colon",function(){var hintList;testDocument.replaceRange(";",{line:12,ch:5}),testEditor.setCursorPos({line:13,ch:9}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"block")}),it("should list all prop-values for 'display' after colon and whitespace",function(){var hintList;testDocument.replaceRange(";",{line:12,ch:5}),testEditor.setCursorPos({line:13,ch:10}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"block")}),it("should list all prop-values starting with 'in' for 'display' after colon and whitespace",function(){var hintList;testDocument.replaceRange(";",{line:13,ch:10}),testEditor.setCursorPos({line:14,ch:12}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"inline")}),it("should NOT list prop-value hints for unknown prop-name",function(){testEditor.setCursorPos({line:15,ch:12}),expectNoHints(CSSCodeHints.cssPropHintProvider)})}),describe("CSS hint provider inside mixed htmlfiles",function(){var defaultContent="<html> \n<head><style>.selector{display: none;}</style></head> \n<body> <style> \n body { \n background-color: red; \n \n} \n</style>\n<div class='selector'></div>\n<style> .foobar { \n colo </style>\n</body></html>";beforeEach(function(){var mock=SpecRunnerUtils.createMockEditor(defaultContent,"html");testEditor=mock.editor,testDocument=mock.doc}),afterEach(function(){SpecRunnerUtils.destroyMockEditor(testDocument),testEditor=null,testDocument=null}),it("should list prop-name hints right after curly bracket",function(){testEditor.setCursorPos({line:3,ch:7}),expectHints(CSSCodeHints.cssPropHintProvider)}),it("should list prop-name hints inside single-line styletags at start",function(){testEditor.setCursorPos({line:1,ch:23}),expectHints(CSSCodeHints.cssPropHintProvider)}),it("should list prop-name hints inside single-line styletags after semicolon",function(){testEditor.setCursorPos({line:1,ch:37}),expectHints(CSSCodeHints.cssPropHintProvider)}),it("should list prop-name hints inside multi-line styletags with cursor in first line",function(){testEditor.setCursorPos({line:9,ch:18}),expectHints(CSSCodeHints.cssPropHintProvider)}),it("should list prop-name hints inside multi-line styletags with cursor in last line",function(){testEditor.setCursorPos({line:10,ch:5});var hintList=expectHints(CSSCodeHints.cssPropHintProvider);expect(hintList.length).toBe(50),expect(hintList[0]).toBe("color"),expect(hintList[1]).toBe("color-adjust")}),it("should NOT list prop-name hints between closed styletag and new opening styletag",function(){testEditor.setCursorPos({line:8,ch:0}),expectNoHints(CSSCodeHints.cssPropHintProvider)}),it("should NOT list hints right before curly bracket",function(){testEditor.setCursorPos({line:3,ch:6}),expectNoHints(CSSCodeHints.cssPropHintProvider)}),it("should NOT list hints inside head-tag",function(){testEditor.setCursorPos({line:1,ch:6}),expectNoHints(CSSCodeHints.cssPropHintProvider)})}),describe("CSS Hint provider in style attribute value context for html mode",function(){beforeEach(function(){var mock=SpecRunnerUtils.createMockEditor(defaultHTMLContent,"html");testEditor=mock.editor,testDocument=mock.doc}),afterEach(function(){SpecRunnerUtils.destroyMockEditor(testDocument),testEditor=null,testDocument=null}),it("should list all prop-name hints right after the open quote for style value context",function(){var hintList;testEditor.setCursorPos({line:4,ch:12}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"display")}),it("should list all prop-name hints in new line for style value context",function(){var hintList;testEditor.setCursorPos({line:5,ch:0}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"display")}),it("should list all prop-name hints starting with 'b' in new line for style value context",function(){var hintList;testEditor.setCursorPos({line:6,ch:2}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"bottom")}),it("should list the second prop-name hint starting with 'b' for style value context",function(){var hintList;testEditor.setCursorPos({line:6,ch:2}),verifySecondAttrHint(expectHints(CSSCodeHints.cssPropHintProvider),"background-color")}),it("should list all prop-name hints starting with 'bord' for style value context",function(){var hintList;testDocument.replaceRange(";",{line:6,ch:2}),testEditor.setCursorPos({line:7,ch:5}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"border")}),it("should list all prop-name hints starting with 'border-' for style value context",function(){var hintList;testDocument.replaceRange(";",{line:7,ch:5}),testEditor.setCursorPos({line:8,ch:8}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"border-radius")}),it("should list only prop-name hint border-color for style value context",function(){testDocument.replaceRange(";",{line:8,ch:8}),testEditor.setCursorPos({line:9,ch:12});var hintList=expectHints(CSSCodeHints.cssPropHintProvider);expect(hintList.length).toBe(17),expect(hintList[0]).toBe("border-color"),expect(hintList[1]).toBe("border-collapse: collapse;")}),it("should list prop-name hints at end of property-value finished by ; for style value context",function(){var hintList;testEditor.setCursorPos({line:10,ch:19}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"display")}),it("should NOT list prop-name hints right before style value context",function(){testEditor.setCursorPos({line:4,ch:11}),expectNoHints(CSSCodeHints.cssPropHintProvider)}),it("should NOT list prop-name hints after style value context",function(){testEditor.setCursorPos({line:10,ch:20}),expectNoHints(CSSCodeHints.cssPropHintProvider)})}),describe("CSS hint provider in other filecontext (e.g. javascript)",function(){var defaultContent="function foobar (args) { \n /* do sth */ \n return 1; \n} \n";beforeEach(function(){var mock=SpecRunnerUtils.createMockEditor(defaultContent,"javascript");testEditor=mock.editor,testDocument=mock.doc}),afterEach(function(){SpecRunnerUtils.destroyMockEditor(testDocument),testEditor=null,testDocument=null}),it("should NOT list hints after function declaration",function(){testEditor.setCursorPos({line:0,ch:24}),expectNoHints(CSSCodeHints.cssPropHintProvider)})}),describe("CSS hint provider cursor placement inside value functions",function(){var defaultContent=".selector { \nshape-inside:\n}\n";beforeEach(function(){var mock=SpecRunnerUtils.createMockEditor(defaultContent,"css");testEditor=mock.editor,testDocument=mock.doc}),afterEach(function(){SpecRunnerUtils.destroyMockEditor(testDocument),testEditor=null,testDocument=null}),it("should should place the cursor between the parens of the value function",function(){var expectedString="shape-inside:polygon()";testEditor.setCursorPos({line:1,ch:15}),expectHints(CSSCodeHints.cssPropHintProvider),selectHint(CSSCodeHints.cssPropHintProvider,"polygon()"),expect(testDocument.getLine(1).length).toBe(expectedString.length),expect(testDocument.getLine(1)).toBe(expectedString),expectCursorAt({line:1,ch:expectedString.length-1})})}),describe("CSS hint provider for regions and exclusions",function(){var defaultContent=".selector { \n shape-inside: \n; shape-outside: \n; region-fragment: \n; region-break-after: \n; region-break-inside: \n; region-break-before: \n; -ms-region\n; -webkit-region\n; flow-from: \n; flow-into: \n;}\n";beforeEach(function(){var mock=SpecRunnerUtils.createMockEditor(defaultContent,"css");testEditor=mock.editor,testDocument=mock.doc}),afterEach(function(){SpecRunnerUtils.destroyMockEditor(testDocument),testEditor=null,testDocument=null}),it("should list 7 value-name hints for shape-inside",function(){testEditor.setCursorPos({line:1,ch:15});var hintList=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hintList,"auto"),verifyAllValues(hintList,["auto","calc()","circle()","ellipse()","inherit","initial","outside-shape","polygon()","rectangle()","unset","var()"])}),it("should list 36 value-name hints for shape-outside",function(){testEditor.setCursorPos({line:2,ch:16});var hintList=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hintList,"border-box"),verifyAllValues(hintList,["-moz-element()","-moz-linear-gradient()","-moz-radial-gradient()","-moz-repeating-linear-gradient()","-moz-repeating-radial-gradient()","-o-linear-gradient()","-o-repeating-linear-gradient()","-webkit-gradient()","-webkit-image-set()","-webkit-linear-gradient()","-webkit-radial-gradient()","-webkit-repeating-linear-gradient()","-webkit-repeating-radial-gradient()","border-box","calc()","circle()","content-box","cross-fade()","element()","ellipse()","image()","image-set()","inherit","initial","inset()","linear-gradient()","margin-box","none","padding-box","polygon()","radial-gradient()","repeating-linear-gradient()","repeating-radial-gradient()","unset","url()","var()"])}),it("should list 2 value-name hints for region-fragment",function(){testEditor.setCursorPos({line:3,ch:18});var hintList=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hintList,"auto"),verifyAllValues(hintList,["auto","break","calc()","inherit","initial","unset","var()"])}),it("should list 11 value-name hints for region-break-after",function(){testEditor.setCursorPos({line:4,ch:21});var hintList=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hintList,"always"),verifyAllValues(hintList,["always","auto","avoid","avoid-column","avoid-page","avoid-region","calc()","column","inherit","initial","left","page","region","right","unset","var()"])}),it("should list 5 value-name hints for region-break-inside",function(){testEditor.setCursorPos({line:5,ch:22});var hintList=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hintList,"auto"),verifyAllValues(hintList,["auto","avoid","avoid-column","avoid-page","avoid-region","calc()","inherit","initial","unset","var()"])}),it("should list 11 value-name hints for region-break-before",function(){testEditor.setCursorPos({line:6,ch:23});var hintList=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hintList,"always"),verifyAllValues(hintList,["always","auto","avoid","avoid-column","avoid-page","avoid-region","calc()","column","inherit","initial","left","page","region","right","unset","var()"])})}),describe("Color names and swatches in a CSS file",function(){beforeEach(function(){setupTest(testContentCSS,"css")}),afterEach(function(){tearDownTest()}),it("should list color names for color",function(){var hintList;testEditor.setCursorPos({line:98,ch:11}),verifyAttrHints(expectHints(CSSCodeHints.cssPropHintProvider),"aliceblue")}),it("should show color swatches for background-color",function(){testEditor.setCursorPos({line:99,ch:22});var hints=expectHints(CSSCodeHints.cssPropHintProvider,void 0,!0).hints;expect(hints[0].text()).toBe("aliceblue"),expect(hints[0].find(".color-swatch").length).toBe(1),expect(hints[0].find(".color-swatch").css("backgroundColor")).toMatch(/^rgb\(240, 248, 255\)$|aliceblue/)}),it("should filter out color names appropriately",function(){testEditor.setCursorPos({line:100,ch:27});var hintList=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hintList,"deeppink"),verifyAllValues(hintList,["deeppink","deepskyblue"])}),it("should always include transparent and currentColor and they should not have a swatch, but class no-swatch-margin",function(){testEditor.setCursorPos({line:101,ch:22});var hints=expectHints(CSSCodeHints.cssPropHintProvider,void 0,!0).hints,hintList=extractHintList(hints);verifyAttrHints(hintList,"currentColor"),verifyAllValues(hintList,["currentColor","transparent"]),expect(hints[0].find(".color-swatch").length).toBe(0),expect(hints[1].find(".color-swatch").length).toBe(0),expect(hints[0].hasClass("no-swatch-margin")).toBeTruthy(),expect(hints[1].hasClass("no-swatch-margin")).toBeTruthy()}),it("should insert color names correctly",function(){var expectedString=" border-left-color: deeppink;",line=100;testEditor.setCursorPos({line:100,ch:27}),expectHints(CSSCodeHints.cssPropHintProvider),selectHint(CSSCodeHints.cssPropHintProvider,"deeppink"),expect(testDocument.getLine(100).length).toBe(expectedString.length),expect(testDocument.getLine(100)).toBe(expectedString),expectCursorAt({line:100,ch:expectedString.length-1})}),it("should not display color names for unrelated properties",function(){testEditor.setCursorPos({line:102,ch:12});var hintList=expectHints(CSSCodeHints.cssPropHintProvider);expect(hintList.indexOf("aliceblue")).toBe(-1)})}),describe("Should not invoke CSS hints on space key",function(){beforeEach(function(){setupTest(testContentHTML,"html")}),afterEach(function(){tearDownTest()}),it("should not trigger CSS property name hints with space key",function(){testEditor.setCursorPos({line:25,ch:11}),expectNoHints(CSSCodeHints.cssPropHintProvider," ")}),it("should not trigger CSS property value hints with space key",function(){testEditor.setCursorPos({line:28,ch:21}),expectNoHints(CSSCodeHints.cssPropHintProvider," ")})});const emmetContent="body {\n m\n bgc\n m0\n pt10\n ma\n}";describe("Emmet hints for CSS",function(){beforeEach(function(){setupTest(emmetContent,"css")}),afterEach(function(){tearDownTest()}),it("should display emmet hint margin when m is pressed",function(){testEditor.setCursorPos({line:1,ch:3});const hints=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hints,"margin"),expect(hints.indexOf("margin")).toBe(0)}),it("should display emmet hint background-color when bgc is pressed",function(){testEditor.setCursorPos({line:2,ch:5});const hints=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hints,"background-color"),expect(hints.indexOf("background-color")).toBe(0)}),it("should complete margin property when m0 is pressed",function(){testEditor.setCursorPos({line:3,ch:4});const hints=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hints,"margin: 0;"),expect(hints.indexOf("margin: 0;")).toBe(0),selectHint(CSSCodeHints.cssPropHintProvider,"margin: 0;"),expect(testDocument.getLine(3)).toBe(" margin: 0;"),expectCursorAt({line:3,ch:12})}),it("should complete padding-top property when pt10 is pressed",function(){testEditor.setCursorPos({line:4,ch:6});const hints=expectHints(CSSCodeHints.cssPropHintProvider);verifyAttrHints(hints,"padding-top: 10px;"),expect(hints.indexOf("padding-top: 10px;")).toBe(0),selectHint(CSSCodeHints.cssPropHintProvider,"padding-top: 10px;"),expect(testDocument.getLine(4)).toBe(" padding-top: 10px;"),expectCursorAt({line:4,ch:20})}),it("should not hint margin when ma is pressed",function(){testEditor.setCursorPos({line:5,ch:4});const hints=expectHints(CSSCodeHints.cssPropHintProvider);expect(hints.indexOf("margin")).toBe(1)})})})});