Ejemplo n.º 1
0
 /**
  * `ExampleTable#forEach` executes the passed in procedure for all table rows. It will generate an
  * error message for all procedures that have failed with the reason why they failed.
  */
 @Test
 @Named("Error message")
 @Order(7)
 public void _errorMessage() throws Exception {
   final Procedure1<Boolean> _function =
       new Procedure1<Boolean>() {
         public void apply(final Boolean it) {
           final Procedure1<UsingTablesSpecExample> _function =
               new Procedure1<UsingTablesSpecExample>() {
                 public void apply(final UsingTablesSpecExample it) {
                   int _value1 = it.getValue1();
                   int _value2 = it.getValue2();
                   int _plus = (_value1 + _value2);
                   int _sum = it.getSum();
                   Assert.assertTrue(
                       "\nExpected value1 + value2 => sum but"
                           + "\n     value1 + value2 is "
                           + new org.hamcrest.StringDescription()
                               .appendValue(Integer.valueOf(_plus))
                               .toString()
                           + "\n     value1 is "
                           + new org.hamcrest.StringDescription().appendValue(_value1).toString()
                           + "\n     value2 is "
                           + new org.hamcrest.StringDescription().appendValue(_value2).toString()
                           + "\n     sum is "
                           + new org.hamcrest.StringDescription()
                               .appendValue(Integer.valueOf(_sum))
                               .toString()
                           + "\n",
                       Should.<Integer>operator_doubleArrow(
                           Integer.valueOf(_plus), Integer.valueOf(_sum)));
                 }
               };
           ExampleTableIterators.<UsingTablesSpecExample>forEach(
               UsingTablesSpec.this.example, _function);
         }
       };
   String _errorMessage = Helpers.errorMessage(_function);
   StringConcatenation _builder = new StringConcatenation();
   _builder.append("example failed");
   _builder.newLine();
   _builder.newLine();
   _builder.append("        ");
   _builder.append("| value1     | value2     | sum     |");
   _builder.newLine();
   _builder.append("        ");
   _builder.append("| 1          | 2          | 3       | ✓");
   _builder.newLine();
   _builder.append("        ");
   _builder.append("| 4          | 5          | 7       | ✘     (1)");
   _builder.newLine();
   _builder.append("        ");
   _builder.append("| 7          | 8          | 14      | ✘     (2)");
   _builder.newLine();
   _builder.newLine();
   _builder.append("(1) | 4          | 5          | 7       | ✘");
   _builder.newLine();
   _builder.append("java.lang.AssertionError: ");
   _builder.newLine();
   _builder.append("Expected value1 + value2 => sum but");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("value1 + value2 is <9>");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("value1 is <4>");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("value2 is <5>");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("sum is <7>");
   _builder.newLine();
   _builder.newLine();
   _builder.append("(2) | 7          | 8          | 14      | ✘");
   _builder.newLine();
   _builder.append("java.lang.AssertionError: ");
   _builder.newLine();
   _builder.append("Expected value1 + value2 => sum but");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("value1 + value2 is <15>");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("value1 is <7>");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("value2 is <8>");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("sum is <14>");
   Helpers.is(_errorMessage, _builder);
 }
 /**
  * Assertions in Jnario are self-explainable. When an assertion fails, it tries to provide as much
  * information about the reason of its failure as possible. It will print the exact expression
  * that has failed together with the actual value of all subexpressions. This means that you don't
  * need to debug a test to see why it actually has failed.
  */
 @Test
 @Named("Self-explaining failures")
 @Order(12)
 public void _selfExplainingFailures() throws Exception {
   final int x = 0;
   final int y = 1;
   final Procedure1<Boolean> _function =
       new Procedure1<Boolean>() {
         public void apply(final Boolean it) {
           boolean _equals = (x == 1);
           boolean _equals_1 = (y == 0);
           boolean _and = (_equals && _equals_1);
           Assert.assertTrue(
               "\nExpected x == 1 && y == 0 but"
                   + "\n     x == 1 is "
                   + new StringDescription().appendValue(_equals).toString()
                   + "\n     x is "
                   + new StringDescription().appendValue(x).toString()
                   + "\n     y == 0 is "
                   + new StringDescription().appendValue(_equals_1).toString()
                   + "\n     y is "
                   + new StringDescription().appendValue(y).toString()
                   + "\n",
               _and);
         }
       };
   String _errorMessage = Helpers.errorMessage(_function);
   StringConcatenation _builder = new StringConcatenation();
   _builder.append("Expected x == 1 && y == 0 but");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("x == 1 is <false>");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("x is <0>");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("y == 0 is <false>");
   _builder.newLine();
   _builder.append("     ");
   _builder.append("y is <1>");
   Helpers.is(_errorMessage, _builder);
   final String greeting = "hello";
   final Procedure1<Boolean> _function_1 =
       new Procedure1<Boolean>() {
         public void apply(final Boolean it) {
           String _upperCase = greeting.toUpperCase();
           boolean _should_be = Should.should_be(_upperCase, "HELLO");
           Assert.assertFalse(
               "\nExpected greeting.toUpperCase should not be \"HELLO\" but"
                   + "\n     greeting.toUpperCase is "
                   + new StringDescription().appendValue(_upperCase).toString()
                   + "\n     greeting is "
                   + new StringDescription().appendValue(greeting).toString()
                   + "\n",
               _should_be);
         }
       };
   String _errorMessage_1 = Helpers.errorMessage(_function_1);
   StringConcatenation _builder_1 = new StringConcatenation();
   _builder_1.append("Expected greeting.toUpperCase should not be \"HELLO\" but");
   _builder_1.newLine();
   _builder_1.append("     ");
   _builder_1.append("greeting.toUpperCase is \"HELLO\"");
   _builder_1.newLine();
   _builder_1.append("     ");
   _builder_1.append("greeting is \"hello\"");
   Helpers.is(_errorMessage_1, _builder_1);
 }