Racket [Scheme] / Cocoa Glue – Part IV

As promised, here’s my current unit tests for my Racket / cocoa glue code. It’s just tests for my conversion code.

#lang racket

(require rackunit
         "cocoa-glue.rkt"
         ffi/unsafe
         ffi/unsafe/objc)

(test-case 
 "string->NSString and back"
 (let ([strings (list "1" "two" "◻" "◻◾" "◻five◾")])
   (for ((s strings))
     (let ([converted-string (string->NSString s)])
       (check-not-equal? s converted-string)
       (check-equal? s (NSString->string converted-string))))))

(test-case 
 "list->NSArray"
 (let* ([strings (list "a" "b" "c")]
        [foreign-strings (map string->NSString strings)]
        [ns-array (list->NSArray foreign-strings)])
   (check-equal? (NSArray-length ns-array)
                 (length strings))
   (for ((i (in-naturals))
         (string strings))
     (check-equal? 
      string
      (NSString->string 
       (NSArray-ref ns-array i))))))

(test-case 
 "list->NSArray->list"
 (let ([strings (list "1" "two" "◻◾◻")])
   (check-equal? 
    strings
    (map NSString->string
         (NSArray->list 
          (list->NSArray
           (map string->NSString strings)))))))

(define-simple-check (check-dict/NSDictionary-equiv? dict NSDict)
  (check-equal? (tell-int32 NSDict count) 
                (dict-count dict))
    (for (((k dict-value) (in-dict dict)))
      (let ([NSDict-value (tell NSDict objectForKey: (string->NSString k))])
        (check-equal? 
         (NSString->string dict-value)
         (NSString->string NSDict-value)))))

(let* ([keys (list "a" "b" "c")]
       [string-values (list "1" "2" "3")]
       [foreign-values (map string->NSString string-values)]
       [alist (map cons keys foreign-values)])
  (test-case 
   "dictionary->NSDictionary"
   (check-dict/NSDictionary-equiv? 
    alist
    (dictionary->NSDictionary alist)))
  (test-case 
   "dictionary->NSMutableDictionary"
   (check-dict/NSDictionary-equiv? 
    alist
    (dictionary->NSMutableDictionary alist))))

Leave a Reply

Your email address will not be published. Required fields are marked *