including lengthy text in narrative test and scriptwriter output

1 view
Skip to first unread message

Douglas Squirrel

unread,
Nov 21, 2010, 8:59:02 AM11/21/10
to narrati...@googlegroups.com
Our first Narrative test for ScriptWriter itself loads a file from the filesystem and parses it. This means that a reader of the test (whether she is looking at the source code or ScriptWriter's HTML output) needs to go read that file to understand the test fully.

Given.the(author).was_able_to(write_a_simple_narrative_test_in(codeDir));
...[snip]...
private Action<ScriptWriter, AuthorActor> write_a_simple_narrative_test_in(final File codeDir) {
return new Action<ScriptWriter, AuthorActor>() {
@Override
public void performFor(AuthorActor actor) {
actor.writeSomeCodeIn(codeDir);
}
};
}
...[snip]...
public void writeSomeCodeIn(File codeDir) {
this.codeDirectory = codeDir;
try {
FileUtils.copyFileToDirectory(new File("scriptwriter2/src/test/fixtures/BasicArithmeticTest.java"), codeDir);
} catch (IOException e) {
e.printStackTrace();
}
}

I'd like to replace the file with inline text so the test is self-contained. Then the test would read

private static final String A_SIMPLE_NARRATIVE_TEST =
"package com.youdevise.narrative.example;
+ "import com.youdevise.test.narrative.*;" + ...[snip]...

Given.the(author).was_able_to(write(A_SIMPLE_NARRATIVE_TEST));
...[snip]...
private Action<ScriptWriter, AuthorActor> write(final String javaCode) {
return new Action<ScriptWriter, AuthorActor>() {
@Override
public void performFor(AuthorActor actor) {
actor.write(javaCode);
}
};
}
...[snip]...
public void write(String javaCode) {
// Write the code to a temporary file on the filesystem; if needed, use actor memory to store the filename
}

and ScriptWriter could render the test as

Given the author was able to write [a simple narrative test]

where the bit in brackets is a link that opens a popup or new page with the text used.

Any thoughts on this? I can't seem to find any provision for heredocs or variables or similar in Cucumber to solve similar problems of lengthy text in tests, though Aslak does suggest you avoid Rails "fixtures" for a similar reason (keeping your tests self-contained): https://github.com/aslakhellesoy/cucumber/wiki/Given-When-Then


Andrew Parker

unread,
Nov 22, 2010, 4:03:58 AM11/22/10
to narrati...@googlegroups.com
I think that would be ok. It does mean that we should keep each test file a little smaller, though, to avoid making it to noisy.

This kind of tension between making something inline vs. putting it in a file is a common tension that I find in langs that don't support heredocs or data sections.

Andy

Jason Larsen

unread,
Nov 22, 2010, 2:52:07 PM11/22/10
to narrative-users
If string concatenation becomes a problem, Scala and Groovy support
multi-line strings.

But, I am interested to see how these tests grow and maintain
readability with more test cases, especially given the self-
referential nature of the test. Would you use "lose the
narrative" (pardon the pun) if you are context switching between
example and test case?

Would it be easier to reference an example project instead of in-
lining? (Could this be a way to drive an new narrative example
project?)

Just some suggestions.

Jason
Reply all
Reply to author
Forward
0 new messages