123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- /*
- * ECAL
- *
- * Copyright 2020 Matthias Ladkau. All rights reserved.
- *
- * This Source Code Form is subject to the terms of the MIT
- * License, If a copy of the MIT License was not distributed with this
- * file, You can obtain one at https://opensource.org/licenses/MIT.
- */
- package tool
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "strings"
- "testing"
- "devt.de/krotik/common/stringutil"
- "devt.de/krotik/common/termutil"
- )
- type testConsoleLineTerminal struct {
- in []string
- out bytes.Buffer
- }
- func (t *testConsoleLineTerminal) StartTerm() error {
- return nil
- }
- func (t *testConsoleLineTerminal) AddKeyHandler(handler termutil.KeyHandler) {
- }
- func (t *testConsoleLineTerminal) NextLine() (string, error) {
- var err error
- var ret string
- if len(t.in) > 0 {
- ret = t.in[0]
- t.in = t.in[1:]
- } else {
- err = fmt.Errorf("Input is empty in testConsoleLineTerminal")
- }
- return ret, err
- }
- func (t *testConsoleLineTerminal) NextLinePrompt(prompt string, echo rune) (string, error) {
- return t.NextLine()
- }
- func (t *testConsoleLineTerminal) WriteString(s string) {
- t.out.WriteString(s)
- }
- func (t *testConsoleLineTerminal) Write(p []byte) (n int, err error) {
- return t.out.Write(p)
- }
- func (t *testConsoleLineTerminal) StopTerm() {
- }
- type testCustomHandler struct {
- }
- func (t *testCustomHandler) CanHandle(s string) bool {
- return s == "@cus"
- }
- func (t *testCustomHandler) Handle(ot OutputTerminal, input string) {}
- func (t *testCustomHandler) LoadInitialFile(tid uint64) error {
- return nil
- }
- type testOutputTerminal struct {
- b bytes.Buffer
- }
- func (t *testOutputTerminal) WriteString(s string) {
- t.b.WriteString(s)
- }
- func TestMatchesFulltextSearch(t *testing.T) {
- ot := &testOutputTerminal{}
- ok := matchesFulltextSearch(ot, "abc", "s[")
- if !ok && strings.HasPrefix(ot.b.String(), "Invalid search expression") {
- t.Error("Unexpected result:", ot.b.String(), ok)
- return
- }
- ot.b = bytes.Buffer{}
- ok = matchesFulltextSearch(ot, "abc", "a*")
- if !ok || ot.b.String() != "" {
- t.Error("Unexpected result:", ot.b.String(), ok)
- return
- }
- ok = matchesFulltextSearch(ot, "abc", "ac*")
- if ok || ot.b.String() != "" {
- t.Error("Unexpected result:", ot.b.String(), ok)
- return
- }
- }
- func TestFillTableRow(t *testing.T) {
- res := fillTableRow([]string{}, "test", stringutil.GenerateRollingString("123 ", 100))
- b, _ := json.Marshal(&res)
- if string(b) != `["test","123 123 123 123 123 123 123 123 123 123 123 123 `+
- `123 123 123 123 123 123 123 123","","123 123 123 123 123","",""]` {
- t.Error("Unexpected result:", string(b))
- return
- }
- }
|