Page MenuHomec4science

test_snippet_helper.py
No OneTemporary

File Metadata

Created
Fri, Mar 29, 00:38

test_snippet_helper.py

#!/bin/env python3
from __future__ import print_function
import unittest
try:
from Slides import snippet_helper as sh
except:
pass
class SnippetHelperTest(unittest.TestCase):
"Unit tests for SnippetHelper"
def setUp(self, ):
pass
def test_cpp_snippet_from_file(self):
sh.Snippet.default_line_number = True
snip = sh.Snippet('tests/snippet.cpp')
snip.save('snippet2.cpp')
f = open('snippet2.cpp')
snip2 = f.read()
f.close()
if snip.get_content() != snip2:
print('AAA ', snip.get_content())
print('BBB ', snip2)
raise RuntimeError('failed test')
def test_cpp_snippet_format(self):
sh.Snippet.default_line_number = True
code = """
#include <iostream>
int main(int argc, char **argv){
return 0;
}
"""
code = code.replace(';\n', ';')
snip = sh.Snippet(code, format_flag=True)
snip.save('snippet2.cpp')
f = open('snippet2.cpp')
snip2 = f.read()
f.close()
self.assertEqual(snip.get_content(), snip2)
self.assertNotEqual(snip.get_content(), code)
def test_cpp_snippet_compile_fail(self):
sh.Snippet.default_line_number = True
code = """
#include <iostream>
int main(int argc, char **argv){
return 0
}
"""
code = code.replace(';\n', ';')
try:
sh.Snippet(code, compile_flag=True)
self.fail("compilation error not detected")
except sh.Snippet.CompilationError as e:
pass
except Exception as e:
self.fail('wrong exception:' + str(e))
def test_cpp_snippet_compile(self):
sh.Snippet.default_line_number = True
code = """
#include <iostream>
int main(int argc, char **argv){
return 0;
}
"""
code = code.replace(';\n', ';')
try:
sh.Snippet(code, compile_flag=True)
except Exception as e:
self.fail('should compile: ' + str(e))

Event Timeline