assertions

Why does my Haskell assertion only happen in IHaskell?

青春壹個敷衍的年華 提交于 2019-12-05 16:02:21
If I define import Control.Exception (assert) import Data.Char (ord) f :: String -> String f s = assert (all (`elem` letters) s) $ (letters!!) <$> (ix <$> s) where ix ch = (ord ch - ord 'A') letters = ['A'..'Z'] then if I execute f "AB.CD" in IHaskell, I get :10:7-12: Assertion failed as I would expect. But in all other settings, the assertion seems to be ignored. For example in GHCi (7.10.2) I get ghci>f "AB.CD" "AB*** Exception: Prelude.!!: negative index and if I put the expression in a program main :: IO () main = do print $ f "AB.CD" I get prgm: Prelude.!!: negative index "AB Why is my

AttributeError: 'module' object has no attribute 'TestCase'

南笙酒味 提交于 2019-12-05 12:52:38
问题 I have file with unittest named: test.py My code: import unittest class Test(unittest.TestCase): def myTest(self): a = 1 self.assertEqual(a, 1) if __name__ == '__main__': unittest.main() When I press F5, I get an error: Traceback (most recent call last): File "/home/mariusz/Pulpit/test.py", line 1, in <module> import unittest File "/home/mariusz/Pulpit/unittest.py", line 3, in <module> AttributeError: 'module' object has no attribute 'TestCase' 回答1: You have a local file named unittest.py

Is there a way how to continue after Debug.Assert() from the code?

别说谁变了你拦得住时间么 提交于 2019-12-05 11:51:41
My code operates on data which "should" be correct. However during development there are occasions when I obtain invalid data. When that happens I would like to raise the debug assert and, if user choose to continue, the code would filter out the invalid records and continue to operate on "safe" data. // assert incorrect data Debug.Assert(person.Items.All(item => item.IsValid), "Inconsistent data!"); // operate on filtered data this.ItemViewModels = new ObservableCollection<ItemViewModel>( person.Items .Where(i =>item.IsValid) // Use only correct data .Select(i => new ItemViewModel(lang, i)));

How to catch an assert with Google test?

北城余情 提交于 2019-12-05 08:27:29
问题 I'm programming some unit test with the Google test framework. But I want to check whether some asserts are well placed and are useful. Is there a way to catch an assert in Google test? Example code under test: int factorial(int n){ assert(n >= 0); //.... } And then the test: #include <gtest/gtest.h> TEST(FactorialTest,assertNegative){ EXPECT_ANY_THROW({ factorial(-1); }); } But EXPECT_ANY_THROW doesn't catch the assert but only exceptions. I'm searching for a solution to catch asserts. 回答1:

Is there any way to check with Python unittest assert if an iterable is not empty?

删除回忆录丶 提交于 2019-12-05 08:21:03
问题 After submitting queries to a service, I get a dictionary / a list back and I want to make sure it's not empty. I am on Python 2.7. I am surprised I don't see any assertEmpty method for the unittest.TestCase class instance. The existing alternatives such as: self.assertTrue(bool(d)) and self.assertNotEqual(d,{}) and self.assertGreater(len(d),0) just don't look right. Is this kind of method is missing in the Python unittest framework? If yes, what would be the most pythonic way to assert that

c# - Asserting with OR condition

怎甘沉沦 提交于 2019-12-05 07:03:16
i am checking a string for three characters Assert.AreEqual(myString.Substring(3,3), "DEF", "Failed as DEF was not observed"); the thing is here it can be DEF or RES , now to handle this what i can think of is the following bool check = false; if( myString.Substring(3,3) == "DEF" || myString.Substring(3,3) == "RED" ) check = true; Assert.IsTrue(check,"Failed"); Console.WriteLine(""Passed); IS THERE a way i can use some OR thing within Assert p.s i'm writing unit test & yes i will use ternary operator instead.... Assert.IsTrue((myString.Substring(3,3) == "DEF" || myString.Substring(3,3) == "RED

Is there any way to check with Python unittest assert if an iterable is not empty?

牧云@^-^@ 提交于 2019-12-04 22:11:49
After submitting queries to a service, I get a dictionary / a list back and I want to make sure it's not empty. I am on Python 2.7. I am surprised I don't see any assertEmpty method for the unittest.TestCase class instance. The existing alternatives such as: self.assertTrue(bool(d)) and self.assertNotEqual(d,{}) and self.assertGreater(len(d),0) just don't look right. Is this kind of method is missing in the Python unittest framework? If yes, what would be the most pythonic way to assert that an iterable is not empty? Empty lists/dicts evaluate to False, so self.assertTrue(d) gets the job done.

How to implement XUnit descriptive Assert message?

怎甘沉沦 提交于 2019-12-04 09:56:02
问题 Context in XUnit github I found this: Add Assert.Equal(expected, actual, message) overload #350 (so a developer ask for a non existing overload see below) Quote from the answer: We are a believer in self-documenting code; that includes your assertions. (so the XUnit team rejects it) OK, I got it. I also believe the self documenting code. Still I can not find out this use case: Sample // Arrange // Create some external soap service client and its wrapper classes // Act // client.SomeMethod();

Using assertion in the Linux kernel

◇◆丶佛笑我妖孽 提交于 2019-12-04 08:51:43
问题 I have a question about assert() in Linux: can I use it in the kernel? If no, what techniques do you usually use if, for example I don't want to enter NULL pointer? 回答1: The corresponding kernel macros are BUG_ON and WARN_ON . The former is for when you want to make the kernel panic and bring the system down (i.e., unrecoverable error). The latter is for when you want to log something to the kernel log (viewable via dmesg ). As @Michael says, in the kernel, you need to validate anything that

Cobertura coverage and the assert keyword

时光总嘲笑我的痴心妄想 提交于 2019-12-04 06:15:21
My line coverage for unit tests measured by Cobertura is suffering, because I have assert statements which are not covered in tests. Should I be testing assert ions, and is there any way to get Cobertura to ignore them so they do not affect my test coverage? The line coverage of your Java assert statements should be simply covered by running your test suite with assertions enabled, i.e., giving -ea as argument to the jvm. If you do this, you'll see that cobertura easily reports 100% line coverage if the rest of your lines are covered as well. Nevertheless, the assert lines will still be