Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ jobs:
- name: make run
run: make run

objc-arc:
name: Objective-C ARC and MRC regression (native Apple runtime)
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- name: Compile runtime fixture without ARC
run: clang -Wall -Wextra -Werror -fno-objc-arc -c tests/objc_arc_probe.m -o /tmp/cest_probe.o
- name: Run Objective-C and Objective-C++ in both ownership modes
run: |
for mode in -fobjc-arc -fno-objc-arc; do
for language in objective-c objective-c++; do
compiler=clang
if [ "$language" = objective-c++ ]; then compiler=clang++; fi
"$compiler" -Wall -Wextra -Werror "$mode" -x "$language" tests/objc_arc.m \
-x none /tmp/cest_probe.o -framework Foundation -o /tmp/cest_arc
/tmp/cest_arc
code=0
/tmp/cest_arc --negative > /tmp/cest_arc_negative.log 2>&1 || code=$?
test "$code" -eq 1
grep 'Received (id:' /tmp/cest_arc_negative.log
done
done

msvc:
name: msvc (cl.exe)
runs-on: windows-latest
Expand Down
32 changes: 25 additions & 7 deletions cest.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#ifndef CEST_H_INCLUDED
#define CEST_H_INCLUDED

#define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE
#ifndef _POSIX_C_SOURCE
# define _POSIX_C_SOURCE 200809L
#endif
#ifndef _DEFAULT_SOURCE
# define _DEFAULT_SOURCE
#endif

#include <stdio.h>
#include <string.h>
Expand All @@ -19,6 +23,16 @@

#ifdef __OBJC__
#import <objc/objc.h>
# if defined(__clang__)
# if __has_feature(objc_arc)
# define _CEST_OBJC_BORROWED __unsafe_unretained
# define _CEST_OBJC_POINTER(value) ((__bridge const void *)(value))
# endif
# endif
# ifndef _CEST_OBJC_BORROWED
# define _CEST_OBJC_BORROWED
# define _CEST_OBJC_POINTER(value) ((const void *)(value))
# endif
#endif

#ifdef __cplusplus
Expand Down Expand Up @@ -274,7 +288,9 @@ typedef struct {
bool b;
unsigned char u8;
#ifdef __OBJC__
id obj;
// Assertion values are synchronous borrows, not object owners.
// A strong id would make this union non-trivial under ARC.
id _CEST_OBJC_BORROWED obj;
#endif
#ifdef __cplusplus
std::regex* re;
Expand Down Expand Up @@ -541,6 +557,8 @@ static inline cest_value_t cest_value(T* v) { return cest_ptr((const void*)v); }
#define cest_value(x) \
_Generic((x), \
_Bool: cest_bool, \
signed char: cest_int, \
unsigned char: cest_int, \
int: cest_int, \
long: cest_int, \
long long: cest_int, \
Expand Down Expand Up @@ -593,7 +611,7 @@ static inline void _cest_print_value(cest_value_t v) {
case CEST_TYPE_BOOL: printf("(bool: %s)", v.as.b ? "true" : "false"); break;
case CEST_TYPE_ARRAY: printf("(array[%d]: %p)", v.len, v.as.p); break;
#ifdef __OBJC__
case CEST_TYPE_OBJC_ID: printf("(id: %p)", (void*)v.as.obj); break;
case CEST_TYPE_OBJC_ID: printf("(id: %p)", _CEST_OBJC_POINTER(v.as.obj)); break;
#else
case CEST_TYPE_OBJC_ID: printf("(id: %p)", v.as.p); break;
#endif
Expand Down Expand Up @@ -655,12 +673,12 @@ static inline int match_eq(cest_value_t a, cest_value_t b, int* diff_pos) {
(a.type == CEST_TYPE_OBJC_ID && b.type == CEST_TYPE_PTR)) {
const void *p = a.type == CEST_TYPE_PTR ? a.as.p : b.as.p;
id obj = a.type == CEST_TYPE_OBJC_ID ? a.as.obj : b.as.obj;
return p == (const void*)obj;
return p == _CEST_OBJC_POINTER(obj);
}
if ((a.type == CEST_TYPE_PTR || a.type == CEST_TYPE_OBJC_ID) &&
(b.type == CEST_TYPE_PTR || b.type == CEST_TYPE_OBJC_ID)) {
const void *p1 = a.type == CEST_TYPE_PTR ? a.as.p : (const void*)a.as.obj;
const void *p2 = b.type == CEST_TYPE_PTR ? b.as.p : (const void*)b.as.obj;
const void *p1 = a.type == CEST_TYPE_PTR ? a.as.p : _CEST_OBJC_POINTER(a.as.obj);
const void *p2 = b.type == CEST_TYPE_PTR ? b.as.p : _CEST_OBJC_POINTER(b.as.obj);
return p1 == p2;
}
#endif
Expand Down
10 changes: 9 additions & 1 deletion docs/en-us/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,12 @@ Output:
● test_string.c
● String
✓ compares
```
```

### Objective-C ownership in assertions

Object assertion values borrow the object for the synchronous assertion; Cest
does not retain the last object in its global assertion context. Keep the object
alive through the assertion expression. ARC and manual-reference-counting builds
are supported, including Objective-C++. The regression sources in `tests/objc_arc*`
exercise nil, BOOL, pointer comparison, failure formatting and ARC deallocation.
45 changes: 45 additions & 0 deletions tests/objc_arc.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Standalone regression: this includes no FoxStep headers or implementation.
* Compile this same source as Objective-C and Objective-C++, with and without
* ARC, against libobjc2 (Linux) or the native Apple runtime. */
#import "objc_arc_probe.h"
#include "../cest.h"
int main(int argc, char **argv) {
@autoreleasepool {
if (argc == 2 && strcmp(argv[1], "--negative") == 0) {
/* Exercises failure formatting (the id-to-void* diagnostic cast). */
expect((id)nil).toBeTruthy();
return cest_result();
}
describe("Objective-C object assertions", {
it("supports nil, BOOL and object/pointer comparisons", {
id object = [CestARCProbe new];
BOOL yes = YES;
BOOL no = NO;
expect(object).toBeTruthy();
expect((id)nil).toBeNull();
expect(yes).toBeTruthy();
expect(no).toBeFalsy();
expect((id)nil).toEqual((const void *)NULL);
expect((const void *)NULL).toEqual((id)nil);
#if !__has_feature(objc_arc)
[object release];
#endif
});
#if __has_feature(objc_arc)
it("does not retain an object in the last assertion's global context", {
__weak id weak;
int before = cest_arc_probe_deallocations;
@autoreleasepool {
id object = [CestARCProbe new];
weak = object;
expect(object).toBeTruthy();
object = nil;
}
expect(weak == nil).toBeTruthy();
expect(cest_arc_probe_deallocations == before + 1).toBeTruthy();
});
#endif
});
return cest_result();
}
}
20 changes: 20 additions & 0 deletions tests/objc_arc_probe.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CEST_ARC_PROBE_H
#define CEST_ARC_PROBE_H
#ifdef __APPLE__
#import <Foundation/Foundation.h>
@interface CestARCProbe : NSObject @end
#else
#import <objc/runtime.h>
__attribute__((objc_root_class))
@interface CestARCProbe { Class isa; }
+ (id)new;
#if !__has_feature(objc_arc)
- (id)retain;
- (void)release;
- (id)autorelease;
- (void)dealloc;
#endif
@end
#endif
extern int cest_arc_probe_deallocations;
#endif
24 changes: 24 additions & 0 deletions tests/objc_arc_probe.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Compile this runtime-only fixture without ARC. No application framework is
* required on Linux/libobjc2; Apple builds use the system NSObject. */
#import "objc_arc_probe.h"
#ifndef __APPLE__
#import <objc/objc-arc.h>
#endif
int cest_arc_probe_deallocations;
@implementation CestARCProbe
#ifndef __APPLE__
+ (id)new { return class_createInstance(self, 0); }
- (id)retain { return objc_retain_fast_np(self); }
- (void)release { objc_release_fast_np(self); }
- (id)autorelease { return objc_autorelease(self); }
- (void)_ARCCompliantRetainRelease {}
#endif
- (void)dealloc {
++cest_arc_probe_deallocations;
#ifdef __APPLE__
[super dealloc];
#else
object_dispose(self);
#endif
}
@end
Loading