簡體   English   中英

OCaml "include Module" 只包含接口暴露的函數

[英]OCaml "include Module" only includes functions exposed by the interface

我正在嘗試測試一個名為Game的 OCaml 模塊。 但是,當在我的項目的正常上下文中使用時,為Game初始化 function 時,只接受幾個參數。 這意味着某些測試需要我初始化Game ,然后執行數百個操作的特定組合,以達到所需的 state,這是不可行的。

我的想法是創建一個新的 init function,它允許用戶對Game object 的每個組件進行硬編碼。 因為它只用於測試,所以讓這個 init function 成為典型 mli 接口的一部分似乎是個壞主意,所以我認為在我的testGame.ml文件中使用關鍵字include會有所幫助。

在我的testGame.ml中, generate_player_idsGame.t類型都是“未綁定的”,即使我在test.ml中定義了它們。 這一定是因為它們沒有在game.mli中公開,但我認為使用include本質上就像復制和粘貼整個模塊,包括 .ml 文件中未公開的部分。 有沒有辦法讓testGame.ml識別這些? 否則,為了測試目的,擁有識別模塊隱藏部分的函數的慣用方法是什么?

測試游戲.ml:

include Game

let init_game_test
  num_players
  board
  turn
  starting_turn
  setup_rounds_left
  scoring_points
  num_rounds =
  let player_ids = generate_player_ids num_players in
  {
    players = players_of_player_ids player_ids;
    player_order = player_ids;
    board;
    turn;
    starting_turn;
    setup_rounds_left;
    scoring_points;
    num_rounds;
  }

游戲.ml:

type t = {
  players : (PlayerId.t * Player.t) list;
  player_order : PlayerId.t list;
  board : Board.t;
  turn : PlayerId.t;
  starting_turn : PlayerId.t;
  setup_rounds_left : int;
  scoring_points : (Cell.soil * int list) list;
  num_rounds : int;
}

let generate_player_ids num_players =
  match num_players with
  | 2 -> [ 1; 2 ]
  | 3 -> [ 1; 2; 3 ]
  | 4 -> [ 1; 2; 3; 4 ]
  | _ -> failwith "Must be 2-4 players"

您假設這是因為它們沒有暴露是非常正確的。 include在 OCaml 的模塊系統上工作,而不是像 C 那樣的直接源包含基礎。

如果您.mli接口文件,則默認情況下會公開所有內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM