test_table.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. --
  2. -- tests/base/test_table.lua
  3. -- Automated test suite for the new table functions.
  4. -- Copyright (c) 2008-2013 Jason Perkins and the Premake project
  5. --
  6. local suite = test.declare("table")
  7. local t
  8. --
  9. -- table.contains() tests
  10. --
  11. function suite.contains_OnContained()
  12. t = { "one", "two", "three" }
  13. test.istrue( table.contains(t, "two") )
  14. end
  15. function suite.contains_OnNotContained()
  16. t = { "one", "two", "three" }
  17. test.isfalse( table.contains(t, "four") )
  18. end
  19. --
  20. -- table.flatten() tests
  21. --
  22. function suite.flatten_OnMixedValues()
  23. t = { "a", { "b", "c" }, "d" }
  24. test.isequal({ "a", "b", "c", "d" }, table.flatten(t))
  25. end
  26. --
  27. -- table.implode() tests
  28. --
  29. function suite.implode()
  30. t = { "one", "two", "three", "four" }
  31. test.isequal("[one], [two], [three], [four]", table.implode(t, "[", "]", ", "))
  32. end
  33. --
  34. -- table.indexof() tests
  35. --
  36. function suite.indexof_returnsIndexOfValueFound()
  37. local idx = table.indexof({ "a", "b", "c" }, "b")
  38. test.isequal(2, idx)
  39. end
  40. --
  41. -- table.isempty() tests
  42. --
  43. function suite.isempty_ReturnsTrueOnEmpty()
  44. test.istrue(table.isempty({}))
  45. end
  46. function suite.isempty_ReturnsFalseOnNotEmpty()
  47. test.isfalse(table.isempty({ 1 }))
  48. end
  49. function suite.isempty_ReturnsFalseOnNotEmptyMap()
  50. test.isfalse(table.isempty({ name = 'premake' }))
  51. end
  52. function suite.isempty_ReturnsFalseOnNotEmptyMapWithFalseKey()
  53. test.isfalse(table.isempty({ [false] = 0 }))
  54. end
  55. --
  56. -- table.insertsorted() tests
  57. --
  58. function suite.insertsorted()
  59. local t = {}
  60. table.insertsorted(t, 5)
  61. table.insertsorted(t, 2)
  62. table.insertsorted(t, 8)
  63. table.insertsorted(t, 4)
  64. table.insertsorted(t, 1)
  65. test.istrue(#t == 5)
  66. test.istrue(t[1] == 1)
  67. test.istrue(t[2] == 2)
  68. test.istrue(t[3] == 4)
  69. test.istrue(t[4] == 5)
  70. test.istrue(t[5] == 8)
  71. end