improve C# dependency test

This commit is contained in:
Alan Hamlett 2015-09-29 21:15:46 -07:00
parent 7bc8ec006b
commit ffa9e410b4
4 changed files with 63 additions and 24 deletions

View file

@ -1,4 +1,12 @@
using System; 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 public class Hello4
{ {

View file

@ -97,12 +97,9 @@ class DependenciesTestCase(utils.TestCase):
'unittest', '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) 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'] 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.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
@ -190,12 +187,9 @@ class DependenciesTestCase(utils.TestCase):
'foobar', '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) 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'] 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.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
@ -240,12 +234,9 @@ class DependenciesTestCase(utils.TestCase):
'openssl', '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) 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'] 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.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
@ -290,12 +281,9 @@ class DependenciesTestCase(utils.TestCase):
'openssl', '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) 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'] 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.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
@ -321,8 +309,9 @@ class DependenciesTestCase(utils.TestCase):
heartbeat = { heartbeat = {
'language': u('C#'), 'language': u('C#'),
'lines': 10, 'lines': 18,
'entity': os.path.realpath(entity), 'entity': os.path.realpath(entity),
'dependencies': ANY,
'project': u(os.path.basename(os.path.realpath('.'))), 'project': u(os.path.basename(os.path.realpath('.'))),
'branch': os.environ.get('TRAVIS_COMMIT', ANY), 'branch': os.environ.get('TRAVIS_COMMIT', ANY),
'time': float(now), 'time': float(now),
@ -333,12 +322,17 @@ class DependenciesTestCase(utils.TestCase):
u('dependencies'): ANY, u('dependencies'): ANY,
u('language'): u('C#'), u('language'): u('C#'),
u('lineno'): None, u('lineno'): None,
u('lines'): 10, u('lines'): 18,
} }
expected_dependencies = [
def normalize(items): 'Proper',
return sorted([u(x) for x in items]) 'Fart',
'Math',
'WakaTime',
]
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None) 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])) 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() self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()

View file

@ -2,6 +2,9 @@
import logging import logging
from wakatime.compat import u
try: try:
import mock import mock
except ImportError: except ImportError:
@ -38,3 +41,9 @@ class TestCase(unittest.TestCase):
def tearDown(self): def tearDown(self):
mock.patch.stopall() 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))

View file

@ -10,12 +10,16 @@
""" """
from . import TokenParser from . import TokenParser
from ..compat import u
class CSharpParser(TokenParser): class CSharpParser(TokenParser):
exclude = [ exclude = [
r'^system$', r'^system$',
r'^microsoft$',
] ]
state = None
buffer = u('')
def parse(self): def parse(self):
for index, token, content in self.tokens: for index, token, content in self.tokens:
@ -23,14 +27,38 @@ class CSharpParser(TokenParser):
return self.dependencies return self.dependencies
def _process_token(self, token, content): 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) self._process_namespace(token, content)
elif self.partial(token) == 'Punctuation':
self._process_punctuation(token, content)
else: else:
self._process_other(token, content) 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): def _process_namespace(self, token, content):
if content != 'import' and content != 'package' and content != 'namespace': if self.state == 'import':
self.append(content, truncate=True) 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): def _process_other(self, token, content):
pass pass