/spec/itemspecification.d

http://github.com/wilkie/djehuty · D · 74 lines · 42 code · 17 blank · 15 comment · 3 complexity · be81ff8604819e765cd9ce26014841a3 MD5 · raw file

  1. /*
  2. * specification.d
  3. *
  4. * This module implements a class that wraps the specification of a particular
  5. * subsection of a module.
  6. *
  7. * Originated: May 6th, 2010
  8. *
  9. */
  10. module spec.itemspecification;
  11. import spec.logic;
  12. import djehuty;
  13. class ItemSpecification {
  14. this(string name) {
  15. _name = name.dup;
  16. }
  17. string name() {
  18. return _name;
  19. }
  20. void add(string specification, it function() testBody) {
  21. _tests[specification] = testBody;
  22. }
  23. // Description: This will test a particular item.
  24. // Returns: When the test is successful, it returns true. It returns false otherwise.
  25. bool test(string name) {
  26. if (!(name in _tests)) {
  27. throw new Exception("Unknown Test");
  28. }
  29. it ret = _tests[name]();
  30. return ret == it.does;
  31. }
  32. size_t length() {
  33. return _tests.length;
  34. }
  35. int opApply(int delegate(ref string) loopBody) {
  36. foreach(test; _tests.keys) {
  37. if (loopBody(test)) {
  38. return 1;
  39. }
  40. }
  41. return 1;
  42. }
  43. // Description: Print out the specification of the package, which serves as
  44. // documentation for the application.
  45. string toString() {
  46. // Item should do this
  47. // Item should do that
  48. string ret = "";
  49. foreach(spec; _tests.keys) {
  50. ret ~= _name ~ " " ~ spec ~ "\n";
  51. }
  52. return ret;
  53. }
  54. private:
  55. string _name;
  56. it function()[string] _tests;
  57. }