assert

Rails ActiveSupport: How to assert that an error is raised?

六眼飞鱼酱① 提交于 2019-12-18 03:01:25
问题 I am wanting to test a function on one of my models that throws specific errors. The function looks something like this: def merge(release_to_delete) raise "Can't merge a release with itself!" if( self.id == release_to_delete.id ) raise "Can only merge releases by the same artist" if( self.artist != release_to_delete.artist ) #actual merge code here end Now I want to do an assert that when I call this function with a parameter that causes each of those exceptions, that the exceptions actually

TestNG shows 0 Test run

痞子三分冷 提交于 2019-12-17 22:28:07
问题 I'm trying to execute my test scripts using testNG and trying the below code, but 0 displayed against run, fail and skip in the console. Because of that I'm unable to verify the results in my script. Java: package com.demoaut.newtours.testcases; import org.testng.Assert; import org.testng.annotations.Test; //import junit.framework.Assert; public class TC002_CheckAssert { @Test public TC002_CheckAssert() { System.out.println("ajkcbh"); try { Assert.assertEquals("Pass", "Pass"); } catch

Custom C++ assert macro

你离开我真会死。 提交于 2019-12-17 21:50:07
问题 I stumbled upon an informative article: http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ which pointed out a great number of problems that exist in my current suite of debugging macros. The full code for the final version of the macro is given near the end of the article if you follow the link. The general form as presented is like this (somebody please correct me if i am wrong in transposing it): #ifdef DEBUG #define ASSERT(cond) \ do \ { \ if (!(cond)) \ { \ ReportFailure

How does Assert.AreEqual determine equality between two generic IEnumerables?

老子叫甜甜 提交于 2019-12-17 17:56:13
问题 I have a unit test to check whether a method returns the correct IEnumerable . The method builds the enumerable using yield return . The class that it is an enumerable of is below: enum TokenType { NUMBER, COMMAND, ARITHMETIC, } internal class Token { public TokenType type { get; set; } public string text { get; set; } public static bool operator == (Token lh, Token rh) { return (lh.type == rh.type) && (lh.text == rh.text); } public static bool operator != (Token lh, Token rh) { return !(lh =

正则表达式使用范例 Linux C++

安稳与你 提交于 2019-12-17 14:38:01
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> POSIX正则库使用 基本介绍: POSIX(Portable Operating System Interface of Unix) 是unix系统提供的系统级通用正则库。 四个主要接口:regcomp, regexec, regerror, regfree (可以通过man命令查询参数含义) 代码示例: #include<iostream> #include<string> #include<sys/types.h> #include<regex.h> #include<assert.h> using namespace std; int main(int argc, char** argv) { string pattern("([[:alnum:]]+):([[:digit:]]+)"); // ([a-z]+):([0-9]+) also works here regex_t regex; //compile int errcode = regcomp(&regex, pattern.c_str(), REG_EXTENDED | REG_NOSUB); char errbuf[128]; if (errcode != 0) { regerror(errcode, &regex, errbuf,

条件变量与生产者消费者问题

佐手、 提交于 2019-12-17 08:57:23
文章目录 使用 if而非 while且只有一个条件变量 使用while但只有一个条件变量 使用while且有两个条件变量 扩展缓冲区大小(从1到数组) 本文主要是探讨<<操作系统导论>>一书第30章-条件变量的知识. 书中介绍了条件变量的概念, 并将条件变量运用在生产者消费者问题中. 从最简单的情况开始, 列举了使用条件变量解决生产者消费者问题的几种错误用法. 本文主要是对这几种情况的代码模拟分析, 分为以下四个部分 使用 if而非 while且只有一个条件变量 使用while但只有一个条件变量 使用while且有两个条件变量 扩展缓冲区大小(从1到数组) 使用 if而非 while且只有一个条件变量 书中提供的第一个方案(有问题), 给生产者和消费者共用一个条件变量, 且使用if来判断缓存区 问题: wait的条件使用了if而不是while, 导致如果有多个消费者的情况, 当一个阻塞的消费者被生产者唤醒了, 准备执行但这时另一个消费者抢占执行并进行了消费导致缓冲区空了. 这时切换到第一个消费者消费, 因为缓冲区空了触发断言, 程序错误. 如果换成while, 那么第二个消费者在醒来的时候, 就会再判断一下条件是否成立, 由于被另一个消费者消费了, 所以它又会调用wait被阻塞. 需要注意wait函数的执行过程. 当一个线程执行wait的时候, 会释放它持有的锁,

Disable assertions in Python

核能气质少年 提交于 2019-12-17 04:21:28
问题 How do I disable assertions in Python? That is, if an assertion fails, I don't want it to throw an AssertionError , but to keep going. How do I do that? 回答1: How do I disable assertions in Python? There are multiple approaches that affect a single process, the environment, or a single line of code. I demonstrate each. For the whole process Using the -O flag (capital O) disables all assert statements in a process. For example: $ python -Oc "assert False" $ python -c "assert False" Traceback

PHPUnit assert that an exception was thrown?

拈花ヽ惹草 提交于 2019-12-17 04:14:46
问题 Does anyone know whether there is an assert or something like that which can test whether an exception was thrown in the code being tested? 回答1: <?php require_once 'PHPUnit/Framework.php'; class ExceptionTest extends PHPUnit_Framework_TestCase { public function testException() { $this->expectException(InvalidArgumentException::class); // or for PHPUnit < 5.2 // $this->setExpectedException(InvalidArgumentException::class); //...and then add your test code that generates the exception

Ways to ASSERT expressions at build time in C

谁说我不能喝 提交于 2019-12-17 03:03:10
问题 I'm tidying up some older code that uses 'magic numbers' all over the place to set hardware registers, and I would like to use constants instead of these numbers to make the code somewhat more expressive (in fact they will map to the names/values used to document the registers). However, I'm concerned that with the volume of changes I might break the magic numbers. Here is a simplified example (the register set is more complex): const short mode0 = 0; const short mode1 = 1; const short mode2

Ways to ASSERT expressions at build time in C

☆樱花仙子☆ 提交于 2019-12-17 03:02:27
问题 I'm tidying up some older code that uses 'magic numbers' all over the place to set hardware registers, and I would like to use constants instead of these numbers to make the code somewhat more expressive (in fact they will map to the names/values used to document the registers). However, I'm concerned that with the volume of changes I might break the magic numbers. Here is a simplified example (the register set is more complex): const short mode0 = 0; const short mode1 = 1; const short mode2