textbuilder_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
  2. // Use of this source code is governed by a MIT license that can
  3. // be found in the LICENSE file.
  4. package termui
  5. import "testing"
  6. func TestReadAttr(t *testing.T) {
  7. m := MarkdownTxBuilder{}
  8. m.baseFg = ColorCyan | AttrUnderline
  9. m.baseBg = ColorBlue | AttrBold
  10. fg, bg := m.readAttr("fg-red,bg-reverse")
  11. if fg != ColorRed|AttrUnderline || bg != ColorBlue|AttrBold|AttrReverse {
  12. t.Error("readAttr failed")
  13. }
  14. }
  15. func TestMTBParse(t *testing.T) {
  16. /*
  17. str := func(cs []Cell) string {
  18. rs := make([]rune, len(cs))
  19. for i := range cs {
  20. rs[i] = cs[i].Ch
  21. }
  22. return string(rs)
  23. }
  24. */
  25. tbls := [][]string{
  26. {"hello world", "hello world"},
  27. {"[hello](fg-red) world", "hello world"},
  28. {"[[hello]](bg-red) world", "[hello] world"},
  29. {"[1] hello world", "[1] hello world"},
  30. {"[[1]](bg-white) [hello] world", "[1] [hello] world"},
  31. {"[hello world]", "[hello world]"},
  32. {"", ""},
  33. {"[hello world)", "[hello world)"},
  34. {"[0] [hello](bg-red)[ world](fg-blue)!", "[0] hello world!"},
  35. }
  36. m := MarkdownTxBuilder{}
  37. m.baseFg = ColorWhite
  38. m.baseBg = ColorDefault
  39. for _, s := range tbls {
  40. m.reset()
  41. m.parse(s[0])
  42. res := string(m.plainTx)
  43. if s[1] != res {
  44. t.Errorf("\ninput :%s\nshould:%s\noutput:%s", s[0], s[1], res)
  45. }
  46. }
  47. m.reset()
  48. m.parse("[0] [hello](bg-red)[ world](fg-blue)")
  49. if len(m.markers) != 2 &&
  50. m.markers[0].st == 4 &&
  51. m.markers[0].ed == 11 &&
  52. m.markers[0].fg == ColorWhite &&
  53. m.markers[0].bg == ColorRed {
  54. t.Error("markers dismatch")
  55. }
  56. m2 := NewMarkdownTxBuilder()
  57. cs := m2.Build("[0] [hellob-e) wrd]fgblue)!", ColorWhite, ColorBlack)
  58. cs = m2.Build("[0] [hello](bg-red) [world](fg-blue)!", ColorWhite, ColorBlack)
  59. if cs[4].Ch != 'h' && cs[4].Bg != ColorRed && cs[4].Fg != ColorWhite {
  60. t.Error("dismatch in Build")
  61. }
  62. }