assert

How to check constructor arguments and throw an exception or make an assertion in a default constructor in Scala?

回眸只為那壹抹淺笑 提交于 2019-11-26 23:05:51
问题 I would like to check constructor arguments and refuse to construct throwing IllegalArgumentException in case the arguments set is not valid (the values don't fit in expected constraints). How to code this in Scala? 回答1: In Scala, the whole body of the class is your primary constructor, so you can add your validation logic there. scala> class Foo(val i: Int) { | if(i < 0) | throw new IllegalArgumentException("the number must be non-negative.") | } defined class Foo scala> new Foo(3) res106:

Groovy

雨燕双飞 提交于 2019-11-26 23:03:53
Q&A Gradle 中的 ext 究竟是什么? gradle 中我们使用 ext 定义额外的各种属性,可是 ext 究竟是什么呢? 参看 ExtraPropertiesExtension - Gradle DSL ,发现 ext 不是 Groovy 固有的定义,而是领域特定的语言(DSL)。使用方式是: // 以下的 project 常常被省略 project.ext { foo = "bar" } assert project.ext.get("foo") == "bar" assert project.ext.foo == "bar" assert project.ext["foo"] == "bar" assert project.foo == "bar" assert project["foo"] == "bar" ext 实质上是一个内置的简单对象,但可以动态添加新属性,这个对象叫 ExtraPropertiesExtension ,它内置在所有 ExtensionAware 中, ExtenstionAware 的已知子类有 Project 、 Settings 、 Task 、 SourceSet ,所以在这些类中可以直接使用所谓的 namespace method 动态新增新属性。 // Extensions are just plain objects,

.NET 4.0 中的契约式编程

假如想象 提交于 2019-11-26 21:43:19
契约式编程不是一门崭新的编程方法论。C/C++ 时代早已有之。Microsoft 在 .NET 4.0 中正式引入契约式编程库。博主以为契约式编程是一种相当不错的编程思想,每一个开发人员都应该掌握。它不但可以使开发人员的思维更清晰,而且对于提高程序性能很有帮助。值得一提的是,它对于并行程序设计也有莫大的益处。 我们先看一段很简单的,未使用契约式编程的代码示例。 // .NET 代码示例 public class RationalNumber { private int numberator; private int denominator; public RationalNumber( int numberator, int denominator) { this .numberator = numberator; this .denominator = denominator; } public int Denominator { get { return this .denominator; } } } 上述代码表示一个在 32 位有符号整型范围内的有理数。数学上,有理数是一个整数 a 和一个非零整数 b 的比,通常写作 a/b,故又称作分数(题外话:有理数这个翻译真是够奇怪)。由此,我们知道,有理数的分母不能为 0 。所以,上述代码示例的构造函数还需要写些防御性代码。通常

如何编译 D-Phoenix 库

随声附和 提交于 2019-11-26 21:43:18
近日有朋友问及如何编译和使用 D-Phoenix 库。于是就有了这篇文章的存在。 这个年代,没有 IDE 进行编码编译是痛苦的。所以,博主将主要介绍使用 Poseidon 来编译 D-Phoenix 库(为啥子要讲 Poseidon ,而不是 CodeBlocks 之类的 IDE。哈,因为 Poseidon 是国产货)。 首先,D 语言编译器是必备武器。 D-Phoenix 库推荐使用 DMD 2.019 for Win32编译器。你可以按照下面的方法安置好 D 语言编译器: 从 http://ftp.digitalmars.com/dmc.zip 处获得 DMC Linker & Utilities for Win32,并解压缩到 C:\ 根目录下。 从 http://ftp.digitalmars.com/dmd.2.019.zip 处获得 DMD 2.019 编译器,并解压缩到 C:\ 根目录下。 然后,从 http://www.dsource.org/projects/poseidon/changeset/head/trunk?old_path=%2F&format=zip 处获得最新版 Poseidon IDE。 接下来,在你指定的位置新建文件夹 d-phoenix 。然后从 http://d-phoenix.googlecode.com/files/Phoenix

Rocket - tilelink - ErrorEvaluator

主宰稳场 提交于 2019-11-26 20:59:40
https://mp.weixin.qq.com/s/NkbW465NAmhDsETksd2M0g 介绍ErrorEvaluator的实现。 ​ ​ 1. 基本介绍 ErrorEvaluator 用于判断请求(Request)是否符合某种模式(Pattern)。 若符合,则下游节点针对这个请求的响应消息中应该报告错误。 2. RequestPattern ​ ​ 1) 测试方法: test: TLBundleA => Bool a. 输入参数: TLBundleA ,可以看出这个方法只能测试channel a的请求; b. 返回值: Bool ,返回测试结果,即是否满足模式; 2) 调用测试方法 根据RequestPattern的定义可以看出:测试方法是固定的,待测试的对象是变化的。需要测试某个请求时,直接把该请求传入RequestPattern对象的apply方法即可: ​ ​ 3) 测试方法实例:overlaps 判断请求中的地址是否与指定的AddressSet序列中的某一个重叠: ​ ​ 包含两个参数列表: a. pattern: Seq [AddressSet] :预先指定的一组AddressSet; b. a: TLBundleA :channel a输入的请求; 这里第一个参数列表命名为pattern并不合适,为了不与RequestPattern中的Pattern混淆

Should one override equals method for asserting the object equality in a unit test?

青春壹個敷衍的年華 提交于 2019-11-26 20:54:16
问题 Let's say we are testing the result of a method by asserting the equality of all the properties of the result object with properties of an expected result object. Should we implement equals method and use Assert.AreEqual(expectedResult, actualResult)... But equals may mean something different in production code. Which is the best practice? Asserting the equality of the objects through overriden equals method or Asserting the equality of all the properties 回答1: I for one use custom assertions.

How to enable the Java keyword assert in Eclipse program-wise?

纵饮孤独 提交于 2019-11-26 20:28:23
How can I enable the assert keyword in Eclipse? public class A { public static void main(String ... args) { System.out.println(1); assert false; System.out.println(2); } } To be specific: Go to Run->run configuration select java application in left nav pan. right click and select New . select Arguments tab Add -ea in VM arguments. If anyone wants to enable assertions by default (in contrast to enabling them for just a single run configuration), it is possible with the following steps: Window (menu bar) Preferences Java Installed JREs Select your JRE/JDK Press Edit... Default VM arguments Add

Use NUnit Assert.Throws method or ExpectedException attribute?

那年仲夏 提交于 2019-11-26 19:03:54
问题 I have discovered that these seem to be the two main ways of testing for exceptions: Assert.Throws<Exception>(()=>MethodThatThrows()); [ExpectedException(typeof(Exception))] Which of these would be best? Does one offer advantages over the other? Or is it simply a matter of personal preference? 回答1: The first allows you to test for more than one exception, with multiple calls: Assert.Throws(()=>MethodThatThrows()); Assert.Throws(()=>Method2ThatThrows()); The second only allows you to test for

Add custom messages in assert?

爱⌒轻易说出口 提交于 2019-11-26 18:53:04
问题 Is there a way to add or edit the message thrown by assert? I'd like to use something like assert(a == b, "A must be equal to B"); Then, the compiler adds line , time and so on... Is it possible? 回答1: A hack I've seen around is to use the && operator. Since a pointer "is true" if it's non-null, you can do the following without altering the condition: assert(a == b && "A is not equal to B"); Since assert shows the condition that failed, it will display your message too. If it's not enough, you

What does static_assert do, and what would you use it for?

天大地大妈咪最大 提交于 2019-11-26 18:48:42
问题 Could you give an example where static_assert(...) ('C++11') would solve the problem in hand elegantly? I am familiar with run-time assert(...) . When should I prefer static_assert(...) over regular assert(...) ? Also, in boost there is something called BOOST_STATIC_ASSERT , is it the same as static_assert(...) ? 回答1: Off the top of my head... #include "SomeLibrary.h" static_assert(SomeLibrary::Version > 2, "Old versions of SomeLibrary are missing the foo functionality. Cannot proceed!");