//------------------------------------------------------------------------- public virtual void test_consumer_success() { System.Action <string> a = Unchecked.consumer((t) => { }); a("A"); }
//------------------------------------------------------------------------- public virtual void test_biConsumer_success() { System.Action <string, string> a = Unchecked.biConsumer((t, u) => { }); a("A", "B"); }
public virtual void test_consumer_fail1() { System.Action <string> a = Unchecked.consumer((t) => { throw new IOException(); }); assertThrows(() => a("A"), typeof(UncheckedIOException)); }
public virtual void test_predicate_fail2() { System.Predicate <string> a = Unchecked.predicate((t) => { throw new Exception(); }); assertThrows(() => a("A"), typeof(Exception)); }
public virtual void test_binaryOperator_fail2() { System.Func <string, string, string> a = Unchecked.binaryOperator((t, u) => { throw new Exception(); }); assertThrows(() => a("A", "B"), typeof(Exception)); }
public virtual void test_biConsumer_fail2() { System.Action <string, string> a = Unchecked.biConsumer((t, u) => { throw new Exception(); }); assertThrows(() => a("A", "B"), typeof(Exception)); }
public virtual void test_unaryOperator_fail1() { System.Func <string, string> a = Unchecked.unaryOperator((t) => { throw new IOException(); }); assertThrows(() => a("A"), typeof(UncheckedIOException)); }
public virtual void test_supplier_fail2() { System.Func <string> a = Unchecked.supplier(() => { throw new Exception(); }); assertThrows(() => a(), typeof(Exception)); }
public virtual void test_biFunction_fail1() { System.Func <string, string, string> a = Unchecked.biFunction((t, u) => { throw new IOException(); }); assertThrows(() => a("A", "B"), typeof(UncheckedIOException)); }
public virtual void test_function_fail2() { System.Func <string, string> a = Unchecked.function((t) => { throw new Exception(); }); assertThrows(() => a("A"), typeof(Exception)); }
public virtual void test_biPredicate_fail2() { System.Func <string, string, bool> a = Unchecked.biPredicate((t, u) => { throw new Exception(); }); assertThrows(() => a("A", "B"), typeof(Exception)); }
public virtual void test_runnable_fail2() { ThreadStart a = Unchecked.runnable(() => { throw new Exception(); }); assertThrows(() => a.run(), typeof(Exception)); }
//------------------------------------------------------------------------- /// <summary> /// Converts checked exceptions to unchecked based on the {@code Predicate} interface. /// <para> /// This wraps the specified predicate returning an instance that handles checked exceptions. /// If a checked exception is thrown it is converted to an <seealso cref="UncheckedIOException"/> /// or <seealso cref="RuntimeException"/> as appropriate. /// /// </para> /// </summary> /// @param <T> the type of the predicate </param> /// <param name="predicate"> the predicate to be decorated </param> /// <returns> the predicate instance that handles checked exceptions </returns> public static System.Predicate <T> predicate <T>(CheckedPredicate <T> predicate) { return((t) => { try { return Unchecked.predicate(t); } catch (Exception ex) { throw propagate(ex); } }); }
//------------------------------------------------------------------------- /// <summary> /// Converts checked exceptions to unchecked based on the {@code Consumer} interface. /// <para> /// This wraps the specified consumer returning an instance that handles checked exceptions. /// If a checked exception is thrown it is converted to an <seealso cref="UncheckedIOException"/> /// or <seealso cref="RuntimeException"/> as appropriate. /// /// </para> /// </summary> /// @param <T> the type of the consumer </param> /// <param name="consumer"> the consumer to be decorated </param> /// <returns> the consumer instance that handles checked exceptions </returns> public static System.Action <T> consumer <T>(CheckedConsumer <T> consumer) { return((t) => { try { Unchecked.consumer(t); } catch (Exception ex) { throw propagate(ex); } }); }
//------------------------------------------------------------------------- /// <summary> /// Converts checked exceptions to unchecked based on the {@code Function} interface. /// <para> /// This wraps the specified function returning an instance that handles checked exceptions. /// If a checked exception is thrown it is converted to an <seealso cref="UncheckedIOException"/> /// or <seealso cref="RuntimeException"/> as appropriate. /// /// </para> /// </summary> /// @param <T> the input type of the function </param> /// @param <R> the return type of the function </param> /// <param name="function"> the function to be decorated </param> /// <returns> the function instance that handles checked exceptions </returns> public static System.Func <T, R> function <T, R>(CheckedFunction <T, R> function) { return((t) => { try { return Unchecked.function(t); } catch (Exception ex) { throw propagate(ex); } }); }
/// <summary> /// Capture system err for testing. /// <para> /// This returns the output from calls to {@code System.err}. /// This is thread-safe, providing that no other utility alters system out. /// </para> /// <para> /// For example: /// <pre> /// String sysErr = captureSystemErr(() -> myCode); /// </pre> /// /// </para> /// </summary> /// <param name="runner"> the lambda containing the code to test </param> /// <returns> the captured output </returns> public static string caputureSystemErr(ThreadStart runner) { lock (typeof(TestHelper)) { // it would be possible to use some form of thread-local PrintStream to increase concurrency, // but that should be done only if synchronized is insufficient assertNotNull(runner, "caputureSystemErr() called with null Runnable"); MemoryStream baos = new MemoryStream(1024); PrintStream ps = Unchecked.wrap(() => new PrintStream(baos, false, UTF_8)); PrintStream old = System.err; try { System.Err = ps; runner.run(); System.err.flush(); } finally { System.Err = old; } return(Unchecked.wrap(() => baos.ToString(UTF_8))); } }
//------------------------------------------------------------------------- public virtual void test_unaryOperator_success() { System.Func <string, string> a = Unchecked.unaryOperator((t) => t); assertEquals(a("A"), "A"); }
public virtual void test_propagate() { Exception error = new Exception("a"); System.ArgumentException argEx = new System.ArgumentException("b"); IOException ioEx = new IOException("c"); URISyntaxException namingEx = new URISyntaxException("d", "e"); // use old-style try-catch to ensure test really working try { Unchecked.propagate(error); fail(); } catch (Exception ex) { assertSame(ex, error); } try { Unchecked.propagate(argEx); fail(); } catch (System.ArgumentException ex) { assertSame(ex, argEx); } try { Unchecked.propagate(ioEx); fail(); } catch (UncheckedIOException ex) { assertEquals(ex.GetType(), typeof(UncheckedIOException)); assertSame(ex.InnerException, ioEx); } try { Unchecked.propagate(namingEx); fail(); } catch (Exception ex) { assertEquals(ex.GetType(), typeof(Exception)); assertSame(ex.InnerException, namingEx); } try { Unchecked.propagate(new InvocationTargetException(error)); fail(); } catch (Exception ex) { assertSame(ex, error); } try { Unchecked.propagate(new InvocationTargetException(argEx)); fail(); } catch (System.ArgumentException ex) { assertSame(ex, argEx); } try { Unchecked.propagate(new InvocationTargetException(ioEx)); fail(); } catch (UncheckedIOException ex) { assertEquals(ex.GetType(), typeof(UncheckedIOException)); assertSame(ex.InnerException, ioEx); } try { Unchecked.propagate(new InvocationTargetException(namingEx)); fail(); } catch (Exception ex) { assertEquals(ex.GetType(), typeof(Exception)); assertSame(ex.InnerException, namingEx); } }
//------------------------------------------------------------------------- public virtual void test_supplier_success() { System.Func <string> a = Unchecked.supplier(() => "A"); assertEquals(a(), "A"); }
//------------------------------------------------------------------------- public virtual void test_binaryOperator_success() { System.Func <string, string, string> a = Unchecked.binaryOperator((t, u) => t + u); assertEquals(a("A", "B"), "AB"); }
//------------------------------------------------------------------------- public virtual void test_predicate_success() { System.Predicate <string> a = Unchecked.predicate((t) => true); assertEquals(a("A"), true); }
//------------------------------------------------------------------------- public virtual void test_biPredicate_success() { System.Func <string, string, bool> a = Unchecked.biPredicate((t, u) => true); assertEquals(a("A", "B"), true); }
//------------------------------------------------------------------------- public virtual void test_function_success() { System.Func <string, string> a = Unchecked.function((t) => t); assertEquals(a("A"), "A"); }