def test_scan_identifiers(): scanner = Scanner("equity cash end_of_the_world_fund") assert list(scanner) == [ Token(Type.IDENT, "equity"), Token(Type.IDENT, "cash"), Token(Type.IDENT, "end_of_the_world_fund") ]
def test_scan_currency_2(): scanner = Scanner("1000 -1.0 0.5 -123.45") assert list(scanner) == [ Token(Type.CURRENCY, 100000), Token(Type.CURRENCY, -100), Token(Type.CURRENCY, 50), Token(Type.CURRENCY, -12345) ]
def test_scan_currency(): scanner = Scanner("100 100.00 100.42 -123.45") assert list(scanner) == [ Token(Type.CURRENCY, 10000), Token(Type.CURRENCY, 10000), Token(Type.CURRENCY, 10042), Token(Type.CURRENCY, -12345) ]
def test_scan_identifiers_2(): scanner = Scanner("cash savings expenses rent bills") assert list(scanner) == [ Token(Type.IDENT, "cash"), Token(Type.IDENT, "savings"), Token(Type.IDENT, "expenses"), Token(Type.IDENT, "rent"), Token(Type.IDENT, "bills") ]
def test_scan_keywords(): scanner = Scanner('''TrAnsFer transfer OPEN BALANCE balance''') toks = [ Token(Type.TRANSFER, "TrAnsFer"), Token(Type.TRANSFER, "transfer"), Token(Type.OPEN, "OPEN"), Token(Type.BALANCE, "BALANCE"), Token(Type.BALANCE, "balance") ] assert [tok for tok in scanner] == toks # iterate a second time assert [tok for tok in scanner] == toks
def ledger(userInput): if (userInput == ""): return [] else: valueDic = {} tokenList = [] ScannerValues = Scanner(userInput) for tok in ScannerValues : tokenList.append(tok) i = 0 while (i < len(tokenList)): if (tokenList[i].type == Type.OPEN): if (tokenList[i+1].type == Type.IDENT ): if (tokenList[i+2].type == Type.CURRENCY): valueDic[tokenList[i+1].value] = tokenList[i+2].value i = i +3 else: valueDic[tokenList[i+1].value] = 0 i = i+2 elif (tokenList[i]. type == Type.TRANSFER): accFrom = tokenList[i+1].value accTo =tokenList[i+2].value value= (float)(tokenList[i+3].value) valueDic[accFrom] = valueDic[accFrom] - value valueDic[accTo] = valueDic[accTo] + value print (valueDic[accFrom] , accFrom) print (valueDic[accTo], accTo) i = i+4 elif (tokenList[i].type == Type.BALANCE): if (tokenList[i+1].value not in valueDic.keys() ): yield (tokenList[i+1].value,0) else : yield (tokenList[i+1].value,valueDic[tokenList[i+1].value]) i= i+2 else : print (tokenList[i]) raise ValueError("Unexpected Token")
def test_scan_bad_3(): scanner = Scanner("!NO") with pytest.raises(ValueError, match="!NO"): next(scanner)
def test_scan_bad_2(): scanner = Scanner("@FAIL") with pytest.raises(ValueError, match="@FAIL"): next(scanner)
def test_scan_bad(): scanner = Scanner("&crash") with pytest.raises(ValueError, match="&crash"): next(scanner)