test_context.lua 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. --
  2. -- tests/base/test_context.lua
  3. -- Test suite for the configuration context API.
  4. -- Copyright (c) 2012-2014 Jason Perkins and the Premake project
  5. --
  6. local p = premake
  7. local suite = test.declare("context")
  8. local context = p.context
  9. local configset = p.configset
  10. local field = p.field
  11. --
  12. -- Setup and teardown
  13. --
  14. local ctx, cset
  15. function suite.setup()
  16. cset = configset.new()
  17. ctx = context.new(cset)
  18. end
  19. --
  20. -- Make sure that new() returns a valid object.
  21. --
  22. function suite.new_returnsValidObject()
  23. test.isequal("table", type(ctx))
  24. end
  25. --
  26. -- Context should be able to retrieve a default value from
  27. -- the configuration set, using the field name.
  28. --
  29. function suite.returnsConfigValue_onExistingValue()
  30. configset.store(cset, field.get("targetextension"), ".so")
  31. test.isequal(".so", ctx.targetextension)
  32. end
  33. --
  34. -- Tokens encountered in enabled fields should be expanded.
  35. --
  36. function suite.doesExpandTokens()
  37. configset.store(cset, field.get("targetname"), "MyProject%{1 + 1}")
  38. test.isequal("MyProject2", ctx.targetname)
  39. end
  40. --
  41. -- Token environment in extended context overrides context.
  42. --
  43. function suite.extent()
  44. -- set in toplevel context.
  45. configset.store(cset, field.get("targetname"), "%{value}")
  46. -- detoken in toplevel context should result in empty string.
  47. test.isequal("", ctx.targetname)
  48. -- create an extended context with a local environ.
  49. local environ = {
  50. value = "text"
  51. }
  52. local ext = context.extent(ctx, environ)
  53. -- detoken in extended context should result in value set in that environ.
  54. test.isequal("text", ext.targetname)
  55. end
  56. --
  57. -- mergeFilters should behave as expected for tags
  58. --
  59. function suite.mergeFilters()
  60. ctx = { terms = { tags = { "ctxtags" } } }
  61. src = { terms = { tags = { "srctags" } } }
  62. context.mergeFilters(ctx, src)
  63. result = { terms = { tags = { "ctxtags", "srctags" } } }
  64. test.isequal(result, ctx)
  65. end
  66. function suite.mergeFilters_keeptype()
  67. ctx = { terms = { kind = "ConsoleApp" } }
  68. src = { terms = { kind = "ConsoleApp" } }
  69. context.mergeFilters(ctx, src)
  70. test.isequal("string", type(ctx.terms.kind))
  71. end
  72. function suite.mergeFilters_createtable()
  73. ctx = { terms = { tags = "ctxtags" } }
  74. src = { terms = { tags = "srctags" } }
  75. context.mergeFilters(ctx, src)
  76. result = { terms = { tags = { "ctxtags", "srctags" } } }
  77. test.isequal(result, ctx)
  78. end