Arc Forumnew | comments | leaders | submitlogin
Thanks zck for upgrading unit-test.arc to v1.0
6 points by akkartik 2479 days ago | 1 comment
I updated all our tests to use the new syntax. Old syntax:

    (suite math
           this-will-pass (assert-same 4 (+ 2 2))
           this-will-fail (assert-same 3 (+ 2 2)))

    ; example with some setup
    (suite-w/setup math (x 6 y 2)
                   adding-works (assert-same 8
                                             (+ x y))
                   multiplying-works (assert-same 12
                                                  (* x y)))
New syntax:

    (suite math
           (test this-will-pass (assert-same 4 (+ 2 2)))
           (test this-will-fail (assert-same 3 (+ 2 2))))

    ; example with some setup
    (suite math
           (setup x 6 y 2)
           (test adding-works (assert-same 8
                                           (+ x y)))
           (test multiplying-works (assert-same 12
                                                (* x y))))
Here's the script I used to automatically translate to the new syntax:

  ;; translate.arc
  (def translate (expr)
    (accum acc
      (translate-2 expr acc)))

  (def translate-2 (expr acc)
    (if (atom expr)
          (acc expr)
        (is car.expr 'suite)
          (do (acc 'suite)
              (let (suite-name . suite-body)  cdr.expr
                (acc suite-name)
                (translate-suite-body suite-body acc)))
        (is car.expr 'suite-w/setup)
          (do (acc 'suite)
              (let (suite-name suite-setup . suite-body)  cdr.expr
                (acc suite-name)
                (acc (cons 'setup suite-setup))
                (translate-suite-body suite-body acc)))
        'else
          (map acc expr)))

  (def translate-suite-body (suite-body acc)
    (if suite-body
      (if (acons car.suite-body)
        ; nested suite
        (let (nested-suite . rest) suite-body
          (acc (accum acc2
                  (translate-2 nested-suite acc2)))
          (translate-suite-body rest acc))
        ; test name must be atomic
        (let (test-name test-body . rest)  suite-body
          (acc `(test ,test-name ,test-body))
          (translate-suite-body rest acc)))))

  ; suite with tests
  (assert:iso '(suite a (test t1 b1) (test t2 b2))
              (translate '(suite a t1 b1 t2 b2)))

  ; suite with tests and nested suites
  (assert:iso '(suite a (test t1 b1) (suite s2 (test t3 b3)) (test t2 b2))
              (translate '(suite a t1 b1 (suite s2 t3 b3) t2 b2)))

  ; suite with setup and tests
  (assert:iso '(suite a (setup x 1 y 2) (test t1 b1) (test t2 b2))
              (translate '(suite-w/setup a (x 1 y 2) t1 b1 t2 b2)))

  ; suite with setup and tests and nested suites
  (assert:iso '(suite a (setup x 1 y 2) (test t1 b1) (suite s2 (test t3 b3)) (test t2 b2))
              (translate '(suite-w/setup a (x 1 y 2) t1 b1 (suite s2 t3 b3) t2 b2)))

  ; read a list of filenames on the commandline, emit translated versions to new filenames with a .2 suffix
  (each f cdr.argv
    (prn f)
    (fromfile string.f
      (tofile (+ string.f ".2")
        (each expr (drain (read) eof)
          (let out translate.expr
            (ppr out)
            (prn))))))
To run it:

   $ arc.sh translate.arc *.t lib/*.t lib/tests/*
   $ for f in *.t lib/*.t lib/tests/*.arc;
     do
       mv $f.2 $f;
     done
There were a few more minor tweaks: https://github.com/arclanguage/anarki/commit/9e42b31bdc

(I also had to slightly update the instructions at http://arclanguage.github.io)



3 points by zck 2478 days ago | link

Thanks akkartik for updating all the tests! I'm hoping this test framework helps out.

Feel free to ping me if there are any questions, problems, or other feedback. I think the test framework is pretty solid now, in terms of features.

-----