Skip to content

Instantly share code, notes, and snippets.

@harrysarson
Last active April 5, 2020 14:43
Show Gist options
  • Save harrysarson/08acee2e8d8ffb0185b4b1020c08d756 to your computer and use it in GitHub Desktop.
Save harrysarson/08acee2e8d8ffb0185b4b1020c08d756 to your computer and use it in GitHub Desktop.

Collecting tests

  1. Use tree-sitter-elm to get all the exposed functions from every test modules.

  2. Create a main module a bit like this:

    module Main exposing (main)
    
    import Platform
    import XXX.YYY
    import AAA.BBB
    -- etc
    
    
    findTest : String -> a -> ()
    findTest =
        Debug.todo "replace me"
    
    
    main =
        let
            () =
                findTest "XXX.YYY.zzz1" XXX.YYY.zzz1
    
            () =
                findTest "XXX.YYY.zzz2" XXX.YYY.zzz2
    
            () =
                findTest "AAA.BBB.ccc" AAA.BBB.ccc
    
            () =
                findTest "AAA.BBB.ddd" AAA.BBB.ddd
    
            -- etc
        in
        Platform.woker
            { ... }
  3. Compile that main module as an elm application.

  4. Replace the generated js for findTest with a function that prints the elm identifier if the second argument is a Test.

  5. Run the elm app and inspect the stdout to see all the tests.

Identifying tests

The test custom type is defined elm-explorations:Test.Internal.Test as follows:

type Test
    = UnitTest (() -> List Expectation)
    | FuzzTest (Random.Seed -> Int -> List Expectation)
    | Labeled String Test
    | Skipped Test
    | Only Test
    | Batch (List Test)

or when we have generated javascript:

var $elm_explorations$test$Test$Internal$UnitTest = function (a) {
	return {$: 'UnitTest', a: a};
};
var $elm_explorations$test$Test$Internal$FuzzTest = function (a) {
	return {$: 'FuzzTest', a: a};
};
var $elm_explorations$test$Test$Internal$Labeled = F2(
	function (a, b) {
		return {$: 'Labeled', a: a, b: b};
	});

/* snip */

var $elm_explorations$test$Test$Runner$Skipping = function (a) {
	return {$: 'Skipping', a: a};
};

First we want to insert the following near the top of the file:

var testSymbol = Symbol('testSymbol');

We want to do a find and replace on this js to instead get:

var $elm_explorations$test$Test$Internal$UnitTest = function (a) {
	return {[testSymbol]: true, $: 'UnitTest', a: a};
};
var $elm_explorations$test$Test$Internal$FuzzTest = function (a) {
	return {[testSymbol]: true, $: 'FuzzTest', a: a};
};
var $elm_explorations$test$Test$Internal$Labeled = F2(
	function (a, b) {
		return {[testSymbol]: true, $: 'Labeled', a: a, b: b};
	});

/* snip */

var $elm_explorations$test$Test$Runner$Skipping = function (a) {
	return {[testSymbol]: true, $: 'Skipping', a: a};
};

Then the findTests function can be:

var $author$project$Main$findTests = F2(
	function (elmIndentifier, value) {
		if (value[testSymbol]) {
            console.log(elmIndentifier);
        }
	});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment