public static LoadedNarrative load() { return new LoadedNarrative(); }
private String readNarrativeFrom(List<String> lines) { StringBuilder description = new StringBuilder(); for (String line : lines) { if (!isMarkup(line) && !isAnnotation(line) && !(isComment(line))) { description.append(line.trim()); description.append(System.lineSeparator()); } } return description.toString().trim(); }
public java.util.Optional<Narrative> fromFile(File narrativeFile, String defaultType) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(narrativeFile), StandardCharsets.UTF_8))) { List<String> lines = readPreambleFrom(reader); String title = null; String type = defaultType; String cardNumber = findCardNumberIn(lines); List<String> versionNumbers = findVersionNumberIn(lines); Optional<String> titleLine = readOptionalTitleFrom(lines); if (titleLine.isPresent()) { title = titleFrom(titleLine.get()); type = typeFrom(titleLine.get()).orElse(defaultType); } String text = readNarrativeFrom(lines); reader.close(); List<TestTag> tags = (StringUtils.isEmpty(title)) ? new ArrayList<>() : Collections.singletonList(TestTag.withName(title).andType("story")); return java.util.Optional.of(new Narrative(Optional.ofNullable(title), Optional.of(narrativeFile.getPath()), Optional.ofNullable(cardNumber), versionNumbers, type, text, tags)); } catch (IOException ex) { ex.printStackTrace(); } return java.util.Optional.empty(); }
private Optional<String> readOptionalTitleFrom(List<String> lines) { if (!lines.isEmpty()) { String firstLine = lines.get(0); if (!isMarkup(firstLine)) { Optional<String> title = Optional.of(stripFeatureFrom(firstLine)); lines.remove(0); return title; } } return Optional.empty(); }
private List<String> readPreambleFrom(BufferedReader reader) throws IOException { List<String> usefulLines = new ArrayList<>(); boolean preambleFinished = false; while (!preambleFinished) { String nextLine = reader.readLine(); if (nextLine == null) { preambleFinished = true; } else { if (preambleFinishedAt(nextLine)) { preambleFinished = true; } else if (thereIsUsefulInformationIn(nextLine)) { usefulLines.add(nextLine); } } } return usefulLines; }
private boolean isAnnotation(String line) { return normalizedLine(line).startsWith("@"); }
private boolean preambleFinishedAt(String nextLine) { return (normalizedLine(nextLine).startsWith("scenario") || normalizedLine(nextLine).startsWith("background:")); }
private boolean thereIsUsefulInformationIn(String nextLine) { String normalizedText = normalizedLine(nextLine); return !normalizedText.startsWith("#") && !normalizedText.startsWith("meta:") && !normalizedText.startsWith("background:") && !(normalizedText.startsWith("@") && (!normalizedText.startsWith("@issue") && (!normalizedText.startsWith("@versions")))); }
public static String definedIn(String fullPath, String defaultType) { Optional<String> narrativePath = narrativePathsFor(fullPath); // String narrativePath = asResourcePath(fileSystemPathOfNarrativeInPackage(fullPath)) + ".txt"; try { if (narrativePath.isPresent()) { // String narrativeFilePath = Resources.getResource(narrativePath).getFile(); // if (new File(narrativeFilePath).exists()) { Optional<Narrative> narrative = LoadedNarrative.load().fromFile(new File(narrativePath.get()), defaultType); if (narrative.isPresent()) { return (narrative.get().getTitle().orElse("") + System.lineSeparator() + narrative.get().getText()).trim(); } } } catch (IllegalArgumentException noNarrativeFileFound) { } return ""; }
private boolean isMarkup(String line) { String normalizedLine = normalizedLine(line); return normalizedLine.startsWith("narrative:") || normalizedLine.startsWith("givenstory:") || normalizedLine.startsWith("background:") || normalizedLine.startsWith("meta") || normalizedLine.startsWith("@") || normalizedLine.startsWith("givenstories:"); }