Skip to content

Commit

Permalink
Merge pull request #138 from obsidiansystems/dn-hspec-nodejs-testing
Browse files Browse the repository at this point in the history
Add HSpec based test suite, using nodejs only
  • Loading branch information
hamishmack authored Sep 8, 2023
2 parents 99b23da + e524215 commit eab914a
Show file tree
Hide file tree
Showing 10 changed files with 479 additions and 6 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/Cabal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ jobs:
- name: Install dependencies
run: |
sudo apt update
sudo apt install libwebkit2gtk-4.0-dev libgirepository1.0-dev phantomjs
sudo apt install libwebkit2gtk-4.0-dev libgirepository1.0-dev
cabal update
cabal build all --dependencies-only --enable-tests --disable-optimization
- name: Build
run: |
cabal build all --enable-tests --disable-optimization 2>&1 | tee build.log
- name: Test
run: |
# cabal test all --disable-optimization
cabal run jsaddle-warp:test-tool -- .
cabal test all --disable-optimization
31 changes: 29 additions & 2 deletions jsaddle-warp/jsaddle-warp.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ library
ghc-options: -ferror-spans -Wall

test-suite test-tool
if impl(ghcjs -any) || os(ios)
buildable: False
-- Disable doctests as they dont work at the moment and the hspec tests
-- covers the testing
buildable: False

build-depends:
QuickCheck -any,
Expand Down Expand Up @@ -90,3 +91,29 @@ test-suite test-tool
default-language: Haskell2010
hs-source-dirs: tests
ghc-options: -ferror-spans -threaded

test-suite spec
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Language.Javascript.JSaddle.ObjectSpec
Language.Javascript.JSaddle.ValueSpec
Language.Javascript.JSaddleSpec
hs-source-dirs:
tests
ghc-options: -Wall
build-depends:
base
, bytestring
, directory
, hspec == 2.*
, jsaddle
, jsaddle-warp
, lens
, mtl
, process
, text
, warp
, websockets
other-modules:
default-language: Haskell2010
34 changes: 34 additions & 0 deletions jsaddle-warp/node-client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const http = require('http');
const ws = require('ws');
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

let port = 3709;

if (process.argv[2]) {
port = Number(process.argv[2]);
}
console.log('Running client on port:', port);


let request = http.get('http://0.0.0.0:' + port + '/jsaddle.js', (res) => {
if (res.statusCode !== 200) {
console.error(`Did not get an OK from the server. Code: ${res.statusCode}`);
res.resume();
return;
}

let data = '';

res.on('data', (chunk) => {
data += chunk;
});

res.on('close', () => {
console.log("Connecting to JSaddle running on port", port);
eval(data);
});
});

request.on('error', (err) => {
console.error(`Encountered an error trying to make a request: ${err.message}`);
});
58 changes: 58 additions & 0 deletions jsaddle-warp/node-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions jsaddle-warp/node-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "jsaddle-warp-node-client",
"version": "0.1.0",
"description": "A minimal JS client to use with JSaddle warp testing",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"ws": "^8.13.0",
"xmlhttprequest": "^1.8.0"
}
}
89 changes: 89 additions & 0 deletions jsaddle-warp/tests/Language/Javascript/JSaddle/ObjectSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{-# LANGUAGE ExtendedDefaultRules #-}
module Language.Javascript.JSaddle.ObjectSpec where

import Prelude hiding ((!!))
import Control.Lens.Operators ((^.))

import qualified Data.Text as T
import Language.Javascript.JSaddle
import Test.Hspec

default ( Int)

spec :: SpecWith JSContextRef
spec = do
let
resultShouldBe res m ctx = do
result <- runJSM (valToText =<< m) ctx
result `shouldBe` (T.pack res)

describe "(!)" $ do
it "Lookup a property based on its name." $
resultShouldBe "11" $ val "Hello World" ! "length"

describe "(!!)" $ do
it "Lookup a property based on its index." $
resultShouldBe "W" $ val "Hello World" !! 6

describe "js" $ do
it "Makes a getter for a particular property name." $
resultShouldBe "11" $ val "Hello World" ^. js "length"

describe "jsf" $ do
it "call a function" $
resultShouldBe "6" $ val "Hello World" ^. jsf "indexOf" ["World"]

describe "js0" $ do
it "call a function that expects no arguments" $
resultShouldBe "hello world" $ val "Hello World" ^. js0 "toLowerCase"

describe "js1" $ do
it "call a function that expects one argument" $
resultShouldBe "6" $ val "Hello World" ^. js1 "indexOf" "World"

describe "jsgf" $ do
it "call a function" $
resultShouldBe "5" $ (eval "globalFunc = function (x) {return x.length;}") >> jsgf "globalFunc" ["World"]

describe "(#)" $ do
it "Call a JavaScript function" $
resultShouldBe "6" $ val "Hello World" # "indexOf" $ ["World"]

describe "(##)" $ do
it "Call a JavaScript function at the given index" $
resultShouldBe "5" $ (eval "something = {}; something[6]=function (x) {return x.length;}; something[6]('World')") >> (jsg "something" ## 6 $ ["World"])

describe "(<#)" $ do
it "Set a JavaScript property" $
resultShouldBe "1" $ do {j <- obj; (j <# "x") 1; j!"x"}

describe "(<##)" $ do
it "Set a JavaScript property at the given index" $
resultShouldBe "1" $ do {j <- obj; (j <## 6) 1; j!!6}

describe "new" $ do
it "create a new JavaScript object" $
resultShouldBe "2013" $ do { a <- new (jsg "Date") (2013, 1, 1); a ^. js0 "getFullYear" }

describe "call" $ do
it "Call function with a given @this@." $
resultShouldBe "Hello" $ do { test <- eval "(function(){return this;})"; call test (val "Hello") () }

describe "obj" $ do
it "Make an empty object using the default constuctor" $
resultShouldBe "Hello" $ do { a <- obj; (a <# "x") "Hello"; a ^. js "x" }

describe "array" $ do
it "Make an JavaScript array from a list of values" $
resultShouldBe "World" $ array ["Hello", "World"] !! 1

it "Make an JavaScript array from a list of values 2" $
resultShouldBe "Hello,,,true,1" $ array ("Hello", JSNull, (), True, 1.0::Double)

describe "propertyNames" $ do
it "Get a list containing the property names present on a given object" $
resultShouldBe "[\"x\",\"y\"]" $ show . map strToText <$> propertyNames (eval "({x:1, y:2})")

describe "nullObject" $ do
it "is equal to null" $
resultShouldBe "true" $ strictEqual nullObject (eval "null")
Loading

0 comments on commit eab914a

Please sign in to comment.