Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: memperbaiki unittesting #1

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,40 @@ Algoritma adalah satu atau lebih fungsi dan/atau kelas yang:
- melakukan beberapa internal kalkulasi atau manipulasi data;
- mengembalikan satu atau lebih nilai hasil;

## Unittesting

Kami menggunakan kustom unittesting yang sudah terkonfigurasi di `config.nims` yang dimana tiap file kamu hanya menambahkan `suite test dari nim sebagai contoh`

```nim

# unittesting ini ditambahkan di akhir setelah inisialisasi
# fungsi sudah dibuat
# ketika file dijalankan sebagain main
when isMainModule:
# import lib unittest
import std/[unittest]

suite "Testing Beberapa Kasus":
test "test fungsi pertama":
check:
testFungsiPertama(10) == 10
testFungsiKedua(1) != 2

suite "Testing Kasus Lain":
test "test fungsi lain":
check:
testFungsiPertama(20) != 300

test "test fungsi jika valuenya lain":
doAssertRaises(ValueError):
discard testFungsiPertama(3.0)
```
Setelah itu lakukan perintah
```
nim config.nims
nim test
```

## Tambahan Perubahan

Jika ingin menambahkan algoritma atau *script* Nim yang sederhana atau menambahkan kode yang sederhana, kamu bisa menambahkan perubahan di folder `other`. Jika terdapat beberapa *file*, sebaiknya *file-file* tersebut ke dalam folder sesuai dengan nama *script* tersebut sebagai contoh:
Expand Down
49 changes: 49 additions & 0 deletions config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
if defined(release) or defined(danger):
--opt: speed
--passC: "-flto"
--passL: "-flto"
--passL: "-s"
else:
--checks: on
--assertions: on
--spellSuggest
--styleCheck: error

--mm:arc

import std/[os, sequtils]
from std/strutils import startsWith, endsWith
from std/strformat import `&`

const IgnorePathPrefixes = ["."]

func isIgnored(path: string): bool =
IgnorePathPrefixes.mapIt(path.startsWith(it)).anyIt(it)

iterator modules(dir: string = getCurrentDir()): string =
for path in walkDirRec(dir, relative = true):
if not path.isIgnored() and path.endsWith(".nim"):
yield path

############ Tasks
task test, "Test semuanya":
--warning: "BareExcept:off"
--hints: off
var failedUnits: seq[string]

for path in modules():
echo &"Testing {path}:"
try: selfExec(&"-f --warning[BareExcept]:off --hints:off r \"{path}\"")
except OSError:
failedUnits.add(path)
if failedUnits.len > 0:
echo "Test yang Gagal:"
for path in failedUnits:
echo &"- {path}"
quit(1)
else:
echo "Semua Test Berjalan dengan baik"

task prettyfy, "Run nimpretty untuk semua file":
for path in modules():
exec(&"nimpretty --indent:2 \"{path}\"")
4 changes: 2 additions & 2 deletions math/abs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func absVal*[T: SomeInteger](angka: T): Natural = (if angka <
0: -angka else: angka)

func absMin*(x: openArray[int]): Natural {.raises: [ValueError].} =
# mengembalikan nilai element terkecil dari nilai absolute dalam sebuah sekuens
# mengembalkan nilai element terkecil dari nilai absolute dalam sebuah sekuens
if x.len == 0:
raise newException(ValueError, "Tidak bisa menemukan nilai absolut terkecil")
result = absVal(x[0])
Expand Down Expand Up @@ -56,7 +56,7 @@ when isMainModule:
test "test fungsi absMax":
check:
absMax(@[0, 5, 1, 11]) == 11
absMax(@[-1, 2, -3]) = 3
absMax(@[-1, 2, -3]) == 3

test "`absMax` jika sekuensnya kosong":
doAssertRaises(ValueError):
Expand Down