elm-verify-examples icon indicating copy to clipboard operation
elm-verify-examples copied to clipboard

Don't generate broken code

Open gampleman opened this issue 7 years ago • 13 comments

Currently, if you mess up one of the formatting rules for the examples (i.e. miss a type signature for a definition), this tool will generate invalid test code, that only actually running the tests will reveal. However than one is left with the somewhat unpleasant experience of debugging generated code. Ideally this tool would actually error out in the generation phase if the code is invalid.

gampleman avatar Jun 19 '17 00:06 gampleman

hmmm. do you have a specific example? It's not possible to find out everything during​ generation, because this is not a complete elm parser. But it might be possible to make some cases better.

stoeffel avatar Jun 19 '17 05:06 stoeffel

For example running on a file where parsing seems to fail (not sure why in my case) or an empty file, I get this output:

module Doc.Visualization.ScaleSpec exposing (spec)

-- This file got generated by [elm-verify-examples](https://github.com/stoeffel/elm-verify-examples).
-- Please don't modify this file by hand!

import Test
import Expect
import Visualization.Scale exposing(..)


spec : Test.Test
spec =
    Test.describe "Visualization.Scale" <|
    ]

which is not valid elm.

gampleman avatar Jun 19 '17 11:06 gampleman

@gampleman I fixed an issue with modules that don't have any tests. would you mind testing with v1.0.2.?

stoeffel avatar Jun 19 '17 11:06 stoeffel

That is slightly better, although I think it would make sense for elm-verify-examples to report an error if a file doesn't have any examples, rather than leaving this to elm-test to complain about.

(I also have to figure out what's going wrong with my file which has plenty of examples, but it seems that it manages to confuse the parser somehow).

gampleman avatar Jun 19 '17 13:06 gampleman

Can you share any code example? Did you add the file in your elm-verify-examples.json?

stoeffel avatar Jun 19 '17 13:06 stoeffel

This seems to not generate any examples:

module Visualization.Scale exposing (nice)

{-| Returns a new scale which extends the domain so that it lands on round values.
The second argument is the same as you would pass to ticks.

    scale : ContinuousScale
    scale = linear ( 0.5, 99 ) ( 50, 100 )
    domain (nice scale 10) --> (0, 100)

-}


nice : Int -> Int
nice a =
    a + 1

edit:

as in it generates:

module Doc.Visualization.ScaleSpec exposing (spec)

-- This file got generated by [elm-verify-examples](https://github.com/stoeffel/elm-verify-examples).
-- Please don't modify this file by hand!

import Test
import Expect
import Visualization.Scale exposing(..)


spec : Test.Test
spec =
    Test.describe "Visualization.Scale" <|
    [
    ]

gampleman avatar Jun 19 '17 14:06 gampleman

The issue is that you need a newline in between the helper-function and the example.

I'm looking in how to make that work.

stoeffel avatar Jun 19 '17 14:06 stoeffel

From the docs.

⚠️ It's important that each example is separated by a newline.

{-| This will break!

    add 1 2 --> 3
    add 1 3 --> 4
-}
{-| This works!

    add 1 2 --> 3

    add 1 3 --> 4
-}

It's a known "issue" atm. Sorry for the inconvenience.

stoeffel avatar Jun 19 '17 14:06 stoeffel

That doesn't seem to make a difference for me.

gampleman avatar Jun 19 '17 14:06 gampleman

💭 hmmm... This 👇 created a test for me:

{-| Returns a new scale which extends the domain so that it lands on round values.
The second argument is the same as you would pass to ticks.

    scale : ContinuousScale
    scale = linear ( 0.5, 99 ) ( 50, 100 )

    domain (nice scale 10) --> (0, 100)

-}


nice : Int -> Int
nice a =
    a + 1

stoeffel avatar Jun 19 '17 15:06 stoeffel

hmm that's pretty strange.

This is what my tests/elm-verify-examples.json looks like:

{
  "root": "../src",
  "tests" : [
    "Visualization.List",
    "Visualization.Path",
    "Visualization.Scale"
  ]
}

gampleman avatar Jun 19 '17 17:06 gampleman

@gampleman sorry totally drop the ball here. is this still an issue for you?

stoeffel avatar Nov 04 '17 07:11 stoeffel

I've encountered it again, but I suspect this might be fixed with just better error reporting, since it tends to be in large files with a lot of stuff going on. So I think if we had better parse failure messages, this would be more straightforward to diagnose.

gampleman avatar Oct 20 '23 12:10 gampleman