整条流水线跑一遍
(每道题开头都有同一段:上面那套被测系统、unittest 和 mock,以及 eng_07 的静音 run(*cases)——交回「跑了几条/挂了几条/出错几条」。)
贯穿全条的被测系统(四段流水线):
parse(line) "苹果 3 2.5" → ("苹果", 3, 2.5);不是三段就 raise ValueError
subtotal / discount 数量×单价;合计满 100 打九折
Store add(名, 数量, 单价);total() = discount(各小计之和)
report(store, rate) 每样一行「名 数量 小计」,末行「合计 X 约 Y 外币」(Y = X / 汇率)
fetch_rate() 真的去连汇率服务——本地没网,一调就 ConnectionError
handle(lines, get_rate) 整条流水线:parse 每行 → Store → report(get_rate())给 handle 三行和固定汇率,和期望比;再给一行坏的看它在哪停:
import io, unittest
from unittest import mock
def parse(line):
w = line.split()
if len(w) != 3:
raise ValueError("格式不对:" + line)
return w[0], int(w[1]), float(w[2])
def subtotal(qty, price):
return qty * price
def discount(amount):
return amount * 0.9 if amount >= 100 else amount
class Store:
def __init__(self):
self.items = []
def add(self, name, qty, price):
self.items.append((name, qty, price))
def total(self):
return discount(sum(subtotal(q, p) for _, q, p in self.items))
def fmt(x):
return ("%.2f" % x).rstrip("0").rstrip(".")
def report(store, rate):
lines = [n + " " + str(q) + " " + fmt(subtotal(q, p)) for n, q, p in store.items]
t = store.total()
lines.append("合计 " + fmt(t) + " 约 " + fmt(t / rate) + " 外币")
return "\n".join(lines)
def fetch_rate():
raise ConnectionError("没有网络:汇率服务连不上")
def handle(lines, get_rate=fetch_rate):
store = Store()
for line in lines:
n, q, p = parse(line)
store.add(n, q, p)
return report(store, get_rate())
def run(*cases):
suite = unittest.TestSuite()
for c in cases:
suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(c))
r = unittest.TextTestRunner(stream=io.StringIO()).run(suite)
return str(r.testsRun) + "/" + str(len(r.failures)) + "/" + str(len(r.errors))
SAMPLE = ["苹果 3 2.5", "西瓜 2 30", "米 10 4.2"]
EXPECT = "苹果 3 7.5\n西瓜 2 60\n米 10 42\n合计 98.55 约 14.08 外币"
text = handle(SAMPLE, lambda: 7.0)
ok = text == EXPECT
try:
handle(SAMPLE + ["盐 1"], lambda: 7.0)
stopped = "没停"
except ValueError as e:
stopped = str(e)
print(str(ok) + "/" + str(len(text.split("\n"))) + "/" + stopped.replace(" ", "_"))
全部评论