Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
moigagoo committed Jan 28, 2025
1 parent 0c14794 commit 939a47d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ demo/battleship/battleship
tests/megatest.nim
tests/megatest

tests/tutils
tests/basicmath
tests/tbasicmath

23 changes: 23 additions & 0 deletions tests/basicmath.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import std/strutils

import climate


proc sum(context: Context): int =
context.args:
if len(args) != 2:
echo "The command expects exactly two arguments"
return 1

echo parseInt(args[0]) + parseInt(args[1])

do:
echo "The command expects exactly two arguments"
return 2

proc root(context: Context): int =
echo "Available commands: sum."


quit parseCommands({"sum": sum}, defaultHandler = root)

41 changes: 41 additions & 0 deletions tests/tbasicmath.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import std/[unittest, os, osproc, strutils, strformat]


const clifile = "tests" / "basicmath"

suite "Basic CLI":
discard execCmd("nim c " & clifile)

test "Run command with two arguments":
const
a = 2
b = 3

let (output, code) = execCmdEx &"{findExe(clifile)} sum {a} {b}"

check code == 0
check parseInt(strip(output)) == a + b

test "Run command with too many arguments":
const
a = 2
b = 3
c = 4

let (output, code) = execCmdEx &"{findExe(clifile)} sum {a} {b} {c}"

check code == 1
check (strip(output)) == "The command expects exactly two arguments"

test "Run command without arguments":
let (output, code) = execCmdEx &"{findExe(clifile)} sum"

check code == 2
check (strip(output)) == "The command expects exactly two arguments"

test "Run without command":
let (output, code) = execCmdEx &"{findExe(clifile)}"

check code == 0
check (strip(output)) == "Available commands: sum."

12 changes: 0 additions & 12 deletions tests/test1.nim

This file was deleted.

0 comments on commit 939a47d

Please sign in to comment.