Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 112 lines (98 sloc) 4.106 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
; Copyright (c) Dave Ray, 2011. All rights reserved.

; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this
; distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.

(ns seesaw.test.util
  (:use seesaw.util)
  (:use [lazytest.describe :only (describe it testing)]
        [lazytest.expect :only (expect)]))

(describe check-args
  (it "returns true if the condition is true"
    (check-args true "yes!"))
  (it "returns throws IllegalArgumentException if condition is false"
    (try
      (do (check-args false "no!") false)
      (catch IllegalArgumentException e true))))

(describe cond-doto
  (it "only executes forms with true conditions"
    (= "firstsecondfifth" (str (cond-doto (StringBuilder.)
         true (.append "first")
         (> 2 1) (.append "second")
         (< 2 1) (.append "third")
         false (.append "fourth")
         (= "HI" "HI") (.append "fifth"))))))

(describe to-seq
  (it "makes a non-seq into a single-element seq"
    (= (seq ["hi"]) (to-seq "hi"))
    (= (seq [:k]) (to-seq :k)))
  (it "makes a collection into a seq"
    (= (seq #{:a :b}) (to-seq #{:a :b}))))


(describe camelize
  (it "turns dashes into camel humps"
    (= "onMouseClicked" (camelize "on-mouse-clicked"))))

(describe boolean?
  (it "returns true for true"
    (boolean? true))
  (it "returns true for false"
    (boolean? false))
  (it "returns false for nil"
    (not (boolean? nil)))
  (it "returns false for non-boolean"
    (not (boolean? "hi"))))

(describe try-cast
  (it "returns its input if cast succeeds"
    (= "TEST" (try-cast java.lang.String "TEST")))
  (it "returns nil if input is nil"
    (nil? (try-cast java.lang.String nil)))
  (it "returns nil if cast fails"
    (nil? (try-cast java.lang.String 99))))

(describe to-url
  (it "returns a URL if (str input) is a valid URL"
    (= "http://darevay.com" (-> (to-url "http://darevay.com") .toExternalForm )))
  (it "returns nil if (str input is not a valid URL"
    (nil? (to-url "not a URL"))))

(describe apply-options
  (it "throws IllegalArgumentException if properties aren't event"
    (try
      (do (apply-options (javax.swing.JPanel.) [1 2 3] {}) false)
      (catch IllegalArgumentException e true)))
  (it "throws IllegalArgumentException for an unknown property"
    (try
      (do (apply-options (javax.swing.JPanel.) [:unknown "unknown"] {}) false)
      (catch IllegalArgumentException e true))))

(describe to-dimension
  (it "should throw an exception if it doesn't know what to do"
    (try
      (do (to-dimension {:a :map}) false)
      (catch IllegalArgumentException e true)))
  (it "should return its input if its already a Dimension"
    (let [d (java.awt.Dimension. 10 20)]
      (expect (= d (to-dimension d)))))
  (it "should return a new Dimension if input is [width :by height]"
    (let [d (to-dimension [1 :by 2])]
      (expect (= java.awt.Dimension (class d)))
      (expect (= 1 (.getWidth d)))
      (expect (= 2 (.getHeight d))))))

(describe to-insets
  (it "should throw an exception if it doesn't know what to do"
    (try
      (do (to-insets "a random string") false)
      (catch IllegalArgumentException e true)))
  (it "should return its input if its already an Insets"
    (let [i (java.awt.Insets. 1 2 3 4)]
      (expect (= i (to-insets i)))))
  (it "should return uniform insets from a number"
    (= (java.awt.Insets. 9 9 9 9) (to-insets 9)))
  (it "should return insets from a 4-element [top, left, bottom, right] vector"
    (= (java.awt.Insets. 1 2 3 4) (to-insets [1 2 3 4])))
  (it "should return insets from a 2-element [top/bottom, left/right] vector"
    (= (java.awt.Insets. 5 6 5 6) (to-insets [5 6]))))

(describe atom?
  (it "should return true for an atom"
    (atom? (atom nil)))
  (it "should return false for a non-atom"
    (not (atom? (ref nil)))))
Something went wrong with that request. Please try again.