-1

I have a simple lambda that is basically x => "Error: "+x+".". I was just wondering if this was worth a test. It's really just a one-liner, so there's not much that can go wrong.

Should I write a test?

DaCool1
  • 37
  • 3
  • While there is the concept that some things are too simple to test, if you have an interpreted language it would be good to at least make sure you don't have any typos. – Berin Loritsch Mar 01 '22 at 03:15

1 Answers1

7

It's about behavior not structure.

Being a lambda isn't the point. Being a one liner isn't the point.

The point is your behavior is well tested string concatenation. Without a requirement I say move along. Nothing interesting to see here.

You can cram an entire operating system into a one liner or a lambda. Stop getting distracted by structure.

candied_orange
  • 102,279
  • 24
  • 197
  • 315
  • 1
    That being said though, if the software requirements specify an _exact_ message, testing may be warranted. This matters for integrated components where the value is not merely meant for human readability. – Flater Mar 01 '22 at 08:42
  • @Flater true, if the behavior is critical then even if the logic is simple you can justify regression tests, integration tests, and acceptance tests. What you should avoid is testing for no better reason then because you don't trust that string concatenation works. Interesting logic though needs testing even if it doesn't show up in the requirements. Question didn't specify any requirements and this code isn't interesting. – candied_orange Mar 01 '22 at 16:23
  • I'm not sure about other languages, but in C++ you simply *can't* unit test a lambda in-situ. You can only unit test a *different* lambda, because each lambda expression defines a unique class type. – Caleth Mar 02 '22 at 10:17
  • @Caleth same is true in Java. What difference does that make? – candied_orange Mar 02 '22 at 16:42
  • @Flater updated. Better now? – candied_orange Mar 03 '22 at 20:06