Skip to content

Commit

Permalink
chore(code-quality): Improve tests
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Mishra <mishra.gaurav@siemens.com>
  • Loading branch information
GMishx committed Apr 28, 2021
1 parent 6af6375 commit 35828eb
Show file tree
Hide file tree
Showing 24 changed files with 1,258 additions and 392 deletions.
21 changes: 21 additions & 0 deletions nirjas/languages/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
SPDX-License-Identifier: LGPL-2.1
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Test scripts for Nirjas
'''
74 changes: 55 additions & 19 deletions nirjas/languages/tests/test_c.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,62 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
SPDX-License-Identifier: LGPL-2.1
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

import unittest
import os
from nirjas.languages import c
from nirjas.binder import readSingleLine, readMultiLineDiff, contSingleLines


class CTest(unittest.TestCase):
'''
Test cases for C language.
:ivar testfile: Location of test file
'''
testfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), "TestFiles/textcomment.c")

def test_output(self):
'''
Check for the scan correctness.
'''
regex = r'''(?<![pst]:)\/\/\s*(.*)'''
sign = '//'
self.syntax_start = "/*"
self.syntax_end = '*/'
comment_single = c.readSingleLine(self.testfile, regex, sign)
comment_multiline = c.readMultiLineDiff(self.testfile, self.syntax_start, self.syntax_end)
comment_contSinglelines = c.contSingleLines(comment_single)
syntax_start = "/*"
syntax_end = '*/'
comment_single = readSingleLine(self.testfile, regex)
comment_multiline = readMultiLineDiff(self.testfile, syntax_start,
syntax_end)
comment_contSinglelines = contSingleLines(comment_single)
self.assertTrue(comment_single)
self.assertTrue(comment_multiline)
self.assertTrue(comment_contSinglelines)

def test_outputFormat(self):
'''
Check for the output format correctness.
'''
regex = r'''(?<![pst]:)\/\/\s*(.*)'''
self.syntax_start = "/*"
self.syntax_end = "*/"
sign = '//'
syntax_start = "/*"
syntax_end = "*/"
expected = c.cExtractor(self.testfile).get_dict()
comment_single = readSingleLine(self.testfile, regex, sign)
comment_multiline = readMultiLineDiff(self.testfile, self.syntax_start, self.syntax_end)
comment_single = readSingleLine(self.testfile, regex)
comment_multiline = readMultiLineDiff(self.testfile, syntax_start,
syntax_end)
comment_contSinglelines = contSingleLines(comment_single)
file = self.testfile.split("/")
output = {
Expand All @@ -51,20 +81,26 @@ def test_outputFormat(self):
output['single_line_comment'].append({"line_number":i[0], "comment": i[1]})

if comment_contSinglelines:
for idx, i in enumerate(comment_contSinglelines[1]):
output['cont_single_line_comment'].append({"start_line": comment_contSinglelines[1][idx], "end_line": comment_contSinglelines[2][idx], "comment": comment_contSinglelines[3][idx]})
for idx, _ in enumerate(comment_contSinglelines[1]):
output['cont_single_line_comment'].append({
"start_line": comment_contSinglelines[1][idx],
"end_line": comment_contSinglelines[2][idx],
"comment": comment_contSinglelines[3][idx]})

if comment_multiline:
try:
for idx, i in enumerate(comment_multiline[0]):
output['multi_line_comment'].append({"start_line": comment_multiline[0][idx], "end_line": comment_multiline[1][idx], "comment": comment_multiline[2][idx]})
except:
pass
for idx, _ in enumerate(comment_multiline[0]):
output['multi_line_comment'].append({
"start_line": comment_multiline[0][idx],
"end_line": comment_multiline[1][idx],
"comment": comment_multiline[2][idx]})
self.assertEqual(output, expected)

def test_Source(self):
'''
Test the source code extraction.
Call the source function and check if new file exists.
'''
name = "source.txt"
newfile = c.cSource(self.testfile, name)

self.assertTrue(newfile)

70 changes: 55 additions & 15 deletions nirjas/languages/tests/test_c_sharp.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
SPDX-License-Identifier: LGPL-2.1
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

import unittest
import os
from nirjas.languages import c_sharp
from nirjas.binder import readSingleLine, readMultiLineDiff, contSingleLines


class CSharpTest(unittest.TestCase):
'''
Test cases for C Sharp language.
:ivar testfile: Location of test file
'''
testfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), "TestFiles/textcomment.cs")

def test_output(self):
'''
Check for the scan correctness.
'''
regex = r'''(?<![pst]:)\/\/\s*(.*)'''
self.syntax_start = "/*"
self.syntax_end = '*/'
sign = '//'
comment_single = c_sharp.readSingleLine(self.testfile, regex, sign)
comment_multiline = c_sharp.readMultiLineDiff(self.testfile, self.syntax_start, self.syntax_end)
comment_contSingleline = c_sharp.contSingleLines(comment_single)
syntax_start = "/*"
syntax_end = '*/'
comment_single = readSingleLine(self.testfile, regex)
comment_multiline = readMultiLineDiff(self.testfile, syntax_start,
syntax_end)
comment_contSingleline = contSingleLines(comment_single)

self.assertTrue(comment_single)
self.assertTrue(comment_multiline)
self.assertTrue(comment_contSingleline)

def test_outputFormat(self):
'''
Check for the output format correctness.
'''
regex = r'''(?<![pst]:)\/\/\s*(.*)'''
self.syntax_start = "/*"
self.syntax_end = '*/'
sign = '//'
syntax_start = "/*"
syntax_end = '*/'
expected = c_sharp.c_sharpExtractor(self.testfile).get_dict()
comment_single = readSingleLine(self.testfile, regex, sign)
comment_multiline = readMultiLineDiff(self.testfile, self.syntax_start, self.syntax_end)
comment_single = readSingleLine(self.testfile, regex)
comment_multiline = readMultiLineDiff(self.testfile, syntax_start,
syntax_end)
comment_contSingleline = contSingleLines(comment_single)
file = self.testfile.split("/")
output = {
Expand All @@ -52,16 +82,26 @@ def test_outputFormat(self):
output['single_line_comment'].append({"line_number":i[0], "comment": i[1]})

if comment_contSingleline:
for idx, i in enumerate(comment_contSingleline[1]):
output['cont_single_line_comment'].append({"start_line": comment_contSingleline[1][idx], "end_line": comment_contSingleline[2][idx], "comment": comment_contSingleline[3][idx]})
for idx, _ in enumerate(comment_contSingleline[1]):
output['cont_single_line_comment'].append({
"start_line": comment_contSingleline[1][idx],
"end_line": comment_contSingleline[2][idx],
"comment": comment_contSingleline[3][idx]})

if comment_multiline:
for idx, i in enumerate(comment_multiline[0]):
output['multi_line_comment'].append({"start_line": comment_multiline[0][idx], "end_line": comment_multiline[1][idx], "comment": comment_multiline[2][idx]})
for idx, _ in enumerate(comment_multiline[0]):
output['multi_line_comment'].append({
"start_line": comment_multiline[0][idx],
"end_line": comment_multiline[1][idx],
"comment": comment_multiline[2][idx]})

self.assertEqual(output, expected)

def test_Source(self):
'''
Test the source code extraction.
Call the source function and check if new file exists.
'''
name = "source.txt"
newfile = c_sharp.c_sharpSource(self.testfile, name)

Expand Down
73 changes: 55 additions & 18 deletions nirjas/languages/tests/test_cpp.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,62 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
SPDX-License-Identifier: LGPL-2.1
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

import unittest
import os
from nirjas.languages import cpp
from nirjas.binder import readSingleLine, readMultiLineDiff, contSingleLines


class CPPTest(unittest.TestCase):
'''
Test cases for C++ language.
:ivar testfile: Location of test file
'''
testfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), "TestFiles/textcomment.cpp")

def test_output(self):
'''
Check for the scan correctness.
'''
regex = r'''(?<![pst]:)\/\/\s*(.*)'''
self.syntax_start = "/*"
self.syntax_end = '*/'
sign = '//'
comment_single = cpp.readSingleLine(self.testfile, regex, sign)
comment_multiline = cpp.readMultiLineDiff(self.testfile, self.syntax_start, self.syntax_end)
comment_contSingleline = cpp.contSingleLines(comment_single)
syntax_start = "/*"
syntax_end = '*/'
comment_single = readSingleLine(self.testfile, regex)
comment_multiline = readMultiLineDiff(self.testfile, syntax_start,
syntax_end)
comment_contSingleline = contSingleLines(comment_single)
self.assertTrue(comment_single)
self.assertTrue(comment_multiline)
self.assertTrue(comment_contSingleline)

def test_outputFormat(self):
'''
Check for the output format correctness.
'''
regex = r'''(?<![pst]:)\/\/\s*(.*)'''
self.syntax_start = "/*"
self.syntax_end = '*/'
sign = '//'
syntax_start = "/*"
syntax_end = '*/'
expected = cpp.cppExtractor(self.testfile).get_dict()
comment_single = readSingleLine(self.testfile, regex, sign)
comment_multiline = readMultiLineDiff(self.testfile, self.syntax_start, self.syntax_end)
comment_single = readSingleLine(self.testfile, regex)
comment_multiline = readMultiLineDiff(self.testfile, syntax_start,
syntax_end)
comment_contSingleline = contSingleLines(comment_single)
file = self.testfile.split("/")
output = {
Expand All @@ -51,19 +81,26 @@ def test_outputFormat(self):
output['single_line_comment'].append({"line_number":i[0], "comment": i[1]})

if comment_contSingleline:
for idx, i in enumerate(comment_contSingleline[1]):
output['cont_single_line_comment'].append({"start_line": comment_contSingleline[1][idx], "end_line": comment_contSingleline[2][idx], "comment": comment_contSingleline[3][idx]})
for idx, _ in enumerate(comment_contSingleline[1]):
output['cont_single_line_comment'].append({
"start_line": comment_contSingleline[1][idx],
"end_line": comment_contSingleline[2][idx],
"comment": comment_contSingleline[3][idx]})

if comment_multiline:
try:
for idx, i in enumerate(comment_multiline[0]):
output['multi_line_comment'].append({"start_line": comment_multiline[0][idx], "end_line": comment_multiline[1][idx], "comment": comment_multiline[2][idx]})
except:
pass
for idx, _ in enumerate(comment_multiline[0]):
output['multi_line_comment'].append({
"start_line": comment_multiline[0][idx],
"end_line": comment_multiline[1][idx],
"comment": comment_multiline[2][idx]})

self.assertEqual(output, expected)

def test_Source(self):
'''
Test the source code extraction.
Call the source function and check if new file exists.
'''
name = "source.txt"
newfile = cpp.cppSource(self.testfile, name)

Expand Down
Loading

0 comments on commit 35828eb

Please sign in to comment.