Skip to content

Commit

Permalink
adding more tests
Browse files Browse the repository at this point in the history
adding initial tests
adding support for minitest
removing deprecated api

removing console logs
  • Loading branch information
roychoo committed Apr 21, 2015
1 parent 80b5c53 commit 29f4a0e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
9 changes: 9 additions & 0 deletions lib/ruby-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ RubyTestView = require './ruby-test-view'

module.exports =
config:
minitestAllCommand:
type: 'string'
default: "ruby -I test test"
minitestFileCommand:
type: 'string'
default: "ruby -I test {relative_path}"
minitestSingleCommand:
type: 'string'
default: "ruby {relative_path} -n \"/{regex}/\""
testAllCommand:
type: 'string'
default: "ruby -I test test"
Expand Down
28 changes: 26 additions & 2 deletions lib/source-info.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports =
test: 'test'
spec: 'rspec'
feature: 'cucumber'
minitest: 'minitest'

currentShell: ->
atom.config.get('ruby-test.shell') || 'bash'
Expand All @@ -31,12 +32,32 @@ module.exports =
currentLine: ->
@_currentLine ||= unless @_currentLine
editor = atom.workspace.getActiveTextEditor()
cursor = editor and editor.getCursor()
cursor = editor and editor.getLastCursor()
if cursor
cursor.getBufferRow() + 1
else
null

minitestRegExp: (text)->
@_minitestRegExp ||= unless @_minitestRegExp
value = text.match(/"(.*?)"/) if text?
if value
value[1]
else
""

isMiniTest: ->
i = @currentLine() - 1
regExp = new RegExp(/^(\s+)(should|test|it)\s+['""'](.*)['""']\s+do\s*(?:#.*)?$/)
editor = atom.workspace.getActiveTextEditor()
while i > 0
text = editor.lineTextForBufferRow(i)
if regExp.test(text)
@minitestRegExp(text)
return true
i--
return false

testFramework: ->
@_testFramework ||= unless @_testFramework
(t = @fileType()) and @frameworkLookup[t] or
Expand All @@ -47,7 +68,10 @@ module.exports =
if not @activeFile()
null
else if matches = @activeFile().match(/_(test|spec)\.rb$/)
matches[1]
if @isMiniTest()
"minitest"
else
matches[1]
else if matches = @activeFile().match(/\.(feature)$/)
matches[1]

Expand Down
3 changes: 2 additions & 1 deletion lib/test-runner.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ module.exports =
else
@testParams.testFileCommand()
cmd.replace('{relative_path}', @testParams.activeFile()).
replace('{line_number}', @testParams.currentLine())
replace('{line_number}', @testParams.currentLine()).
replace('{regex}', @testParams.minitestRegExp())

cancel: ->
@shell.kill()
39 changes: 37 additions & 2 deletions spec/source-info-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ describe "SourceInfo", ->
atom.config.set("ruby-test.#{framework}SingleCommand", "foo-#{framework}SingleCommand")


setUpOpenFile = ->
setUpOpenFile = ()->
editor = {buffer: {file: {path: "foo_test.rb"}}}
cursor =
getBufferRow: ->
99
editor.getLastCursor = -> cursor
editor.lineTextForBufferRow = (line) ->
""
spyOn(atom.workspace, 'getActiveTextEditor').andReturn(editor)

setUpWithOpenFile = ->
Expand All @@ -52,6 +58,13 @@ describe "SourceInfo", ->
setUpWithoutOpenFile()
expect(sourceInfo.cwd()).toBe("fooPath")

describe "::fileType", ->
it "correctly detects a minitest file", ->
setUpWithOpenFile()
editor.lineTextForBufferRow = (line) ->
" it \"test something\" do"
expect(sourceInfo.fileType()).toBe("minitest")

describe "::projectType", ->
it "correctly detects a test directory", ->
spyOn(fs, 'existsSync').andCallFake (filePath) ->
Expand Down Expand Up @@ -118,14 +131,36 @@ describe "SourceInfo", ->
cursor =
getBufferRow: ->
99
editor.getCursor = -> cursor
editor.getLastCursor = -> cursor
expect(sourceInfo.currentLine()).toBe(100)

describe "without editor", ->
it "is null", ->
setUpWithoutOpenFile()
expect(sourceInfo.currentLine()).toBeNull()

describe "::minitestRegExp", ->
it "correctly returns the matching regex", ->
setUpWithoutOpenFile()
expect(sourceInfo.minitestRegExp(" it \"test something\" do")).toBe("test something")

it "should return empty string if no match", ->
setUpWithoutOpenFile()
expect(sourceInfo.minitestRegExp("test something")).toBe("")

describe "::isMiniTest", ->
it "correctly returns true if it is minitest spec file", ->
setUpWithOpenFile()
editor.lineTextForBufferRow = (line) ->
" it \"test something\" do"
expect(sourceInfo.isMiniTest("")).toBe(true)

it "returns false if it is not a minitest spec file", ->
setUpWithOpenFile()
editor.lineTextForBufferRow = (line) ->
"def something"
expect(sourceInfo.isMiniTest("")).toBe(false)

describe "::currentShell", ->
it "when ruby-test.shell is null", ->
setUpWithoutOpenFile()
Expand Down
8 changes: 8 additions & 0 deletions spec/test-runner-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe "TestRunner", ->
spyOn(@testRunnerParams, 'setTestInfo')
spyOn(SourceInfo.prototype, 'activeFile').andReturn('fooTestFile')
spyOn(SourceInfo.prototype, 'currentLine').andReturn(100)
spyOn(SourceInfo.prototype, 'minitestRegExp').andReturn('test foo')
spyOn(SourceInfo.prototype, 'testFileCommand').andReturn('fooTestCommand {relative_path}')
spyOn(SourceInfo.prototype, 'testSingleCommand').andReturn('fooTestCommand {relative_path}:{line_number}')

Expand All @@ -31,3 +32,10 @@ describe "TestRunner", ->
runner = new TestRunner(@testRunnerParams)
runner.run()
expect(@testRunnerParams.setTestInfo).toHaveBeenCalledWith("fooTestCommand fooTestFile:100")

it "constructs a single-minitest command when testScope is 'single'", ->
SourceInfo.prototype.testSingleCommand.andReturn('fooTestCommand {relative_path} -n \"/{regex}/\"')
@testRunnerParams.testScope = "single"
runner = new TestRunner(@testRunnerParams)
runner.run()
expect(@testRunnerParams.setTestInfo).toHaveBeenCalledWith("fooTestCommand fooTestFile -n \"/test foo/\"")

0 comments on commit 29f4a0e

Please sign in to comment.