Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request mono#816 from my-personal-forks/new-tests
Browse files Browse the repository at this point in the history
Sublime Text: Add tests
  • Loading branch information
rneatherway committed Nov 18, 2014
2 parents 07cc262 + b89ce0c commit 3bc4eb7
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 6 deletions.
17 changes: 15 additions & 2 deletions sublimetext/FSharp.sublime-project
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@
"build_systems":
[
{
"name": "FSharp: Debug",
"name": "Run",
"working_dir": "${project_path}/FSharp",
"shell_cmd": "./build.sh",
"windows": { "shell_cmd": "powershell -noninteractive -file \"$project_path\\bin\\Build.ps1\"" }
"windows": { "shell_cmd": "powershell -noninteractive -file \"$project_path\\bin\\Build.ps1\"" },

"variants": [
{
"name": "FSharp: Test (All)",
"target": "run_fsharp_tests",
},

{
"name": "FSharp: Test (This File Only)",
"target": "run_fsharp_tests",
"active_file_only": true
}
]
}
],

Expand Down
2 changes: 1 addition & 1 deletion sublimetext/FSharp/fsac/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(self, cmd):

def stop(self):
self._internal_comm.put(STOP_SIGNAL)
self.prco.stdin.close()
self.proc.stdin.close()


def start():
Expand Down
6 changes: 4 additions & 2 deletions sublimetext/FSharp/lib/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# All rights reserved. Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.)

import os

from FSharp.sublime_plugin_lib.path import find_file_by_extension
from FSharp.sublime_plugin_lib.path import extension_equals

Expand Down Expand Up @@ -59,7 +61,7 @@ def is_project_file(self):
class FSharpProjectFile (object):
def __init__(self, path):
assert path.endswith('fsproj'), 'wrong fsproject path: %s' % path
self.path
self.path = path
self.parent = os.path.dirname (self.path)

def __eq__(self, other):
Expand All @@ -70,7 +72,7 @@ def governs(self, fname):
return fname.startswith(self.parent)

@classmethod
def from_path(self, path):
def from_path(cls, path):
'''
@path
A path to a file or directory.
Expand Down
2 changes: 1 addition & 1 deletion sublimetext/FSharp/sublime_plugin_lib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def find_file_by_extension(start, extension):
if os.path.dirname(start) == start:
return

return find_file(os.path.dirname(start), extension)
return find_file_by_extension(os.path.dirname(start), extension)


def find_file(start, fname):
Expand Down
40 changes: 40 additions & 0 deletions sublimetext/FSharp/sublime_plugin_lib/testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) 2014, Guillermo López-Anglada. Please see the AUTHORS file for details.
# All rights reserved. Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.)
'''Provides classes that make it easier to test several aspects of a package.
'''

import unittest

import sublime


class ViewTest(unittest.TestCase):
def setUp(self):
self.view = sublime.active_window().new_file()

def append(self, text):
self.view.run_command('append', {'characters': text})

def tearDown(self):
self.view.set_scratch(True)
self.view.close()


class SyntaxTest(ViewTest):
def _setSyntax(self, rel_path):
self.view.set_syntax_file(rel_path)

def getScopeNameAt(self, pt):
return self.view.scope_name(pt)

def getFinestScopeNameAt(self, pt):
return self.getScopeNameAt(pt).split()[-1]

def getScopeNameAtRowCol(self, row, col):
text_pt = self.view.text_point(row, col)
return self.getScopeNameAt(text_pt)

def getFinestScopeNameAtRowCol(self, row, col):
return self.getScopeNameAtRowCol(row, col).split()[-1]

64 changes: 64 additions & 0 deletions sublimetext/FSharp/test_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (c) 2014, Guillermo López-Anglada. Please see the AUTHORS file for details.
# All rights reserved. Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.)

import sublime
import sublime_plugin

import os
import unittest
import contextlib
import threading


from FSharp.sublime_plugin_lib.panels import OutputPanel


class RunFsharpTests(sublime_plugin.WindowCommand):
'''Runs tests and displays the result.
- Do not use ST while tests are running.
@working_dir
Required. Should be the parent of the top-level directory for `tests`.
@loader_pattern
Optional. Only run tests matching this glob.
@active_file_only
Optional. Only run tests in the active file in ST. Shadows
@loader_pattern.
To use this runner conveniently, open the command palette and select one
of the `Build: Dart - Test *` commands.
'''
@contextlib.contextmanager
def chdir(self, path=None):
old_path = os.getcwd()
if path is not None:
assert os.path.exists(path), "'path' is invalid {}".format(path)
os.chdir(path)
yield
if path is not None:
os.chdir(old_path)

def run(self, **kwargs):
with self.chdir(kwargs.get('working_dir')):
p = os.path.join(os.getcwd(), 'tests')
patt = kwargs.get('loader_pattern', 'test*.py',)
# TODO(guillermooo): I can't get $file to expand in the build
# system. It should be possible to make the following code simpler
# with it.
if kwargs.get('active_file_only') is True:
patt = os.path.basename(self.window.active_view().file_name())
suite = unittest.TestLoader().discover(p, pattern=patt)

file_regex = r'^\s*File\s*"([^.].*?)",\s*line\s*(\d+),.*$'
display = OutputPanel('fs.tests', file_regex=file_regex)
display.show()
runner = unittest.TextTestRunner(stream=display, verbosity=1)

def run_and_display():
runner.run(suite)

threading.Thread(target=run_and_display).start()
Empty file.
Empty file.
Empty file.
121 changes: 121 additions & 0 deletions sublimetext/FSharp/tests/lib/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import contextlib
import glob
import os
import tempfile
import time
import unittest

import sublime

from FSharp.lib.project import find_fsproject
from FSharp.lib.project import FSharpFile
from FSharp.lib.project import FSharpProjectFile
from FSharp.sublime_plugin_lib.io import touch


@contextlib.contextmanager
def make_directories(dirs):
tmp_dir = tempfile.TemporaryDirectory()
current = tmp_dir.name
for dd in dirs:
for d in dd:
current = os.path.join(current, d)
os.mkdir(current)
current = tmp_dir.name
yield tmp_dir.name
tmp_dir.cleanup()


class Test_find_fsproject(unittest.TestCase):
def testCanFind(self):
with make_directories([["foo", "bar", "baz"]]) as tmp_root:
fs_proj_file = os.path.join(tmp_root, 'hey.fsproj')
touch(fs_proj_file)
found = find_fsproject (os.path.join (tmp_root, 'foo/bar/baz'))
self.assertEquals(found, fs_proj_file)


class Test_FSharpProjectFile (unittest.TestCase):
def testCanCreateFromPath(self):
with tempfile.TemporaryDirectory () as tmp:
f = os.path.join (tmp, 'foo.fsproj')
touch (f)
fs_project = FSharpProjectFile.from_path(f)
self.assertEquals(fs_project.path, f)

def testCanReturnParent(self):
with tempfile.TemporaryDirectory () as tmp:
f = os.path.join (tmp, 'foo.fsproj')
touch (f)
fs_project = FSharpProjectFile.from_path(f)
self.assertEquals(fs_project.parent, tmp)

def testCanBeCompared(self):
with tempfile.TemporaryDirectory () as tmp:
f = os.path.join (tmp, 'foo.fsproj')
touch (f)
fs_project_1 = FSharpProjectFile.from_path(f)
fs_project_2 = FSharpProjectFile.from_path(f)
self.assertEquals(fs_project_1, fs_project_2)

def test_governs_SameLevel(self):
with tempfile.TemporaryDirectory () as tmp:
f = os.path.join (tmp, 'foo.fsproj')
f2 = os.path.join (tmp, 'foo.fs')
touch (f)
touch (f2)
fs_proj = FSharpProjectFile.from_path(f)
self.assertTrue(fs_proj.governs (f2))


class Test_FSharpFile (unittest.TestCase):
def setUp(self):
self.win = sublime.active_window()

def tearDown(self):
self.win.run_command ('close')

def testCanDetectCodeFile(self):
with tempfile.TemporaryDirectory () as tmp:
f = os.path.join (tmp, 'foo.fs')
touch (f)
v = self.win.open_file(f)
time.sleep(0.01)
fs_file = FSharpFile (v)
self.assertTrue (fs_file.is_code_file)

def testCanDetectScriptFile(self):
with tempfile.TemporaryDirectory () as tmp:
f = os.path.join (tmp, 'foo.fsx')
touch (f)
v = self.win.open_file(f)
time.sleep(0.01)
fs_file = FSharpFile (v)
self.assertTrue (fs_file.is_script_file)

def testCanDetectCodeForCodeFile(self):
with tempfile.TemporaryDirectory () as tmp:
f = os.path.join (tmp, 'foo.fs')
touch (f)
v = self.win.open_file(f)
time.sleep(0.01)
fs_file = FSharpFile (v)
self.assertTrue (fs_file.is_code)

def testCanDetectCodeForScriptFile(self):
with tempfile.TemporaryDirectory () as tmp:
f = os.path.join (tmp, 'foo.fsx')
touch (f)
v = self.win.open_file(f)
time.sleep(0.01)
fs_file = FSharpFile (v)
self.assertTrue (fs_file.is_code)

def testCanDetectProjectFile(self):
with tempfile.TemporaryDirectory () as tmp:
f = os.path.join (tmp, 'foo.fsproj')
touch (f)
v = self.win.open_file(f)
time.sleep(0.01)
fs_file = FSharpFile (v)
self.assertTrue (fs_file.is_project_file)
Empty file.
37 changes: 37 additions & 0 deletions sublimetext/FSharp/tests/syntax_def/test_syntax_def.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from FSharp.tests.tools import FSharpSyntaxTest


class Test_DoubledQuotedStrings(FSharpSyntaxTest):
def testCanDetectDoubleQuotedStrings(self):
self.append('''
let foo = "some string here"
0123456789
0123456789
0123456
''')
actual = self.getFinestScopeNameAtRowCol(1, 11)
self.assertEqual(actual, 'string.quoted.double.fsharp')


class Test_TripleQuotedStrings(FSharpSyntaxTest):
def testCanDetectTripleQuotedStrings(self):
self.append('''
let foo = """some string here"""
0123456789
0123456789
0123456
''')
actual = self.getFinestScopeNameAtRowCol(1, 13)
self.assertEqual(actual, 'string.quoted.triple.fsharp')


class Test_VerbatimStrings(FSharpSyntaxTest):
def testCanDetectVerbatimStrings(self):
self.append('''
let foo = @"some string here"
0123456789
0123456789
0123456
''')
actual = self.getFinestScopeNameAtRowCol(1, 12)
self.assertEqual(actual, 'string.quoted.double.verbatim.fsharp')
11 changes: 11 additions & 0 deletions sublimetext/FSharp/tests/tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import unittest

import sublime

from FSharp.sublime_plugin_lib.testing import SyntaxTest


class FSharpSyntaxTest(SyntaxTest):
def setUp(self):
super().setUp()
self._setSyntax('Packages/FSharp/FSharp.tmLanguage')

0 comments on commit 3bc4eb7

Please sign in to comment.