From: Francois B. <fb...@us...> - 2003-06-24 05:14:09
|
Hi ! I have a test which tests for the presence of two keys in a Map. If one or both keys are missing, do A, else B. So, here's what the code looks like at the moment: if (false =3D=3D getConstantsMap().containsKey(constantName + CLASS_SUFFIX) || false =3D=3D getConstantsMap().containsKey(constantName + VALUE_SUFFIX)) { No, Java is able to short-circuit the evaluation of a condition if the truth of a condition can be determined early. Right now, I am setting up my dynamock so: final Mock mockMap =3D new Mock(Map.class); mockMap.expectAndReturn("containsKey", C.eq(constantName + ".class"), false); mockMap.expectAndReturn("containsKey", C.eq(constantName + ".value"), true); mockMap.expect("put", C.args( C.eq(constantName + ".class"), C.eq(Integer.TYPE) ) ); My test fails because Java short circuits the evaluation of the second condition. I changed to using a matchAndReturn for the second containsKey, but this is an implementation detail. What would you guys recommend ? Should I use matchAndReturn for both containsKey() setups, or should I change the implementation to use temp variables and then test these ? Thanks, Fran=E7ois -- Francois Beausoleil Developer of Java Gui Builder http://jgb.sourceforge.net/ |
From: Nat P. <nat...@b1...> - 2003-06-24 08:45:10
|
You can use the 'Or' Constraint to test for a containsKey call with either the keys. If used with expectAndReturn, the mock will expect *one* call to containsKey for *either* of those values. This will take into account short circuited evaluation. However, if you don't want your test to expose details of short circuited evaluation, you can set up two matchAndReturn calls for both keys. I would recommend this option because you are stubbing the behaviour of the map and testing the behaviour of your object in response to what is in the map. The exact sequence of calls that your object makes to examine the map are not important; only what the map returns to your object and what your object does in response. E.g. Using Or: String constantClass = constantName + ".class"; String constantValue = constantName + ".value"; mockMap.expectAndReturn( "containsKey", C.or(C.eq(constantClass),c.eq(constantValue)), false ); E.g. using matchAndReturn: mockMap.matchAndReturn( "containsKey", constantClass, false ); mockMap.matchAndReturn( "containsKey", constantValue, false ); Cheers, Nat. P.S. I would recommend refactoring your if statement: private boolean definesConstant( String name ) { return getConstantsMap().containsKey( name ); } ... String constantClass = constantName + CLASS_SUFFIX; String constantValue = constantName + VALUE_SUFFIX; if ( !definesConstant(constantClass) || !definesConstant(constantValue) ) { ... blah blah blah ... } else { ... blah blah blah ... } _______________________ Dr. Nathaniel Pryce B13media Ltd. http://www.b13media.com +44 (0)7712 526 661 ----- Original Message ----- From: "Francois Beausoleil" <fb...@us...> To: <moc...@li...> Sent: Tuesday, June 24, 2003 6:14 AM Subject: [MO-java-dev] How to test double conditional implementation, which could change... Hi ! I have a test which tests for the presence of two keys in a Map. If one or both keys are missing, do A, else B. So, here's what the code looks like at the moment: if (false == getConstantsMap().containsKey(constantName + CLASS_SUFFIX) || false == getConstantsMap().containsKey(constantName + VALUE_SUFFIX)) { No, Java is able to short-circuit the evaluation of a condition if the truth of a condition can be determined early. Right now, I am setting up my dynamock so: final Mock mockMap = new Mock(Map.class); mockMap.expectAndReturn("containsKey", C.eq(constantName + ".class"), false); mockMap.expectAndReturn("containsKey", C.eq(constantName + ".value"), true); mockMap.expect("put", C.args( C.eq(constantName + ".class"), C.eq(Integer.TYPE) ) ); My test fails because Java short circuits the evaluation of the second condition. I changed to using a matchAndReturn for the second containsKey, but this is an implementation detail. What would you guys recommend ? Should I use matchAndReturn for both containsKey() setups, or should I change the implementation to use temp variables and then test these ? Thanks, François -- Francois Beausoleil Developer of Java Gui Builder http://jgb.sourceforge.net/ ------------------------------------------------------- This SF.Net email is sponsored by: INetU Attention Web Developers & Consultants: Become An INetU Hosting Partner. Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission! INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php _______________________________________________ Mockobjects-java-dev mailing list Moc...@li... https://lists.sourceforge.net/lists/listinfo/mockobjects-java-dev |
From: Francois B. <fb...@us...> - 2003-06-24 20:13:45
|
Hi ! Thanks for the suggestion. I really want to hide the implementation detail, so I will use matchAndReturn. Thanks for the refactoring suggestion too :) Bye ! Fran=E7ois On Tue, 24 Jun 2003 09:42:06 +0100, "Nat Pryce" <nat...@b1...> said: > You can use the 'Or' Constraint to test for a containsKey call with > either > the keys. If used with expectAndReturn, the mock will expect *one* call > to > containsKey for *either* of those values. This will take into account > short > circuited evaluation. >=20 > However, if you don't want your test to expose details of short circuited > evaluation, you can set up two matchAndReturn calls for both keys. I > would > recommend this option because you are stubbing the behaviour of the map > and > testing the behaviour of your object in response to what is in the map.= =20 > The > exact sequence of calls that your object makes to examine the map are not > important; only what the map returns to your object and what your object > does in response. >=20 > E.g. Using Or: >=20 > String constantClass =3D constantName + ".class"; > String constantValue =3D constantName + ".value"; >=20 > mockMap.expectAndReturn( "containsKey", >=20 > C.or(C.eq(constantClass),c.eq(constantValue)), > false ); >=20 > E.g. using matchAndReturn: >=20 > mockMap.matchAndReturn( "containsKey", constantClass, false ); > mockMap.matchAndReturn( "containsKey", constantValue, false ); >=20 > Cheers, > Nat. >=20 >=20 > P.S. I would recommend refactoring your if statement: >=20 > private boolean definesConstant( String name ) { > return getConstantsMap().containsKey( name ); > } >=20 > ... >=20 > String constantClass =3D constantName + CLASS_SUFFIX; > String constantValue =3D constantName + VALUE_SUFFIX; >=20 > if ( !definesConstant(constantClass) || > !definesConstant(constantValue) ) { > ... blah blah blah ... > } else { > ... blah blah blah ... > } >=20 > _______________________ > Dr. Nathaniel Pryce > B13media Ltd. > http://www.b13media.com > +44 (0)7712 526 661 >=20 > ----- Original Message ----- > From: "Francois Beausoleil" <fb...@us...> > To: <moc...@li...> > Sent: Tuesday, June 24, 2003 6:14 AM > Subject: [MO-java-dev] How to test double conditional implementation, > which > could change... >=20 >=20 > Hi ! >=20 > I have a test which tests for the presence of two keys in a Map. If one > or both keys are missing, do A, else B. So, here's what the code looks > like at the moment: > if (false =3D=3D getConstantsMap().containsKey(constantName + CLASS_SUFFI= X) > || false =3D=3D getConstantsMap().containsKey(constantName + > VALUE_SUFFIX)) { >=20 > No, Java is able to short-circuit the evaluation of a condition if the > truth of a condition can be determined early. Right now, I am setting up > my dynamock so: > final Mock mockMap =3D new Mock(Map.class); > mockMap.expectAndReturn("containsKey", > C.eq(constantName + ".class"), > false); > mockMap.expectAndReturn("containsKey", > C.eq(constantName + ".value"), > true); > mockMap.expect("put", > C.args( > C.eq(constantName + ".class"), > C.eq(Integer.TYPE) > ) > ); >=20 > My test fails because Java short circuits the evaluation of the second > condition. I changed to using a matchAndReturn for the second > containsKey, but this is an implementation detail. >=20 > What would you guys recommend ? Should I use matchAndReturn for both > containsKey() setups, or should I change the implementation to use temp > variables and then test these ? >=20 > Thanks, > Fran=E7ois > -- > Francois Beausoleil > Developer of Java Gui Builder > http://jgb.sourceforge.net/ >=20 >=20 > ------------------------------------------------------- > This SF.Net email is sponsored by: INetU > Attention Web Developers & Consultants: Become An INetU Hosting Partner. > Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission! > INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php > _______________________________________________ > Mockobjects-java-dev mailing list > Moc...@li... > https://lists.sourceforge.net/lists/listinfo/mockobjects-java-dev >=20 >=20 -- Francois Beausoleil Developer of Java Gui Builder http://jgb.sourceforge.net/ |
From: Dan C. <da...@dy...> - 2003-06-24 18:02:26
|
Why not run a few tests. One in which the both Booleans are false, one in which both Booleans are true and one in which the first is false and the second is true? -----Original Message----- From: moc...@li... [mailto:moc...@li...] On Behalf Of Francois Beausoleil Sent: Monday, June 23, 2003 10:14 PM To: moc...@li... Subject: [MO-java-dev] How to test double conditional implementation, which could change... Hi ! I have a test which tests for the presence of two keys in a Map. If one or both keys are missing, do A, else B. So, here's what the code looks like at the moment: if (false =3D=3D getConstantsMap().containsKey(constantName + = CLASS_SUFFIX) || false =3D=3D getConstantsMap().containsKey(constantName + VALUE_SUFFIX)) { No, Java is able to short-circuit the evaluation of a condition if the truth of a condition can be determined early. Right now, I am setting up my dynamock so: final Mock mockMap =3D new Mock(Map.class); mockMap.expectAndReturn("containsKey", C.eq(constantName + ".class"), false); mockMap.expectAndReturn("containsKey", C.eq(constantName + ".value"), true); mockMap.expect("put", C.args( C.eq(constantName + ".class"), C.eq(Integer.TYPE) ) ); My test fails because Java short circuits the evaluation of the second condition. I changed to using a matchAndReturn for the second containsKey, but this is an implementation detail. What would you guys recommend ? Should I use matchAndReturn for both containsKey() setups, or should I change the implementation to use temp variables and then test these ? Thanks, Fran=E7ois -- Francois Beausoleil Developer of Java Gui Builder http://jgb.sourceforge.net/ ------------------------------------------------------- This SF.Net email is sponsored by: INetU Attention Web Developers & Consultants: Become An INetU Hosting Partner. Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission! INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php _______________________________________________ Mockobjects-java-dev mailing list Moc...@li... https://lists.sourceforge.net/lists/listinfo/mockobjects-java-dev |