add basic elm testing for correctness

This commit is contained in:
Matthew Griffith 2022-02-13 09:30:41 -05:00
parent f9562fdbbb
commit 402258cbb5
7 changed files with 195 additions and 75 deletions

View File

@ -22,6 +22,7 @@
},
"scripts": {
"test": "jest",
"test-elm": "cd test/Elm ; sh run.sh",
"help": "ts-node -T src/index.ts --help",
"prepare": "tsc --project tsconfig.json && npm run copy-replacements",
"build": "tsc --project tsconfig.json && npm run copy-replacements",

5
test/Elm/README.md Normal file
View File

@ -0,0 +1,5 @@
## Instructions for running elm tests
Compile via
`elm make src/Replacements --output=dist/elm.js --optimize`

24
test/Elm/elm.json Normal file
View File

@ -0,0 +1,24 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0"
},
"indirect": {
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}

25
test/Elm/index.js Normal file
View File

@ -0,0 +1,25 @@
const level1 = require('./dist/elm.js');
const level2 = require('./dist/elm-lvl-2.js');
const level3 = require('./dist/elm-lvl-3.js');
const run = (name, elm) =>
new Promise((resolve, reject) => {
const app = elm.Elm.Tests.init({ flags: {} });
app.ports.onSuccessSend.subscribe(resolve);
app.ports.onFailureSend.subscribe(reject);
})
.then((_) => console.info(`${name} -> Success!`))
.catch((reason) => {
console.error(`${name} -> Failing tests: ` + reason);
process.exit(1);
});
const main = () => {
return Promise.all([
run('normal', level1),
run('level-2', level2),
run('level-3', level3),
]);
};
main();

13
test/Elm/run.sh Normal file
View File

@ -0,0 +1,13 @@
# Compile normally
elm make src/Tests.elm --output=dist/elm.js --optimize
# Compile with level 2
../../bin/elm-optimize-level-2 --output=dist/elm-lvl-2.js src/Tests.elm
# Compile with level-3
../../bin/elm-optimize-level-2 --output=dist/elm-lvl-3.js -O3 src/Tests.elm
# Run tests
node index.js

127
test/Elm/src/Tests.elm Normal file
View File

@ -0,0 +1,127 @@
port module Tests exposing (..)
main : Program {} () ()
main =
Platform.worker
{ init =
\json ->
( ()
, case run suite of
[] ->
onSuccessSend "Pass!"
errors ->
onFailureSend (String.join ", " errors)
)
, update =
\msg model ->
( model, Cmd.none )
, subscriptions = \_ -> Sub.none
}
port onSuccessSend : String -> Cmd msg
port onFailureSend : String -> Cmd msg
suite : Test
suite =
describe "Tests for js code replacements"
[ list
, string
]
type Test
= Describe String (List Test)
| Test String (() -> Bool)
run : Test -> List String
run testcase =
case testcase of
Test name toResult ->
if toResult () then
[]
else
[ name ]
Describe name tests ->
List.foldl
(\t results ->
run t ++ results
)
[]
tests
test =
Test
describe =
Describe
list : Test
list =
describe "List"
[ test "List.all" <|
\_ ->
True
== List.all (\i -> i == 1) [ 1, 1, 1, 1 ]
, test "List.append" <|
\_ ->
[ 1, 2, 3, 4, 5, 6 ]
== List.append [ 1, 2, 3 ] [ 4, 5, 6 ]
, test "List.concat" <|
\_ ->
[ 1, 2, 3, 4, 5, 6 ]
== List.concat [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
, test "List.concatMap" <|
\_ ->
[ 1, 2, 3, 4, 5, 6 ]
== List.concatMap identity [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
, test "List.filter" <|
\_ ->
[ 1, 2, 3, 4, 5, 6 ]
== List.filter (\_ -> True) [ 1, 2, 3, 4, 5, 6 ]
, test "List.filter (only even)" <|
\_ ->
[ 2, 4, 6 ]
== List.filter (\i -> modBy 2 i == 0) [ 1, 2, 3, 4, 5, 6 ]
, test "List.indexedMap" <|
\_ ->
[ 0, 1, 2, 3, 4, 5 ]
== List.indexedMap (\i n -> i) [ 1, 2, 3, 4, 5, 6 ]
, test "List.intersperse" <|
\_ ->
[ 1, 10, 2, 10, 3, 10, 4, 10, 5, 10, 6 ]
== List.intersperse 10 [ 1, 2, 3, 4, 5, 6 ]
, test "List.take" <|
\_ ->
[ 1, 2 ]
== List.take 2 [ 1, 2, 3, 4, 5, 6 ]
, test "List.unzip" <|
\_ ->
( [ 1, 2 ], [ 3, 4 ] )
== List.unzip [ ( 1, 3 ), ( 2, 4 ) ]
]
string : Test
string =
describe "String"
[ test "String.repeat 4" <|
\_ ->
"nananana"
== String.repeat 4 "na"
, test "String.repeat 10" <|
\_ ->
"nananananananananana"
== String.repeat 10 "na"
]

View File

@ -1,75 +0,0 @@
module Replacements exposing (..)
import Expect exposing (Expectation)
import Fuzz exposing (Fuzzer, int, list, string)
import Test exposing (..)
suite : Test
suite =
describe "Tests for js code replacements"
[ list
, string
]
list : Test
list =
describe "List"
[ test "List.all" <|
\_ ->
Expect.equal True
(List.all (\i -> i == 1) [ 1, 1, 1, 1 ])
, test "List.append" <|
\_ ->
Expect.equal [ 1, 2, 3, 4, 5, 6 ]
(List.append [ 1, 2, 3 ] [ 4, 5, 6 ])
, test "List.concat" <|
\_ ->
Expect.equal [ 1, 2, 3, 4, 5, 6 ]
(List.concat [ [ 1, 2, 3 ], [ 4, 5, 6 ] ])
, test "List.concatMap" <|
\_ ->
Expect.equal [ 1, 2, 3, 4, 5, 6 ]
(List.concatMap identity [ [ 1, 2, 3 ], [ 4, 5, 6 ] ])
, test "List.filter" <|
\_ ->
Expect.equal [ 1, 2, 3, 4, 5, 6 ]
(List.filter (\_ -> True) [ 1, 2, 3, 4, 5, 6 ])
, test "List.filter (only even)" <|
\_ ->
Expect.equal [ 2, 4, 6 ]
(List.filter (\i -> modBy 2 i == 0) [ 1, 2, 3, 4, 5, 6 ])
, test "List.indexedMap" <|
\_ ->
Expect.equal [ 0, 1, 2, 3, 4, 5 ]
(List.indexedMap (\i n -> i) [ 1, 2, 3, 4, 5, 6 ])
, test "List.intersperse" <|
\_ ->
Expect.equal [ 1, 10, 2, 10, 3, 10, 4, 10, 5, 10, 6 ]
(List.intersperse 10 [ 1, 2, 3, 4, 5, 6 ])
, test "List.take" <|
\_ ->
Expect.equal [ 1, 2 ]
(List.take 2 [ 1, 2, 3, 4, 5, 6 ])
, test "List.unzip" <|
\_ ->
Expect.equal ( [ 1, 2 ], [ 3, 4 ] )
(List.unzip [ ( 1, 3 ), ( 2, 4 ) ])
]
string : Test
string =
describe "String"
[ test "String.repeat 4" <|
\_ ->
Expect.equal
"nananana"
(String.repeat 4 "na")
, test "String.repeat 10" <|
\_ ->
Expect.equal
"nananananananananana"
(String.repeat 10 "na")
]