1 | import { expect } from 'chai'
|
2 | import { Rules } from "../src/Rules"
|
3 | import { Context } from "../src/Context"
|
4 | import { ContextType } from "../src/Context"
|
5 |
|
6 | describe("Context rules", () => {
|
7 | describe("Comments", () => {
|
8 | it("Should ignore lines starting with '//'.", () => {
|
9 | let commentedContext = new Context(new Array<string>(
|
10 | 'global class CommentedClass {',
|
11 | ' // this is a little comment',
|
12 | '}'
|
13 | ))
|
14 | expect(commentedContext.getLineCount()).to.equal(2)
|
15 | })
|
16 | }),
|
17 | describe("Line length", () => {
|
18 | it("Should not log an error because there is no line with more than 120 characters.", () => {
|
19 | let clearContext = new Context(new Array<string>(
|
20 | 'global class AClass {',
|
21 | ' global static variable = \'FOO\';',
|
22 | '}'
|
23 | ))
|
24 | expect(clearContext.getErrors().length).to.equal(0)
|
25 | }),
|
26 | it("Should log an error because there is a line with more than 120 characters.", () => {
|
27 | let erroredContext = new Context(new Array<string>(
|
28 | 'global class BClass {',
|
29 | ' global static variableThatHasAReallyRandomLongNameWhichIsOfcourseUnacceptableByThisLinterStandardsAndShouldBeMarkedAsAnError = \'FOO\';',
|
30 | '}'
|
31 | ))
|
32 | expect(erroredContext.getErrors().length).to.equal(1)
|
33 | })
|
34 | })
|
35 | })
|
36 |
|
37 | describe("SOQL queries.", () => {
|
38 | describe("About queries on a single line.", () => {
|
39 | it("Should give an error about one-liners with more than one field on the query.", () => {
|
40 | let queryContext = new Context(new Array<string>(
|
41 | 'private class QueryClass {',
|
42 | ' public List<Account> getAccounts() {',
|
43 | ' List<Case> cases = [SELECT Id, Subject FROM Case WHERE Something__c = \'Email\'];',
|
44 | ' }',
|
45 | '}'
|
46 | ))
|
47 | expect(queryContext.getSOQLCount()).to.equal(1)
|
48 | expect(queryContext.getErrors().length).to.equal(1)
|
49 | }),
|
50 | it("Should give an error about one-liners with more than one condition on the query.", () => {
|
51 | let queryContext = new Context(new Array<string>(
|
52 | 'private class QueryClass {',
|
53 | ' public List<Account> getAccounts() {',
|
54 | ' return [SELECT Id FROM Account WHERE Name = \'Account\' AND NumberField__c > 0];',
|
55 | ' }',
|
56 | '}'
|
57 | ))
|
58 | expect(queryContext.getSOQLCount()).to.equal(1)
|
59 | expect(queryContext.getErrors().length).to.equal(1)
|
60 | })
|
61 | }),
|
62 | describe("Queries without condition.", () => {
|
63 | it("Should accuse an error with a query without condition", () => {
|
64 | let queryContext = new Context(new Array<string>(
|
65 | 'private class QueryClass {',
|
66 | ' public List<Account> getAccounts() {',
|
67 | ' return [SELECT Id FROM Account];',
|
68 | ' }',
|
69 | '}'
|
70 | ))
|
71 | expect(queryContext.getSOQLCount()).to.equal(1)
|
72 | expect(queryContext.getErrors().length).to.equal(1)
|
73 | })
|
74 | })
|
75 | })
|
76 |
|
77 | describe("'TODOS:'", () => {
|
78 | it("Should point to lines with 'TODO's.", () => {
|
79 | let fileContext = new Context(new Array<string>(
|
80 | 'private class MyClass {',
|
81 | ' // TODO: do something',
|
82 | ' public Boolean something() {',
|
83 | ' return true;',
|
84 | ' }',
|
85 | '}'
|
86 | ))
|
87 | expect(fileContext.getTodos()).to.equal(1)
|
88 | })
|
89 | })
|
90 |
|
91 | describe("Whitespace", () => {
|
92 | describe("Should warn about missing whitespaces.", () => {
|
93 | it("Should't warn when there are no errors.", () => {
|
94 | let fileContext = new Context(new Array<string>(
|
95 | 'private class MyClass {',
|
96 | ' public String returnString () {',
|
97 | ' if (condition) {',
|
98 | ' something;',
|
99 | ' }',
|
100 | ' }',
|
101 | '}'
|
102 | ))
|
103 | expect(fileContext.getErrors().length).to.equal(0)
|
104 | }),
|
105 | it("Should warn on 'if's.", () => {
|
106 | let fileContext = new Context(new Array<string>(
|
107 | 'private class MyClass {',
|
108 | ' public String returnString () {',
|
109 | ' if(condition) {',
|
110 | ' something;',
|
111 | ' }',
|
112 | ' }',
|
113 | '}'
|
114 | ))
|
115 | expect(fileContext.getErrors().length).to.equal(1)
|
116 | expect(fileContext.getErrors()[0].getLine()).to.equal(3)
|
117 | }),
|
118 | it("Should warn on 'for's.", () => {
|
119 | let fileContext = new Context(new Array<string>(
|
120 | 'private class MyClass {',
|
121 | ' public String returnString () {',
|
122 | ' for(condition) {',
|
123 | ' something;',
|
124 | ' }',
|
125 | ' }',
|
126 | '}'
|
127 | ))
|
128 | expect(fileContext.getErrors().length).to.equal(1)
|
129 | expect(fileContext.getErrors()[0].getLine()).to.equal(3)
|
130 | }),
|
131 | it("Should warn on 'else's.", () => {
|
132 | let fileContext = new Context(new Array<string>(
|
133 | 'private class MyClass {',
|
134 | ' public String returnString () {',
|
135 | ' if (condition) {',
|
136 | ' something;',
|
137 | ' }else {',
|
138 | ' something;',
|
139 | ' }',
|
140 | ' }',
|
141 | '}'
|
142 | ))
|
143 | expect(fileContext.getErrors().length).to.equal(1)
|
144 | expect(fileContext.getErrors()[0].getLine()).to.equal(5)
|
145 | }),
|
146 | it("Should warn on 'else's.", () => {
|
147 | let fileContext = new Context(new Array<string>(
|
148 | 'private class MyClass {',
|
149 | ' public String returnString () {',
|
150 | ' if (condition) {',
|
151 | ' something;',
|
152 | ' } else{',
|
153 | ' something;',
|
154 | ' }',
|
155 | ' }',
|
156 | '}'
|
157 | ))
|
158 | expect(fileContext.getErrors().length).to.equal(1)
|
159 | expect(fileContext.getErrors()[0].getLine()).to.equal(5)
|
160 | }),
|
161 | it("Should warn on 'else's.", () => {
|
162 | let fileContext = new Context(new Array<string>(
|
163 | 'private class MyClass {',
|
164 | ' public String returnString () {',
|
165 | ' if (condition) {',
|
166 | ' something;',
|
167 | ' }else{',
|
168 | ' something;',
|
169 | ' }',
|
170 | ' }',
|
171 | '}'
|
172 | ))
|
173 | expect(fileContext.getErrors().length).to.equal(2)
|
174 | expect(fileContext.getErrors()[0].getLine()).to.equal(5)
|
175 | expect(fileContext.getErrors()[1].getLine()).to.equal(5)
|
176 | })
|
177 | })
|
178 | }) |
\ | No newline at end of file |