Skip to content

Commit

Permalink
CellProcessorBase : virtual class //
Browse files Browse the repository at this point in the history
moved has_symbol() to SymbolConverter class // ////
CellListProcessor : may accept cell_processor as an argument // ////
FileProcessor : may accept cell processor as the argument. Would pass to instantiate the cell list processor
  • Loading branch information
kangwonlee committed Dec 26, 2017
1 parent be6136e commit b0ddd02
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
50 changes: 0 additions & 50 deletions test_nb_file_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,6 @@ def test_read_notebook(self):
self.assertIn('nbformat', result)
self.assertIn('nbformat_minor', result)

def test_has_symbol(self):
self.maxDiff = None

file = self.file_processor.read_file()
cells = file['cells']
result = []

cp = nbutils.CellProcessorBase()

for k, cell in enumerate(cells):
cp.set_cell(cell)
cell_result = cp.has_symbol()
if cell_result:
result.append((k, cell_result))

# compare with an expected list
expected = [(8, [{'line number': 0, 'source': "L_AB_m = sy.symbols('L_AB_m', real=True, nonnegative=True)"}]),
(10, [{'line number': 0, 'source': "w0_N_m = sy.symbols('w0_N_m', real=True)"}]),
(12, [{'line number': 0, 'source': "E_Pa, I_m4 = sy.symbols('E_Pa, I_m4', positive=True)"}]),
(14, [{'line number': 0, 'source': "x_m = sy.symbols('x_m', nonnegative=True)"}]),
(16, [{'line number': 0,
'source': "R_A_N, M_A_Nm, R_B_N = sy.symbols('R_A_N, M_A_Nm, R_B_N', real=True)"}])]

self.assertSequenceEqual(expected, result)

def test_replace_symbol(self):
self.maxDiff = None
file = self.file_processor.read_file()
cells = file['cells']
result = []

cp = nbutils.CellProcessorBase()

for k, cell in enumerate(cells):
cp.set_cell(cell)
cell_result = cp.has_symbol()
if cell_result:
result.append((k, cell_result))

# compare with an expected list
expected = [
(8, [{'line number': 0, 'source': "L_AB_m = sy.symbols('L_{AB}[m]', real=True, nonnegative=True)"}]),
(10, [{'line number': 0, 'source': "w0_N_m = sy.symbols('w0[N/m]', real=True)"}]),
(12, [{'line number': 0, 'source': "E_Pa, I_m4 = sy.symbols('E[Pa], I[m^{4}]', positive=True)"}]),
(14, [{'line number': 0, 'source': "x_m = sy.symbols('x[m]', nonnegative=True)"}]),
(16, [{'line number': 0,
'source': "R_A_N, M_A_Nm, R_B_N = sy.symbols('R_{A}[N], M_{A}[Nm], R_{B}[N]', real=True)"}])]

self.assertSequenceEqual(expected, result)


if __name__ == '__main__':
unittest.main()
Expand Down
50 changes: 50 additions & 0 deletions test_symbol_converter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import unittest

import nb_file_util as fu
import symbol_converter as sc


class TestSymbolConverter(unittest.TestCase):
def setUp(self):
self.input_file_name = 'sample.ipynb'
self.file_processor = fu.FileProcessor(self.input_file_name)
# class under test
self.cp = sc.SymbolConverter()

def test_cells_with_symbol(self):
self.maxDiff = None
Expand All @@ -26,3 +30,49 @@ def test_cells_with_symbol(self):
'result': expected_result}

self.assertDictEqual(expected, result)

def test_has_symbol(self):
self.maxDiff = None

file = self.file_processor.read_file()
cells = file['cells']
result = []

for k, cell in enumerate(cells):
self.cp.set_cell(cell)
cell_result = self.cp.has_symbol()
if cell_result:
result.append((k, cell_result))

# compare with an expected list
expected = [(8, [{'line number': 0, 'source': "L_AB_m = sy.symbols('L_AB_m', real=True, nonnegative=True)"}]),
(10, [{'line number': 0, 'source': "w0_N_m = sy.symbols('w0_N_m', real=True)"}]),
(12, [{'line number': 0, 'source': "E_Pa, I_m4 = sy.symbols('E_Pa, I_m4', positive=True)"}]),
(14, [{'line number': 0, 'source': "x_m = sy.symbols('x_m', nonnegative=True)"}]),
(16, [{'line number': 0,
'source': "R_A_N, M_A_Nm, R_B_N = sy.symbols('R_A_N, M_A_Nm, R_B_N', real=True)"}])]

self.assertSequenceEqual(expected, result)

def test_replace_symbol(self):
self.maxDiff = None
file = self.file_processor.read_file()
cells = file['cells']
result = []

for k, cell in enumerate(cells):
self.cp.set_cell(cell)
cell_result = self.cp.has_symbol()
if cell_result:
result.append((k, cell_result))

# compare with an expected list
expected = [
(8, [{'line number': 0, 'source': "L_AB_m = sy.symbols('L_{AB}[m]', real=True, nonnegative=True)"}]),
(10, [{'line number': 0, 'source': "w0_N_m = sy.symbols('w0[N/m]', real=True)"}]),
(12, [{'line number': 0, 'source': "E_Pa, I_m4 = sy.symbols('E[Pa], I[m^{4}]', positive=True)"}]),
(14, [{'line number': 0, 'source': "x_m = sy.symbols('x[m]', nonnegative=True)"}]),
(16, [{'line number': 0,
'source': "R_A_N, M_A_Nm, R_B_N = sy.symbols('R_{A}[N], M_{A}[Nm], R_{B}[N]', real=True)"}])]

self.assertSequenceEqual(expected, result)

0 comments on commit b0ddd02

Please sign in to comment.