前端之家收集整理的这篇文章主要介绍了
TDD表达式再次实现(待完善),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
def num(s,idx):
return int(s[idx])
def plus(s):
return num(s,0) + num(s,2)
def cur(s,idx):
return s[idx]
def plus2(s):
v = num(s,0)
i = 1
op = cur(s,i)
while (op == '+'):
if (i + 1 < len(s)):
i += 1
v += num(s,i)
else:
break
if (i + 1 < len(s)):
i += 1
op = cur(s,i)
else:
break
return v
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Filename:test_expr.py
import unittest
from expression2 import *
class ExprTestCase(unittest.TestCase):
def setUp(self):
return
def tearDown(self):
return
def testNum(self):
self.assertEqual(1,num("1",0))
self.assertEqual(3,num("1+3",2))
self.assertEqual(5,num("1+3+5",4))
return True
def testPlus(self):
self.assertEqual(4,plus("1+3"))
self.assertEqual(9,plus2("1+3+5"))
if __name__ == '__main__':
unittest.main()
原文链接:https://www.f2er.com/javaschema/286142.html