A test suite for Elm

Through working on the eco compiler I have become quite familiar with many aspects of testing Elm and its quirks. I have also created a huge number of tests. Part of eco’s test suite is a set of what I call the E2E tests, which are real Elm programs designed to test Elm, and the elm/* packages. I have extracted that test suite into the above linked project, and run it over Elm 0.19.1, 0.19.2, 0.19.3 and Lambera 1.5.0 (build on 0.19.3).

Here are the current results:

  ┌───────────────┬────────┬────────┐
  │   Compiler    │ Passed │ Failed │
  ├───────────────┼────────┼────────┤
  │ Elm 0.19.1    │ 1,375  │ 2      │
  ├───────────────┼────────┼────────┤
  │ Elm 0.19.2    │ 1,377  │ 0      │
  ├───────────────┼────────┼────────┤
  │ Elm 0.19.3    │ 1,371  │ 6      │
  ├───────────────┼────────┼────────┤
  │ Lamdera 1.5.0 │ 1,372  │ 5      │
  └───────────────┴────────┴────────┘

Eight tests have a different outcome on at least one compiler:

  ┌──────────────────────────────────────────────────────┬────────┬────────┬──────────────────┬──────────────────┐
  │                         Test                         │ 0.19.1 │ 0.19.2 │      0.19.3      │  Lamdera 1.5.0   │
  ├──────────────────────────────────────────────────────┼────────┼────────┼──────────────────┼──────────────────┤
  │ elm-bug-fixes/CaseCharEscapedDuplicateTest           │ fail   │ pass   │ pass             │ pass             │
  ├──────────────────────────────────────────────────────┼────────┼────────┼──────────────────┼──────────────────┤
  │ elm-bug-fixes/ParseErrorCasePatternUnfinishedTest    │ fail   │ pass   │ pass             │ pass             │
  ├──────────────────────────────────────────────────────┼────────┼────────┼──────────────────┼──────────────────┤
  │ AliasProblemExtensibleRecordUnitTupleAndFunctionTest │ pass   │ pass   │ hang (60 s       │ internal crash   │
  │                                                      │        │        │ timeout)         │                  │
  ├──────────────────────────────────────────────────────┼────────┼────────┼──────────────────┼──────────────────┤
  │ AliasProblemQualifiedTypeTest                        │ pass   │ pass   │ hang (60 s       │ internal crash   │
  │                                                      │        │        │ timeout)         │                  │
  ├──────────────────────────────────────────────────────┼────────┼────────┼──────────────────┼──────────────────┤
  │ AliasProblemRecursiveAliasWithCommentedParensTest    │ pass   │ pass   │ SIGSEGV          │ internal crash   │
  ├──────────────────────────────────────────────────────┼────────┼────────┼──────────────────┼──────────────────┤
  │ CyclicDefinitionATopLevelValueDefinedByItselfTest    │ pass   │ pass   │ SIGSEGV          │ internal crash   │
  ├──────────────────────────────────────────────────────┼────────┼────────┼──────────────────┼──────────────────┤
  │ CyclicValueALetValueDefinedByItselfTest              │ pass   │ pass   │ SIGSEGV          │ pass             │
  ├──────────────────────────────────────────────────────┼────────┼────────┼──────────────────┼──────────────────┤
  │ elm-conformance/DebugToStringTest                    │ pass   │ pass   │ fail (record     │ fail (record     │
  │                                                      │        │        │ field order)     │ field order)     │
  └──────────────────────────────────────────────────────┴────────┴────────┴──────────────────┴──────────────────┘

The reason you see 2 fails on 0.19.1 and 0 on 0.19.2, is that the changes made around those tests in 0.19.2 look like bug fixes, not regressions, so I put them into a bugfix category.

The test suite is likely to have very high coverage of code branches in the compiler, but not 100%. The reason for this is that I ran a coverage driven loop, generating more test cases to try and hit more branches in the compiler - effectively the same compiler as the guida port, which is 1:1 with the Haskell - therefore likely to be getting good coverage against the Haskel. But this has not been measured (perhaps it could be if anyone knows about Haskell coverage tools ?).

I am also aware that I have obviously not captured all known bugs in Elm in this test suite yet. Some “bugs” may be pinned by tests in the elm-conformance category. The elm-conformance categeory is where most of the quirky behaviour, that we might consider buggy (or not!) sits currently.

I have the elm-janitor bug sheet too, and could run a scan over all the github issues to try and extract and categorize more bugs as failing tests.

To give an example, I don’t have the type rank = 2 thing exposed as a test fail yet, or crashing on modBy 0, but I think it would be good to have those recorded in this suite as bugs.

This is not a criticism of Elm and the hard work spent creating it - I understand the volume of work and time involved and that its impossible to do everything. I have also had the benefit of coming at this in the age of AI; I did not write a single one of these tests by hand. Also worth noting that creating them was not effort free or slop, my effort has gone into curating and shaping this test suite over the development of eco itself.

I am happy to act as maintainer for this test suite. Rather than forking it into many fragmented opinions on what is the “correct” definition of Elm - raise an issue or PR against this project and lets refine it together.

16 Likes

Debug printing order changed in 0.19.3 - probably should not be relying on that and enforcing it with a test. I put it in the elm-conformance quirks pile for now.

Recently shared this on Elm Slack, but would be relevant here as well: each published Elm package can also act as a test - does the tested compiler compile the exposed modules of the latest version of the package correctly?

There are enough weird code patterns eg. in elm-css that exercise edge cases in the compiler.

A list of packages lives eg. at https://package.elm-lang.org/search.json .

EDIT: just realized you also wrote and the elm/* packages, so you’re already doing this to some extent! :+1:

That’s the approach I took with Alm. It worked quite well I believe. But hard to tell fully without having people try it on their proprietary code bases of course.

I have now mined all the github issues (or rather, instructed AI to) to look for reported bugs that could be reproduced as failing tests.

If something is a bug in all Elm 0.19.x versions, it goes under elm-bugs/. If its fixed in a later version in the 0.19.x series, it gets moved to elm-bug-fixes/, which is why you see things labelled as fixes failing in earlier versions.

 ┌──────────┬────────┬────────┐ 
 │ Compiler │ Passed │ Failed │ 
 ├──────────┼────────┼────────┤ 
 │ 0.19.1   │ 1380   │ 133    │ 
 ├──────────┼────────┼────────┤ 
 │ 0.19.2   │ 1387   │ 126    │ 
 ├──────────┼────────┼────────┤ 
 │ 0.19.3   │ 1383   │ 130    │ 
 ├──────────┼────────┼────────┤ 
 │ Lamdera  │ 1396   │ 117    │ 
 └──────────┴────────┴────────┘ 

0.19.1: 133 failures

The 124 elm-bugs tests (open issues)

Compiler, about 80 tests. The group counts below are my rough grouping of the test names.

  • Type checker crashes (about 34). The compiler stops with an internal error and a Haskell CallStack, mostly the “rank” crash. Typical triggers are let-annotations with type variables, extensible records over unit, Int or String, and record updates.
  • Type checker hangs (11). The compiler hits the memory limit on infinite types or extensible records that don’t fit together.
  • Wrong accept or reject decisions (about 17). Examples are phantom types that let wrong code through, aliases of aliases, polymorphic recursion, mutual recursion, and import order changing which type a name means.
  • Wrong generated JS (about 7). Tail-call optimisation with closures and continuations, large Int literals, NaN in tuple equality, and multi-line strings.
  • Poor error messages (7). The wrong branch is blamed, the hints are misleading, or the suggestions are bad.
  • Crash on a module named File. The user’s File module clashes with elm/file’s.

elm/core (17 tests).

  • NaN and number edge cases: logBase, clamp, pow with a negative exponent, and String.toInt limits.
  • Unicode: Char.toUpper on ß, and lone surrogates.
  • Ports and the scheduler: declaration order, a nested Cmd.batch overflowing the stack, killing a process twice.

Other packages (24 tests).

  • elm/parser (11): number parsing, chompUntil positions, nested comments, deadEndsToString.
  • elm/regex (4), elm/bytes (3), elm/time (2), elm/url (2), elm/json (1) and elm/random (1).
  • elm/html and elm/virtual-dom (5 tests).
  • Setting type_ on <select> or <textarea> throws.
  • An empty attribute name throws.
  • The lazy root is not redrawn.

The 9 elm-bug-fixes tests (bugs that later releases fix)

  • These fail on 0.19.1 by design.

0.19.2: 126 failures

  • All 124 elm-bugs tests fail.
  • 2 elm-bug-fixes tests fail: the record-update crashes #1890 and #2206.

Fixes compared with 0.19.1 (7 tests):

  • Name-clash error output: the caret positions and the long runs of whitespace (#2012, #2147, #2302).
  • Long-line list handling (#2281).
  • Parser error messages:
  • the wrong EXTRA COMMA on an empty record update (#372);
  • UNFINISHED PARENTHESES in a case pattern;
  • escaped duplicate char patterns in a case.

0.19.3: 130 failures

  • All 124 elm-bugs tests fail.
  • 5 elm-compile-errors tests fail. The compiler is killed by a segmentation fault (SIGSEGV) on alias-problem and cyclic-definition errors that 0.19.1 and 0.19.2 report correctly.
  • DebugToStringTest fails because Debug.toString now prints record fields in a different order.

Fixes compared with 0.19.2 (2 tests):

  • The record-update crashes #1890 (mutual recursion) and #2206 (in a let).
  • Compiler #2177 is also fixed, but we have no test for it yet.

Lamdera 1.5.0: 117 failures

  • 112 elm-bugs tests fail.
  • 4 elm-compile-errors tests fail. On alias-problem and cyclic-definition errors there’s no segmentation fault, but the error title is missing.
  • DebugToStringTest fails, with the same field order as 0.19.3.

Fixes compared with 0.19.3 (13 tests):

  • 8 extensible-record type checker bugs:
  • 4 hangs: #1691, #1866, #2027 and #2108;
  • 4 crashes: #2064, #2072, #2097 and #2176.
  • 4 virtual-dom bugs: the type_ property on <select> and <textarea> (html #184 and #215, virtual-dom #181), and the lazy root not redrawing (virtual-dom #110).
  • 1 of the 5 0.19.3 segmentation faults (CyclicValueALetValueDefinedByItselfTest) now passes.
1 Like

The packages are a good source to mine, I agree. Although the tests in this suite do more than just check compilation - they also check some intended runtime behaviour is demonstrated.

There is a test driver that runs the programs which contain check statements in comments like below. The test driver looks at Debug.log output to confirm. Which of course means that these tests do not work in --optimized mode, which could be a real shortcoming of the technique.

-- CHECK: <text>
-- CHECK-NOT: <text>

A downside of package compilation is that it does not go all the way to codegen, only as far as the .elmo and .elmi outputs or the artifacts.dat, which is a binary encoding of the compilers internal Optimized IR.

It would be nice if there was a standard for elm-test tests in Elm packages - that is always in test/ folder, and always run with elm test or something like that. That would allow package testing to provide more guarantees in a standard way.

I recently started to compare my compiler port and the original against all packages. Besides comparing the resulting artifacts.dat files, I also generate one file per exposed module which looks like

module VerifyExports1 exposing (exports)

import Eetf.Encode


exports : List ()
exports =
    [ toUnit Eetf.Encode.array
    , toUnit Eetf.Encode.bool
    , toUnit Eetf.Encode.encode
    ...
    ]


toUnit : a -> ()
toUnit _ =
    ()

and then compare the generated JavaScript code. In the generated files, I use all exposed values, constructors, record aliases, and operators, so the generated JavaScript file should contain everything that is reachable.

1 Like

Very neat! Thanks for sharing that one.

(elm-pages FTW)

2 Likes

Thank you for creating this test page that shows differences between different compiler versions.

I will definitely be interested in the upcoming changes of Elm 0.19.4-0.19.6 and beyond

Listing eco, pine and the other compiler rewrites that have been popping up lately would also be neat, but I don’t know if you already tried with any of the ones you did not create (or if they would actually lend to it).

Partial list (in historical order - as I remember - might be wrong :wink:):

I have not tried any yet, although the test suite did originally come from eco, it has diverged a bit as I have explored some of the corner cases and corrected things - so no free pass for eco.

Any that can build from an elm.json via elm make, and can handle at least elm/core and ideally all of elm/*, should be testable.

I am going to put out a new version of this in the near future, which takes the whole idea in an interesting direction…

2 Likes

For rewrites that don’t target JavaScript, the Debug.log CHECK harness may be the blocker rather than elm make. Tagging tests as compile-only vs runtime would let you publish partial results without treating missing runtime support as a straight failure.

I ran the test suite for my compiler port (https://github.com/pithub/elm-compiler-in-elm), which is currently undergoing a fairly fundamental restructuring. In fact, three errors have been found in my port, which I am now going to look into. Thank you very much, Rupert, for this great tool :+1:

1 Like

Did you find it gave any errors that disagreed with implementation choices you had made ?

For example, this one is possibly the most controversial in the suite: CharLiteralSurrogateEscapeTest · Elm Exactly

This expects a compile error for a unicode lone surrogate as Char literal, since A) that is not a unicode character. Elm will accept it.

An alternative was to allow it as a Char literal, but set the actual Char to something else - B) wrong answer to avoid a runtime, which has precedent in Elm ( // 0).

Or allow it as a Char literal with no restriction, but then there are bugs that are hard to cleanly resolve. The problem in Elm is that Char is not really a character, its just a small string and strings can be sliced into things that are not unicode characters. I felt that making this internally consistent in a way that could eliminate all the bugs around this means doing A) or at the very least B).

The goal of my port is to be as close to the original as possible. Compared to the 0.19.1 version on which my port is based upon, your test suite found 11 differences:

  • 8 cases where the original crashed, while my port didn’t (for example because I don’t have parallel threads, so no “thread blocked indefinitely in an MVar operation” exceptions)
  • 2 stack overflows (I fixed one but kept the other, because the original also would overflow the stack, but only if it grows larger than the available memory)
  • 1 test where both compilers crash, but my port reported the crash differently from the original
2 Likes

Thanks for sharing this. I am still learning Elm, and seeing how much testing goes into the compiler helps me understand why small programs feel so reliable.