UNPKG

4.47 kBPlain TextView Raw
1import { expect } from 'chai'
2import { Context } from "../src/Context"
3import { ContextType } from "../src/Context"
4
5describe("Scope context detection.", () => {
6 it("Should detect the class context.", () => {
7 let testContext = new Context(new Array<string>(
8 'private class MyClass {',
9 ' // ...',
10 '}'
11 ))
12 expect(testContext.getContext()).to.equal(ContextType.CLASS)
13 }),
14 it("Should detect the method context.", () => {
15 let testContext = new Context(new Array<string>(
16 ' public void doSomething() {',
17 ' empty();',
18 ' }',
19 ))
20 expect(testContext.getContext()).to.equal(ContextType.METHOD)
21 }),
22 it("Should detect the class and method contexts.", () => {
23 let fileContext = new Context(new Array<string>(
24 'private class MyClass {',
25 ' public void doSomething() {',
26 ' empty();',
27 ' }',
28 '}'
29 ))
30 expect(fileContext.getContext()).to.equal(ContextType.CLASS)
31 // the class context should contain the method context
32 let fileContextChildren = fileContext.getChildContexts()
33
34 // should get the method context
35 expect(fileContextChildren.length).to.equal(1)
36
37 // the method context should not have children
38 expect(fileContextChildren[0].getChildContexts().length).to.equal(0)
39
40 // the method context should have be flagged accordingly
41 expect(fileContextChildren[0].getContext()).to.equal(ContextType.METHOD)
42 })
43})
44
45describe("Context sorting/nesting", () => {
46 it("Should correctly nest methods inside classes when analyzing contexts.", () => {
47 let classContext = new Context(new Array<string>(
48 'private class QueryClass {',
49 ' public List<Account> getAccounts() {',
50 ' return [SELECT Id FROM Account];',
51 ' }',
52 '}'
53 ))
54 expect(classContext.getChildContexts().length).to.equal(1)
55 }),
56 it("Should not set any child context.", () => {
57 let classContext = new Context(new Array<string>(
58 'private class QueryClass {',
59 ' // hello! ignore me',
60 '}'
61 ))
62 expect(classContext.getChildContexts().length).to.equal(0)
63 })
64})
65
66describe("SOQL queries.", () => {
67 describe("Should know when the context has a SOQL query", () => {
68 it("Should indicate that the provided context has SOQL query inside of it.", () => {
69 let queryContext1 = new Context(new Array<string>(
70 'private class QueryClass {',
71 ' public List<Account> getAccounts() {',
72 ' return [SELECT Id FROM Account];',
73 ' }',
74 '}'
75 ))
76 let queryContext2 = new Context(new Array<string>(
77 'public List<Account> methodWithMultipleLinesSOQL () {',
78 ' return [',
79 ' SELECT',
80 ' Id',
81 ' ,Name',
82 ' FROM Account',
83 ' ];',
84 '}'
85 ))
86 expect(queryContext1.getSOQLCount()).to.equal(1)
87 expect(queryContext2.getSOQLCount()).to.equal(1)
88 }),
89 it("Should be able to tell when there is more than one query inside the context.", () => {
90 let queryContext = new Context(new Array<string>(
91 'private class QueryClass {',
92 ' public List<Account> getAccounts() {',
93 ' List<Case> cases = [SELECT Id FROM Case];',
94 ' return [SELECT Id FROM Account];',
95 ' }',
96 '}'
97 ))
98 expect(queryContext.getSOQLCount()).to.equal(2)
99 }),
100 it("Should NOT indicate that the provided context has a SOQL query inside of it.", () => {
101 let queryContext = new Context(new Array<string>(
102 'public class QuerylessClass {',
103 ' public List<Account> getAccounts () {',
104 ' return new List<Account>();',
105 ' }',
106 '}'
107 ))
108 expect(queryContext.getSOQLCount()).to.equal(0)
109 })
110 })
111})
\No newline at end of file