Commit 268ab8ee authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Create test cases for the random value generator

parent d1cca318
#!/usr/bin/env python
import sys
import re
sys.path.insert(0, '../..')
from asn1_value_editor.ValueGenerator import (compute_random_value,
compute_combinations)
from asn1_value_editor.vn import fromValueNotationToPySide as parse_gser
from opengeode import Asn1scc as asn1scc
'''
Use py.test-2.7 to run these tests
'''
TEST = ['data/dv1.asn']
dataview = asn1scc.parse_asn1(TEST,
rename_policy=asn1scc.ASN1.RenameOnlyConflicting,
ast_version=asn1scc.ASN1.UniqueEnumeratedNames,
flags=[asn1scc.ASN1.AstOnly])
pool = dataview.types
def test_random_bool(qtbot):
''' Test boolean values '''
typeName = "Type-SingleBool"
results = []
for i in range(100):
result = compute_random_value(pool[typeName], pool)
assert result in ('TRUE', 'FALSE')
results.append(result)
assert 'TRUE' in results and 'FALSE' in results
def test_random_int(qtbot):
''' Test integer value '''
typeName = "Type-SingleInt"
for i in range(1000):
result = compute_random_value(pool[typeName], pool)
assert 0 <= int(result) <= 255
def test_random_real(qtbot):
''' Test float value '''
typeName = "Type-SingleReal"
for i in range(1000):
result = compute_random_value(pool[typeName], pool)
assert -5.0 <= float(result) <= 5.0
def test_random_enum(qtbot):
''' Test enumerated value '''
typeName = "Type-SingleEnum"
results = []
for i in range(100):
result = compute_random_value(pool[typeName], pool)
assert result in ('enum-one', 'enum-two')
results.append(result)
assert 'enum-one' in results and 'enum-two' in results
def test_random_string(qtbot):
''' Test octet string value '''
typeName = "Type-SingleString"
for i in range(1000):
result = compute_random_value(pool[typeName], pool)
assert re.sub('[X\"]', '', result) == '' and 2 <= len(result) <= 22
def test_random_simpleseq(qtbot):
''' Test SEQUENCE '''
typeName = "Type-SimpleSeq"
for i in range(1000):
result = compute_random_value(pool[typeName], pool)
assert result.startswith("{") and result.endswith("}") and \
'item-a' in result and 'item-b' in result and 'item-C' \
in result and 'item-d' in result
def test_random_simplechoice(qtbot):
''' Test CHOICE. '''
typeName = "Type-SingleChoice"
for i in range(1000):
result = compute_random_value(pool[typeName], pool)
if 'choice-B' in result:
assert 'TRUE' in result or 'FALSE' in result
elif 'choice-C' in result:
assert 'enum-ONE' in result or 'enum-TWO' in result or \
'enum-THREE' in result
elif 'choice-A' in result:
pass
else:
assert False, 'No valid choice in result'
def test_random_simpleseqof(qtbot):
''' Test SEQOF '''
typeName = "Type-SingleSeqOf"
for i in range(1000):
result = compute_random_value(pool[typeName], pool)
value = parse_gser('result', result)['result']
assert 0 <= len(value) <= 5
for each in value:
assert type(each) is int and 0 <= each <= 255
def test_random_mydata(qtbot):
''' Test more complex sequence '''
typeName = "MyData"
for i in range(1000):
result = compute_random_value(pool[typeName], pool)
value = parse_gser('result', result)['result']
assert type(value) is list and 1 <= len(value) <= 10
for item in value:
assert 'a' in item and 'b' in item
assert type(item['a']) is list and len(item['a']) == 10
for each in item['a']:
assert type(each) is int and 0 <= each <= 255
assert item['b']['Enum'] in ('enum-one', 'enum-two')
def test_random_seq2(qtbot):
''' Test more complex sequence '''
typeName = "Type-Seq"
for i in range(1000):
result = compute_random_value(pool[typeName], pool)
value = parse_gser('result', result)['result']
assert 'item-a' in value and 'item-b' in value and 'item-C' in value \
and 'item-d' in value
assert type(value['item-a']) is float and -5.0 <= value['item-a'] <= 5.0
assert value['item-b']['Choice'] in ('choice-A', 'choice-B', 'choice-C')
if __name__ == '__main__':
print('You must run py.test-2.7 to execute this test script')
print('Make sure you have pytest-qt (pip install --user pytest-qt)')
sys.exit(1)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment