test_standalone.py 3.99 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
2
3
#!/usr/bin/env python

import sys
4
from os.path import dirname
Maxime Perrotin's avatar
Maxime Perrotin committed
5
6
7

sys.path.insert(0, '../..')
from asn1_value_editor import standalone_editor as editor
8
from asn1_value_editor.vn import format_gser
Maxime Perrotin's avatar
Maxime Perrotin committed
9
10
editor.logger.setLevel(editor.logging.DEBUG)

11
'''
Maxime Perrotin's avatar
Maxime Perrotin committed
12
Use py.test-2.7 to run these tests, and make sure you have installed pytest-qt
13
14
15
pip install --user pytest-qt
'''

16
TEST = editor.SingleValueEditor(dirname(__file__) + '/data/dv1.asn')
Maxime Perrotin's avatar
Maxime Perrotin committed
17
18

def common(typeName, defValue):
Maxime Perrotin's avatar
Maxime Perrotin committed
19
    ''' Set up test case: create asn1 editor and fill with default value '''
Maxime Perrotin's avatar
Maxime Perrotin committed
20
    widget = TEST.setupEdit(typeName, defValue=defValue)['editor']
Maxime Perrotin's avatar
Maxime Perrotin committed
21
22
23
24
25
26
    # Create a new type instance to put the result, so that we make sure
    # that the result value is really taken from the ASN.1 editor
    dest_instance = getattr(TEST.asn1ctypes, typeName.replace('-', '_'))()
    widget.getVariable(dest=dest_instance)
    assert widget.asn1Instance.GSER() == dest_instance.GSER()
    return dest_instance.GSER()
Maxime Perrotin's avatar
Maxime Perrotin committed
27

Maxime Perrotin's avatar
Maxime Perrotin committed
28
29
def test_bool(qtbot):
    ''' Test boolean values '''
30
    typeName = "Type-SingleBool"
Maxime Perrotin's avatar
Maxime Perrotin committed
31
    defValue = 'TRUE'
Maxime Perrotin's avatar
Maxime Perrotin committed
32
    result = common(typeName, defValue)
Maxime Perrotin's avatar
Maxime Perrotin committed
33
    assert result == defValue
34

Maxime Perrotin's avatar
Maxime Perrotin committed
35
    defValue = 'FALSE'
Maxime Perrotin's avatar
Maxime Perrotin committed
36
    result = common(typeName, defValue)
Maxime Perrotin's avatar
Maxime Perrotin committed
37
38
    assert result == defValue

Maxime Perrotin's avatar
Maxime Perrotin committed
39
40
def test_int(qtbot):
    ''' Test integer value '''
Maxime Perrotin's avatar
Maxime Perrotin committed
41
42
    typeName = "Type-SingleInt"
    defValue = '42'
Maxime Perrotin's avatar
Maxime Perrotin committed
43
    result = common(typeName, defValue)
Maxime Perrotin's avatar
Maxime Perrotin committed
44
    assert result == defValue
Maxime Perrotin's avatar
Maxime Perrotin committed
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def test_real(qtbot):
    ''' Test float value '''
    typeName = "Type-SingleReal"
    defValue = '1.25'
    result = common(typeName, defValue)
    assert result == defValue

def test_enum(qtbot):
    ''' Test enumerated value '''
    typeName = "Type-SingleEnum"
    defValue = 'enum-one'
    result = common(typeName, defValue)
    assert result == defValue

    defValue = 'enum-two'
    result = common(typeName, defValue)
    assert result == defValue

def test_string(qtbot):
    ''' Test octet string value '''
    typeName = "Type-SingleString"
    defValue = '"Hello, world"'
    result = common(typeName, defValue)
    assert result == defValue

Maxime Perrotin's avatar
Maxime Perrotin committed
71
def test_simpleseq(qtbot):
Maxime Perrotin's avatar
Maxime Perrotin committed
72
73
    ''' Test SEQUENCE '''
    typeName = "Type-SimpleSeq"
Maxime Perrotin's avatar
Maxime Perrotin committed
74
    defValue = '{item-a 42, item-b TRUE, item-C enum-two, item-d "Hello world"}'
Maxime Perrotin's avatar
Maxime Perrotin committed
75
    result = common(typeName, defValue)
76
    assert format_gser(result) == format_gser(defValue)
Maxime Perrotin's avatar
Maxime Perrotin committed
77

Maxime Perrotin's avatar
Maxime Perrotin committed
78
def test_simplechoice(qtbot):
79
    ''' Test CHOICE. '''
Maxime Perrotin's avatar
Maxime Perrotin committed
80
    typeName = "Type-SingleChoice"
Maxime Perrotin's avatar
Maxime Perrotin committed
81
82
83
84
    defValue = 'choice-B: TRUE'
    result = common(typeName, defValue)
    assert format_gser(result) == format_gser(defValue)
    defValue = 'choice-A: 99'
Maxime Perrotin's avatar
Maxime Perrotin committed
85
    result = common(typeName, defValue)
86
    assert format_gser(result) == format_gser(defValue)
87
88
89
    defValue = 'choice-C: enum-THREE'
    result = common(typeName, defValue)
    assert format_gser(result) == format_gser(defValue)
Maxime Perrotin's avatar
Maxime Perrotin committed
90
91
92
93

def test_simpleseqof(qtbot):
    ''' Test SEQOF '''
    typeName = "Type-SingleSeqOf"
94
    defValue = '{1, 2, 3, 4, 5}'
Maxime Perrotin's avatar
Maxime Perrotin committed
95
    result = common(typeName, defValue)
96
    assert format_gser(result) == format_gser(defValue)
Maxime Perrotin's avatar
Maxime Perrotin committed
97

Maxime Perrotin's avatar
Maxime Perrotin committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def test_mydata(qtbot):
    ''' Test more complex sequence '''
    typeName = "MyData"
    defValue = '{{a {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, b enum-one}, {a {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, b enum-two}}'
    result = common(typeName, defValue)
    assert format_gser(result) == format_gser(defValue)

def test_seq2(qtbot):
    ''' Test more complex sequence '''
    typeName = "Type-Seq"
    defValue = '{item-a 42.0, item-b choice-B: TRUE, item-C {{a {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, b enum-one}, {a {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, b enum-two}}, item-d "Hello, world"}'
    result = common(typeName, defValue)
    assert format_gser(result) == format_gser(defValue)
    defValue = '{item-a 42.0, item-b choice-A: 99, item-C {{a {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, b enum-one}}, item-d "Hi"}'
    result = common(typeName, defValue)
    assert format_gser(result) == format_gser(defValue)

Maxime Perrotin's avatar
Maxime Perrotin committed
115
if __name__ == '__main__':
Maxime Perrotin's avatar
Maxime Perrotin committed
116
117
118
    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)