-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add class initialization skeleton * moved mock data to test files * Use patch instead of changing snippetProvider object * Moved snippet provider instance to snippet provider module * Implemented initialize method * Fixes from manual testing * Added docstrings and refactored a bit * Implemented most of the tests for snippet provider * Finished initialization test implementation * Added os.path.join use to ensure multiple platform support
- Loading branch information
Showing
4 changed files
with
90 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,43 @@ | ||
import os | ||
import json | ||
import logging | ||
|
||
|
||
class SnippetProvider: | ||
""" | ||
Class for translating simple snippets into code. | ||
Uses a database provided in json files located in path variable. | ||
""" | ||
|
||
data = { | ||
"for": "for # in !:\n\tpass" | ||
} | ||
def __init__(self, language="", path=[]): | ||
""" | ||
Initialize snippet provider instance. | ||
:param language: language to compile to | ||
:param path: list of directories to search for json files containing snippets | ||
""" | ||
self.data = {} | ||
self.language = language | ||
self.path = path | ||
for item in self.path: | ||
for file in filter(lambda x: x.endswith(".json"), os.listdir(item)): | ||
try: | ||
for snippet in filter(lambda x: x["language"].lower() == self.language.lower(), json.load(open(os.path.join(item, file)))): | ||
if snippet["name"] in self.data: | ||
logging.warning("Multiple definition for %s" % snippet["name"]) | ||
else: | ||
self.data[snippet["name"]] = snippet["snippet"] | ||
except (ValueError, OSError): | ||
logging.warning("Could not get data from file %s" % file, exc_info=True) | ||
|
||
def __getitem__(self, item): | ||
if item in SnippetProvider.data: | ||
return SnippetProvider.data[item] | ||
""" | ||
Expand single snippet | ||
:param item: snippet | ||
:return: snippet expansion | ||
""" | ||
if item in self.data: | ||
return self.data[item] | ||
return item | ||
|
||
|
||
snippetProvider = SnippetProvider() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,61 @@ | ||
from unittest import TestCase | ||
from unittest.mock import patch, MagicMock | ||
from unittest.mock import mock_open | ||
|
||
import os | ||
|
||
import homotopy.snippet_provider as sp | ||
|
||
|
||
class TestSnippetProvider(TestCase): | ||
def test_basic(self): | ||
def test_getitem(self): | ||
provider = sp.SnippetProvider() | ||
|
||
provider.data = { | ||
"for": "for # in !:\n\tpass" | ||
} | ||
|
||
self.assertEqual(provider['for'], "for # in !:\n\tpass") | ||
self.assertEqual(provider['i==5'], "i==5") | ||
|
||
@patch('os.listdir') | ||
def test_initialization(self, listdir): | ||
listdir.return_value = ['test.json', 'test.txt'] | ||
|
||
self.assertDictEqual(sp.SnippetProvider().data, {}, "Snippet provider should be empty.") | ||
|
||
with patch('builtins.open', mock_open(read_data='[{"name": "for","language": "C++","snippet": "if(#){$}"}]')) as m: | ||
provider = sp.SnippetProvider("c++", ["test"]) | ||
|
||
self.assertEqual("if(#){$}", provider["for"]) | ||
|
||
m.assert_called_once_with(os.path.join("test", "test.json")) | ||
|
||
listdir.assert_called_once_with("test") | ||
|
||
with patch('builtins.open', mock_open(read_data='[{"name": "for","language": "C++","snippet": "if(#){$}"}]')) as m: | ||
provider = sp.SnippetProvider("java", ["test"]) | ||
|
||
self.assertEqual("for", provider["for"]) | ||
|
||
with patch('logging.warning', MagicMock()) as m: | ||
with patch('builtins.open', mock_open()) as m_open: | ||
m_open.side_effect = IOError() | ||
|
||
provider = sp.SnippetProvider("c++", ["test"]) | ||
|
||
m.assert_called_once_with("Could not get data from file test.json", exc_info=True) | ||
|
||
with patch('logging.warning', MagicMock()) as m: | ||
with patch('builtins.open', mock_open(read_data='invalidJSON')) as m_open: | ||
provider = sp.SnippetProvider("c++", ["test"]) | ||
|
||
m.assert_called_once_with("Could not get data from file test.json", exc_info=True) | ||
|
||
with patch('logging.warning', MagicMock()) as m: | ||
with patch('builtins.open', mock_open( | ||
read_data='[{"name": "for","language": "C++","snippet": "if(#){$}"}' | ||
',{"name": "for","language": "C++","snippet": "if(#){$}"}]')) as m_open: | ||
provider = sp.SnippetProvider("c++", ["test"]) | ||
|
||
m.assert_called_once_with("Multiple definition for for") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters