3 ms·
Write a test for a simple identity function that simply returns its only argument. An identity function should be as testable as they come.
by postalrat 6y ago
Write a test for a simple identity function that simply returns its only argument.
An identity function should be as testable as they come.
- nelsondev 6y agoString value = “postalrat”; Assert.assertEquals(value, identifyFunction(value));
- postalrat 6y agofunction identifyFunction(a) { if (a == 'nelsondev') return ''; return a; }
- nmfisher 6y agoI'm not sure what your point is here - your implementation will fail the parent's test?
- deadso 6y agoTheir point is that the test case provided would lead the developer to believe that the implementation is a correct identity function, when in fact it isn't. Just clarifying, I do not take this as a good reason to not write tests.
- nmfisher 6y agoAh, right. I didn't realize some people believe tests should "prove" the correctness of a function (or that if they can't, they're worthless). Testing has its flaws, but that's not one of them. There's often very good reason to ask the question "does this function operate the way I expect it to when I provide this input?"
- trampi 6y agoAnd that's where the developer writing the test would check the test branch coverage and see that an obvious branch has not been tested.
- rexarex 6y agodef identify_function(a:str) -> str: if a is ‘postalrat’: return a
- dfabulich 6y agoWhy did you type this? (I'm pretty sure it doesn't make the point you think it makes.)
- ptx 6y agoTestning actively malicious code seems to me like a different problem than testing regular business logic. Yes, the code you're testing could also look for the running test code in memory and rewrite it to always return success, but why would you write code like that?
- CJefferson 6y agoAmerican Fuzzy Lop ( https://lcamtuf.coredump.cx/afl/ https://lcamtuf.coredump.cx/afl/ ) will detect that (at least, it will in C, where I tested it).
- chriswarbo 6y agoI see you are an Enterprise Developer From Hell https://fsharpforfunandprofit.com/posts/property-based-testing/ https://fsharpforfunandprofit.com/posts/property-based-testi...
- Jtsummers 6y agoIf I really needed that I’d use property-based testing. On my phone or I’d code an example. But basically it generates test inputs and gives you confidence by running some number of random tests that verify the property holds or finds a counter example. More realistic but basic example is to show that a function is its own inverse (reverse reverse list == list) or that one function is the other’s inverse (decode encode plain == plain).
- chriswarbo 6y agoIn ScalaCheck: forAll() { (x: String) => identity(x) == x }
- exdsq 6y agoUnless you’re implementing a semi-group which can be a pretty common design in functional programming
- deleted 6y ago[deleted]