Run glob test code directly through elm-pages so the test assertions can be against Elm data instead of JSON.

This commit is contained in:
Dillon Kearns 2022-02-28 14:43:57 -08:00
parent 81ab07897f
commit ace71d09ce
3 changed files with 173 additions and 117 deletions

View File

@ -1,21 +1,7 @@
globTest(1, ["content/index.md"]);
globTest(2, ["content/foo/index.md"]);
globTest(3, ["content/bar.md"]);
globTest(4, ["about", "posts"]);
globTest(5, [
{ first: "glob-test-cases/", second: "content1/", wildcard: "about.md" },
]);
globTest(6, [{ first: "data-file", second: "JSON" }]);
globTest(7, [{ year: 1977, month: 6, day: 10, slug: "apple-2-released" }]);
globTest(8, [["JSON", "YAML", "JSON", "JSON"]]);
function globTest(number, expected) {
it(`glob test ${number}`, () => {
cy.request("GET", `/glob-test/${number}`).then((res) => {
console.log(number, res.body);
expect(res.headers["content-type"]).to.eq("application/json");
expect(res.status).to.eq(200);
expect(res.body).to.deep.equal(expected);
});
it(`glob tests`, () => {
cy.request("GET", `/tests`).then((res) => {
expect(res.headers["content-type"]).to.eq("text/plain");
expect(res.status).to.eq(200);
expect(res.body).to.match(/^Pass\n/)
});
}
});

View File

@ -3,10 +3,10 @@ module Api exposing (routes)
import ApiRoute exposing (ApiRoute)
import DataSource exposing (DataSource)
import DataSource.Glob as Glob exposing (Glob)
import DataSource.Internal.Glob
import Html exposing (Html)
import Json.Decode as Decode
import Json.Encode as Encode
import List.NonEmpty
import Result.Extra
import Route exposing (Route)
import Server.Request as Request
import Server.Response as Response
@ -18,8 +18,8 @@ routes :
-> List (ApiRoute.ApiRoute ApiRoute.Response)
routes getStaticRoutes htmlToString =
[ greet
, globTestRouteNew
]
++ globTests
greet : ApiRoute ApiRoute.Response
@ -49,31 +49,123 @@ greet =
|> ApiRoute.serverRender
globTest : Glob Encode.Value -> Int -> ApiRoute ApiRoute.Response
globTest pattern testNumber =
ApiRoute.succeed
(Request.succeed
(pattern
|> Glob.toDataSource
|> DataSource.map (Encode.list identity)
|> DataSource.map Response.json
)
)
|> ApiRoute.literal "glob-test"
|> ApiRoute.slash
|> ApiRoute.literal (String.fromInt testNumber)
|> ApiRoute.serverRender
--findBySplat : List String -> Glob String
globTestsNew : List (DataSource (Result String ()))
globTestsNew =
[ test
{ name = "1"
, glob = findBySplat []
, expected = [ "content/index.md" ]
}
, test
{ name = "2"
, glob = findBySplat [ "foo" ]
, expected = [ "content/foo/index.md" ]
}
, test
{ name = "3"
, glob = findBySplat [ "bar" ]
, expected = [ "content/bar.md" ]
}
, test
{ name = "4"
, glob =
Glob.succeed identity
|> Glob.match (Glob.literal "glob-test-cases/content1/")
|> Glob.capture Glob.wildcard
|> Glob.match (Glob.oneOf ( ( ".md", () ), [ ( "/", () ) ] ))
|> Glob.match Glob.wildcard
, expected = [ "about", "posts" ]
}
, test
{ name = "5"
, glob =
Glob.succeed
(\first second wildcardPart ->
{ first = first
, second = second
, wildcard = wildcardPart
}
)
|> Glob.capture (Glob.literal "glob-test-cases/")
|> Glob.capture (Glob.literal "content1/")
|> Glob.capture Glob.wildcard
, expected =
[ { first = "glob-test-cases/"
, second = "content1/"
, wildcard = "about.md"
}
]
}
, test
{ name = "6"
, glob =
Glob.succeed Tuple.pair
|> Glob.match (Glob.literal "glob-test-cases/")
|> Glob.capture Glob.wildcard
|> Glob.match (Glob.literal ".")
|> Glob.capture
(Glob.oneOf
( ( "yml", YAML )
, [ ( "json", JSON )
]
)
)
, expected = [ ( "data-file", JSON ) ]
}
, test
{ name = "7"
, glob =
Glob.succeed
(\year month day slug ->
{ year = year
, month = month
, day = day
, slug = slug
}
)
|> Glob.match (Glob.literal "glob-test-cases/")
|> Glob.match (Glob.literal "archive/")
|> Glob.capture Glob.int
|> Glob.match (Glob.literal "/")
|> Glob.capture Glob.int
|> Glob.match (Glob.literal "/")
|> Glob.capture Glob.int
|> Glob.match (Glob.literal "/")
|> Glob.capture Glob.wildcard
|> Glob.match (Glob.literal ".md")
, expected =
[ { day = 10
, month = 6
, year = 1977
, slug = "apple-2-released"
}
]
}
, test
{ name = "8"
, glob =
Glob.succeed identity
|> Glob.match (Glob.literal "glob-test-cases/at-least-one/")
|> Glob.match Glob.wildcard
|> Glob.match (Glob.literal ".")
|> Glob.capture
(Glob.atLeastOne
( ( "yml", YAML )
, [ ( "json", JSON )
]
)
)
, expected = [ ( JSON, [ YAML, JSON, JSON ] ) ]
}
]
findBySplat : List String -> Glob String
findBySplat splat =
(if splat == [] then
if splat == [] then
Glob.literal "content/index.md"
else
else
Glob.succeed identity
|> Glob.captureFilePath
|> Glob.match (Glob.literal "content/")
@ -85,81 +177,58 @@ findBySplat splat =
)
)
|> Glob.match (Glob.literal ".md")
)
|> Glob.map Encode.string
globTests : List (ApiRoute ApiRoute.Response)
globTests =
[ findBySplat []
, findBySplat [ "foo" ]
, findBySplat [ "bar" ]
, Glob.succeed identity
|> Glob.match (Glob.literal "glob-test-cases/content1/")
|> Glob.capture Glob.wildcard
|> Glob.match (Glob.oneOf ( ( ".md", () ), [ ( "/", () ) ] ))
|> Glob.match Glob.wildcard
|> Glob.map Encode.string
, Glob.succeed
(\first second wildcardPart ->
Encode.object
[ ( "first", Encode.string first )
, ( "second", Encode.string second )
, ( "wildcard", Encode.string wildcardPart )
]
)
|> Glob.capture (Glob.literal "glob-test-cases/")
|> Glob.capture (Glob.literal "content1/")
|> Glob.capture Glob.wildcard
, Glob.succeed
(\first second ->
Encode.object
[ ( "first", Encode.string first )
, ( "second", Encode.string second )
]
)
|> Glob.match (Glob.literal "glob-test-cases/")
|> Glob.capture Glob.wildcard
|> Glob.match (Glob.literal ".")
|> Glob.capture
(Glob.oneOf
( ( "yml", "YAML" )
, [ ( "json", "JSON" )
]
)
test : { name : String, glob : Glob value, expected : List value } -> DataSource (Result String ())
test { glob, name, expected } =
Glob.toDataSource glob
|> DataSource.map
(\actual ->
if actual == expected then
Ok ()
else
Err <|
name
++ " failed\nPattern: `"
++ DataSource.Internal.Glob.toPattern glob
++ "`\nExpected\n"
++ Debug.toString expected
++ "\nActual\n"
++ Debug.toString actual
)
type JsonOrYaml
= JSON
| YAML
globTestRouteNew : ApiRoute ApiRoute.Response
globTestRouteNew =
ApiRoute.succeed
(Request.succeed
(globTestsNew
|> DataSource.combine
|> DataSource.map
(\testResults ->
case
testResults
|> Result.Extra.combine
of
Ok _ ->
Response.plainText
("Pass\n"
++ String.fromInt (List.length testResults)
++ " successful tests"
)
Err error ->
("Fail\n\n" ++ error)
|> Response.plainText
|> Response.withStatusCode 500
)
)
, Glob.succeed
(\year month day slug ->
Encode.object
[ ( "year", Encode.int year )
, ( "month", Encode.int month )
, ( "day", Encode.int day )
, ( "slug", Encode.string slug )
]
)
|> Glob.match (Glob.literal "glob-test-cases/")
|> Glob.match (Glob.literal "archive/")
|> Glob.capture Glob.int
|> Glob.match (Glob.literal "/")
|> Glob.capture Glob.int
|> Glob.match (Glob.literal "/")
|> Glob.capture Glob.int
|> Glob.match (Glob.literal "/")
|> Glob.capture Glob.wildcard
|> Glob.match (Glob.literal ".md")
, Glob.succeed (List.NonEmpty.toList >> Encode.list Encode.string)
|> Glob.match (Glob.literal "glob-test-cases/at-least-one/")
|> Glob.match Glob.wildcard
|> Glob.match (Glob.literal ".")
|> Glob.capture
(Glob.atLeastOne
( ( "yml", "YAML" )
, [ ( "json", "JSON" )
]
)
)
]
|> List.indexedMap
(\index pattern ->
globTest pattern (index + 1)
)
|> ApiRoute.literal "tests"
|> ApiRoute.serverRender

View File

@ -28,6 +28,7 @@
"elm/virtual-dom": "1.0.2",
"elm-community/dict-extra": "2.4.0",
"elm-community/list-extra": "8.3.0",
"elm-community/result-extra": "2.4.0",
"jluckyiv/elm-utc-date-strings": "1.0.0",
"lamdera/codecs": "1.0.0",
"lamdera/core": "1.0.0",