| 1 | %%% Copyright (C) Dominic Williams, Nicolas Charpentier |
|---|
| 2 | %%% All rights reserved. |
|---|
| 3 | %%% See file COPYING. |
|---|
| 4 | |
|---|
| 5 | -module (tests_test). |
|---|
| 6 | -export ([filter_by_attribute/0]). |
|---|
| 7 | -test (exports). |
|---|
| 8 | |
|---|
| 9 | filter_by_attribute () -> |
|---|
| 10 | non_tests_returns_empty_list(), |
|---|
| 11 | test_exports_returns_list_of_tests_from_unloaded_binary (), |
|---|
| 12 | test_non_exports_also (), |
|---|
| 13 | ok. |
|---|
| 14 | |
|---|
| 15 | non_tests_returns_empty_list () -> |
|---|
| 16 | Tree = [{file, "my_code.erl", |
|---|
| 17 | ["-module (my_code).", |
|---|
| 18 | "-export ([ok/0]).", |
|---|
| 19 | "ok () ->", |
|---|
| 20 | "ok."]}], |
|---|
| 21 | Fun = fun non_tests_returns_empty_list/2, |
|---|
| 22 | ok = fixtures: use_tree (Tree, Fun). |
|---|
| 23 | |
|---|
| 24 | non_tests_returns_empty_list (Dir, [{file, Name, _}]) -> |
|---|
| 25 | Path = filename: join (Dir, Name), |
|---|
| 26 | Binary = modules: to_binary (Path), |
|---|
| 27 | {_, my_code, []} = tests: filter_by_attribute (Binary), |
|---|
| 28 | ok. |
|---|
| 29 | |
|---|
| 30 | test_exports_returns_list_of_tests_from_unloaded_binary () -> |
|---|
| 31 | Tree = [{file, "my_tests.erl", |
|---|
| 32 | ["-module (my_tests).", |
|---|
| 33 | "-test (exports).", |
|---|
| 34 | "-export ([ok/0, nok/0]).", |
|---|
| 35 | "ok () ->", |
|---|
| 36 | " ok = list_to_existing_atom (\"ok\").", |
|---|
| 37 | "nok () ->", |
|---|
| 38 | " ok = list_to_existing_atom (\"nok\")."]}], |
|---|
| 39 | Fun = fun test_exports_returns_list_of_tests_from_unloaded_binary/2, |
|---|
| 40 | ok = fixtures: use_tree (Tree, Fun). |
|---|
| 41 | |
|---|
| 42 | test_exports_returns_list_of_tests_from_unloaded_binary (Dir, [{_, Name, _}]) -> |
|---|
| 43 | Path = filename: join (Dir, Name), |
|---|
| 44 | Binary = modules: to_binary (Path), |
|---|
| 45 | false = code: is_loaded (my_tests), |
|---|
| 46 | {_, my_tests, [ok, nok]} = tests: filter_by_attribute (Binary), |
|---|
| 47 | false = code: is_loaded (my_tests), |
|---|
| 48 | ok. |
|---|
| 49 | |
|---|
| 50 | test_non_exports_also () -> |
|---|
| 51 | Tree = [{file, "my_test_not_exports.erl", |
|---|
| 52 | ["-module (my_test_not_exports).", |
|---|
| 53 | "-test (ok).", |
|---|
| 54 | "-export ([ok/0, nok/0]).", |
|---|
| 55 | "", |
|---|
| 56 | "ok () ->", |
|---|
| 57 | " ok = list_to_existing_atom (\"ok\").", |
|---|
| 58 | "", |
|---|
| 59 | "nok () ->", |
|---|
| 60 | " ok = list_to_existing_atom (\"nok\")."]}], |
|---|
| 61 | Fun = fun test_non_exports_also/2, |
|---|
| 62 | ok = fixtures: use_tree (Tree, Fun). |
|---|
| 63 | |
|---|
| 64 | test_non_exports_also (Dir, [{file, Name, _}]) -> |
|---|
| 65 | Path = filename: join (Dir, Name), |
|---|
| 66 | Binary = modules: to_binary (Path), |
|---|
| 67 | false = code: is_loaded (my_test_not_exports), |
|---|
| 68 | {_, my_test_not_exports, [ok]} = tests: filter_by_attribute (Binary), |
|---|
| 69 | false = code: is_loaded (my_test_not_exports), |
|---|
| 70 | ok. |
|---|