ada - 打破测试功能的 Ada 隐私

标签 ada

假设我有两个包:

with Ada.Streams; use Ada.Streams;
package P is
   type SEA is new Stream_Element_Array (1..4);
   function foo (Input : in SEA) return Natural;
private
   type PRecord is record
      Bar : Natural := 0;
   end record;
   function To_PRecord is new Ada.Unchecked_Conversion
     (Source => SEA,
      Target => PRecord);
end P;
------
package body P is
   function foo (Input : in SEA) return Natural
   is
      Tmp : constant PRecord := To_PRecord (Input);
   begin
      return Tmp.Bar;
   end foo;
end P;

-- Say SEA was defined in a third package and is accessible
package Q is
   function foo (Input : SEA) return Natural;
end P;
-----
with P;
package body Q is
   function foo (Input : SEA) return Natural
   is
   begin
      return P.foo(Input);
   end foo;
end Q;

包 P 很容易测试:

with AUnit.Assertions; use AUnit.Assertions;
package body P.Tests is
   function To_SEA is new Ada.Unchecked_Conversion
     (Source => PRecord,
      Target => SEA);

   procedure Test_Foo
   is
      Input : PRecord := (Bar => 42);
   begin
      Assert (foo(To_SEA (Input)) = 42, "foo failed miserably");
   end Test_Foo;
end P.Tests;

但是我该如何测试 Q? PRecord 不可访问。因此,我看不出 Q 如何在不改变我正在测试的代码的情况下进行测试。在 C++ 中,我会将包 Q.Tests 定义为 Pfriend 并获得访问权限。

我想我可以在 PQ.Tests 会用到的包中定义 PRecord 但是否有解决方案可以做到不需要更改 P 或 Q(因为我真的不喜欢为了测试而需要更改生产代码)。

最佳答案

子包 P.Spy 可以查看整个 P 的规范; Q.Tests 可以调用它来进行验证检查。

关于ada - 打破测试功能的 Ada 隐私,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67990722/

相关文章:

ada - 将导入的 C 指针包装在相同大小的 Ada 标记类型中

ada - GNAT 编程工作室自动修复问题

java - 创建不兼容的数字子类型

string - 在 Ada 中打印出字符

command-line-arguments - GNAT.Command_Line 中的选项参数存在问题

string - Ada 如何逐个字符获取输入

ada - (Ada 2012) 编译时错误 "expected private type... found composite type"

ada - 从适用于 Linux 的 Windows 子系统调用时,GPRBuild 找不到 gprconfig

ada - 使用 Ravenscar 在嵌入式设备上进行多任务处理

Ada SPARK 将字符串转换为整数