summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_random.py
diff options
context:
space:
mode:
authorHugo van Kemenade <1324225+hugovk@users.noreply.github.com>2024-05-05 06:30:03 (GMT)
committerGitHub <noreply@github.com>2024-05-05 06:30:03 (GMT)
commit3b32575ed6b0905f434f9395d26293c0ae928032 (patch)
treeeed1443529d3d17b3f14457de994cfdf10270cae /Lib/test/test_random.py
parentfed8d73fde779fca41026398376cb3038e9b2b5f (diff)
downloadcpython-3b32575ed6b0905f434f9395d26293c0ae928032.zip
cpython-3b32575ed6b0905f434f9395d26293c0ae928032.tar.gz
cpython-3b32575ed6b0905f434f9395d26293c0ae928032.tar.bz2
gh-118131: Command-line interface for the `random` module (#118132)
Diffstat (limited to 'Lib/test/test_random.py')
-rw-r--r--Lib/test/test_random.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index b1e4ef4..9a44ab1 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -4,6 +4,7 @@ import random
import os
import time
import pickle
+import shlex
import warnings
import test.support
@@ -1397,5 +1398,47 @@ class TestModule(unittest.TestCase):
support.wait_process(pid, exitcode=0)
+class CommandLineTest(unittest.TestCase):
+ def test_parse_args(self):
+ args, help_text = random._parse_args(shlex.split("--choice a b c"))
+ self.assertEqual(args.choice, ["a", "b", "c"])
+ self.assertTrue(help_text.startswith("usage: "))
+
+ args, help_text = random._parse_args(shlex.split("--integer 5"))
+ self.assertEqual(args.integer, 5)
+ self.assertTrue(help_text.startswith("usage: "))
+
+ args, help_text = random._parse_args(shlex.split("--float 2.5"))
+ self.assertEqual(args.float, 2.5)
+ self.assertTrue(help_text.startswith("usage: "))
+
+ args, help_text = random._parse_args(shlex.split("a b c"))
+ self.assertEqual(args.input, ["a", "b", "c"])
+ self.assertTrue(help_text.startswith("usage: "))
+
+ args, help_text = random._parse_args(shlex.split("5"))
+ self.assertEqual(args.input, ["5"])
+ self.assertTrue(help_text.startswith("usage: "))
+
+ args, help_text = random._parse_args(shlex.split("2.5"))
+ self.assertEqual(args.input, ["2.5"])
+ self.assertTrue(help_text.startswith("usage: "))
+
+ def test_main(self):
+ for command, expected in [
+ ("--choice a b c", "b"),
+ ('"a b c"', "b"),
+ ("a b c", "b"),
+ ("--choice 'a a' 'b b' 'c c'", "b b"),
+ ("'a a' 'b b' 'c c'", "b b"),
+ ("--integer 5", 4),
+ ("5", 4),
+ ("--float 2.5", 2.266632777287572),
+ ("2.5", 2.266632777287572),
+ ]:
+ random.seed(0)
+ self.assertEqual(random.main(shlex.split(command)), expected)
+
+
if __name__ == "__main__":
unittest.main()