使用 Kiwi (Sentesting) Kit 比较两个数组的等效对象

发布于 2024-12-08 09:54:18 字数 1248 浏览 6 评论 0原文

我想比较两个数组中的等效对象,一个是我的类中的属性,另一个是我的测试方法中的属性。

我无法直接比较,因为对象将被单独分配,因此具有不同的内存位置。

为了解决这个问题,我在我的对象上实现了描述,以字符串形式列出它的属性:(vel是一个CGPoint)

- (NSString *)description {
return [NSString stringWithFormat:@"vel:%.5f%.5f",vel.x,vel.y];
}

我测试:

NSLog(@"moveArray description: %@",[moveArray description]);
NSLog(@"currentMoves description: %@", [p.currentMoves description]);

[[theValue([moveArray description]) should] equal:theValue([p.currentMoves description])];

我的NSLog的产量:

Project[13083:207] moveArray description: (
"vel:0.38723-0.92198"
)

Project[13083:207] currentMoves description: (
"vel:0.38723-0.92198"
)

但我的测试失败:

/ProjectPath/ObjectTest.m:37: error: -[ObjectTest example] : 'Object should pass test' [FAILED], expected subject to equal <9086b104>, got <7099e004>

theValue用字节和objective-C类型初始化一个KWValue并将其值设置为

- (id)initWithBytes:(const void *)bytes objCType:(const char *)anObjCType {
if ((self = [super init])) {
    objCType = anObjCType;
    value = [[NSValue alloc] initWithBytes:bytes objCType:anObjCType];
}

return self;
}

如何比较这两个数组具有相同值的对象?

I want to compare two arrays for equivalent objects, one a property inside my class and the other in my test method.

I cannot compare directly since the objects will be allocated seperately and thus have different memory locations.

To get around this I implemented description on my object to list out its properties in a string: (vel is a CGPoint)

- (NSString *)description {
return [NSString stringWithFormat:@"vel:%.5f%.5f",vel.x,vel.y];
}

I test with:

NSLog(@"moveArray description: %@",[moveArray description]);
NSLog(@"currentMoves description: %@", [p.currentMoves description]);

[[theValue([moveArray description]) should] equal:theValue([p.currentMoves description])];

My NSLog's yield:

Project[13083:207] moveArray description: (
"vel:0.38723-0.92198"
)

Project[13083:207] currentMoves description: (
"vel:0.38723-0.92198"
)

But my test fails:

/ProjectPath/ObjectTest.m:37: error: -[ObjectTest example] : 'Object should pass test' [FAILED], expected subject to equal <9086b104>, got <7099e004>

theValue initializes a KWValue with bytes and an objective-C type and sets its value with

- (id)initWithBytes:(const void *)bytes objCType:(const char *)anObjCType {
if ((self = [super init])) {
    objCType = anObjCType;
    value = [[NSValue alloc] initWithBytes:bytes objCType:anObjCType];
}

return self;
}

How can I compare that these two arrays have objects of equivalent values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁客 2024-12-15 09:54:18

您的测试失败,因为您正在比较指针地址,而不是值。

您可以迭代一个数组,并将每个对象与第二个数组中同等位置的对象进行比较。确保针对要比较的值类型正确进行比较。如果每个元素都有不同的类型,那么事情就会变得更加棘手。

// in some class
- (BOOL)compareVelocitiesInArray:(NSArray *)array1 withArray:(NSArray *)array2
{
    BOOL result = YES;

    for (uint i = 0; i < [array1 count]; i++) {
        CustomObject *testObj1 = [array1 objectAtIndex:i]
        CustomObject *testObj2 = [array2 objectAtIndex:i]

        // perform your test here ...
        if ([testObj1 velocityAsFloat] != [testObj2 velocityAsFloat]) {
            result = NO;
        }
    }

    return result;
}

// in another class
NSArray *myArray = [NSArray arrayWithObjects:obj1, obj2, nil];
NSArray *myOtherArray = [NSArray arrayWithObjects:obj3, obj4, nil];
BOOL result;

result = [self compareVelocitiesInArray:myArray withArray:myOtherArray];
NSLog(@"Do the arrays pass my test? %@", result ? @"YES" : @"NO");

Your test fails because you are comparing pointer addresses, not values.

You can iterate through one array and compare each object against the equivalently positioned object in the second array. Make sure your comparison is done correctly for the type of value you are comparing. If every element has a different type then it gets trickier.

// in some class
- (BOOL)compareVelocitiesInArray:(NSArray *)array1 withArray:(NSArray *)array2
{
    BOOL result = YES;

    for (uint i = 0; i < [array1 count]; i++) {
        CustomObject *testObj1 = [array1 objectAtIndex:i]
        CustomObject *testObj2 = [array2 objectAtIndex:i]

        // perform your test here ...
        if ([testObj1 velocityAsFloat] != [testObj2 velocityAsFloat]) {
            result = NO;
        }
    }

    return result;
}

// in another class
NSArray *myArray = [NSArray arrayWithObjects:obj1, obj2, nil];
NSArray *myOtherArray = [NSArray arrayWithObjects:obj3, obj4, nil];
BOOL result;

result = [self compareVelocitiesInArray:myArray withArray:myOtherArray];
NSLog(@"Do the arrays pass my test? %@", result ? @"YES" : @"NO");
断念 2024-12-15 09:54:18

使用 Kiwi 比较两个数组是否内容相等的另一种可能性:

[[theValue(array1.count == array2.count) should] beTrue];
[[array1 should] containObjectsInArray:array2];
[[array2 should] containObjectsInArray:array1];

比较计数可确保其中一个数组不多次包含对象,从而确保它们确实相等。

Another possibility to compare two arrays for equal contents with Kiwi:

[[theValue(array1.count == array2.count) should] beTrue];
[[array1 should] containObjectsInArray:array2];
[[array2 should] containObjectsInArray:array1];

Comparing the count makes sure one of the array does not contain an object multiple times and therefore makes sure they're REALLY equal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文