test_override.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. --
  2. -- tests/base/test_override.lua
  3. -- Verify function override support.
  4. -- Copyright (c) 2012 Jason Perkins and the Premake project
  5. --
  6. local p = premake
  7. local suite = test.declare("base_override")
  8. --
  9. -- Setup
  10. --
  11. local X = {}
  12. function suite.setup()
  13. X.testfunc = function(value)
  14. return value or "testfunc"
  15. end
  16. end
  17. --
  18. -- Should be able to completely replace the function with one of my own.
  19. --
  20. function suite.canOverride()
  21. p.override(X, "testfunc", function()
  22. return "canOverride"
  23. end)
  24. test.isequal("canOverride", X.testfunc())
  25. end
  26. --
  27. -- Should be able to reference the original implementation.
  28. --
  29. function suite.canCallOriginal()
  30. p.override(X, "testfunc", function(base)
  31. return "canOverride > " .. base()
  32. end)
  33. test.isequal("canOverride > testfunc", X.testfunc())
  34. end
  35. --
  36. -- Arguments should pass through.
  37. --
  38. function suite.canPassThroughArguments()
  39. p.override(X, "testfunc", function(base, value)
  40. return value .. " > " .. base()
  41. end)
  42. test.isequal("testval > testfunc", X.testfunc("testval"))
  43. end
  44. --
  45. -- Can override the same function multiple times.
  46. --
  47. function suite.canOverrideMultipleTimes()
  48. p.override(X, "testfunc", function(base, value)
  49. return string.format("[%s > %s]", value, base("base1"))
  50. end)
  51. p.override(X, "testfunc", function(base, value)
  52. return string.format("{%s > %s}", value, base("base2"))
  53. end)
  54. test.isequal("{base3 > [base2 > base1]}", X.testfunc("base3"))
  55. end