-
Notifications
You must be signed in to change notification settings - Fork 269
/
code-context-spec.js
150 lines (129 loc) · 5.38 KB
/
code-context-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"use babel" // TODO
/* eslint-disable no-invalid-this */
import path from "path"
import temp from "temp"
temp.track()
import CodeContext from "../lib/code-context"
describe("CodeContext", () => {
const testFile = "test.txt"
let testFilePath
beforeEach(() => {
testFilePath = path.join(temp.mkdirSync(""), testFile)
this.codeContext = new CodeContext(testFile, testFilePath, null)
// TODO: Test using an actual editor or a selection?
this.dummyTextSource = {}
this.dummyTextSource.getText = () => "print 'hello world!'"
})
describe("fileColonLine when lineNumber is not set", () => {
it("returns the full filepath when fullPath is truthy", () => {
expect(this.codeContext.fileColonLine()).toEqual(testFilePath)
expect(this.codeContext.fileColonLine(true)).toEqual(testFilePath)
})
it("returns only the filename and line number when fullPath is falsy", () => {
expect(this.codeContext.fileColonLine(false)).toEqual(testFile)
})
})
describe("fileColonLine when lineNumber is set", () => {
it("returns the full filepath when fullPath is truthy", () => {
this.codeContext.lineNumber = 42
expect(this.codeContext.fileColonLine()).toEqual(`${testFilePath}:42`)
expect(this.codeContext.fileColonLine(true)).toEqual(`${testFilePath}:42`)
})
it("returns only the filename and line number when fullPath is falsy", () => {
this.codeContext.lineNumber = 42
expect(this.codeContext.fileColonLine(false)).toEqual(`${testFile}:42`)
})
})
describe("getCode", () => {
it("returns undefined if no textSource is available", () => {
expect(this.codeContext.getCode()).toBe(null)
})
it("returns a string prepended with newlines when prependNewlines is truthy", () => {
this.codeContext.textSource = this.dummyTextSource
this.codeContext.lineNumber = 3
const code = this.codeContext.getCode(true)
expect(typeof code).toEqual("string")
// Since Array#join will create newlines for one less than the the number
// of elements line number 3 means there should be two newlines
expect(code).toMatch("\n\nprint 'hello world!'")
})
it("returns the text from the textSource when available", () => {
this.codeContext.textSource = this.dummyTextSource
const code = this.codeContext.getCode()
expect(typeof code).toEqual("string")
expect(code).toMatch("print 'hello world!'")
})
})
describe("shebangCommand when no shebang was found", () =>
it("returns undefined when no shebang is found", () => {
const lines = this.dummyTextSource.getText()
const firstLine = lines.split("\n")[0]
if (firstLine.match(/^#!/)) {
this.codeContext.shebang = firstLine
}
expect(this.codeContext.shebangCommand()).toBe(null)
}))
describe("shebangCommand when a shebang was found", () => {
it("returns the command from the shebang", () => {
const lines = "#!/bin/bash\necho 'hello from bash!'"
const firstLine = lines.split("\n")[0]
if (firstLine.match(/^#!/)) {
this.codeContext.shebang = firstLine
}
expect(this.codeContext.shebangCommand()).toMatch("bash")
})
it("returns /usr/bin/env as the command if applicable", () => {
const lines = "#!/usr/bin/env ruby -w\nputs 'hello from ruby!'"
const firstLine = lines.split("\n")[0]
if (firstLine.match(/^#!/)) {
this.codeContext.shebang = firstLine
}
expect(this.codeContext.shebangCommand()).toMatch("env")
})
it("returns a command with non-alphabet characters", () => {
const lines = "#!/usr/bin/python2.7\nprint 'hello from python!'"
const firstLine = lines.split("\n")[0]
if (firstLine.match(/^#!/)) {
this.codeContext.shebang = firstLine
}
expect(this.codeContext.shebangCommand()).toMatch("python2.7")
})
})
describe("shebangCommandArgs when no shebang was found", () =>
it("returns [] when no shebang is found", () => {
const lines = this.dummyTextSource.getText()
const firstLine = lines.split("\n")[0]
if (firstLine.match(/^#!/)) {
this.codeContext.shebang = firstLine
}
expect(this.codeContext.shebangCommandArgs()).toMatch([])
}))
describe("shebangCommandArgs when a shebang was found", () => {
it("returns the command from the shebang", () => {
const lines = "#!/bin/bash\necho 'hello from bash!'"
const firstLine = lines.split("\n")[0]
if (firstLine.match(/^#!/)) {
this.codeContext.shebang = firstLine
}
expect(this.codeContext.shebangCommandArgs()).toMatch([])
})
it("returns the true command as the first argument when /usr/bin/env is used", () => {
const lines = "#!/usr/bin/env ruby -w\nputs 'hello from ruby!'"
const firstLine = lines.split("\n")[0]
if (firstLine.match(/^#!/)) {
this.codeContext.shebang = firstLine
}
const args = this.codeContext.shebangCommandArgs()
expect(args[0]).toMatch("ruby")
expect(args).toMatch(["ruby", "-w"])
})
it("returns the command args when the command had non-alphabet characters", () => {
const lines = "#!/usr/bin/python2.7\nprint 'hello from python!'"
const firstLine = lines.split("\n")[0]
if (firstLine.match(/^#!/)) {
this.codeContext.shebang = firstLine
}
expect(this.codeContext.shebangCommandArgs()).toMatch([])
})
})
})