Skip to content

Commit

Permalink
Add unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Dec 16, 2024
1 parent 0817c91 commit 62c64aa
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 25 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Unit Tests

on:
push:
branches: [ "main" ]

permissions:
contents: read

jobs:
test:
name: 'Build & Test'
strategy:
matrix:
os: [macOS-latest]
arch: [x86_64, arm64]
dc: [ldc-beta, dmd-master]
build: [debug, release]
exclude:
- { dc: dmd-master, arch: arm64 }

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- name: Install compiler
uses: dlang-community/setup-dlang@v1.4.0
with:
compiler: ${{ matrix.dc }}

- name: 'Build and test with ${{ matrix.os }} ${{ matrix.dc }} (${{ matrix.build }})'
run: |
dub test --config=unittest --build=${{ matrix.build }}
7 changes: 7 additions & 0 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ configuration "static" {
lflags "-framework" "Foundation" "-all_load"
}

configuration "unittest" {
platforms "osx"

lflags "-framework" "Foundation" "-all_load"
dependency "silly" version="~>1.0"
}

# For platforms that don't support it.
configuration "other_platforms" {
targetType "none"
Expand Down
60 changes: 48 additions & 12 deletions source/foundation/nsarray.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import core.attribute : selector, optional;
private alias iter_func(T) = int delegate(T);
private alias iter_i_func(T) = int delegate(size_t, T);

nothrow @nogc:
version(D_ObjectiveC):

/**
A static ordered collection of objects.
*/
Expand All @@ -32,27 +35,31 @@ public:
/**
Creates and returns an empty array.
*/
static NSArray!T array() @selector("array");
static NSArray!T create() @selector("array");

/**
Creates and returns an array containing the objects in another given array.
*/
static NSArray!T array(ref NSArray!T array) @selector("arrayWithArray:");
static NSArray!T create(ref NSArray!T array) @selector("arrayWithArray:");

/**
Creates and returns an array containing a given object.
*/
static NSArray!T array(T object) @selector("arrayWithObject:");
static NSArray!T create(T object) @selector("arrayWithObject:");

/**
Creates and returns an array containing the objects in the argument list.
*/
static NSArray!T array(T firstObject, ...) @selector("arrayWithObjects:");
static NSArray!T create(T* objects, NSUInteger count) @selector("arrayWithObjects:count:");

/**
Creates and returns an array containing the objects in the argument list.
Allows creating NSArrays from a D slice.
*/
static NSArray!T array(T* objects, NSUInteger count) @selector("arrayWithObjects:count:");
extern(D)
final // @suppress(dscanner.useless.final)
static NSArray!T create(T[] objects) {
return typeof(this).create(objects.ptr, objects.length);
}

/**
Allocates a new NSArray
Expand Down Expand Up @@ -159,6 +166,7 @@ public:
final
int opApply(scope iter_func!T dg) {
auto ngc_dg = assumeNothrowNoGC!(iter_func!T)(dg);

foreach (i; 0..length) {
int result = ngc_dg(this[i]);
if (result)
Expand Down Expand Up @@ -224,12 +232,6 @@ public:
alias opDollar = length;
}

unittest {
NSString myString = NSString.alloc.init("");
NSArray!NSString myArray = NSArray!(NSString).array();

}

/**
A Mutable NSArray
*/
Expand Down Expand Up @@ -312,4 +314,38 @@ public:
void opIndexAssign(DRTBindable value, size_t index) {
this.message!void("insertObject:atIndex:", value, index);
}
}

@("NSArray: multiple template instantiation")
unittest {
NSArray!NSObject objarray;
NSArray!NSString strarray;
}

@("NSArray: create and index")
unittest {
NSString myString = NSString.create("A");
NSString myString2 = NSString.create("B");
NSString[2] darray = [myString, myString2];

auto myArray = NSArray!(NSString).create(darray);

foreach(i; 0..myArray.length) {
assert(myArray[i].isEqual(darray[i]));
}
}

@("NSArray: foreach")
unittest {
NSString myString = NSString.create("A");
NSString myString2 = NSString.create("B");
NSString[2] darray = [myString, myString2];

auto myArray = NSArray!(NSString).create(darray);

foreach(item; myArray)
assert(item !is null);

foreach_reverse(item; myArray)
assert(item !is null);
}
2 changes: 1 addition & 1 deletion source/foundation/nsbundle.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import objc;

import core.attribute : selector, optional;


nothrow @nogc:
version(D_ObjectiveC):

/**
Constants that describe the CPU types that a bundle’s executable code supports.
Expand Down
3 changes: 3 additions & 0 deletions source/foundation/nscoder.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import objc;

import core.attribute : selector, optional;

nothrow @nogc:
version(D_ObjectiveC):

/**
A protocol that enables an object to be encoded and
decoded for archiving and distribution.
Expand Down
2 changes: 2 additions & 0 deletions source/foundation/nsdictionary.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import objc;

import core.attribute : selector, optional;

nothrow @nogc:
version(D_ObjectiveC):

/**
NSDictionary
Expand Down
4 changes: 3 additions & 1 deletion source/foundation/nsenumerator.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import foundation;
import objc;

import core.attribute : selector, optional;

import core.stdc.config : c_ulong;

nothrow @nogc:
version(D_ObjectiveC):

/**
This defines the structure used as contextual information
in the NSFastEnumeration protocol.
Expand Down
3 changes: 3 additions & 0 deletions source/foundation/nserror.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import objc;

import core.attribute : selector, optional;

nothrow @nogc:
version(D_ObjectiveC):

/**
An error domain
*/
Expand Down
6 changes: 5 additions & 1 deletion source/foundation/nsobject.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import objc.basetypes;
import objc;
import core.attribute : selector, optional;

nothrow @nogc:
version(D_ObjectiveC):

/**
Base class of all Objective-C classes.
*/
Expand Down Expand Up @@ -269,4 +272,5 @@ auto ref inout(T) released(T)(auto ref inout(T) value) if (isObjcClassInstance!T
auto ref inout(T) autoreleased(T)(auto ref inout(T) value) if (isObjcClassInstance!T) {
(cast(T)value).autorelease();
return value;
}
}

4 changes: 4 additions & 0 deletions source/foundation/nsproto.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ module foundation.nsproto;
import foundation;
import objc.basetypes;
import objc.rt;

import core.attribute : selector, optional;

nothrow @nogc:
version(D_ObjectiveC):

/**
The base protocol all Objective-C protocols should follow.
*/
Expand Down
7 changes: 6 additions & 1 deletion source/foundation/nsset.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
*/
module foundation.nsset;
import foundation;
import objc;
import objc;



nothrow @nogc:
version(D_ObjectiveC):
6 changes: 5 additions & 1 deletion source/foundation/nsstring.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import objc;

import core.attribute : selector, optional;

nothrow @nogc:
version(D_ObjectiveC):

/**
The following constants are provided by NSString as
possible string encodings.
Expand Down Expand Up @@ -639,7 +642,8 @@ public:
Converts to a D string.
*/
extern(D)
final string toString() {
final // @suppress(dscanner.useless.final)
string toString() {
return cast(string)ptr[0..length];
}
}
Expand Down
3 changes: 3 additions & 0 deletions source/foundation/nsurl.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import objc;

import core.attribute : selector, optional;

nothrow @nogc:
version(D_ObjectiveC):

/**
An object that represents the location of a resource, such as an item on a
remote server or the path to a local file.
Expand Down
11 changes: 3 additions & 8 deletions source/foundation/nsvalue.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import objc;

import core.attribute : selector, optional;

nothrow @nogc:
version(D_ObjectiveC):

/**
A simple container for a single C or Objective-C data item.
*/
Expand Down Expand Up @@ -208,12 +211,4 @@ public:
Gets whether this number is equal to another.
*/
bool isEqual(NSNumber other) @selector("isEqualToNumber:");

/**
Implements equality comparison
*/
extern(D)
bool opEquals(T)(T other) if (is(T : NSNumber)) {
return this.isEqual(other.self_);
}
}
3 changes: 3 additions & 0 deletions source/foundation/nszone.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import objc;

import core.attribute : selector, optional;

nothrow @nogc:
version(D_ObjectiveC):

/**
A protocol that objects adopt to provide functional copies of themselves.
*/
Expand Down

0 comments on commit 62c64aa

Please sign in to comment.