assert

assert()的使用

我们两清 提交于 2019-12-01 08:04:00
assert()是定义在assert.h中的恶宏,其用法是 assert(条件表达式); 若条件为真,什么也不做;做条件为假,强制终止程序。 比如说在条件分支中,如果程序没有Bug,我们确定某个分支绝对不会被走到,可以这样写: … case .. : … default: assert(0); 如果程序没有Bug,肯定不会走到分支default;在这个分支里强行退出程序,这种方法是值得肯定的。 来源: https://www.cnblogs.com/freshair_cnblog/p/11671818.html

Flutter 错误捕获的正确姿势

不羁岁月 提交于 2019-12-01 07:42:19
背景 我们知道,在软件开发过程中,错误和异常总是在所难免。 不管是客户端的逻辑错误导致的,还是服务器的数据问题导致的,只要出现了异常,我们都需要一个机制来通知我们去处理。 在 APP 的开发过程中,我们通过一些第三方的平台,比如 Fabric、Bugly 等可以实现异常的日志上报。 Flutter 也有一些第三方的平台,比如 Sentry 可以实现异常的日志上报。 但是为了更加通用一些,本篇不具体讲解配合某个第三方平台的异常日志捕获,我们会告知大家如何在 Flutter 里面捕获异常。 至于具体的上报途径,不管是上报到自家的后台服务器,还是通过第三方的 SDK API 接口进行异常上报,都是可以的。 Demo 初始状态 首先我们新建 Flutter 项目,修改 main.dart 代码如下: import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar:

java bean 属性验证框架 valid

大憨熊 提交于 2019-12-01 07:17:37
项目介绍 java 开发中,参数校验是非常常见的需求。 但是 hibernate-validator 在使用过程中,依然会存在一些问题。 特性 支持 fluent-validation 支持 jsr-303 注解 支持 i18n 支持用户自定义策略 支持用户自定义注解 开源地址 valid 创作目的 hibernate-validator 无法满足的场景 如今 java 最流行的 hibernate-validator 框架,但是有些场景是无法满足的。 比如: 验证新密码和确认密码是否相同。(同一对象下的不同属性之间关系) 当一个属性值满足某个条件时,才进行其他值的参数校验。 多个属性值,至少有一个不能为 null 其实,在对于多个字段的关联关系处理时,hibernate-validator 就会比较弱。 本项目结合原有的优点,进行这一点的功能强化。 validation-api 过于复杂 validation-api 提供了丰富的特性定义,也同时带来了一个问题。 实现起来,特别复杂。 然而我们实际使用中,常常不需要这么复杂的实现。 valid-api 提供了一套简化很多的 api,便于用户自行实现。 自定义缺乏灵活性 hibernate-validator 在使用中,自定义约束实现是基于注解的,针对单个属性校验不够灵活。 本项目中,将属性校验约束和注解约束区分开,便于复用和拓展。

Can pytest be made to fail if nothing is asserted?

不问归期 提交于 2019-12-01 06:37:55
Today I had a failing test that happily succeeded, because I forgot a rather important line at the end: assert actual == expected I would like to have the machine catch this mistake in the future. Is there a way to make pytest detect if a test function does not assert anything, and consider this a test failure? Of course, this needs to be a "global" configuration setting; annotating each test function with @fail_if_nothing_is_asserted would defeat the purpose. This is one of the reasons why it really helps to write a failing test before writing the code to make the test pass. It's that one

How do I structure tests for asynchronous functions?

主宰稳场 提交于 2019-12-01 05:43:13
I'm accustomed to writing Mocha tests using the standard NodeJs assert library like this: describe('Some module', () => { var result = someCall(); it('Should <something>', () => { assert.ok(...); }); }) but now my call returns a promise... so I want to write: describe('Some module', async () => { var result = await someCall(); it('Should <something>', () => { assert.ok(...); }); }) but it doesn't work. My tests don't run at all. Curiously, describe('Some module', async () => { it('Should <something>', () => { var result = await someCall(); assert.ok(...); }); }) works fine but the problem is

pytest官网文档の第一章:安装和快速开始

依然范特西╮ 提交于 2019-12-01 05:33:33
1.1 安装 安装命令 pip install pytest 查看版本 pytest --version 1.2 创建第一个测试函数 测试代码如下: # -*- coding: utf-8 -*- # 开发人员 :RayChou # 开发时间 :2019-10-12 10:27 # 文件名称 :test_simple.PY # 开发工具 :PyCharm def func(x): return x + 1 def test_answer(): assert func(3) == 5 执行结果如下: (venv) D:\workspace\demo1\test_module>pytest =========================================================================================================================== test session starts =========================================================================================================================== platform win32 -- Python 3.6.5,

assert(false) vs RuntimeException?

巧了我就是萌 提交于 2019-12-01 04:20:53
I'm reading the source of XWalkUIClientInternal and I ran into the following code: switch(type) { case JAVASCRIPT_ALERT: return onJsAlert(view, url, message, result); case JAVASCRIPT_CONFIRM: return onJsConfirm(view, url, message, result); case JAVASCRIPT_PROMPT: return onJsPrompt(view, url, message, defaultValue, result); case JAVASCRIPT_BEFOREUNLOAD: // Reuse onJsConfirm to show the dialog. return onJsConfirm(view, url, message, result); default: break; } assert(false); return false; I've never really seen this technique nor really thought about it before, but I guess this essentially means

Can pytest be made to fail if nothing is asserted?

£可爱£侵袭症+ 提交于 2019-12-01 04:18:50
问题 Today I had a failing test that happily succeeded, because I forgot a rather important line at the end: assert actual == expected I would like to have the machine catch this mistake in the future. Is there a way to make pytest detect if a test function does not assert anything, and consider this a test failure? Of course, this needs to be a "global" configuration setting; annotating each test function with @fail_if_nothing_is_asserted would defeat the purpose. 回答1: This is one of the reasons

Why is assert not enabled by default in java

你离开我真会死。 提交于 2019-12-01 04:04:23
My question is from the perspective of language design. Why is assert treated differently i.e. it raises a error and not an exception, it is not enabled by default etc.. It does seem elegant(very subjective opinion), easy to read(again subjective) for doing validations & also there are tools(IDE) which can live-evaluate it and provide warnings based on assertions. I'd say that the reason is that defaults for Java are meant to build "release" version of software - if users need to build your code they will use provided defaults and if you are developer and want to have better reporting you can

How do I structure tests for asynchronous functions?

天涯浪子 提交于 2019-12-01 03:26:05
问题 I'm accustomed to writing Mocha tests using the standard NodeJs assert library like this: describe('Some module', () => { var result = someCall(); it('Should <something>', () => { assert.ok(...); }); }) but now my call returns a promise... so I want to write: describe('Some module', async () => { var result = await someCall(); it('Should <something>', () => { assert.ok(...); }); }) but it doesn't work. My tests don't run at all. Curiously, describe('Some module', async () => { it('Should