transform_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package stringutil
  10. import (
  11. "testing"
  12. )
  13. func TestStripCStyleComments(t *testing.T) {
  14. test := `
  15. // Comment1
  16. This is a test
  17. /* A
  18. comment
  19. // Comment2
  20. */ bla
  21. `
  22. if out := string(StripCStyleComments([]byte(test))); out != `
  23. This is a test
  24. bla
  25. ` {
  26. t.Error("Unexpected return:", out)
  27. return
  28. }
  29. }
  30. func TestCreateDisplayString(t *testing.T) {
  31. testdata := []string{"this is a tEST", "_bla", "a_bla", "a__bla", "a__b_la", "",
  32. "a fool a to be to"}
  33. expected := []string{"This Is a Test", "Bla", "A Bla", "A Bla", "A B La", "",
  34. "A Fool a to Be To"}
  35. for i, str := range testdata {
  36. res := CreateDisplayString(str)
  37. if res != expected[i] {
  38. t.Error("Unexpected result for creating a display string from:", str,
  39. "result:", res, "expected:", expected[i])
  40. }
  41. }
  42. }
  43. func TestStripUniformIndentation(t *testing.T) {
  44. testdata := []string{`
  45. aaa
  46. aaa
  47. aaa
  48. `, `
  49. bbb
  50. xx xx
  51. bbb
  52. bbb`, `
  53. ccc
  54. ccc
  55. ccc
  56. `}
  57. expected := []string{`
  58. aaa
  59. aaa
  60. aaa
  61. `, `
  62. bbb
  63. xx xx
  64. bbb
  65. bbb`, `
  66. ccc
  67. ccc
  68. ccc
  69. `}
  70. for i, str := range testdata {
  71. res := StripUniformIndentation(str)
  72. if res != expected[i] {
  73. t.Error("Unexpected result:", str,
  74. "result: '"+res+"' expected:", expected[i])
  75. return
  76. }
  77. }
  78. }
  79. func TestNewLineTransform(t *testing.T) {
  80. res := TrimBlankLines(ToUnixNewlines("\r\n test123\r\ntest123\r\n"))
  81. if res != " test123\ntest123" {
  82. t.Error("Unexpected result:", res)
  83. return
  84. }
  85. }