test_string.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. --
  2. -- tests/test_string.lua
  3. -- Automated test suite for the new string functions.
  4. -- Copyright (c) 2008 Jason Perkins and the Premake project
  5. --
  6. local suite = test.declare("string")
  7. --
  8. -- string.endswith() tests
  9. --
  10. function suite.endswith_ReturnsTrue_OnMatch()
  11. test.istrue(string.endswith("Abcdef", "def"))
  12. end
  13. function suite.endswith_ReturnsFalse_OnMismatch()
  14. test.isfalse(string.endswith("Abcedf", "efg"))
  15. end
  16. function suite.endswith_ReturnsFalse_OnLongerNeedle()
  17. test.isfalse(string.endswith("Abc", "Abcdef"))
  18. end
  19. function suite.endswith_ReturnsFalse_OnNilHaystack()
  20. test.isfalse(string.endswith(nil, "ghi"))
  21. end
  22. function suite.endswith_ReturnsFalse_OnNilNeedle()
  23. test.isfalse(string.endswith("Abc", nil))
  24. end
  25. function suite.endswith_ReturnsTrue_OnExactMatch()
  26. test.istrue(string.endswith("/", "/"))
  27. end
  28. --
  29. -- string.explode() tests
  30. --
  31. function suite.explode_ReturnsParts_OnValidCall()
  32. test.isequal({"aaa","bbb","ccc"}, string.explode("aaa/bbb/ccc", "/", true))
  33. end
  34. --
  35. -- string.startswith() tests
  36. --
  37. function suite.startswith_OnMatch()
  38. test.istrue(string.startswith("Abcdef", "Abc"))
  39. end
  40. function suite.startswith_OnMismatch()
  41. test.isfalse(string.startswith("Abcdef", "ghi"))
  42. end
  43. function suite.startswith_OnLongerNeedle()
  44. test.isfalse(string.startswith("Abc", "Abcdef"))
  45. end
  46. function suite.startswith_OnEmptyHaystack()
  47. test.isfalse(string.startswith("", "Abc"))
  48. end
  49. function suite.startswith_OnEmptyNeedle()
  50. test.istrue(string.startswith("Abcdef", ""))
  51. end
  52. --
  53. -- string.escapepattern() tests
  54. --
  55. function suite.escapepattern_escapes()
  56. test.isequal("boost_filesystem%-vc140%.1%.61%.0%.0", string.escapepattern("boost_filesystem-vc140.1.61.0.0"))
  57. test.isequal("footage/down/temp/cars_%[100%]_upper/cars_%[100%]_upper%.exr", string.escapepattern("footage/down/temp/cars_[100]_upper/cars_[100]_upper.exr"))
  58. end
  59. function suite.escapepattern_doesntEscape()
  60. local s = '<something foo="bar" />'
  61. test.isequal(s, s:escapepattern())
  62. s = 'lorem ipsum dolor sit amet'
  63. test.isequal(s, s:escapepattern())
  64. s = 'forward/slashes/foo/bar'
  65. test.isequal(s, s:escapepattern())
  66. s = '\\back\\slashes'
  67. test.isequal(s, s:escapepattern())
  68. s = 'new\nlines'
  69. test.isequal(s, s:escapepattern())
  70. end