2

我通常以这种方式编写我的 Perl 测试

 for my $i ( 0, 1, 2 ) {
    is_deeply( $fetch_public_topic_ids->[$i],
 $expected_sorted_topic_list->[$i], 'Match' );

when$expected_sorted_topic是我的测试用例数据的数组引用。我有时会收到反馈说我应该避免在我的“for”中写 0、1、2、3... 或 0...5,因为这被认为是“不好的风格”?

但是我有什么替代品呢?

4

3 回答 3

5

您想遍历数组的索引,但在确定索引时没有数组数字。问题是索引的硬编码。

for my $i (0..$#$fetch_public_topic_ids) {
   ...
}
于 2013-07-22T03:41:32.340 回答
5

为什么你甚至使用循环?

你应该能够做到

is_deeply( $fetch_public_topic_ids, $expected_sorted_topic_list );
于 2013-07-22T05:49:40.340 回答
1

将您的测试放入哈希数组中:

my @tests = (
    {
        fetch_public_topic_ids     => [ "whatever" ],
        expected_sorted_topic_list => [ "whatever" ],
        test_name                  => "Match",
    },
    # repeat as needed
);

for my $test ( @tests ){
    is_deeply( $test->{ fetch_public_topic_ids     },
               $test->{ expected_sorted_topic_list },
               $test->{ test_name                  },
           );
}
于 2013-07-22T12:09:37.450 回答