unity断言_Unity断言库

unity断言

Unity 5.1 shipped with a brand new Assertion Library. In this post, we will explain what an assertion is and how you can use it to improve runtime error diagnostics in your games.

Unity 5.1随附了全新的断言库。 在这篇文章中,我们将解释什么是断言,以及如何使用它来改善游戏中的运行时错误诊断。

什么是断言,我为什么要关心? (What is an assertion and why should I care?)

An assertion is a method that checks for a condition. If the condition is true, the method returns and the execution continues. If something unexpected happens and the assumption in the condition is not met, a message is printed that shows the callstack and optionally a user specified message. Let’s look at an example:

断言是一种检查条件的方法。 如果条件为true,则该方法返回并继续执行。 如果发生了意外情况,并且不满足该条件中的假设,则会显示一条消息,显示调用堆栈,并显示用户指定的消息(可选)。 让我们看一个例子:

1

2
//Make sure the player GameObject is active
Assert.IsTrue(playerGameObject.isActive, “Player GameObject is not active”);

1

2
//Make sure the player GameObject is active
Assert . IsTrue ( playerGameObject . isActive , “ Player GameObject is not active ” ) ;

If the referenced GameObject was not active, an error log message would be printed showing the callstack and specified message “Player GameObject is not active”.

如果引用的GameObject未激活,则会显示一条错误日志消息,显示调用堆栈和指定的消息“ Player GameObject未激活”。

Assertion message

Many developers know asserts from unit tests. In a unit tests written using the Arrange-Act-Assert pattern, it’s the final part of the test method, where you compare expected and actual results. Assertions are not just for tests, though. You can use them in production code as well to get warned, at runtime, when invariants are violated. However, not all asserts should be used in runtime code.

许多开发人员都知道单元测试中的断言。 在使用Arrange-Act-Assert模式编写的单元测试中,它是测试方法的最后一部分,您可以在其中比较预期结果和实际结果。 断言不仅用于测试。 您也可以在生产代码中使用它们,以便在运行时遇到不变量时得到警告。 但是,并非所有断言都应在运行时代码中使用。

单元测试框架中的断言库又如何呢? (What about the assertion libraries from unit tests frameworks?)

It is very likely that you have first encountered assertions in a unit tests framework. As an example, let’s take NUnit. NUnit has a rich and battle-tested library for asserting tests you write with it. A natural thing to ask is why wouldn’t you simply want to use this library to test your production code? There are few reasons for that and one simple answer: it’s not meant for that purpose.

您很可能首先在单元测试框架中遇到断言。 以NUnit为例。 NUnit有一个丰富且经过考验的库,可以断言您使用它编写的测试。 一个自然的问题是,为什么您不只是想使用此库来测试您的生产代码? 这样做的原因很少,只有一个简单的答案:这并非出于此目的。

NUnit assertions allow you to test many things. From simple equality comparisons through more complex operations on collections to testing throwing exceptions. That is all fine but it all makes it too slow for using it in runtime. Low level assertions need to be as lean as possible and not give any unnecessary overhead in execution. The assertion library was written to run with no extra memory allocation and unnecessary operations.

NUnit断言允许您测试许多事情。 从简单的相等性比较到对集合的更复杂的操作,再到测试引发异常。 没关系,但是这在运行时使用它太慢了。 低级别的断言必须尽可能地精简,并且在执行过程中不会产生任何不必要的开销。 断言库被编写为无需额外的内存分配和不必要的操作即可运行。

An important thing for an assertion library is the possibility to strip the method calls out from the release build. The assertions can be very useful for developers during the production, but will be meaningless for the end-users if they fail. When building your final build you would want to get rid of all the calls to assertions. You could potentially comment all of the code out, but that wouldn’t be too smart. Luckily, .NET has a conditional compilation mechanism for such situations. The assertions library is implemented with Conditional attribute that will only include the assertion call in the developer’s build of your players. It is, however, still possible to include the assertions by forcing it via a compilation option.

对于断言库,重要的事情是可以从发行版本中剥离方法调用。 这些断言对于开发人员在生产过程中可能非常有用,但对于最终用户失败则毫无意义。 在构建最终版本时,您将希望摆脱所有对断言的调用。 您可能会注释掉所有代码,但这并不是太聪明。 幸运的是,.NET具有针对这种情况的条件编译机制。 断言库是使用Conditional属性实现的,该条件属性仅将断言调用包括在播放器的开发人员构建中。 但是,仍然可以通过编译选项来强制包含断言。

Last but not least, it’s common for unit test assertion libraries to be based on exceptions. An exception is thrown on a failure and the execution flow is broken. This, obviously, is not desired in runtime code. The assertion library has been integrated with the Unity logging system and in case of a failure, a message will be logged. It makes the library versatile for all the platforms Unity supports. It also means the assertions work on AOT platforms that do not support exceptions.

最后但并非最不重要的一点是,单元测试断言库通常基于异常。 失败引发异常,执行流程中断。 显然,这在运行时代码中是不需要的。 断言库已与Unity日志记录系统集成,如果发生故障,将记录一条消息。 它使该库可用于Unity支持的所有平台。 这也意味着断言可以在不支持异常的AOT平台上工作。

那么,图书馆里有什么? (So, what is in the library?)

The library offers a several type-specific comparison methods and a generic equality comparer. Some of the assertion methods:

该库提供了几种特定于类型的比较方法和一个通用的相等比较器。 一些断言方法:

    All the methods can be found in the Assert documentation.

    所有方法都可以在Assert文档中找到。

    A cool thing about this library is it’s out-of-the-box compatibility with Unity Test Tools. Without any extra effort, the assertion will fail any integration tests calling the code guarded by them.

    关于这个库的一个很酷的事情是它与Unity Test Tools具有开箱即用的兼容性。 无需任何额外的努力,该断言将不会通过任何集成测试调用它们保护的代码而失败。

    扩展图书馆 (Extending the library)

    When you want to get the most out of a feature, it’s natural to want to extend it. The library’s AreEqual methods allow you to pass your own implementation of a comparer for a specific type. The comparer has to implement the IEqualityComparer interface.

    当您想充分利用某项功能时,自然就想扩展它。 该库的AreEqual方法允许您传递自己的特定类型比较器的实现。 比较器必须实现IEqualityComparer接口。

    The library comes with FloatComparer used for comparing floats. It also allows you to do comparison with a relative error check. This comparer is used by AreApproximatellyEqual method.

    该库带有用于比较浮点数的FloatComparer 。 它还允许您与相对错误检查进行比较。 此比较器由AreroxllyEqual方法使用。

    结语 (Wrap-up)

    Using assertion library to protect your code from bugs and undesired state is an easy and little-impact mechanism you can start using right away. The library will be improved with time and, as usual, your comments and suggestions are more than welcome!

    使用断言库来保护代码免受错误和不期望的状态的侵害,是一种可以立即开始使用的简单且影响很小的机制。 该库将随着时间的推移而得到改进,并且像往常一样,欢迎您提出意见和建议!

    翻译自: https://blogs.unity3d.com/2015/08/25/the-unity-assertion-library/

    unity断言

    • 0
      点赞
    • 2
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值