Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions 79 Lib/test/test_xxlimited.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import unittest
from test.support import import_helper
import types

Comment thread
encukou marked this conversation as resolved.
xxlimited = import_helper.import_module('xxlimited')
xxlimited_35 = import_helper.import_module('xxlimited_35')


class CommonTests:
module: types.ModuleType

def test_xxo_new(self):
xxo = self.module.Xxo()

def test_xxo_attributes(self):
xxo = self.module.Xxo()
with self.assertRaises(AttributeError):
xxo.foo
with self.assertRaises(AttributeError):
del xxo.foo

xxo.foo = 1234
self.assertEqual(xxo.foo, 1234)

del xxo.foo
with self.assertRaises(AttributeError):
xxo.foo

def test_foo(self):
# the foo function adds 2 numbers
self.assertEqual(self.module.foo(1, 2), 3)

def test_str(self):
self.assertTrue(issubclass(self.module.Str, str))
self.assertIsNot(self.module.Str, str)

custom_string = self.module.Str("abcd")
self.assertEqual(custom_string, "abcd")
self.assertEqual(custom_string.upper(), "ABCD")

def test_new(self):
xxo = self.module.new()
self.assertEqual(xxo.demo("abc"), "abc")


class TestXXLimited(CommonTests, unittest.TestCase):
module = xxlimited

def test_xxo_demo(self):
xxo = self.module.Xxo()
other = self.module.Xxo()
self.assertEqual(xxo.demo("abc"), "abc")
self.assertEqual(xxo.demo(xxo), xxo)
self.assertEqual(xxo.demo(other), other)
self.assertEqual(xxo.demo(0), None)

def test_error(self):
with self.assertRaises(self.module.Error):
raise self.module.Error


class TestXXLimited35(CommonTests, unittest.TestCase):
module = xxlimited_35

def test_xxo_demo(self):
xxo = self.module.Xxo()
other = self.module.Xxo()
self.assertEqual(xxo.demo("abc"), "abc")
self.assertEqual(xxo.demo(0), None)

def test_roj(self):
# the roj function always fails
with self.assertRaises(SystemError):
self.module.roj(0)

def test_null(self):
null1 = self.module.Null()
null2 = self.module.Null()
self.assertNotEqual(null1, null2)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add: if __name__ == "__main__": unittest.main() (not sure of the code, look at the other files). Some people like to run directly a test file, without "python -m test".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is unnecessary; ./python -m test should work just fine.
(Or is the requirement for the if __name__ == "__main__": hack in the devguide?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not a requirement. It's just that other test_xxx.py files have it. You can leave this file as it is.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Update the ``xxlimited`` module to be a better example of how to use the
limited C API.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.