Skip to content

Commit

Permalink
Merge pull request #3 from fossology/HastagAB/Developement
Browse files Browse the repository at this point in the history
UnitTest and Automated Test Script.
  • Loading branch information
hastagAB authored Jul 10, 2020
2 parents f97c27a + b58bcdd commit 7e90dd8
Show file tree
Hide file tree
Showing 45 changed files with 1,260 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ __pycache__/
# C extensions
*.so

TestFiles/

# Distribution / packaging
.Python
build/
Expand Down
11 changes: 7 additions & 4 deletions extractor/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ def readMultiLineSame(file, syntax: str):
def readMultiLineDiff(file, startSyntax: str, endSyntax: str):
output, startLine, endLine = [], [], []
content = ""
lines_of_comment = 0
total_lines, line_of_comments, blank_lines = 0,0,0
copy = False
with open(file) as f:
for lineNumber, line in enumerate(f, start=1):
total_lines += 1
if line.strip() == startSyntax:
copy = True
startLine.append(lineNumber)
Expand All @@ -92,12 +93,14 @@ def readMultiLineDiff(file, startSyntax: str, endSyntax: str):
content = ""
endLine.append(lineNumber)
if copy:
lines_of_comment += 1
line_of_comments += 1
content = content + line.replace('\n','')
lines_of_comment += len(output)
if not line.strip():
blank_lines += 1
line_of_comments += len(output)
output = [s.strip(startSyntax) for s in output]
output = [s.strip(endSyntax) for s in output]
return startLine, endLine, output, lines_of_comment
return startLine, endLine, output, line_of_comments, total_lines, blank_lines


class CommentSyntax:
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def cExtractor(file):
result = CommentSyntax()
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/c_sharp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def c_sharpExtractor(file):
result = CommentSyntax()
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def cppExtractor(file):
result = CommentSyntax()
Expand Down
48 changes: 46 additions & 2 deletions extractor/languages/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,54 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def cssExtractor(file):
output = CommentSyntax()
result1 = output.slashStar(file)
file = file.split("/")

return result1
output = {
"metadata": [{
"filename": file[-1],
"lang": "CSS",
"total_lines": result1[4],
"total_lines_of_comments": result1[3],
"blank_lines": result1[5],
"sloc": result1[4]-(result1[3]+result1[5])
}],
"single_line_comment": [],
"multi_line_comment": []
}
if result1:
for idx,i in enumerate(result1[0]):
output['multi_line_comment'].append({"start_line": result1[0][idx], "end_line": result1[1][idx], "comment": result1[2][idx]})
return output


def cssSource(file, newFile: str):
closingCount = 0
copy = True
with open(newFile, 'w+') as f1:
with open(file) as f:
for lineNumber, line in enumerate(f, start=1):
if line.strip() == '/*':
closingCount+=1
copy = False
if closingCount%2 == 0:
copy = True

if line.strip() == '*/':
closingCount+=1
copy = False
if closingCount%2 == 0:
copy = True

if copy:
if line.strip() != '/*' and line.strip() != '*/':
# Templine = line.replace(" ","")
# if Templine[0:2] != "syntax": # Syntax for single line comment
f1.write(line)
f.close()
f1.close()
return newFile
2 changes: 1 addition & 1 deletion extractor/languages/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def goExtractor(file):
result = CommentSyntax()
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/haskell.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def haskellExtractor(file):
result = CommentSyntax()
Expand Down
8 changes: 4 additions & 4 deletions extractor/languages/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def htmlExtractor(file):
result = CommentSyntax()
Expand All @@ -31,10 +31,10 @@ def htmlExtractor(file):
"metadata": [{
"filename": file[-1],
"lang": "HTML",
"total_lines": result1[1],
"total_lines": result1[4],
"total_lines_of_comments": result1[3]+result2[3],
"blank_lines": result1[2],
"sloc": result1[1]-(result1[3]+result2[3]+result1[2])
"blank_lines": result1[5],
"sloc": result1[4]-(result1[3]+result2[3]+result1[5])
}],
"single_line_comment": [],
"multi_line_comment": []
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def javaExtractor(file):
result = CommentSyntax()
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def javascriptExtractor(file):
result = CommentSyntax()
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/kotlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def kotlinExtractor(file):
result = CommentSyntax()
Expand Down
6 changes: 3 additions & 3 deletions extractor/languages/matlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def matlabExtractor(file):
result = CommentSyntax()
result1 = result.percentageCurlybraces(file)
result2 = result.percentage(file)
result2 = result.percentageCurlybraces(file)
result1 = result.percentage(file)
file = file.split("/")
output = {
"metadata": [{
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/perl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def perlExtractor(file):
result = CommentSyntax()
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/php.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def phpExtractor(file):
result = CommentSyntax()
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax


def pythonExtractor(file):
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/r.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def rExtractor(file):
result = CommentSyntax()
Expand Down
4 changes: 2 additions & 2 deletions extractor/languages/ruby.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def rubyExtractor(file):
result = CommentSyntax()
Expand Down Expand Up @@ -51,7 +51,7 @@ def rubyExtractor(file):
return output


def crubySource(file, newFile: str):
def rubySource(file, newFile: str):
closingCount = 0
copy = True
with open(newFile, 'w+') as f1:
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def rustExtractor(file):
result = CommentSyntax()
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/scala.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def scalaExtractor(file):
result = CommentSyntax()
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def shellExtractor(file):
result = CommentSyntax()
Expand Down
2 changes: 1 addition & 1 deletion extractor/languages/swift.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from extractor.binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax
from binder import readSingleLine, readMultiLineSame, readMultiLineDiff, CommentSyntax

def swiftExtractor(file):
result = CommentSyntax()
Expand Down
Empty file.
58 changes: 58 additions & 0 deletions extractor/languages/tests/test_c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import unittest
import re, os
from languages import c
from binder import readSingleLine,readMultiLineDiff

class CTest(unittest.TestCase):

def test_output(self):
path = os.path.join(os.getcwd(),"languages/tests/TestFiles/textcomment.c")
regex = r'''(\/\/\s*[\w #\.()@+-_*\d]*)'''
self.syntax_start = "/*"
self.syntax_end ='*/'
comment_single = c.readSingleLine(path,regex)
comment_multiline = c.readMultiLineDiff(path,self.syntax_start,self.syntax_end)

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



def test_outputFormat(self):
path = os.path.join(os.getcwd(),"languages/tests/TestFiles/textcomment.c")
regex = r'''(\/\/\s*[\w #\.()@+-_*\d]*)'''
self.syntax_start = "/*"
self.syntax_end = "*/"
expected = c.cExtractor(path)
comment_single = readSingleLine(path,regex)
comment_multiline = readMultiLineDiff(path,self.syntax_start,self.syntax_end)
file = path.split("/")
output = {
"metadata": [{
"filename": file[-1],
"lang": "C",
"total_lines": comment_single[1],
"total_lines_of_comments": comment_single[3]+comment_multiline[3],
"blank_lines": comment_single[2],
"sloc": comment_single[1]-(comment_single[3]+comment_multiline[3]+comment_single[2])
}],
"single_line_comment": [],
"multi_line_comment": []
}
if comment_single:
for i in comment_single[0]:
output['single_line_comment'].append({"line_number" :i[0],"comment": i[1]})

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]})

self.assertEqual(output,expected)

def test_Source(self):
path = os.path.join(os.getcwd(),"languages/tests/TestFiles/textcomment.c")
name = "source.txt"
newfile = c.cSource(path,name)

self.assertTrue(newfile)

Loading

0 comments on commit 7e90dd8

Please sign in to comment.