Skip to content

Commit

Permalink
Add Unit tests 🧪 for VST Maker (#49)
Browse files Browse the repository at this point in the history
* added basic tests

* Added tests for all the methods exposed in the VST Maker

* Simply ported the test cases from [libvarnam](https://github.com/varnamproject/libvarnam/blob/master/tests/vst-compilation.c).
* Prefix tree test is pending since the API itself is incomplete.

* Bumps runner image to ubuntu-20.04

* Removed commented line
  • Loading branch information
anoopmsivadas authored Nov 9, 2023
1 parent f95b043 commit 648da8a
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
jobs:

build:
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
env:
VARNAM_UPSTREAM: https://varnam.subinsb.com

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
include:
- os: ubuntu-18.04
- os: ubuntu-20.04
file: "*.zip"

steps:
Expand Down
234 changes: 230 additions & 4 deletions govarnam/vst_maker_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,234 @@
package govarnam

import "testing"
import (
"context"
"path"
"strings"
"testing"
)

func TestVM(t *testing.T) {
// TODO
// varnam := getVarnamInstance("ml")
// initialize VST Maker for tests
func initTestVM() (*Varnam, error) {
return VMInit(path.Join(testTempDir, "test.vst"))
}

func TestCreatTokenWithoutBuffering(t *testing.T) {
varnam, err := initTestVM()
checkError(err)

err = varnam.VMCreateToken("pattern", "value1", "value2", "value3", "tag", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_EXACT, 0, 0, false)
checkError(err)

search := NewSearchSymbol()
search.Pattern = "pattern"
symbols, err := varnam.SearchSymbolTable(context.Background(), search)
checkError(err)
assertEqual(t, len(symbols) == 1, true)
}

func TestCreatTokenWithBuffering(t *testing.T) {
varnam, err := initTestVM()

checkError(err)
err = varnam.VMCreateToken("pattern1", "value1", "value2", "", "value3", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_EXACT, 0, 0, true)
checkError(err)

search := NewSearchSymbol()
search.Pattern = "pattern1"
symbols, err := varnam.SearchSymbolTable(context.Background(), search)
checkError(err)
assertEqual(t, len(symbols) == 1, true)

err = varnam.VMFlushBuffer()
checkError(err)
}

func TestDeleteToken(t *testing.T) {
varnam, err := initTestVM()
checkError(err)

search := NewSearchSymbol()
search.Pattern = "pattern"
err = varnam.VMDeleteToken(search)
checkError(err)

symbols, err := varnam.SearchSymbolTable(context.Background(), search)
checkError(err)
assertEqual(t, len(symbols) == 0, true)

search.Pattern = "pattern1"
err = varnam.VMDeleteToken(search)
checkError(err)

symbols, err = varnam.SearchSymbolTable(context.Background(), search)
checkError(err)
assertEqual(t, len(symbols) == 0, true)
}

func TestGetAllTokens(t *testing.T) {
varnam, err := initTestVM()
checkError(err)

err = varnam.VMCreateToken("pattern", "value1", "value2", "", "", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_EXACT, 0, 0, false)
checkError(err)

err = varnam.VMCreateToken("pattern1", "value11", "value21", "", "", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_EXACT, 0, 0, false)
checkError(err)

err = varnam.VMCreateToken("pattern2", "value12", "value22", "", "", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_EXACT, 0, 0, false)
checkError(err)

varnam.VSTMakerConfig.UseDeadConsonants = false

err = varnam.VMCreateToken("r", "v", "v", "", "", VARNAM_SYMBOL_CONSONANT, VARNAM_MATCH_EXACT, 0, 0, false)
checkError(err)

err = varnam.VMCreateToken("r", "v12", "v", "", "", VARNAM_SYMBOL_CONSONANT, VARNAM_MATCH_POSSIBILITY, 0, 0, false)
checkError(err)

search := NewSearchSymbol()
search.Type = VARNAM_SYMBOL_VOWEL

symbols, err := varnam.SearchSymbolTable(context.Background(), search)
checkError(err)
assertEqual(t, len(symbols) == 3, true)

search.Type = VARNAM_SYMBOL_CONSONANT
symbols, err = varnam.SearchSymbolTable(context.Background(), search)
checkError(err)
assertEqual(t, len(symbols) == 2, true)
}

func TestSetSchemeDetails(t *testing.T) {
varnam, err := initTestVM()
checkError(err)
sd := SchemeDetails{
LangCode: "tl",
DisplayName: "Test",
Author: "Anon",
}
err = varnam.VMSetSchemeDetails(sd)
checkError(err)
}

func TestIgnoreDuplicates(t *testing.T) {
varnam, err := initTestVM()
checkError(err)

varnam.VSTMakerConfig.IgnoreDuplicateTokens = false

// should succeed
err = varnam.VMCreateToken("pattern21", "value1", "value2", "value3", "tag", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_EXACT, 0, 0, false)
checkError(err)

// should return error
err = varnam.VMCreateToken("pattern21", "value1", "value2", "value3", "tag", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_EXACT, 0, 0, false)
assertEqual(t, err != nil, true)

// Ignore Duplicate Tokens
varnam.VSTMakerConfig.IgnoreDuplicateTokens = true

err = varnam.VMCreateToken("pattern21", "value1", "value2", "value3", "tag", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_EXACT, 0, 0, false)
checkError(err)
}

func TestCreateDeadConsonants(t *testing.T) {
varnam, err := initTestVM()
checkError(err)

// Use dead consonants
varnam.VSTMakerConfig.UseDeadConsonants = true

err = varnam.VMCreateToken("~", "്", "", "tag", "", VARNAM_SYMBOL_VIRAMA, VARNAM_MATCH_EXACT, 1, 0, false)
checkError(err)

err = varnam.VMCreateToken("ka", "ക", "", "tag", "", VARNAM_SYMBOL_CONSONANT, VARNAM_MATCH_EXACT, 1, 0, false)
checkError(err)

err = varnam.VMCreateToken("p", "പ്", "", "tag", "value3", VARNAM_SYMBOL_CONSONANT, VARNAM_MATCH_EXACT, 1, 0, false)
checkError(err)

varnam.VMFlushBuffer()

search := NewSearchSymbol()
search.Type = VARNAM_SYMBOL_DEAD_CONSONANT

symbols, err := varnam.SearchSymbolTable(context.Background(), search)
checkError(err)

deadConsonantFound := false
for _, sym := range symbols {
if sym.Pattern == "k" {
deadConsonantFound = true
}
}
assertEqual(t, deadConsonantFound, true)
}

func TestCreateExactMatchDuplicates(t *testing.T) {
varnam, err := initTestVM()
checkError(err)

// should be success
err = varnam.VMCreateToken("patterna", "value1", "value2", "value3", "tag", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_EXACT, 0, 0, false)
checkError(err)

// disable IgnoreDuplicateTokens flag
varnam.VSTMakerConfig.IgnoreDuplicateTokens = false

// should return error
err = varnam.VMCreateToken("patterna", "value1", "value2", "value3", "tag", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_EXACT, 0, 0, false)
assertEqual(t, err != nil, true)
}

func TestCreatePossibilityMatchDuplicates(t *testing.T) {
varnam, err := initTestVM()
checkError(err)

// disable IgnoreDuplicateTokens flag
varnam.VSTMakerConfig.IgnoreDuplicateTokens = false

err = varnam.VMCreateToken("pattern3", "value1", "value2", "", "value3", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_POSSIBILITY, 0, 0, false)
checkError(err)

// should be allowed since it has different values
err = varnam.VMCreateToken("pattern3", "value11", "value22", "", "value3", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_POSSIBILITY, 0, 0, false)
checkError(err)

// should return error
err = varnam.VMCreateToken("pattern3", "value1", "value2", "", "value3", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_POSSIBILITY, 0, 0, false)
assertEqual(t, err != nil, true)
}

func TestRestrictInvalidMatchTypes(t *testing.T) {
varnam, err := initTestVM()
checkError(err)

// should return error
err = varnam.VMCreateToken("pattern0", "value1", "value2", "", "value3", VARNAM_SYMBOL_VOWEL, 11, 0, 0, false)
assertEqual(t, err != nil, true)
}

func TestMaxLengthCheck(t *testing.T) {
varnam, err := initTestVM()
checkError(err)

pattern := strings.Builder{}
pattern.Grow(VARNAM_SYMBOL_MAX + 2)

value1 := strings.Builder{}
value1.Grow(VARNAM_SYMBOL_MAX + 2)

value2 := strings.Builder{}
value2.Grow(VARNAM_SYMBOL_MAX + 2)

// should return error
err = varnam.VMCreateToken(pattern.String(), value1.String(), value2.String(), "", "value3", VARNAM_SYMBOL_VOWEL, VARNAM_MATCH_EXACT, 0, 0, false)
assertEqual(t, err != nil, true)
}

// TODO: incomplete API
func TestPrefixTree(t *testing.T) {
// varnam, err := initTestVM()
// checkError(err)
}

0 comments on commit 648da8a

Please sign in to comment.