nonebug icon indicating copy to clipboard operation
nonebug copied to clipboard

更好的文本差异比较

Open AzideCupric opened this issue 1 year ago • 3 comments

目前的nonebug在测试时遇到和期望不相符的文本时,只会显示这样的格式:

f"Application got api call {api} with data {data} but expected {model.data}"

这经常会造成如下对人眼检查不友好的输出:

Application got api call ... with data {'foo': 1, 'bar': {'hello': 'world'}} but expected {'foo': 1, 'bar': {'hello': 'nonebug'}}

希望可以加入对 期望输出 和 实际输出 的比较,类似这样的格式:

  {
      "foo": 1,
      "bar": {
-         "hello": "world"
?                   ^ ^^^

+         "hello": "nonebot"
?                   ^ ^^^^^

      }
  }

这样的输出可以通过标准库difflib实现,大体类似于:

from difflib import ndiff
from json import dumps

print(
	"\n".join(
		ndiff(
			dumps(data, indent=4).splitlines(),
			dumps(model.data, indent=4).splitlines()
		)
	)
)

AzideCupric avatar Aug 16 '23 16:08 AzideCupric