public static Either<CommandResult> runCommand( final Context context, final String command, final File path, final List<String> arguments) { try { final List<String> commandAndArgs = new ArrayList<String>(); commandAndArgs.add(command); commandAndArgs.addAll(arguments); final ProcessBuilder pb = new ProcessBuilder(commandAndArgs); if (path != null) { pb.directory(path); } logCommand(context, pb); final Process compilation = pb.start(); final ConsumeStream result = ConsumeStream.start(compilation.getInputStream(), context); final ConsumeStream error = ConsumeStream.start(compilation.getErrorStream(), context); final int exitCode = compilation.waitFor(); result.join(); error.join(); if (result.exception != null) { return Either.fail(result.exception); } if (error.exception != null) { return Either.fail(error.exception); } return Either.success( new CommandResult(result.output.toString(), error.output.toString(), exitCode)); } catch (IOException ex) { return Either.fail(ex); } catch (InterruptedException ex) { return Either.fail(ex); } }
@Test public void shouldBimapRight() { final Either<Integer, String> actual = Either.<Integer, String>right("1").bimap(i -> i + 1, s -> s + "1"); final Either<Integer, String> expected = Either.right("11"); assertThat(actual).isEqualTo(expected); }
@Test public void shouldBimapLeft() { final Either<Integer, String> actual = Either.<Integer, String>left(1).bimap(i -> i + 1, s -> s + "1"); final Either<Integer, String> expected = Either.left(2); assertThat(actual).isEqualTo(expected); }
@Test public void shouldPeekOnRightProjectionOfLeft() { final List<Integer> actual = new ArrayList<>(); final Either<String, Integer> testee = Either.<String, Integer>left("1").right().peek(actual::add).toEither(); assertThat(actual.isEmpty()).isTrue(); assertThat(testee).isEqualTo(Either.<String, Integer>left("1")); }
@Test public void shouldPeekOnRightProjectionOfRight() { final List<Integer> actual = new ArrayList<>(); final Either<String, Integer> testee = Either.<String, Integer>right(1).right().peek(actual::add).toEither(); assertThat(actual).isEqualTo(Collections.singletonList(1)); assertThat(testee).isEqualTo(Either.right(1)); }
@Test public void shouldFlatMapOnRightProjectionOfLeft() { final Either<String, Integer> actual = Either.<String, Integer>left("1") .right() .flatMap(i -> Either.<String, Integer>right(i + 1).right()) .toEither(); assertThat(actual).isEqualTo(Either.left("1")); }
@Test public void shouldFlatMapOnLeftProjectionOfLeft() { final Either<Integer, String> actual = Either.<Integer, String>left(1) .left() .flatMap(i -> Either.<Integer, String>left(i + 1).left()) .toEither(); assertThat(actual).isEqualTo(Either.left(2)); }
public static Either<String> readFile(final File file) { try { final FileInputStream stream = new FileInputStream(file); try { final String content = read(stream); return Either.success(content); } finally { stream.close(); } } catch (IOException ex) { return Either.fail(ex); } }
public static synchronized Either<Document> readXml(final InputStream stream) { try { final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); return Either.success(dBuilder.parse(stream)); } catch (IOException ex) { return Either.fail(ex); } catch (ParserConfigurationException ex) { return Either.fail(ex); } catch (SAXException ex) { return Either.fail(ex); } }
@Test public void shouldFlatMapRightProjectionOfLeftOnRightProjectionOfRight() { final Either<String, String> good = Either.right("good"); final Either<String, String> bad = Either.left("bad"); final RightProjection<String, Tuple2<String, String>> actual = good.right().flatMap(g -> bad.right().map(b -> Tuple.of(g, b))); assertThat(actual.toEither()).isEqualTo(Either.left("bad")); }
@Test public void shouldReturnRightWhenOrElseRunOnRightProjectionOfRight() { final boolean[] actual = new boolean[] {true}; Either.<String, Integer>right(1) .right() .orElseRun( s -> { actual[0] = false; }); assertThat(actual[0]).isTrue(); }
@Test public void shouldReturnOtherWhenOrElseRunOnRightProjectionOfLeft() { final boolean[] actual = new boolean[] {false}; Either.<String, Integer>left("1") .right() .orElseRun( s -> { actual[0] = true; }); assertThat(actual[0]).isTrue(); }
public static Either<String> findCommand( final Context context, final String path, final String name, final String contains) { final String simple = path != null ? new File(path, name).getAbsolutePath() : name; if (testCommand(context, simple, contains, new ArrayList<String>())) { context.log("Found " + name + " in " + simple); return Either.success(simple); } if (isWindows()) { final String bat = path != null ? new File(path, name + ".bat").getAbsolutePath() : name + ".bat"; if (testCommand(context, bat, contains, new ArrayList<String>())) { context.log("Found " + name + " in " + bat); return Either.success(bat); } final String cmd = path != null ? new File(path, name + ".cmd").getAbsolutePath() : name + ".cmd"; if (testCommand(context, cmd, contains, new ArrayList<String>())) { context.log("Found " + name + " in " + cmd); return Either.success(cmd); } } return Either.fail("File not found: " + name); }
@Test public void shouldHashLeft() { assertThat(Either.left(1).hashCode()).isEqualTo(Objects.hashCode(1)); }
@Test public void shouldConvertRightProjectionOfRightToString() { assertThat(Either.right(1).right().toString()).isEqualTo("RightProjection(Right(1))"); }
@Test public void shouldHashRightProjectionOfLeft() { assertThat(Either.left(1).right().hashCode()).isEqualTo(Objects.hashCode(Either.left(1))); }
@Test public void shouldEqualRightProjectionOfLeft() { assertThat(Either.left(1).right()).isEqualTo(Either.left(1).right()); }
@Test public void shouldNotEqualRightProjectionOfLeftIfObjectIsOfDifferentType() { assertThat(Either.left(1).right().equals(new Object())).isFalse(); }
@Test // a property holds for all elements of no elements public void shouldNotHoldPropertyForAllOfRightProjectionOfLeft() { assertThat(Either.right(1).left().forAll(e -> true)).isTrue(); }
@Test public void shouldEqualLeft() { assertThat(Either.left(1)).isEqualTo(Either.left(1)); }
@Test public void shouldNotHoldPropertyExistsOfRightProjectionOfLeft() { assertThat(Either.right(1).left().exists(e -> true)).isFalse(); }
@Test public void shouldForEachOnRightProjectionOfLeft() { final List<Integer> actual = new ArrayList<>(); Either.<String, Integer>left("1").right().forEach(actual::add); assertThat(actual.isEmpty()).isTrue(); }
@Test public void shouldForEachOnRightProjectionOfRight() { final List<Integer> actual = new ArrayList<>(); Either.<String, Integer>right(1).right().forEach(actual::add); assertThat(actual).isEqualTo(Collections.singletonList(1)); }
@Test public void shouldConvertLeftToString() { assertThat(Either.left(1).toString()).isEqualTo("Left(1)"); }
@Test public void shouldEqualRightProjectionOfLeftIfObjectIsSame() { final RightProjection<?, ?> r = Either.left(1).right(); assertThat(r.equals(r)).isTrue(); }
@Test public void shouldNotEqualRightProjectionOfLeftIfObjectIsNull() { assertThat(Either.left(1).right().equals(null)).isFalse(); }
@Test public void shouldMapOnRightProjectionOfRight() { final Either<String, Integer> actual = Either.<String, Integer>right(1).right().map(i -> i + 1).toEither(); assertThat(actual).isEqualTo(Either.right(2)); }
@Test public void shouldBeAwareOfPropertyThatNotHoldsForAllOfRightProjectionOfRight() { assertThat(Either.right(1).right().forAll(i -> i == 2)).isFalse(); }
private static boolean parse( final String[] args, final Context context, final List<CompileParameter> parameters) { if (args.length == 1 && ("/?".equals(args[0]) || "-?".equals(args[0]) || "?".equals(args[0]))) { showHelpAndExit(context, true, parameters); return false; } final List<ParameterParser> customParsers = new ArrayList<ParameterParser>(); for (final CompileParameter cp : parameters) { if (cp instanceof ParameterParser) { customParsers.add((ParameterParser) cp); } } final List<String> errors = new ArrayList<String>(); for (String a : args) { if (a.startsWith("-") || a.startsWith("/")) a = a.substring(1); final int eq = a.indexOf('='); final String name = a.substring(0, eq != -1 ? eq : a.length()); final String value = eq == -1 ? null : a.substring(eq + 1); final CompileParameter cp = from(name, parameters); if (cp == null) { boolean matched = false; for (final ParameterParser parser : customParsers) { final Either<Boolean> tryParse = parser.tryParse(name, value, context); if (!tryParse.isSuccess()) { errors.add(tryParse.explainError()); matched = true; break; } else if (tryParse.get()) { matched = true; break; } } if (!matched) { errors.add("Unknown parameter: " + name); } } else { if (eq == -1 && cp.getUsage() != null) { if (cp instanceof ParameterParser) { Either<Boolean> tryParse = ((ParameterParser) cp).tryParse(name, null, context); if (tryParse.isSuccess() && tryParse.get()) { context.put(cp, null); } else { errors.add("Expecting " + cp.getUsage() + " after = for " + a); } } else { errors.add("Expecting " + cp.getUsage() + " after = for " + a); } } else { context.put(cp, value); } } } if (args.length == 0 || errors.size() > 0) { for (final String err : errors) { context.error(err); } showHelpAndExit(context, args.length == errors.size(), parameters); return false; } return true; }
@Test public void shouldReturnIteratorOfLeftOfRightProjection() { assertThat((Iterator<Object>) Either.left(1).right().iterator()).isNotNull(); }