是否有一种方法可以在docstring中以十六进制表示法指定预期的整数结果?
assignKrypterID = async() => {
try {
let krypter_ids = await Krypter.find({}).map(obj => obj.krypter_id)
let flag = true
while (flag) {
let id = Math.floor(Math.random() * (999999 - 100000) + 100000);
console.log(id);
if (krypter_ids.includes(id)) {
continue;
} else {
flag = false
return id.toString()
}
}
} catch (err) {
throw err
console.log(err)
}
}
async function foo() {
try {
let krypter_id = (await assignKrypterID()) || ""
const newKrypter = new Krypter({
handle: req.body.handle,
password: req.body.password, // plain text
krypter_id: krypter_id
});
// Hashes the plaintext password and saves it
let salt = await bcrypt.genSalt(10)
let hash = await bcrypt.hash(newKrypter.password, salt)
newKrypter.password = hash; // encrypted password
let krypter = await newKrypter.save()
res.json(krypter)
} catch (err) {
console.log(err)
throw err
}
}
foo()
Doctest无法解释十六进制表示法,从而导致失败:
def identity(val):
"""
>>> identity(243)
243
>>> identity(243)
0xf3
"""
return val
if __name__ == "__main__":
import doctest
doctest.testmod()
我知道我可以搏击文档字符串:
**********************************************************************
File "hextest.py", line 5, in __main__.identity
Failed example:
identity(243)
Expected:
0xf3
Got:
243
**********************************************************************
1 items had failures:
1 of 2 in __main__.identity
***Test Failed*** 1 failures.
但是让doctest以小数点后的8、16为底的literal integers似乎很自然。
答案 0 :(得分:1)
当然,您可以编写自己的OutputChecker类来处理数字,但需要:
def identity(val):
"""
>>> identity(243)
0xf3
>>> identity(243)
243
"""
return val
if __name__ == "__main__":
import doctest
OutputChecker = doctest.OutputChecker
class HexOutputChecker(OutputChecker):
def check_output(self, want, got, optionflags):
if want.startswith('0x'):
want_str = str(int(want, 16)) + '\n'
return super().check_output(want_str, got, optionflags)
else:
return super().check_output(want, got, optionflags)
doctest.OutputChecker = HexOutputChecker
doctest.testmod(verbose=True)