assertions

How to programmatically enable assert?

 ̄綄美尐妖づ 提交于 2019-12-18 11:42:05
问题 How can I programmatically enable assert for particular classes, instead of specifying command line param "-ea"? public class TestAssert { private static final int foo[] = new int[]{4,5,67}; public static void main(String []args) { assert foo.length == 10; } } 回答1: This was a comment to @bala's good answer, but it got too long. If you just enable assertions then call your main class--your main class will be loaded before assertions are enabled so you will probably need a loader that doesn't

C++ error-codes vs ASSERTS vs Exceptions choices choices :( [closed]

笑着哭i 提交于 2019-12-18 03:42:10
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . Code In question I have heard (and regurgitated) the C++ exception mantra on both sides of the fence. It has been a while and I just want to centre myself once more, and this discussion is specific to the code I have linked (or low level classes such as containers) , and it's

Debug.Assert vs Exception Throwing

假如想象 提交于 2019-12-17 22:29:15
问题 I've read plenty of articles (and a couple of other similar questions that were posted on StackOverflow) about how and when to use assertions, and I understood them well. But still, I don't understand what kind of motivation should drive me to use Debug.Assert instead of throwing a plain exception. What I mean is, in .NET the default response to a failed assertion is to "stop the world" and display a message box to the user. Though this kind of behavior could be modified, I find it highly

How do I use Assert.Throws to assert the type of the exception?

我怕爱的太早我们不能终老 提交于 2019-12-17 10:11:53
问题 How do I use Assert.Throws to assert the type of the exception and the actual message wording. Something like this: Assert.Throws<Exception>( ()=>user.MakeUserActive()).WithMessage("Actual exception message") The method I am testing throws multiple messages of the same type, with different messages, and I need a way to test that the correct message is thrown depending on the context. 回答1: Assert.Throws returns the exception that's thrown which lets you assert on the exception. var ex = Assert

When should I use Debug.Assert()?

爱⌒轻易说出口 提交于 2019-12-17 07:58:10
问题 I've been a professional software engineer for about a year now, having graduated with a CS degree. I've known about assertions for a while in C++ and C, but had no idea they existed in C# and .NET at all until recently. Our production code contains no asserts whatsoever and my question is this... Should I begin using Asserts in our production code? And if so, When is its use most appropriate? Would it make more sense to do Debug.Assert(val != null); or if ( val == null ) throw new exception(

What does the Java assert keyword do, and when should it be used?

橙三吉。 提交于 2019-12-16 20:04:06
问题 What are some real life examples to understand the key role of assertions? 回答1: Assertions (by way of the assert keyword) were added in Java 1.4. They are used to verify the correctness of an invariant in the code. They should never be triggered in production code, and are indicative of a bug or misuse of a code path. They can be activated at run-time by way of the -ea option on the java command, but are not turned on by default. An example: public Foo acquireFoo(int id) { Foo result = null;

How to modify TestNG assertEquals?

∥☆過路亽.° 提交于 2019-12-14 03:01:31
问题 I have actual and expected objects each containing some data members. For one data member, I need to do a contains check instead of equals check and for the rest, equals check is done. Is there a way to do this ? 回答1: Not implicitly, but you have at least the following 2 options: use TestNG's assertTrue use an additional library such as Hamcrest, AssertJ, etc Dependencies: <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.3</version> <scope>test<

When should you use assertions and preconditions and when you can use guard statements, forced unwrapping and error handling? [closed]

浪子不回头ぞ 提交于 2019-12-13 10:38:26
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . I've already read Difference between “precondition” and “assert” in swift. But still can't draw a clear line between the (different ways of unwrapping ie guard & ! + error handling) vs assertions. If I want my application to no longer work, can't I just force unwrap something

How to access parent element in XSD assertion XPath?

微笑、不失礼 提交于 2019-12-12 20:13:21
问题 I am trying to write an assertion that will make the values of @row and @column less than or equal to the values of @rows and @columns in the parent element <structure> . <xs:element name="structure"> <xs:complexType> <xs:sequence> <xs:element name="cell" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="row" type="xs:positiveInteger"/> <xs:attribute name="column" type="xs:positiveInteger"/> <xs:assert test="@row le @rows"/> <xs:assert test="@column le @columns"/> </xs:complexType>

How can I use NUnit's EqualTo().Within() constraint with a custom data type?

只谈情不闲聊 提交于 2019-12-12 14:17:42
问题 I love NUnit's constraint-based API. I often use floating point comparison like this: double d = foo.SomeComputedProperty; Assert.That(d, Is.EqualTo(42.0).Within(0.001)); Very readable! However, if I have a custom class whose equality depends on floating point comparison: class Coord { Coord(double radius, double radians) { this.Radius = radius; this.Radians = radians; } double Radius { get; } double Radians { get; } public override bool Equals(Object obj) { Coord c = obj as Coord; if (obj ==