Skip to content

更好的文本差异比较 #15

@AzideCupric

Description

@AzideCupric

目前的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()
		)
	)
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions