Skip to content

Commit

Permalink
Merge pull request #7 from ahrefs/reason-syntax
Browse files Browse the repository at this point in the history
Generate docs for both reason & ocaml syntax
  • Loading branch information
feihong authored Mar 5, 2024
2 parents a3c2104 + cc56e26 commit 86729a4
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 29 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ on:
# Runs on pushes targeting the `master` branch.
push:
branches:
- master
- master
schedule:
# Prime the caches every Monday
- cron: 0 1 * * MON
- cron: 0 1 * * MON

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down Expand Up @@ -38,7 +38,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20
cache: npm
- uses: ocaml/setup-ocaml@v2
with:
Expand All @@ -55,6 +55,6 @@ jobs:
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./_build/default/_doc/_html
publish_dir: ./_docs
enable_jekyll: false
force_orphan: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
node_modules
_build
_opam
_docs
dist/
.vscode
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Unreleased

- Add bindings for `node:test` and `node:assert/strict`
26 changes: 14 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ DUNE = opam exec -- dune

.DEFAULT_GOAL := help

# Make sure build directory is the same in CI even though no local switch is being used
DUNE_BUILD_DIR := $(shell pwd)/_build

.PHONY: help
help: ## Print this help message
@echo "List of available make commands";
Expand Down Expand Up @@ -32,14 +35,6 @@ build: ## Build the project
build_verbose: ## Build the project
$(DUNE) build --verbose

.PHONY: serve
serve: ## Serve the application with a local HTTP server
npm run serve

.PHONY: bundle
bundle: ## Bundle the JavaScript application
npm run bundle

.PHONY: clean
clean: ## Clean build artifacts and other generated files
$(DUNE) clean
Expand All @@ -54,16 +49,23 @@ format-check: ## Checks if format is correct

.PHONY: watch
watch: ## Watch for the filesystem and rebuild on every change
$(DUNE) build --watch @react @node
$(DUNE) build --watch

.PHONY: test
test: ## Run test suite
$(DUNE) test --no-buffer
$(DUNE) build @runtest --no-buffer

.PHONY: docs
docs: ## Build the docs
$(DUNE) build @doc
rm -rf _docs
mkdir _docs
cp docs/index.html _docs
ODOC_SYNTAX=reason $(DUNE) build @doc
mv _build/default/_doc/_html/odoc.support _docs
mv _build/default/_doc/_html/melange-fest _docs/reason
ODOC_SYNTAX=ocaml $(DUNE) build @doc
mv _build/default/_doc/_html/melange-fest _docs/ocaml

.PHONY: preview
preview: docs ## Preview the docs
cd _build/default/_doc/_html/ && python -m http.server
cd _docs && python -m http.server
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
# melange-fest

A minimal test framework for Melange using Node [test runner](https://nodejs.org/api/test.html).
A minimal test framework for Melange using [Node test
runner](https://nodejs.org/api/test.html).

## Quick Start

tbd
```reason
open Fest
test("1+1=2", () => expect |> equal(1 + 1, 2));
```

### React

React support is provided by
[`reason-react`](https://github.com/reasonml/reason-react/).

More info tbd.
```reason
open Fest;
module RTL = ReactTestingLibrary;
[@mel.get] external textContent: Dom.element => string = "textContent";
Domloader.init();
module Hello = {
[@react.component]
let make = (~text) => <div> {"Hello " ++ text |> React.string} </div>;
};
let () =
test("test component", () => {
let result = RTL.render(<Hello text="Nila" />);
// Check that Hello component renders the name properly.
let el = RTL.getByText(~matcher=`Str("Hello Nila"), result);
expect |> equal(textContent(el), "Hello Nila");
});
```

## Commands

tbd
Run all tests:

```bash
make test
```
20 changes: 20 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>index</title>
<link rel="stylesheet" href="./odoc.support/odoc.css"/>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
</head>
<body>
<main class="content">
<div class="by-name">
<h2>melange-fest documentation</h2>
<ul>
<li><a href="reason">Reason docs</a></li>
<li><a href="ocaml">OCaml docs</a></li>
</ul>
</div>
</main>
</body>
</html>
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"scripts": {
"bundle": "webpack --mode production --entry ./_build/default/src/output/src/ReactApp.js",
"serve": "webpack serve --open --mode development --entry ./_build/default/src/output/src/ReactApp.js"
},
"dependencies": {
"@testing-library/react": "^14.0.0",
"jsdom": "^22.1.0",
Expand Down
7 changes: 5 additions & 2 deletions src/domloader.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
(* just here to allow other modules to load the raw code below *)
let init () = ()
(** Initialize the Node environment to make it ready to run tests *)
let init () =
(* this function doesn't do anything, it just allows other modules to load the raw code below *)
()

(* Source: https://samthor.au/2022/test-react-builtin/ *)
[%%mel.raw
{|
import * as jsdom from 'jsdom';
Expand Down
6 changes: 6 additions & 0 deletions src/fest.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
(** Bindings for functions from {{: https://nodejs.org/api/test.html#test-runner }node:test} and {{: https://nodejs.org/api/assert.html#strict-assertion-mode }node:assert/strict} modules *)

external test : string -> (unit -> unit) -> unit = "test"
[@@mel.module "node:test"]
(** Create a test with a given name and callback function that runs the test *)
Expand All @@ -20,6 +22,10 @@ external expect : assertion = "node:assert/strict"
[@@mel.module]
(** The {{: https://nodejs.org/api/assert.html#strict-assertion-mode} node:assert/strict} module object *)

external ok : bool -> unit = "ok"
[@@mel.send.pipe: assertion]
(** Tests if the given value is true *)

external equal : 'a -> 'a -> unit = "strictEqual"
[@@mel.send.pipe: assertion]
(** Tests strict equality between the actual and expected parameters as determined by {{: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is }Object.is()} *)
Expand Down
5 changes: 3 additions & 2 deletions test/bindings.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
open Fest

let () = test "equal" (fun () -> expect |> equal 1 1)
let () = test "equal 2" (fun () -> expect |> equal "foo" "foo")
let () = test "equal" (fun () -> expect |> equal (4 - 3) 1)
let () = test "equal 2" (fun () -> expect |> equal ("f" ^ "oo") "foo")
let () = test "ok" (fun () -> expect |> ok (true || false))

module Deep_strict_equal = struct
type foo = Foo of int
Expand Down
2 changes: 1 addition & 1 deletion test/components.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Domloader.init();

module Hello = {
[@react.component]
let make = (~text) => <div> {("Hello " ++ text)->React.string} </div>;
let make = (~text) => <div> {"Hello " ++ text |> React.string} </div>;
};

let () =
Expand Down
1 change: 1 addition & 0 deletions test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
(rule
(alias runtest)
(deps
(alias_rec main)
(:input ./node/test/bindings.mjs))
(action
(run node %{input})))
Expand Down

0 comments on commit 86729a4

Please sign in to comment.