Cavet
Cavet

Reputation: 152

Testing elm lib with local Native directory

How do we organize our test directory when developing some libraries that uses Native js code?

I tried to work this out, but I'm blocked here, with this error at runtime when running test/test.sh:

Elm.Native.Mylib = {};
   ^

TypeError: Cannot read property 'Native' of undefined

git repository

My directories are structured this way:

Mylib:
   - src :
      - Mylib.elm
      - Native :
         - MyLib.js

   - tests :
      - Test.elm
      - Test.sh
      - elm-package.json

the tests/elm-package.json contains :

{
    "version": "1.0.0",
    "summary": "helpful summary of your project, less than 80 characters",
    "repository": "https://github.com/user/project.git",
    "license": "BSD3",
    "source-directories": [
      "."
      ,"../src"
    ],
  "exposed-modules": [],
  "native-modules": true,
    "dependencies": {
        "elm-community/elm-test": "1.1.0 <= v < 2.0.0",
        "elm-lang/core": "4.0.1 <= v < 5.0.0"
    },
    "elm-version": "0.17.0 <= v < 0.18.0"
}

the tests/Test.elm is :

module Main exposing (..)

import Basics exposing (..)
import ElmTest exposing (..)
import Mylib exposing (..)

tests : Test
tests =
    suite "elm-Mylib Library Tests"
  [ ]

main =
    runSuite tests

The tests/test.sh is

#!/bin/sh

elm-package install -y

elm-make --yes --output test.js Test.elm
node test.js

The src/Mylib.elm is

module Mylib exposing (..)

import Native.Mylib exposing (..)
import Task exposing (Task)
import Time exposing (Time)

print : a -> Task x ()
print value =
  Native.Mylib.log (toString value)

getCurrentTime : Task x Time
getCurrentTime =
  Native.Mylib.getCurrentTime

The src/Native/Mylib.js is

Elm.Native.Mylib = {};
Elm.Native.Mylib.make = function(localRuntime) {

    localRuntime.Native = localRuntime.Native || {};
    localRuntime.Native.Mylib = localRuntime.Native.Mylib || {};
    if (localRuntime.Native.Mylib.values)
    {
        return localRuntime.Native.Mylib.values;
    }

    var Task = Elm.Native.Task.make(localRuntime);
    var Utils = Elm.Native.Utils.make(localRuntime);


    function log(string)
    {
        return Task.asyncFunction(function(callback) {
            console.log(string);
            return callback(Task.succeed(Utils.Tuple0));
        });
    }


    var getCurrentTime = Task.asyncFunction(function(callback) {
        return callback(Task.succeed(Date.now()));
    });


    return localRuntime.Native.Mylib.values = {
        log: log,
        getCurrentTime: getCurrentTime
    };
};

Upvotes: 1

Views: 301

Answers (1)

DenisKolodin
DenisKolodin

Reputation: 15081

Try this:

var _user$project$Native_MyLib = function() {
return {
    exported: function(arg) { return "One" },
    exported2: F2(function(arg) { return "Two" }),
    exported3: F3(function(arg) { return "Three" }),
}
}();

It works for grater than Elm 0.17.

Buy you should also use full qualified import:

import Natve.MyLib

exported : String -> String
    Native.MyLib.exported

exported2 : String -> String -> String
    Native.MyLib.exported2

exported3 : String -> String -> String -> String
    Native.MyLib.exported3

User and project values are from your/local elm-package.json:

 "repository": "https://github.com/user/project.git",

Upvotes: 2

Related Questions