diff --git a/tests/samples/codefiles/seesharp.cs b/tests/samples/codefiles/seesharp.cs index 3aedba7..545a68e 100644 --- a/tests/samples/codefiles/seesharp.cs +++ b/tests/samples/codefiles/seesharp.cs @@ -1,4 +1,12 @@ using System; +using System.Diagnostics; +using System.IO; +using System.Net; +using Microsoft.Win32; +using WakaTime.Forms; +using static Math.Foo; +using Task = Fart.Threading.Tasks.Task; +using static Proper.Bar; public class Hello4 { diff --git a/tests/test_dependencies.py b/tests/test_dependencies.py index 986fc5b..7848aa9 100644 --- a/tests/test_dependencies.py +++ b/tests/test_dependencies.py @@ -97,12 +97,9 @@ class DependenciesTestCase(utils.TestCase): 'unittest', ] - def normalize(items): - return sorted([u(x) for x in items]) - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None) dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertEquals(normalize(dependencies), normalize(expected_dependencies)) + self.assertListsEqual(dependencies, expected_dependencies) self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() @@ -190,12 +187,9 @@ class DependenciesTestCase(utils.TestCase): 'foobar', ] - def normalize(items): - return sorted([u(x) for x in items]) - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None) dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertEquals(normalize(dependencies), normalize(expected_dependencies)) + self.assertListsEqual(dependencies, expected_dependencies) self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() @@ -240,12 +234,9 @@ class DependenciesTestCase(utils.TestCase): 'openssl', ] - def normalize(items): - return sorted([u(x) for x in items]) - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None) dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertEquals(normalize(dependencies), normalize(expected_dependencies)) + self.assertListsEqual(dependencies, expected_dependencies) self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() @@ -290,12 +281,9 @@ class DependenciesTestCase(utils.TestCase): 'openssl', ] - def normalize(items): - return sorted([u(x) for x in items]) - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None) dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertEquals(normalize(dependencies), normalize(expected_dependencies)) + self.assertListsEqual(dependencies, expected_dependencies) self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() @@ -321,8 +309,9 @@ class DependenciesTestCase(utils.TestCase): heartbeat = { 'language': u('C#'), - 'lines': 10, + 'lines': 18, 'entity': os.path.realpath(entity), + 'dependencies': ANY, 'project': u(os.path.basename(os.path.realpath('.'))), 'branch': os.environ.get('TRAVIS_COMMIT', ANY), 'time': float(now), @@ -333,12 +322,17 @@ class DependenciesTestCase(utils.TestCase): u('dependencies'): ANY, u('language'): u('C#'), u('lineno'): None, - u('lines'): 10, + u('lines'): 18, } - - def normalize(items): - return sorted([u(x) for x in items]) + expected_dependencies = [ + 'Proper', + 'Fart', + 'Math', + 'WakaTime', + ] self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None) self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] + self.assertListsEqual(dependencies, expected_dependencies) self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() diff --git a/tests/utils.py b/tests/utils.py index 9129bca..4add119 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -2,6 +2,9 @@ import logging +from wakatime.compat import u + + try: import mock except ImportError: @@ -38,3 +41,9 @@ class TestCase(unittest.TestCase): def tearDown(self): mock.patch.stopall() + + def normalize_list(self, items): + return sorted([u(x) for x in items]) + + def assertListsEqual(self, first_list, second_list): + self.assertEquals(self.normalize_list(first_list), self.normalize_list(second_list)) diff --git a/wakatime/dependencies/dotnet.py b/wakatime/dependencies/dotnet.py index 1c0019b..7d67341 100644 --- a/wakatime/dependencies/dotnet.py +++ b/wakatime/dependencies/dotnet.py @@ -10,12 +10,16 @@ """ from . import TokenParser +from ..compat import u class CSharpParser(TokenParser): exclude = [ r'^system$', + r'^microsoft$', ] + state = None + buffer = u('') def parse(self): for index, token, content in self.tokens: @@ -23,14 +27,38 @@ class CSharpParser(TokenParser): return self.dependencies def _process_token(self, token, content): - if self.partial(token) == 'Namespace': + if self.partial(token) == 'Keyword': + self._process_keyword(token, content) + if self.partial(token) == 'Namespace' or self.partial(token) == 'Name': self._process_namespace(token, content) + elif self.partial(token) == 'Punctuation': + self._process_punctuation(token, content) else: self._process_other(token, content) + def _process_keyword(self, token, content): + if content == 'using': + self.state = 'import' + self.buffer = u('') + def _process_namespace(self, token, content): - if content != 'import' and content != 'package' and content != 'namespace': - self.append(content, truncate=True) + if self.state == 'import': + if u(content) != u('import') and u(content) != u('package') and u(content) != u('namespace') and u(content) != u('static'): + if u(content) == u(';'): # pragma: nocover + self._process_punctuation(token, content) + else: + self.buffer += u(content) + + def _process_punctuation(self, token, content): + if self.state == 'import': + if u(content) == u(';'): + self.append(self.buffer, truncate=True) + self.buffer = u('') + self.state = None + elif u(content) == u('='): + self.buffer = u('') + else: + self.buffer += u(content) def _process_other(self, token, content): pass