这些示例演示了如何构建从程序输出(测试中快照测试)自身重写的故事情节。这可以通过以下方式完成
self.current_step.rewrite("argument").to("new output")
代码示例
example.story
Append text to file:
steps:
- Run: echo hello >> mytext.txt
- Run: echo hello >> mytext.txt
- Run: echo hello >> mytext.txt
variations:
Output text to:
following steps:
- Run and get output:
command: cat mytext.txt
will output: old value
from hitchstory import BaseEngine
class Engine(BaseEngine):
def __init__(self, rewrite=True):
self._rewrite = rewrite
def run(self, command):
pass
def run_and_get_output(self, command, will_output):
if self._rewrite:
self.current_step.rewrite("will_output").to("hello\nhello")
带有代码
from hitchstory import StoryCollection
from pathlib import Path
from engine import Engine
当使用 rewrite=True 时,故事将被重写
StoryCollection(Path(".").glob("*.story"), Engine(rewrite=True)).ordered_by_name().play()
将输出
RUNNING Append text to file in /path/to/working/example.story ... SUCCESS in 0.1 seconds.
RUNNING Append text to file/Output text to in /path/to/working/example.story ... SUCCESS in 0.1 seconds.
文件 example.story 现在应包含
Append text to file:
steps:
- Run: echo hello >> mytext.txt
- Run: echo hello >> mytext.txt
- Run: echo hello >> mytext.txt
variations:
Output text to:
following steps:
- Run and get output:
command: cat mytext.txt
will output: |-
hello
hello
当使用 rewrite=False 时,故事将保持不变。
StoryCollection(Path(".").glob("*.story"), Engine(rewrite=False)).ordered_by_name().play()
将输出
RUNNING Append text to file in /path/to/working/example.story ... SUCCESS in 0.1 seconds.
RUNNING Append text to file/Output text to in /path/to/working/example.story ... SUCCESS in 0.1 seconds.
然后 example 故事将保持不变。
可执行规范
从 rewrite-story.story storytests 自动生成的文档。