Skip to content

Commit

Permalink
feature: add elm spa tests (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet authored Jan 15, 2024
1 parent 303f12f commit 651de81
Show file tree
Hide file tree
Showing 37 changed files with 5,922 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.tmp
extension.tar.br

# Logs
Expand Down
28 changes: 28 additions & 0 deletions ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
This project incorporates components from the projects listed below, that may have licenses
differing from this project:


1) License Notice for test/cases/elm-spa* (from https://github.com/rtfeldman/elm-spa-example)
---------------------------------------

MIT License

Copyright (c) 2017-2018 Richard Feldman and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,42 @@
"prettier": {
"semi": false,
"singleQuote": true
},
"test-tokenize": {
"skip": [
"elm-spa-api-endpoint",
"elm-spa-api",
"elm-spa-article-body",
"elm-spa-article-comment",
"elm-spa-article-feed",
"elm-spa-article-slug",
"elm-spa-article-tag",
"elm-spa-article",
"elm-spa-asset",
"elm-spa-author",
"elm-spa-avatar",
"elm-spa-commentid",
"elm-spa-email",
"elm-spa-loading",
"elm-spa-log",
"elm-spa-main",
"elm-spa-page-article-editor",
"elm-spa-page-article",
"elm-spa-page-blank",
"elm-spa-page-home",
"elm-spa-page-login",
"elm-spa-page-notfound",
"elm-spa-page-profile",
"elm-spa-page-register",
"elm-spa-page-settings",
"elm-spa-page",
"elm-spa-paginatedlist",
"elm-spa-profile",
"elm-spa-route",
"elm-spa-session",
"elm-spa-timestamp",
"elm-spa-username",
"elm-spa-viewer"
]
}
}
62 changes: 62 additions & 0 deletions scripts/copy-elm-spa-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { execaCommand } from 'execa'
import path, { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { cp, readdir, readFile, rm, writeFile } from 'node:fs/promises'

const __dirname = dirname(fileURLToPath(import.meta.url))
const root = path.join(__dirname, '..')

const REPO = 'https://github.com/rtfeldman/elm-spa-example'
const COMMIT = 'cb32acd73c3d346d0064e7923049867d8ce67193'

const getTestName = (line) => {
return (
'elm-spa-' +
line
.toLowerCase()
.trim()
.replaceAll(' ', '-')
.replaceAll('/', '-')
.replace('.elm', '')
)
}

const getAllTests = async (folder) => {
const dirents = await readdir(folder, { recursive: true })
const allTests = []
for (const dirent of dirents) {
if (!dirent.endsWith('.elm')) {
continue
}
const filePath = `${folder}/${dirent}`
const testName = getTestName(dirent)
const fileContent = await readFile(filePath, 'utf8')
allTests.push({
testName,
testContent: fileContent,
})
}
return allTests
}

const writeTestFiles = async (allTests) => {
for (const test of allTests) {
await writeFile(`${root}/test/cases/${test.testName}.elm`, test.testContent)
}
}

const main = async () => {
process.chdir(root)
await rm(`${root}/.tmp`, { recursive: true, force: true })
await execaCommand(`git clone ${REPO} .tmp/elm-spa`)
process.chdir(`${root}/.tmp/elm-spa`)
await execaCommand(`git checkout ${COMMIT}`)
process.chdir(root)
await cp(`${root}/.tmp/elm-spa/src`, `${root}/.tmp/elm-spa-src`, {
recursive: true,
})
const allTests = await getAllTests(`${root}/.tmp/elm-spa-src`)
await writeTestFiles(allTests)
}

main()
127 changes: 127 additions & 0 deletions test/cases/elm-spa-api-endpoint.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
module Api.Endpoint exposing (Endpoint, article, articles, comment, comments, favorite, feed, follow, login, profiles, request, tags, user, users)

import Article.Slug as Slug exposing (Slug)
import CommentId exposing (CommentId)
import Http
import Url.Builder exposing (QueryParameter)
import Username exposing (Username)


{-| Http.request, except it takes an Endpoint instead of a Url.
-}
request :
{ body : Http.Body
, expect : Http.Expect a
, headers : List Http.Header
, method : String
, timeout : Maybe Float
, url : Endpoint
, withCredentials : Bool
}
-> Http.Request a
request config =
Http.request
{ body = config.body
, expect = config.expect
, headers = config.headers
, method = config.method
, timeout = config.timeout
, url = unwrap config.url
, withCredentials = config.withCredentials
}



-- TYPES


{-| Get a URL to the Conduit API.
This is not publicly exposed, because we want to make sure the only way to get one of these URLs is from this module.
-}
type Endpoint
= Endpoint String


unwrap : Endpoint -> String
unwrap (Endpoint str) =
str


url : List String -> List QueryParameter -> Endpoint
url paths queryParams =
-- NOTE: Url.Builder takes care of percent-encoding special URL characters.
-- See https://package.elm-lang.org/packages/elm/url/latest/Url#percentEncode
Url.Builder.crossOrigin "https://conduit.productionready.io"
("api" :: paths)
queryParams
|> Endpoint



-- ENDPOINTS


login : Endpoint
login =
url [ "users", "login" ] []


user : Endpoint
user =
url [ "user" ] []


users : Endpoint
users =
url [ "users" ] []


follow : Username -> Endpoint
follow uname =
url [ "profiles", Username.toString uname, "follow" ] []



-- ARTICLE ENDPOINTS


article : Slug -> Endpoint
article slug =
url [ "articles", Slug.toString slug ] []


comments : Slug -> Endpoint
comments slug =
url [ "articles", Slug.toString slug, "comments" ] []


comment : Slug -> CommentId -> Endpoint
comment slug commentId =
url [ "articles", Slug.toString slug, "comments", CommentId.toString commentId ] []


favorite : Slug -> Endpoint
favorite slug =
url [ "articles", Slug.toString slug, "favorite" ] []


articles : List QueryParameter -> Endpoint
articles params =
url [ "articles" ] params


profiles : Username -> Endpoint
profiles uname =
url [ "profiles", Username.toString uname ] []


feed : List QueryParameter -> Endpoint
feed params =
url [ "articles", "feed" ] params


tags : Endpoint
tags =
url [ "tags" ] []
Loading

0 comments on commit 651de81

Please sign in to comment.