2016-06-20 33 views
1

在開發一些使用Native js代碼的庫時,我們如何組織我們的測試目錄?用本地Native目錄測試elm lib

Elm.Native.Mylib = {}; 
^

TypeError: Cannot read property 'Native' of undefined 

git repository

我的目錄結構是這樣的:

我運行測試/ test.sh時試圖來解決這一問題,但我在這裏阻塞,在運行時出現此錯誤:

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

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

測試/榆樹的package.json包含:

{ 
    "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" 
} 

測試/ Test.elm是:

module Main exposing (..) 

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

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

main = 
    runSuite tests 

測試/ test.sh

#!/bin/sh 

elm-package install -y 

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

SRC/Mylib.elm

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 

的src /本地/ Mylib.js

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 
    }; 
}; 
+1

看起來你一直在關注一些過時的教程。來自'0.17.0'的編譯器不支持這種原生模塊。這是一個使用當前版本的編譯器[elm-custom-task-example](https://github.com/tgecho/elm-custom-task-example)的本機代碼的例子。你也可以檢查現有的軟件包與本機代碼,因爲沒有關於這個問題的文件。 – halfzebra

+0

感謝halfzebra,我剛剛在幾個小時前完成了它。問題是從0.17開始語法已經改變。 – Cavet

回答

2

試試這個:

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

它適用於研磨器比榆樹0.17

賣,你也應該使用完全合格的進口:

import Natve.MyLib 

exported : String -> String 
    Native.MyLib.exported 

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

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

用戶和項目價值是從你的/本地elm-package.json

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