Changes between Version 39 and Version 40 of ProgrammingGuidelines


Ignore:
Timestamp:
07/12/11 16:33:17 (8 years ago)
Author:
welberge
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ProgrammingGuidelines

    v39 v40  
    169169can write your own asserts for custom data types. Some HMI specic asserts 
    170170(for example to assert Quat4f or Vec3f equality) are stored in !HmiTestUtil. 
     171 
     172=== Custom assertions using hamcrest === 
     173[[http://code.google.com/p/hamcrest/|hamcrest]] is a utility library providing custom assertions with good human-readable error messages. Some examples (added here, because the documentation coming with hamcrest sucks). 
     174Assert that there are 3 items in someList, prints the content of the list if 
     175this is not the case: 
     176{{{ 
     177import static org.hamcrest.MatcherAssert.assertThat; 
     178import static org.hamcrest.collection.IsCollectionWithSize.hasSize; 
     179... 
     180assertThat(someCollection,hasSize(3)); 
     181}}} 
     182 
     183Asserts that x > 4 : 
     184{{{ 
     185import static org.hamcrest.MatcherAssert.assertThat; 
     186import static org.hamcrest.number.OrderingComparison.greaterThan; 
     187... 
     188assertThat(x,greaterThan(4)); 
     189}}} 
     190 
     191Asserts that {{{x instanceof X}}}: 
     192{{{ 
     193import static org.hamcrest.MatcherAssert.assertThat; 
     194... 
     195assertThat(x, instanceOf(X.class)); 
     196}}} 
     197 
     198Asserts that someList has items item1, item2, item3 in any order; someList 
     199may contain other items. 
     200{{{ 
     201import static org.hamcrest.MatcherAssert.assertThat; 
     202import static org.hamcrest.Matchers.hasItems; 
     203... 
     204assertThat(someList,hasItems(item1,item2,item3)); 
     205}}} 
     206 
     207Asserts that someList has items item1, item2, item3 exactly in that order; 
     208someList may not contain any other items. 
     209{{{ 
     210import static org.hamcrest.MatcherAssert.assertThat; 
     211import org.hamcrest.collection.*; 
     212... 
     213assertThat(someList,IsIterableContainingInOrder.contains("item1","item2","item3")); 
     214}}} 
     215 
     216Asserts that someList has items item1, item2, item3 in order; someList 
     217may not contain any other items. 
     218{{{ 
     219import static org.hamcrest.MatcherAssert.assertThat; 
     220import org.hamcrest.collection.*; 
     221... 
     222assertThat(someList,IsIterableContainingInOrder.contains("item1","item2","item3")); 
     223}}} 
     224 
     225Asserts that someList has items item1, item2, item3 in any order; someList 
     226may not contain any other items. 
     227{{{ 
     228import static org.hamcrest.MatcherAssert.assertThat; 
     229import org.hamcrest.collection.*; 
     230... 
     231assertThat(someList,IsIterableContainingInAnyOrder.contains("item1","item2","item3")); 
     232}}} 
     233 
     234 
     235Asserts that someCollection has size 1 (does not show list items if this is not 
     236the case) 
     237{{{ 
     238import static org.hamcrest.MatcherAssert.assertThat; 
     239import static org.hamcrest.collection.IsCollectionWithSize.hasSize; 
     240... 
     241assertThat(someCollection,hasSize(1)); 
     242}}} 
     243Asserts that someCollection is an empty list of String (and shows the list 
     244items if this is not the case): 
     245{{{ 
     246import static org.hamcrest.MatcherAssert.assertThat; 
     247import org.hamcrest.Matchers; 
     248... 
     249assertThat(someCollection,Matchers.<String>empty()); 
     250}}} 
     251 
    171252[[ViewTopic()]] 
    172253!^[[ElckerlycDocumentation]]