SpecialString.coffee 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. S = require '../../src/layout/SpecialString'
  2. describe "SpecialString", ->
  3. describe 'SpecialString()', ->
  4. it 'should return instance', ->
  5. S('s').should.be.instanceOf S
  6. describe 'length()', ->
  7. it 'should return correct length for normal text', ->
  8. S('hello').length.should.equal 5
  9. it 'should return correct length for text containing tabs and tags', ->
  10. S('<a>he<you />l\tlo</a>').length.should.equal 13
  11. it "shouldn't count empty tags as tags", ->
  12. S('<>><').length.should.equal 4
  13. it "should count length of single tag as 0", ->
  14. S('<html>').length.should.equal 0
  15. it "should work correctly with html quoted characters", ->
  16. S(' &gt;&lt; &sp;').length.should.equal 5
  17. describe 'splitIn()', ->
  18. it "should work correctly with normal text", ->
  19. S("123456").splitIn(3).should.be.like ['123', '456']
  20. it "should work correctly with normal text containing tabs and tags", ->
  21. S("12\t3<hello>456").splitIn(3).should.be.like ['12', '\t', '3<hello>45', '6']
  22. it "should not trimLeft all lines when trimLeft is no", ->
  23. S('abc def').splitIn(3).should.be.like ['abc', ' de', 'f']
  24. it "should trimLeft all lines when trimLeft is true", ->
  25. S('abc def').splitIn(3, yes).should.be.like ['abc', 'def']
  26. describe 'cut()', ->
  27. it "should work correctly with text containing tabs and tags", ->
  28. original = S("12\t3<hello>456")
  29. cut = original.cut(2, 3)
  30. original.str.should.equal '123<hello>456'
  31. cut.str.should.equal '\t'
  32. it "should trim left when trimLeft is true", ->
  33. original = S ' 132'
  34. cut = original.cut 0, 1, yes
  35. original.str.should.equal '32'
  36. cut.str.should.equal '1'
  37. it "should be greedy", ->
  38. S("ab<tag>a").cut(0, 2).str.should.equal "ab<tag>"
  39. describe 'isOnlySpecialChars()', ->
  40. it "should work", ->
  41. S("12\t3<hello>456").isOnlySpecialChars().should.equal no
  42. S("<hello>").isOnlySpecialChars().should.equal yes
  43. describe 'clone()', ->
  44. it "should return independent instance", ->
  45. a = S('hello')
  46. b = a.clone()
  47. a.str.should.equal b.str
  48. a.should.not.equal b
  49. describe 'trim()', ->
  50. it "should return an independent instance", ->
  51. s = S('')
  52. s.trim().should.not.equal s
  53. it 'should return the same string when trim is not required', ->
  54. S('hello').trim().str.should.equal 'hello'
  55. it 'should return trimmed string', ->
  56. S(' hello').trim().str.should.equal 'hello'
  57. describe 'trimLeft()', ->
  58. it "should only trim on the left", ->
  59. S(' hello ').trimLeft().str.should.equal 'hello '
  60. describe 'trimRight()', ->
  61. it "should only trim on the right", ->
  62. S(' hello ').trimRight().str.should.equal ' hello'