From 56f1d311d7bd7c281459fa8e41054c670df914f6 Mon Sep 17 00:00:00 2001 From: d33pster Date: Wed, 10 Apr 2024 12:55:30 +0530 Subject: [PATCH] added tests --- tests/demo.py | 29 +++++++++++++++++++++++ tests/demo2.py | 19 +++++++++++++++ tests/test_basic.py | 41 +++++++++++++++++++++++++++++++++ tests/test_compulsory_args.py | 13 +++++++++++ tests/test_ifthisthennotthat.py | 13 +++++++++++ tests/test_ignore.py | 13 +++++++++++ 6 files changed, 128 insertions(+) create mode 100644 tests/demo.py create mode 100644 tests/demo2.py create mode 100644 tests/test_basic.py create mode 100644 tests/test_compulsory_args.py create mode 100644 tests/test_ifthisthennotthat.py create mode 100644 tests/test_ignore.py diff --git a/tests/demo.py b/tests/demo.py new file mode 100644 index 0000000..4d6b9f2 --- /dev/null +++ b/tests/demo.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +from sys import argv +from optioner import options + +shortargs = ['o1', 'o2', 'o3'] +longargs = ['option1', 'option2', 'option3'] +ignore = ['--option2', '-o2'] +compulsory_s = ['o3'] +compulsory_l = ['option3'] +ifthisthennotthat = [['o1','option1'],['o3', 'option3']] + +ctrl = options(shortargs, longargs, argv[1:], compulsory_s, compulsory_l, ignore, ifthisthennotthat) +args, check, error, falseargs = ctrl._argparse() + +try: + arr = args[0] + if len(args)>1: + for i in range(1, len(args)): + arr += f":{args[i]}" + else: + pass +except IndexError: + arr = '' + +print(arr) +print(check) +print(error) +print(ctrl._what_is_(args[0].split('-')[len(args[0].split('-'))-1])) \ No newline at end of file diff --git a/tests/demo2.py b/tests/demo2.py new file mode 100644 index 0000000..85f006c --- /dev/null +++ b/tests/demo2.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +from sys import argv +from optioner import options + +shortargs = ['o1', 'o2', 'o3'] +longargs = ['option1', 'option2', 'option3'] +ignore = ['--option2', '-o2'] +compulsory_s = ['o3'] +compulsory_l = ['option3'] +ifthisthennotthat = [['o1','option1'],['o3', 'option3']] + +ctrl = options(shortargs, longargs, argv[1:], compulsory_s, compulsory_l, ignore, ifthisthennotthat) +args, check, error, falseargs = ctrl._argparse() + +val1, val2 = ctrl._what_is_(args[0].split('-')[len(args[0].split('-'))-1], 2) + +print(val1) +print(val2) \ No newline at end of file diff --git a/tests/test_basic.py b/tests/test_basic.py new file mode 100644 index 0000000..3c1ed70 --- /dev/null +++ b/tests/test_basic.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +from os import popen, getcwd +from os.path import join + +def test_one(): + """test case 1: + test options class methods against a predefined conditions + """ + output = popen(f"python3 {join(getcwd(), 'tests', 'demo.py')} -o2").readlines() + + assert output[0].replace('\n','') == '-o2' + assert output[1].replace('\n','') == 'True' + +def test_two(): + """test case 2: + test options class for wrong arg + """ + output = popen(f"python3 {join(getcwd(), 'tests', 'demo.py')} -o hehe").readlines() + + assert output[1].replace('\n','') == 'False' + +def test_three(): + """test case 3: + test options class for value + """ + output = popen(f"python3 {join(getcwd(), 'tests', 'demo.py')} -o3 hehe").readlines() + + assert output[0].replace('\n','') == '-o3' + assert output[1].replace('\n','') == 'True' + assert output[3].replace('\n','') == 'hehe' + +def test_four(): + """test case 4: + test options class for multiple values + """ + + output = popen(f"python3 {join(getcwd(), 'tests', 'demo2.py')} -o3 hehe huhu").readlines() + + assert output[0].replace('\n','') == 'hehe' + assert output[1].replace('\n','') == 'huhu' \ No newline at end of file diff --git a/tests/test_compulsory_args.py b/tests/test_compulsory_args.py new file mode 100644 index 0000000..ecaad9c --- /dev/null +++ b/tests/test_compulsory_args.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +from os import popen, getcwd +from os.path import join + +def test_one(): + """test case 5: + test compulsory arg + """ + + output = popen(f"python3 {join(getcwd(), 'tests', 'demo.py')} -o1 hehe").readlines() + + assert output[1].replace('\n','') == 'False' \ No newline at end of file diff --git a/tests/test_ifthisthennotthat.py b/tests/test_ifthisthennotthat.py new file mode 100644 index 0000000..1c2cdbd --- /dev/null +++ b/tests/test_ifthisthennotthat.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +from os import popen, getcwd +from os.path import join + +def test_one(): + """test case 7: + test ifthisthennotthat + """ + + output = popen(f"python3 {join(getcwd(), 'tests', 'demo.py')} -o1 hehe -o3 huhu").readlines() + + assert output[1].replace('\n','') == 'False' \ No newline at end of file diff --git a/tests/test_ignore.py b/tests/test_ignore.py new file mode 100644 index 0000000..bf84888 --- /dev/null +++ b/tests/test_ignore.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +from os import popen, getcwd +from os.path import join + +def test_one(): + """test case 6: + test ignore + """ + + output = popen(f"python3 {join(getcwd(), 'tests', 'demo.py')} -o2 hehe").readlines() + + assert output[1].replace('\n','') == 'True' \ No newline at end of file