helper_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package tool
  11. import (
  12. "bytes"
  13. "encoding/json"
  14. "fmt"
  15. "strings"
  16. "testing"
  17. "devt.de/krotik/common/stringutil"
  18. "devt.de/krotik/common/termutil"
  19. )
  20. type testConsoleLineTerminal struct {
  21. in []string
  22. out bytes.Buffer
  23. }
  24. func (t *testConsoleLineTerminal) StartTerm() error {
  25. return nil
  26. }
  27. func (t *testConsoleLineTerminal) AddKeyHandler(handler termutil.KeyHandler) {
  28. }
  29. func (t *testConsoleLineTerminal) NextLine() (string, error) {
  30. var err error
  31. var ret string
  32. if len(t.in) > 0 {
  33. ret = t.in[0]
  34. t.in = t.in[1:]
  35. } else {
  36. err = fmt.Errorf("Input is empty in testConsoleLineTerminal")
  37. }
  38. return ret, err
  39. }
  40. func (t *testConsoleLineTerminal) NextLinePrompt(prompt string, echo rune) (string, error) {
  41. return t.NextLine()
  42. }
  43. func (t *testConsoleLineTerminal) WriteString(s string) {
  44. t.out.WriteString(s)
  45. }
  46. func (t *testConsoleLineTerminal) Write(p []byte) (n int, err error) {
  47. return t.out.Write(p)
  48. }
  49. func (t *testConsoleLineTerminal) StopTerm() {
  50. }
  51. type testCustomHandler struct {
  52. }
  53. func (t *testCustomHandler) CanHandle(s string) bool {
  54. return s == "@cus"
  55. }
  56. func (t *testCustomHandler) Handle(ot OutputTerminal, input string) {}
  57. func (t *testCustomHandler) LoadInitialFile(tid uint64) error {
  58. return nil
  59. }
  60. type testOutputTerminal struct {
  61. b bytes.Buffer
  62. }
  63. func (t *testOutputTerminal) WriteString(s string) {
  64. t.b.WriteString(s)
  65. }
  66. func TestMatchesFulltextSearch(t *testing.T) {
  67. ot := &testOutputTerminal{}
  68. ok := matchesFulltextSearch(ot, "abc", "s[")
  69. if !ok && strings.HasPrefix(ot.b.String(), "Invalid search expression") {
  70. t.Error("Unexpected result:", ot.b.String(), ok)
  71. return
  72. }
  73. ot.b = bytes.Buffer{}
  74. ok = matchesFulltextSearch(ot, "abc", "a*")
  75. if !ok || ot.b.String() != "" {
  76. t.Error("Unexpected result:", ot.b.String(), ok)
  77. return
  78. }
  79. ok = matchesFulltextSearch(ot, "abc", "ac*")
  80. if ok || ot.b.String() != "" {
  81. t.Error("Unexpected result:", ot.b.String(), ok)
  82. return
  83. }
  84. }
  85. func TestFillTableRow(t *testing.T) {
  86. res := fillTableRow([]string{}, "test", stringutil.GenerateRollingString("123 ", 100))
  87. b, _ := json.Marshal(&res)
  88. if string(b) != `["test","123 123 123 123 123 123 123 123 123 123 123 123 `+
  89. `123 123 123 123 123 123 123 123","","123 123 123 123 123","",""]` {
  90. t.Error("Unexpected result:", string(b))
  91. return
  92. }
  93. }