22

I'm defining a TestList (HUnit) and want to spread the definition over multiple lines. I came to the following solution:

tests = TestList ([TestLabel "test1" test1] ++ [TestLabel "test2" test2] ++ [TestLabel "test3" test3] ++ [TestLabel "test4" test4] ++ [TestLabel "test5" test5]) 
  • Is the use of the ++ operator the proper way to do such things?
  • Are there better or more elegant ways to do this?
1

3 Answers 3

30

I'd write

tests = TestList [ TestLabel "test1" test1 , TestLabel "test2" test2 , TestLabel "test3" test3 , TestLabel "test4" test4 , TestLabel "test5" test5 ] 
Sign up to request clarification or add additional context in comments.

1 Comment

Fascinating! A Haskeller in his natural habitat! Note his Haskellisms: it is common for wild Haskellers to line the commas up with the open bracket. Since Haskell's whitespace is significant, it is also common for wild Haskellers to go to the next line when beginning a list definition or similar. Crikey!
13

There's still place for improvements for @ephemient variant: don't use TestLabel at all, use ~: shortcut:

tests = TestList [ "test1" ~: test1 , "test2" ~: test2 , "test3" ~: test3 , "test4" ~: test4 , "test5" ~: test5 ] 

Note that there are more operators to construct assertions: @?, @=?, @?=. See http://hunit.sourceforge.net/HUnit-1.0/Guide.html or http://hackage.haskell.org/package/HUnit for details. The shortcuts use priorities and type classes cleverly, so you will get much less parentheses noise at the cost of slightly worse error messages.

Comments

6

Maybe I'm missing something, but why not just commas? This doesn't seem particularly unlike a normal list.

tests = TestList ([TestLabel "test1" test1, TestLabel "test2" test2, TestLabel "test3" test3, TestLabel "test4" test4, TestLabel "test5" test5]) 

2 Comments

Because it was too obvious... :( I had a typo so I guessed that this doesn't work for some reason...
Scolytus: it's likely you made a mistake with indentation. Haskell's particular flavor of significant whitespace takes a little getting used to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.