Skip to content

Commit

Permalink
improving relative line numbers, and testing the getLineRelativeTo fu…
Browse files Browse the repository at this point in the history
…nction
  • Loading branch information
quintijn committed Oct 22, 2022
1 parent 8d75c68 commit c33b52c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
32 changes: 1 addition & 31 deletions src/unimacro/_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# _control.py, adapted version of_gramutils.py
# Author: Bart Jan van Os, Version: 1.0, nov 1999
# starting new version Quintijn Hoogenboom, August 2003
#pylint:disable=C0115, C0116, W0702, R0904, R0911, R0912, R0914, R0915, W0201, W0613, W0107
#pylint:disable=C0115, C0116, W0702, R0904, R0911, R0912, R0914, R0915, W0201, W0613, W0107, C0209, E0601, W0602
#pylint:disable=E1101

import os
Expand Down Expand Up @@ -38,38 +38,10 @@

tracecount = list(map(str, list(range(1, 10))))

#Words that are 'filtered out' (actually: removed) in Filter Mode
#See below for the different modes
def ReadFilteredWords(Filename):
#reads all words from the Filtered words file
#does not really belong here
try:
File=open(Filename,'r')
except:
return []
Words = File.readlines()
File.close()
for w in Words:
Words[Words.index(w)]=w[:-1]
freq={}
for w in Words:
if w in freq:
freq[w]=freq[w]+1
else:
freq[w]=1
Words=list(freq.keys())
return Words

FilteredWords = ['in','the','minimum','to','and','end','a','of','that','it',
'if', 'its', 'is', 'this', 'booth', 'on', 'with',"'s"]
#(taken from natlinkmain, to prevent import:)
baseDirectory = status.getUnimacroUserDirectory()
unimacroDirectory = status.getUnimacroDirectory()

FilterFileName=baseDirectory+'\\filtered.txt'
FilteredWords=natbj.Union(FilteredWords, ReadFilteredWords(FilterFileName))


#Constants for the UtilGrammar
Normal=0
Training=1 #obsolete
Expand Down Expand Up @@ -651,8 +623,6 @@ def offInfo(self, grammar):
actions.Message(t)

def UnimacroControlPostLoad(self):
newKeys = natbj.getRegisteredGrammarNames()
# print(f'_control, postLoad: newKeys: {newKeys}')
prevSet = set(self.Lists['gramnames'])
newSet = set(natbj.getRegisteredGrammarNames())
if prevSet != newSet:
Expand Down
46 changes: 46 additions & 0 deletions tests/test_grammar_lines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pylint:disable=E1101
from pathlib import Path
import pytest
import natlink
from unimacro.UnimacroGrammars import _lines
from natlinkcore import natlinkstatus
status = natlinkstatus.NatlinkStatus

thisDir = Path(__file__).parent

def test_getLineRelativeTo():
"""test the relative to (modulo 100 or modulo 10) trick for getting line numbers
"""
modulo = 10
current, relative, expect = 55, 2, 52
result = _lines.getLineRelativeTo(relative, currentLine=current, modulo=modulo)
assert result == expect

current, relative, expect = 355, 1, 351
result = _lines.getLineRelativeTo(relative, currentLine=current, modulo=modulo)
assert result == expect

current, relative, expect = 55, 0, 60
result = _lines.getLineRelativeTo(relative, currentLine=current, modulo=modulo)
assert result == expect

current, relative, expect = 355, 0, 360
result = _lines.getLineRelativeTo(relative, currentLine=current, modulo=modulo)
assert result == expect

modulo = 100
current, relative, expect = 55, 3, 103
result = _lines.getLineRelativeTo(relative, currentLine=current, modulo=modulo)
assert result == expect

current, relative, expect = 255, 9, 209
result = _lines.getLineRelativeTo(relative, currentLine=current, modulo=modulo)
assert result == expect

current, relative, expect = 251, 1, 301
result = _lines.getLineRelativeTo(relative, currentLine=current, modulo=modulo)
assert result == expect v


if __name__ == "__main__":
pytest.main(['test_grammar_lines.py'])

0 comments on commit c33b52c

Please sign in to comment.