testing - 如何定义导出单个函数的模块?

标签 testing module ocaml

我想学习 OCaml 很多年了,最近我终于决定去学习它,但我第一次真正接触 OCaml 并不顺利......

我正在使用一组练习来学习它(这种方法在我学习其他语言时效果很好)。每个练习都带有一个测试脚本。第一个测试脚本是这样的:

(* test.ml *)

open Core.Std
open OUnit2
open Mymod

let ae exp got _test_ctxt = assert_equal ~printer:String.to_string exp got

let tests = ["hello" >:: ae "hello!" (echo "hello!");]

let () =
    run_test_tt_main ("tests" >::: tests)

我整个上午都花了 字面上 ,但未能使它正常工作。

我已经尝试找出如何在所有这些地方做到这一点:

https://realworldocaml.org/v1/en/html/files-modules-and-programs.html

http://caml.inria.fr/pub/distrib/ocaml-4.01/ocaml-4.01-refman.html

https://ocaml.org/learn/tutorials/modules.html

...以及其他几个地方,没有看起来与问题相关(或有效)的东西。

尽管那里有一页又一页的 OCaml 文档,但我找不到任何可以告诉我如何编写 Mymod 模块的内容。

我已经尝试将以下任何内容放入我的 mymod.mli 文件中:

let echo x = x ;;
let echo : string -> string = fun x -> x ;;
let echo x : string = x ;;

...以及其他几十种变体;它们甚至都无法编译,更不用说通过测试了。编译器错误消息是难以理解的(例如:illegal begin of interf),我找不到任何文档来解释它们。

我在文档中也找不到用于定义仅返回其(字符串)输入的函数的语法。

我会很感激一些帮助。

最佳答案

要详细说明@camlspotter 指出的内容,您应该有一个像这样的 mymod.mli 文件:

val echo : string -> string

和一个像这样的 mymod.ml 文件:

let echo s = s

可以这样编译运行:

$ ocamlopt -c mymod.mli
$ ocamlopt -c mymod.ml
$ echo 'Printf.printf "saw %s\n" (Mymod.echo "hello world")' > main.ml
$ ocamlopt -o main mymod.cmx main.ml
$ ./main
saw hello world

这在 Section 2.5 of the OCaml manual 中有描述.

关于testing - 如何定义导出单个函数的模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26806568/

相关文章:

types - OCaml 中的依赖类型

ocaml - ocaml 中的粉状机器

元组上的 OCaml 映射

xml - 如何为 Spring 测试创建 TestContext?

testing - 在 groovy(Spock) 中测试文件结构

unit-testing - Angular 2——模拟——没有 HTTP 提供者

使用大型查找表的 Python 类

drupal - 如何在 Drupal 7 中以编程方式创建新的内容类型?

ruby-on-rails - 测试流程(交叉 Controller )的最佳方法是什么?

linux - 如何取消映射和释放 highmem 页面