Skip to content

Commit

Permalink
tests: updated unit tests to the new version of UserInput
Browse files Browse the repository at this point in the history
FossilOrigin-Name: f4244ce4eadf115b85a366206340b8d43942631ddf6f37f9a134ae74ef25efdf
  • Loading branch information
thindil committed Jan 19, 2024
1 parent 14cc3a6 commit 771a533
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
32 changes: 14 additions & 18 deletions tests/completion.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/[os, tables]
import utils/utils
import ../src/[aliases, completion, commandslist, constants, db, lstring, resultcode]
import ../src/[aliases, completion, commandslist, constants, db, resultcode]
import norm/sqlite
import unittest2

Expand All @@ -9,7 +9,7 @@ suite "Unit tests for completion module":
checkpoint "Initializing the tests"
let db = initDb("test5.db")
var
myaliases = newOrderedTable[LimitedString, int]()
myaliases = newOrderedTable[string, int]()
commands = newTable[string, CommandData]()
completions: seq[string]

Expand Down Expand Up @@ -48,60 +48,56 @@ suite "Unit tests for completion module":
test "Getting the shell's completion ID":
checkpoint "Getting ID of an existing completion"
check:
getCompletionId(initLimitedString(capacity = 8, text = "delete 1"),
getCompletionId("delete 1",
db).int == 1
checkpoint "Getting ID of a non-existing completion"
check:
getCompletionId(initLimitedString(capacity = 9, text = "delete 22"),
getCompletionId("delete 22",
db).int == 0

test "Listing the defined commands' completions":
check:
listCompletion(initLimitedString(capacity = 4, text = "list"), db) == QuitSuccess
listCompletion("list", db) == QuitSuccess

test "Deleting a command's completion":
checkpoint "Deleting an existing completion"
check:
deleteCompletion(initLimitedString(capacity = 8, text = "delete 1"),
deleteCompletion("delete 1",
db) == QuitSuccess
db.count(Completion) == 0
var completion = newCompletion(command = "ala", cType = custom,
cValues = "something")
db.insert(completion)
checkpoint "Deleting a non-existing completion"
check:
deleteCompletion(initLimitedString(capacity = 8, text = "delete 2"),
deleteCompletion("delete 2",
db) == QuitFailure
db.count(Completion) == 1

test "Show a command's completion":
checkpoint "Showing an existing completion"
check:
showCompletion(initLimitedString(capacity = 6, text = "show 1"), db) == QuitSuccess
showCompletion("show 1", db) == QuitSuccess
checkpoint "Showing a non-existing completion"
check:
showCompletion(initLimitedString(capacity = 6, text = "show 2"), db) == QuitFailure
showCompletion("show 2", db) == QuitFailure

test "Exporting a command's completion":
checkpoint "Exporting an existing completion"
check:
exportCompletion(initLimitedString(capacity = 17,
text = "export 1 test.txt"), db) == QuitSuccess
exportCompletion("export 1 test.txt", db) == QuitSuccess
checkpoint "Exporting a non-existing completion"
check:
exportCompletion(initLimitedString(capacity = 17,
text = "export 2 test.txt"), db) == QuitFailure
exportCompletion("export 2 test.txt", db) == QuitFailure

test "Importing a command's completion":
checkpoint "Importing a new completion"
discard deleteCompletion(initLimitedString(capacity = 8, text = "delete 1"), db)
discard deleteCompletion("delete 1", db)
check:
importCompletion(initLimitedString(capacity = 15,
text = "import test.txt"), db) == QuitSuccess
importCompletion("import test.txt", db) == QuitSuccess
checkpoint "Importing an existing completion"
check:
importCompletion(initLimitedString(capacity = 15,
text = "import test.txt"), db) == QuitFailure
importCompletion("import test.txt", db) == QuitFailure

suiteTeardown:
closeDb(QuitSuccess.ResultCode, db)
17 changes: 7 additions & 10 deletions tests/input.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import std/parseopt
import utils/utils
import ../src/[constants, input, lstring]
import ../src/[constants, input]
import unittest2
when defined(testInput):
import ../src/theme
import nimalyzer

suite "Unit tests for input module":

Expand All @@ -16,16 +17,15 @@ suite "Unit tests for input module":
conjCommands: bool = true
arguments: UserInput = getArguments(userCommand, conjCommands)
check:
arguments == initLimitedString(capacity = maxInputLength,
text = "ls -ab --foo --bar=20 file.txt")
arguments == "ls -ab --foo --bar=20 file.txt"

test "Reading the user's input":
when not defined(testInput):
skip()
else:
echo "exit"
check:
readInput(db = db) == initLimitedString(capacity = maxInputLength, text = "exit")
readInput(db = db) == "exit"

test "Reading a character from the user's input":
checkpoint "Reading a lowercase character"
Expand All @@ -37,26 +37,23 @@ suite "Unit tests for input module":

test "Deleting a character":
var
inputString = initLimitedString(capacity = maxInputLength,
text = "my text")
inputString = "my text"
cursorPosition: Natural = 1
deleteChar(inputString, cursorPosition, db)
check:
inputString == "y text"
cursorPosition == 0

test "Moving the cursor":
let inputString = initLimitedString(capacity = maxInputLength,
text = "my text")
let inputString = "my text"
var cursorPosition: Natural = 1
moveCursor('D', cursorPosition, inputString, db)
check:
cursorPosition == 0

test "Updating the user's input":
var
inputString = initLimitedString(capacity = maxInputLength,
text = "my text")
inputString = "my text"
cursorPosition: Natural = 7
updateInput(cursorPosition, inputString, false, "a", db)
check:
Expand Down
4 changes: 2 additions & 2 deletions tests/suggestion.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import std/tables
import ../src/[commandslist, lstring, suggestion]
import ../src/[commandslist, suggestion]
import utils/utils
import unittest2

Expand All @@ -8,7 +8,7 @@ suite "Unit tests for suggestion module":
checkpoint "Initializing the tests"
let db = initDb("test14.db")
var
myaliases = newOrderedTable[LimitedString, int]()
myaliases = newOrderedTable[string, int]()
commands = newTable[string, CommandData]()

checkpoint "Adding testing aliases if needed"
Expand Down

0 comments on commit 771a533

Please sign in to comment.